diff --git a/README.md b/README.md index 78d6806..1590840 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![ReadMeSupportPalestine](https://raw.githubusercontent.com/Safouene1/support-palestine-banner/master/banner-support.svg)](https://sahem.ksrelief.org/Pages/ProgramDetails/1ca8852b-9e6d-ee11-b83f-005056ac5498) # laravel-permission-mongodb [![Latest Version on Packagist][ico-version]][link-releases] @@ -9,6 +10,7 @@ [![StyleCI][ico-styleci]][link-styleci] [![Coverage Status][ico-coveralls]][link-coveralls] [![Total Downloads][ico-downloads]][link-packagist] +[![StandWithPalestine](https://raw.githubusercontent.com/Safouene1/support-palestine-banner/master/StandWithPalestine.svg)](https://sahem.ksrelief.org/Pages/ProgramDetails/1ca8852b-9e6d-ee11-b83f-005056ac5498) This package allows you to manage user permissions and roles in a database. It is inspired from [laravel-permission][link-laravel-permission]. Same code same every thing but it is compatible with [laravel-mongodb][link-laravel-mongodb] @@ -156,6 +158,25 @@ return [ 'permissions' => 'permissions', ], + 'user_props_names' => [ + + /* + * When using the "HasRoles" trait from this package, we need to know which + *Users table property should be used to retrieve their roles. We chose a basic + * default value, but you can easily change it to any property you want. + */ + + 'roles' => 'roles_id', + + /* + * When using the "HasRoles" trait from this package, we need to know which + *Users table property should be used to retrieve their roles. We chose a basic + * default value, but you can easily change it to any property you want. + */ + + 'permissions' => 'permissions_id', + ], + /* * By default all permissions will be cached for 24 hours unless a permission or * role is updated. Then the cache will be flushed immediately. diff --git a/config/permission.php b/config/permission.php index 6f79028..dcfba7b 100644 --- a/config/permission.php +++ b/config/permission.php @@ -47,6 +47,25 @@ 'permissions' => 'permissions', ], + 'user_props_names' => [ + + /* + * When using the "HasRoles" trait from this package, we need to know which + *Users table property should be used to retrieve their roles. We chose a basic + * default value, but you can easily change it to any property you want. + */ + + 'roles' => 'roles_id', + + /* + * When using the "HasRoles" trait from this package, we need to know which + *Users table property should be used to retrieve their roles. We chose a basic + * default value, but you can easily change it to any property you want. + */ + + 'permissions' => 'permissions_id', + ], + /* * By default all permissions will be cached for 24 hours unless a permission or * role is updated. Then the cache will be flushed immediately. diff --git a/src/Models/Permission.php b/src/Models/Permission.php index b23f4dd..f0b1b1a 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -103,7 +103,7 @@ public static function findOrCreate(string $name, string $guardName = null): Per public function rolesQuery(): mixed { $roleClass = $this->getRoleClass(); - return $roleClass->query()->where('permission_ids', 'all', [$this->_id]); + return $roleClass->query()->where(config('permission.user_props_names.permissions'), 'all', [$this->_id]); } /** @@ -122,7 +122,7 @@ public function getRolesAttribute(): mixed public function usersQuery(): mixed { $usersClass = app($this->helpers->getModelForGuard($this->attributes['guard_name'])); - return $usersClass->query()->where('permission_ids', 'all', [$this->_id]); + return $usersClass->query()->where(config('permission.user_props_names.permissions'), 'all', [$this->_id]); } /** diff --git a/src/Models/Role.php b/src/Models/Role.php index ac1c76e..e26b97c 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -129,7 +129,7 @@ public static function findByName(string $name, string $guardName = null): RoleI public function usersQuery(): mixed { $usersClass = app($this->helpers->getModelForGuard($this->attributes['guard_name'])); - return $usersClass->query()->where('role_ids', 'all', [$this->_id]); + return $usersClass->query()->where(config('permission.user_props_names.roles'), 'all', [$this->_id]); } /** @@ -164,6 +164,6 @@ public function hasPermissionTo(string|PermissionInterface $permission): bool throw new GuardDoesNotMatch($this->helpers->getGuardDoesNotMatchMessage($expected, $given)); } - return in_array($permission->_id, $this->permission_ids ?? [], true); + return in_array($permission->_id, $this->getAttribute(config('permission.user_props_names.permissions')) ?? [], true); } } diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php index a373f5d..2c39ac5 100644 --- a/src/Traits/HasPermissions.php +++ b/src/Traits/HasPermissions.php @@ -38,7 +38,7 @@ public function getPermissionClass() public function permissionsQuery() { $permission = $this->getPermissionClass(); - return $permission::whereIn('_id', $this->permission_ids ?? []); + return $permission::whereIn('_id', $this->getAttribute(config('permission.user_props_names.permissions')) ?? []); } /** @@ -59,9 +59,9 @@ public function getPermissionsAttribute() */ public function givePermissionTo(...$permissions): self { - $this->permission_ids = collect($this->permission_ids ?? []) + $this->setAttribute(config('permission.user_props_names.permissions'), collect($this->getAttribute(config('permission.user_props_names.permissions')) ?? []) ->merge($this->getPermissionIds($permissions)) - ->all(); + ->all()); $this->save(); @@ -80,7 +80,7 @@ public function givePermissionTo(...$permissions): self */ public function syncPermissions(...$permissions): self { - $this->permission_ids = $this->getPermissionIds($permissions); + $this->setAttribute(config('permission.user_props_names.permissions'), $this->getPermissionIds($permissions)); $this->save(); return $this->givePermissionTo($permissions); @@ -98,11 +98,11 @@ public function revokePermissionTo(...$permissions): self { $permissions = $this->getPermissionIds($permissions); - $this->permission_ids = collect($this->permission_ids ?? []) + $this->$this->setAttribute(config('permission.user_props_names.permissions'), collect($this->getAttribute(config('permission.user_props_names.permissions')) ?? []) ->filter(function ($permission) use ($permissions) { return ! in_array($permission, $permissions, true); }) - ->all(); + ->all()); $this->save(); @@ -206,7 +206,7 @@ public function getPermissionNames(): Collection */ public function getPermissionsViaRoles(): Collection { - $permissionIds = $this->roles->pluck('permission_ids')->flatten()->unique()->values(); + $permissionIds = $this->roles->pluck(config('permission.user_props_names.permissions'))->flatten()->unique()->values(); return $this->getPermissionClass()->query()->whereIn('_id', $permissionIds)->get(); /*return $this->load('roles', 'roles.permissions') ->roles->flatMap(function (Role $role) { @@ -345,8 +345,8 @@ public function scopePermission(Builder $query, $permissions): Builder } $roles = $roles->unique(); - return $query->orWhereIn('permission_ids', $permissions->pluck('_id')) - ->orWhereIn('role_ids', $roles->pluck('_id')); + return $query->orWhereIn(config('permission.user_props_names.permissions'), $permissions->pluck('_id')) + ->orWhereIn(config('permission.user_props_names.roles'), $roles->pluck('_id')); } /** diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php index dfb0421..63369d8 100644 --- a/src/Traits/HasRoles.php +++ b/src/Traits/HasRoles.php @@ -61,7 +61,7 @@ public function scopeRole(Builder $query, $roles): Builder { $roles = $this->convertToRoleModels($roles); - return $query->whereIn('role_ids', $roles->pluck('_id')); + return $query->whereIn(config('permission.user_props_names.roles'), $roles->pluck('_id')); } /**