Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

merge & fix syncRoles(), add syncPermissions() update documentation #140

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ 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)
- [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)
Expand Down Expand Up @@ -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...
*/
Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -157,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']),
Expand All @@ -181,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) {
//
Expand Down Expand Up @@ -211,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).

Expand All @@ -229,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
Expand Down
16 changes: 16 additions & 0 deletions src/Bican/Roles/Contracts/HasRoleAndPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
8 changes: 8 additions & 0 deletions src/Bican/Roles/Contracts/RoleHasRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
28 changes: 27 additions & 1 deletion src/Bican/Roles/Traits/HasRoleAndPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ public function detachAllRoles()
return $this->roles()->detach();
}

/**
* Sync roles for a user.
*
* @param array|\Bican\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles
* @return array
*/
public function syncRoles($roles)
{
$this->roles = null;

return $this->roles()->sync($roles);
}

/**
* Get role level of a user.
*
Expand Down Expand Up @@ -327,10 +340,23 @@ public function detachPermission($permission)
public function detachAllPermissions()
{
$this->permissions = null;

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->userPermissions()->sync($permissions);
}

/**
* Check if pretend option is enabled.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Bican/Roles/Traits/RoleHasRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}