1

I have 3 models for managing user permissions.

class User extends Authenticatable
{
    public function roles()
    {
        return $this->belongsToMany('App\Models\Role');
    }
}

class Role extends Model
{
    public function users()
    {
        return $this->belongsToMany('App\Models\User');
    }

    public function permissions()
    {
        return $this->belongsToMany('App\Models\Permission');
    }
}

class Permission extends Model
{
    public function roles()
    {
        return $this->belongsToMany('App\Models\Role');
    }

    public function roleHavePermission(Role $role)
    {
        if ($this->roles()->find($role->id)) {
            return true;
        }

        return false;
    }

    public function userHavePermission(User $user = null)
    {
        $roles = [];

        if (is_null($user)) {
            $roles[] = Role::where('slug', 'guest')->first();
        } else {
            foreach ($user->roles as $role) {
                $roles[] = $role;
            }
        }

        foreach ($roles as $role) {
            if ($this->roleHavePermission($role)) {
                return true;
            }
        }

        return false;
    }
}

Now because my application is grown, I'm moving to repositories. For example this is my PermissionRepository:

class PermissionRepository implements PermissionRepositoryInterface
{
    protected $roleRepository;

    /**
     * PermissionRepository constructor.
     * @param RoleRepositoryInterface $roleRepository
     */
    public function __construct(RoleRepositoryInterface $roleRepository)
    {
        $this->roleRepository = $roleRepository;
    }

    public function action($routeName)
    {
        return Permission::where('action', $routeName)->first();
    }
}

How can I implement roleHavePermission and userHavePermission in this repository? I tried implementing roles method with this syntax:

public function roles()
{
    return Permission::roles();
}

But it wont work since Permission's roles method can not called statically. Thanks.

1 Answer 1

2

In fact in my opinion you shouldn't implement it in repository. When you call

$permission = $permissionRepository->action('abc.name');

you are getting permission object, so there is no point to implement it in repository. All you need is running:

$roles = $permission->roles;

In theory you could add for example into PermissionRepository method like this:

public function getRoles(Permission $permission) 
{
  return $permission->roles;
}

so you could now run

$roles = $permissionRepository->getRoles($permission);

to get roles of given permission but I don't see any point to do it like this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.