Imo your over thinking it. Are you trying to set the max post count dynamically? Ie: Change it any time. If not the && check with post count is pretty simple and effective.
User roles and permissions with conditions
I want to implement roles & permissions and have some sort of conditions for the permissions. What I mean by conditions is, for example:
- There are two roles: user and superuser
- There is a permission: add-post
- user can only add 5 posts
- superuser can add unlimited number of posts
I looked for some packages I could use, for example:
- https://github.com/Zizaco/entrust
- https://cartalyst.com/manual/sentry/2.1
- https://github.com/romanbican/roles
All of them have nice examples, but I couldn't find any information about the "condition" part I would like to have.
I guess the simplest solution would be to use one of the packages and just hard-code the condition:
if ( ! $user->can('add-post')) {
// throw exception or whatever
}
if ( ! $user->hasRole('superuser') && $postCount > 5) {
//
}
But I don'y really like that...
This kind of API would be cool to use:
if ( ! $user->can('add-post', ['count' => $postCount])) {
//
}
Is there some package with this kind of functionality? Couldn't find any, maybe I didn't use the right keywords when searching... If not, I guess I would need to extend some existing package/create this from scratch.
Have you implemented this kind of functionality in your projects/how would you implement this?
Thanks!
Please or to participate in this conversation.