From 5a5ad9dbf58e5070f1158815007d2aacb68016d5 Mon Sep 17 00:00:00 2001 From: Konstantin L Date: Thu, 17 Dec 2015 14:35:22 +0300 Subject: [PATCH] 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); + } }