From 46f985799d9f5ca4f6d1bf5f1c1ea18e5540c752 Mon Sep 17 00:00:00 2001 From: webafra Date: Tue, 24 Oct 2023 11:08:51 +0330 Subject: [PATCH 1/4] replace mongodb driver with mongodb/laravel-mongodb replace mongodb driver with new official driver -> mongodb/laravel-mongodb --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9e8236e..11269e1 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "illuminate/auth": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", - "jenssegers/mongodb": "^3.9" + "mongodb/laravel-mongodb": "^3.9" }, "require-dev": { "monolog/monolog": "^3.2", From c46c2b9977601b4baf028da83bb0d12edc392a16 Mon Sep 17 00:00:00 2001 From: Mostafa Abd El-Salam Maklad Date: Tue, 30 Jan 2024 16:44:50 +0300 Subject: [PATCH 2/4] Stand with Palestine - Free Palestine --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 78d6806..dd9be33 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] From f3784725147baf84d8e4e2639e5d130eadd9541e Mon Sep 17 00:00:00 2001 From: Bruno Wisniewski Date: Fri, 19 Apr 2024 10:36:51 -0300 Subject: [PATCH 3/4] Added dynamic property support This way, it is possible to change the default property of the collection ('roles_id') and ('permissions_ids'), being able to use the library in several projects with the same collection of users, segmenting the property for each one, and can be used in multiple projects that use unique logins. --- README.md | 19 +++++++++++++++++++ config/permission.php | 19 +++++++++++++++++++ src/Models/Permission.php | 4 ++-- src/Models/Role.php | 4 ++-- src/Traits/HasPermissions.php | 18 +++++++++--------- src/Traits/HasRoles.php | 2 +- 6 files changed, 52 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index dd9be33..1590840 100644 --- a/README.md +++ b/README.md @@ -158,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')); } /** From d7c7a545ebf07e5823e066e02e66e23859f0c808 Mon Sep 17 00:00:00 2001 From: Bruno Wisniewski Date: Fri, 19 Apr 2024 10:36:51 -0300 Subject: [PATCH 4/4] Added dynamic property support This way, it is possible to change the default property of the collection ('roles_id') and ('permissions_ids'), being able to use the library in several projects with the same collection of users, segmenting the property for each one, and can be used in multiple projects that use unique logins. --- README.md | 19 +++++++++++++++++++ config/permission.php | 19 +++++++++++++++++++ src/Models/Permission.php | 4 ++-- src/Models/Role.php | 4 ++-- src/Traits/HasPermissions.php | 18 +++++++++--------- src/Traits/HasRoles.php | 2 +- 6 files changed, 52 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index dd9be33..1590840 100644 --- a/README.md +++ b/README.md @@ -158,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')); } /**