I have a scenario where I'm having 2 components. Let's say:
I can choose to just install Users then there's basic authentication, 1 role only. If I choose to also install Permissions then the option of different roles would be added to the system.
I want Permissions to add some extra features to the users, like $user->hasRole('admin'), but I don't want to modify in the Users package nor do I want to have to create a parent class for both.
How would you solve this?
I have a few ideas, I haven't tested any of them out.
Having an Extender parent observer, that any class can add a method to. Then Permissions would have to do something like:
Extender::add(
Addons\User::class,
function($role) {
// $user = Somehow obtain user class when called from there
// Check if $user has $role
});
The User class must have a magic method:
function __call($method, $params)
{
return Extender(Addons\User::class, $method($params));
}
The idea is the ability to call the user directly:
$user->hasRole($role);
I hope you get the idea, but I don't think this is an optimal solution.
Permissions gets feeded an user objectThe idea is that a user object is injected into the permission:
class Permission
{
public function __construct(User $user)
{
$this->user = $user;
}
public function hasRole($role)
{
return Role::user($user)->has($role);
}
}
That would of course be called from:
Permission($user)->hasRole($role);
Emil Moe
Senior Data Engineer
Maybe I'm not getting it, but why not just use:
class Permission extends Addons\User
Then Permission gets all the properties and methods of Addons\User without having to go through all that observer/caller/feeder stuff with no need to modify User. Or do I not understand what you're trying to do?
Faiz
dev
why wouldn't you use traits?
I will rate the second method 1 on the scale of 1 to 10. Its just too bad nobody want to pass around the user object for checking permissions.