How I Can Create Middleware On October CMS Only On Component Class Or Plugin?

I readed doku and search about middleware on october informations, but nothing find what me can help.

My Problem: I created Plugin and use RainLab User Plugin for Frontend Editoring of Content. Frontend Content spilt up on Section, Group and Article. Single Users become different access: hiddem, show, edit and more for sections and articles. The site works with one plugin on content load dynamic.

How i can load content for single user by access config?

My solution:

I added on backend some functions to give access for users. rainlab user content access

All access configs saved and works. My idee is to create middleware to load content but i find nothing what can help me.

Docu tell me you can create middleware but middleware load all time on frontend, backend, all pages on website. This is not good solution.

In addition comes if i debug on middleware, so rainlab user functions not working, no informations about url request.

Why add middleware when you must load and create all classes and functions by himself??

I need middleware only for frontend and only for plugin or component. I hope somebody can me help to find another solution or idee to fix it.

I think there is nothing left but to implement the content access in respective component on onRun function. :( and problem by this solution is: return Redirect not working outside a class or function (only on onRun)



There is Problem with your approach, as you want middleware and also you want to make sure it run for specific plugins and component. middleware worked based on request and request may have information about URL etc. but not about plugin & component etc so it can not determine when to run based on plugin etc..`

So you may use Different approach. and it will work with Frontend also with ajax requests.

You can create component inside your plugin, It will assessment all the access for logged in user if user is logged in. if user is not logged in you can provide different assessment access.

Now you need to add this component to your layout and now all pages which are using this layout have this access information from the beginning.

Now this component inject access information to page, So your Page and plugin's components can have this information readily available. now from your component inside onRun you can handle redirect at very top.

Component is not to define access, its just inject predefined access information which is in database [ as you said you use users plugin so fetch it and inject it ] to the page

Component will be specifically just inject access information to page. And that Component will be on layout so. it will automatically trigger for that layout pages. so it will executed first and based on that you can decide how to use it. you can also render conditional component based on that. as you can have access information in markup part as well so

if you have any doubt or questions please comment.