Skip to content

Commit

Permalink
New version with simple caching
Browse files Browse the repository at this point in the history
  • Loading branch information
romanbican committed May 18, 2015
1 parent df4cd2c commit 5922d8c
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 234 deletions.
26 changes: 2 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Pull this package in through Composer.
```js
{
"require": {
"bican/roles": "1.6.*"
"bican/roles": "1.7.*"
}
}
```
Expand Down Expand Up @@ -98,20 +98,12 @@ if ($user->is('admin|moderator')) // or $user->is('admin, moderator') and also $
// if user has at least one role
}

if ($user->is('admin|moderator', 'all')) // or $user->is('admin, moderator', 'all') and also $user->is(['admin', 'moderator'], 'all')
if ($user->is('admin|moderator', 'All')) // or $user->is('admin, moderator', 'All') and also $user->is(['admin', 'moderator'], 'All')
{
// if user has all roles
}
```

There is a handy `scope role`. Example:

```php
\App\User::role('admin')->get(); // Collection with all users that has admin role

\App\Company::where('name', 'Company')->users()->role('admin');
```

When you are creating roles, there is also optional parameter `level`. It is set to `1` by default, but you can overwrite it and then you can do something like this:

```php
Expand Down Expand Up @@ -154,20 +146,6 @@ if ($user->canAnotherPermission())

You can check for multiple permissions the same way as roles.

You can also use placeholders (wildcards) to check any matching permission.

```php
if ($user->can("edit.*"))
{
//
}

if ($user->can("*.articles"))
{
//
}
```

## Permissions Inheriting

Role with higher level is inheriting permission from roles with lower level.
Expand Down
41 changes: 24 additions & 17 deletions src/Bican/Roles/Contracts/HasRoleAndPermissionContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ interface HasRoleAndPermissionContract
public function roles();

/**
* Check if the user has a provided role or roles.
* Get all roles as collection.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getRoles();

/**
* Check if the user has a role or roles.
*
* @param int|string|array $role
* @param string $methodName
Expand All @@ -22,40 +29,40 @@ public function roles();
public function is($role, $methodName = 'One');

/**
* Attach role.
* Attach role to a user.
*
* @param int|\Bican\Roles\Models\Role $role
* @return mixed
* @return null|bool
*/
public function attachRole($role);

/**
* Detach role.
* Detach role from a user.
*
* @param int|\Bican\Roles\Models\Role $role
* @return mixed
* @return int
*/
public function detachRole($role);

/**
* Detach all roles.
* Detach all roles from a user.
*
* @return mixed
* @return int
*/
public function detachAllRoles();

/**
* Get users level.
* Get role level of a user.
*
* @return int
* @throws \Bican\Roles\Exceptions\RoleNotFoundException
*/
public function level();

/**
* Get all role permissions.
* Get all permissions from roles.
*
* @return
* @return \Illuminate\Database\Eloquent\Builder
* @throws \Bican\Roles\Exceptions\RoleNotFoundException
*/
public function rolePermissions();
Expand All @@ -68,11 +75,11 @@ public function rolePermissions();
public function userPermissions();

/**
* Merge role permissions and user permissions.
* Get all permissions as collection.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function permissions();
public function getPermissions();

/**
* Check if the user has a permission or permissions.
Expand All @@ -97,23 +104,23 @@ public function can($permission, $methodName = 'One', $from = '');
public function allowed($providedPermission, $entity, $owner = true, $ownerColumn = 'user_id');

/**
* Attach permission.
* Attach permission to a user.
*
* @param int|\Bican\Roles\Models\Permission $permission
* @return mixed
* @return null|bool
*/
public function attachPermission($permission);

/**
* Detach permission.
* Detach permission from a user.
*
* @param int|\Bican\Roles\Models\Permission $permission
* @return mixed
* @return int
*/
public function detachPermission($permission);

/**
* Detach all permissions.
* Detach all permissions from a user.
*
* @return int
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Bican/Roles/Contracts/PermissionContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ interface PermissionContract
/**
* Permission belongs to many roles.
*
* @return mixed
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles();

/**
* Permission belongs to many users.
*
* @return mixed
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users();
}
16 changes: 8 additions & 8 deletions src/Bican/Roles/Contracts/RoleContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ interface RoleContract
/**
* Role belongs to many permissions.
*
* @return mixed
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function permissions();

/**
* Role belongs to many users.
*
* @return mixed
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users();

/**
* Attach permission.
* Attach permission to a role.
*
* @param int|Permission $permission
* @return mixed
* @param int|\Bican\Roles\Models\Permission $permission
* @return int|bool
*/
public function attachPermission($permission);

/**
* Detach permission.
* Detach permission from a role.
*
* @param int|Permission $permission
* @return mixed
* @param int|\Bican\Roles\Models\Permission $permission
* @return int
*/
public function detachPermission($permission);

Expand Down
4 changes: 3 additions & 1 deletion src/Bican/Roles/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Bican\Roles\Exceptions;

class InvalidArgumentException extends \Exception
use Exception;

class InvalidArgumentException extends Exception
{

}
4 changes: 3 additions & 1 deletion src/Bican/Roles/Exceptions/RoleNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Bican\Roles\Exceptions;

class RoleNotFoundException extends \Exception
use Exception;

class RoleNotFoundException extends Exception
{

}
7 changes: 2 additions & 5 deletions src/Bican/Roles/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Bican\Roles\Models;

use Bican\Roles\Traits\SlugableTrait;
use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Model;
use Bican\Roles\Traits\PermissionTrait;
use Bican\Roles\Contracts\PermissionContract;
Expand All @@ -23,14 +22,12 @@ class Permission extends Model implements PermissionContract
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return mixed
* @return void
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);

if ($connection = Config::get('roles.connection')) {
$this->connection = $connection;
}
if ($connection = config('roles.connection')) { $this->connection = $connection; }
}
}
7 changes: 2 additions & 5 deletions src/Bican/Roles/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Bican\Roles\Traits\RoleTrait;
use Bican\Roles\Traits\SlugableTrait;
use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Model;
use Bican\Roles\Contracts\RoleContract;

Expand All @@ -23,14 +22,12 @@ class Role extends Model implements RoleContract
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return mixed
* @return void
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);

if ($connection = Config::get('roles.connection')) {
$this->connection = $connection;
}
if ($connection = config('roles.connection')) { $this->connection = $connection; }
}
}

0 comments on commit 5922d8c

Please sign in to comment.