From e80c95cd62c9ac49e2c55af8314329741292fa5f Mon Sep 17 00:00:00 2001 From: bogdan Date: Thu, 12 Nov 2015 17:40:15 -0500 Subject: [PATCH 1/5] sync roles --- src/Bican/Roles/Traits/HasRoleAndPermission.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Bican/Roles/Traits/HasRoleAndPermission.php b/src/Bican/Roles/Traits/HasRoleAndPermission.php index 840cc1c..40a18a9 100644 --- a/src/Bican/Roles/Traits/HasRoleAndPermission.php +++ b/src/Bican/Roles/Traits/HasRoleAndPermission.php @@ -141,6 +141,20 @@ public function detachAllRoles() return $this->roles()->detach(); } + + /** + * Sync roles from a user. + * + * @param int|\Bican\Roles\Models\Role $roles + * @return int + */ + public function syncRoles(array $roles) + { + $this->roles = null; + + return $this->roles()->sync($roles); + } + /** * Get role level of a user. * @@ -151,6 +165,7 @@ public function level() return ($role = $this->getRoles()->sortByDesc('level')->first()) ? $role->level : 0; } + /** * Get all permissions from roles. * From 6687ccb163dc0261a22cd5ddefba3927c1937e29 Mon Sep 17 00:00:00 2001 From: Konstantin L Date: Thu, 17 Dec 2015 13:11:31 +0300 Subject: [PATCH 2/5] fix HasRoleAndPermission::syncRoles() method, update README --- README.md | 8 +++----- src/Bican/Roles/Traits/HasRoleAndPermission.php | 12 +++++------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 31deb2c..5dfe997 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Powerful package for handling roles and permissions in Laravel 5 (5.1 and also 5 - [HasRoleAndPermission Trait And Contract](#hasroleandpermission-trait-and-contract) - [Usage](#usage) - [Creating Roles](#creating-roles) - - [Attaching And Detaching Roles](#attaching-and-detaching-roles) + - [Attaching, Detaching and Syncing Roles](#attaching-detaching-and-syncing-roles) - [Checking For Roles](#checking-for-roles) - [Levels](#levels) - [Creating Permissions](#creating-permissions) @@ -119,7 +119,7 @@ $moderatorRole = Role::create([ > Because of `Slugable` trait, if you make a mistake and for example leave a space in slug parameter, it'll be replaced with a dot automatically, because of `str_slug` function. -### Attaching And Detaching Roles +### Attaching, Detaching and Syncing Roles It's really simple. You fetch a user from database and call `attachRole` method. There is `BelongsToMany` relationship between `User` and `Role` model. @@ -129,11 +129,9 @@ use App\User; $user = User::find($id); $user->attachRole($adminRole); // you can pass whole object, or just an id -``` - -```php $user->detachRole($adminRole); // in case you want to detach role $user->detachAllRoles(); // in case you want to detach all roles +$user->syncRoles($roles); // you can pass Eloquent collection, or just an array of ids ``` ### Checking For Roles diff --git a/src/Bican/Roles/Traits/HasRoleAndPermission.php b/src/Bican/Roles/Traits/HasRoleAndPermission.php index 40a18a9..cd892b0 100644 --- a/src/Bican/Roles/Traits/HasRoleAndPermission.php +++ b/src/Bican/Roles/Traits/HasRoleAndPermission.php @@ -141,14 +141,13 @@ public function detachAllRoles() return $this->roles()->detach(); } - /** - * Sync roles from a user. + * Sync roles for a user. * - * @param int|\Bican\Roles\Models\Role $roles - * @return int + * @param array|\Bican\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles + * @return array */ - public function syncRoles(array $roles) + public function syncRoles($roles) { $this->roles = null; @@ -165,7 +164,6 @@ public function level() return ($role = $this->getRoles()->sortByDesc('level')->first()) ? $role->level : 0; } - /** * Get all permissions from roles. * @@ -342,7 +340,7 @@ public function detachPermission($permission) public function detachAllPermissions() { $this->permissions = null; - + return $this->userPermissions()->detach(); } From 5a5ad9dbf58e5070f1158815007d2aacb68016d5 Mon Sep 17 00:00:00 2001 From: Konstantin L Date: Thu, 17 Dec 2015 14:35:22 +0300 Subject: [PATCH 3/5] add syncPermissions() method for user and for role --- .../Roles/Contracts/HasRoleAndPermission.php | 16 ++++++++++++++++ src/Bican/Roles/Contracts/RoleHasRelations.php | 8 ++++++++ src/Bican/Roles/Traits/HasRoleAndPermission.php | 13 +++++++++++++ src/Bican/Roles/Traits/RoleHasRelations.php | 11 +++++++++++ 4 files changed, 48 insertions(+) diff --git a/src/Bican/Roles/Contracts/HasRoleAndPermission.php b/src/Bican/Roles/Contracts/HasRoleAndPermission.php index 4286e27..fd01f65 100644 --- a/src/Bican/Roles/Contracts/HasRoleAndPermission.php +++ b/src/Bican/Roles/Contracts/HasRoleAndPermission.php @@ -76,6 +76,14 @@ public function detachRole($role); */ public function detachAllRoles(); + /** + * Sync roles for a user. + * + * @param array|\Bican\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles + * @return array + */ + public function syncRoles($roles); + /** * Get role level of a user. * @@ -170,4 +178,12 @@ public function detachPermission($permission); * @return int */ public function detachAllPermissions(); + + /** + * Sync permissions for a user. + * + * @param array|\Bican\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions + * @return array + */ + public function syncPermissions($permissions); } diff --git a/src/Bican/Roles/Contracts/RoleHasRelations.php b/src/Bican/Roles/Contracts/RoleHasRelations.php index 7227329..2db21a9 100644 --- a/src/Bican/Roles/Contracts/RoleHasRelations.php +++ b/src/Bican/Roles/Contracts/RoleHasRelations.php @@ -40,4 +40,12 @@ public function detachPermission($permission); * @return int */ public function detachAllPermissions(); + + /** + * Sync permissions for a role. + * + * @param array|\Bican\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions + * @return array + */ + public function syncPermissions($permissions); } diff --git a/src/Bican/Roles/Traits/HasRoleAndPermission.php b/src/Bican/Roles/Traits/HasRoleAndPermission.php index cd892b0..855ede7 100644 --- a/src/Bican/Roles/Traits/HasRoleAndPermission.php +++ b/src/Bican/Roles/Traits/HasRoleAndPermission.php @@ -344,6 +344,19 @@ public function detachAllPermissions() return $this->userPermissions()->detach(); } + /** + * Sync permissions for a user. + * + * @param array|\Bican\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions + * @return array + */ + public function syncPermissions($permissions) + { + $this->permissions = null; + + return $this->permissions()->sync($permissions); + } + /** * Check if pretend option is enabled. * diff --git a/src/Bican/Roles/Traits/RoleHasRelations.php b/src/Bican/Roles/Traits/RoleHasRelations.php index 16cc7c8..2fa50c4 100644 --- a/src/Bican/Roles/Traits/RoleHasRelations.php +++ b/src/Bican/Roles/Traits/RoleHasRelations.php @@ -55,4 +55,15 @@ public function detachAllPermissions() { return $this->permissions()->detach(); } + + /** + * Sync permissions for a role. + * + * @param array|\Bican\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions + * @return array + */ + public function syncPermissions($permissions) + { + return $this->permissions()->sync($permissions); + } } From 19564fb37dbcc7d096ac674d41310237fa4434c7 Mon Sep 17 00:00:00 2001 From: Konstantin L Date: Thu, 17 Dec 2015 14:42:30 +0300 Subject: [PATCH 4/5] update README: add syncPermissions(), clear trailing whitespacing --- README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5dfe997..4f49d33 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Powerful package for handling roles and permissions in Laravel 5 (5.1 and also 5 - [Checking For Roles](#checking-for-roles) - [Levels](#levels) - [Creating Permissions](#creating-permissions) - - [Attaching And Detaching Permissions](#attaching-and-detaching-permissions) + - [Attaching, Detaching and Syncing Permissions](#attaching-detaching-and-syncing-permissions) - [Checking For Permissions](#checking-for-permissions) - [Permissions Inheriting](#permissions-inheriting) - [Entity Check](#entity-check) @@ -53,14 +53,14 @@ Add the package to your application service providers in `config/app.php` file. ```php 'providers' => [ - + /* * Laravel Framework Service Providers... */ Illuminate\Foundation\Providers\ArtisanServiceProvider::class, Illuminate\Auth\AuthServiceProvider::class, ... - + /** * Third Party Service Providers... */ @@ -155,7 +155,7 @@ if ($user->isAdmin()) { And of course, there is a way to check for multiple roles: ```php -if ($user->is('admin|moderator')) { +if ($user->is('admin|moderator')) { /* | Or alternatively: | $user->is('admin, moderator'), $user->is(['admin', 'moderator']), @@ -179,7 +179,7 @@ if ($user->is('admin|moderator', true)) { ### Levels When you are creating roles, there is optional parameter `level`. It is set to `1` by default, but you can overwrite it and then you can do something like this: - + ```php if ($user->level() > 4) { // @@ -209,7 +209,7 @@ $deleteUsersPermission = Permission::create([ ]); ``` -### Attaching And Detaching Permissions +### Attaching, Detaching and Syncing Permissions You can attach permissions to a role or directly to a specific user (and of course detach them as well). @@ -227,9 +227,11 @@ $user->attachPermission($deleteUsersPermission); // permission attached to a use ```php $role->detachPermission($createUsersPermission); // in case you want to detach permission $role->detachAllPermissions(); // in case you want to detach all permissions +$role->syncPermissions($permissions); // you can pass Eloquent collection, or just an array of ids $user->detachPermission($deleteUsersPermission); $user->detachAllPermissions(); +$user->syncPermissions($permissions); // you can pass Eloquent collection, or just an array of ids ``` ### Checking For Permissions From 7f91d9c0a148a64e3ddf7f1253dac360e71b51c4 Mon Sep 17 00:00:00 2001 From: Konstantin L Date: Fri, 5 Feb 2016 22:29:43 +0300 Subject: [PATCH 5/5] fix syncPermissions() method --- src/Bican/Roles/Traits/HasRoleAndPermission.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bican/Roles/Traits/HasRoleAndPermission.php b/src/Bican/Roles/Traits/HasRoleAndPermission.php index 855ede7..01f1689 100644 --- a/src/Bican/Roles/Traits/HasRoleAndPermission.php +++ b/src/Bican/Roles/Traits/HasRoleAndPermission.php @@ -354,7 +354,7 @@ public function syncPermissions($permissions) { $this->permissions = null; - return $this->permissions()->sync($permissions); + return $this->userPermissions()->sync($permissions); } /**