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

Some love to tests. #172

Open
wants to merge 8 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Roles And Permissions For Laravel 5

[![Build Status](https://travis-ci.org/Asvae/bican-roles-test.svg)](https://travis-ci.org/Asvae/bican-roles-test)

Powerful package for handling roles and permissions in Laravel 5 (5.1 and also 5.0).

- [Installation](#installation)
Expand Down Expand Up @@ -386,6 +388,11 @@ public function render($request, Exception $e)

You can change connection for models, slug separator, models path and there is also a handy pretend feature. Have a look at config file for more information.

## Tests

Tests are handled by [external package](https://github.com/Asvae/bican-roles-test) as it's not viable to test the library
outside of laravel framework.

## More Information

For more information, please have a look at [HasRoleAndPermission](https://github.com/romanbican/roles/blob/master/src/Bican/Roles/Contracts/HasRoleAndPermission.php) contract.
Expand Down
7 changes: 7 additions & 0 deletions src/Bican/Roles/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
use Bican\Roles\Traits\PermissionHasRelations;
use Bican\Roles\Contracts\PermissionHasRelations as PermissionHasRelationsContract;

/**
* @property integer id
* @property string name
* @property string slug
* @property string description
* @property string model
*/
class Permission extends Model implements PermissionHasRelationsContract
{
use Slugable, PermissionHasRelations;
Expand Down
7 changes: 7 additions & 0 deletions src/Bican/Roles/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
use Bican\Roles\Traits\RoleHasRelations;
use Bican\Roles\Contracts\RoleHasRelations as RoleHasRelationsContract;

/**
* @property integer id
* @property string name
* @property string slug
* @property string description
* @property integer level
*/
class Role extends Model implements RoleHasRelationsContract
{
use Slugable, RoleHasRelations;
Expand Down
90 changes: 60 additions & 30 deletions src/Bican/Roles/Traits/HasRoleAndPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public function roles()
*/
public function getRoles()
{
return (!$this->roles) ? $this->roles = $this->roles()->get() : $this->roles;
return (! $this->roles) ? $this->roles = $this->roles()->get() : $this->roles;
}

/**
* Check if the user has a role or roles.
*
* @param int|string|array $role
* @param bool $all
* @param int|string|array|\Bican\Roles\Models\Role $role
* @param bool $all
* @return bool
*/
public function is($role, $all = false)
Expand All @@ -61,7 +61,7 @@ public function is($role, $all = false)
/**
* Check if the user has at least one role.
*
* @param int|string|array $role
* @param int|string|array|\Bican\Roles\Models\Role $role
* @return bool
*/
public function isOne($role)
Expand All @@ -78,13 +78,13 @@ public function isOne($role)
/**
* Check if the user has all roles.
*
* @param int|string|array $role
* @param int|string|array|\Bican\Roles\Models\Role $role
* @return bool
*/
public function isAll($role)
{
foreach ($this->getArrayFrom($role) as $role) {
if (!$this->hasRole($role)) {
if (! $this->hasRole($role)) {
return false;
}
}
Expand All @@ -95,12 +95,16 @@ public function isAll($role)
/**
* Check if the user has role.
*
* @param int|string $role
* @param int|string|\Bican\Roles\Models\Role $role
* @return bool
*/
public function hasRole($role)
{
return $this->getRoles()->contains(function ($key, $value) use ($role) {
if ($role instanceof Model) {
return $value->id == $role->id;
}

return $role == $value->id || Str::is($role, $value->slug);
});
}
Expand All @@ -113,7 +117,7 @@ public function hasRole($role)
*/
public function attachRole($role)
{
return (!$this->getRoles()->contains($role)) ? $this->roles()->attach($role) : true;
return (! $this->getRoles()->contains($role)) ? $this->roles()->attach($role) : true;
}

/**
Expand Down Expand Up @@ -160,14 +164,20 @@ public function rolePermissions()
{
$permissionModel = app(config('roles.models.permission'));

if (!$permissionModel instanceof Model) {
if (! $permissionModel instanceof Model) {
throw new InvalidArgumentException('[roles.models.permission] must be an instance of \Illuminate\Database\Eloquent\Model');
}

return $permissionModel::select(['permissions.*', 'permission_role.created_at as pivot_created_at', 'permission_role.updated_at as pivot_updated_at'])
->join('permission_role', 'permission_role.permission_id', '=', 'permissions.id')->join('roles', 'roles.id', '=', 'permission_role.role_id')
->whereIn('roles.id', $this->getRoles()->lists('id')->toArray()) ->orWhere('roles.level', '<', $this->level())
->groupBy(['permissions.id', 'pivot_created_at', 'pivot_updated_at']);
return $permissionModel::select([
'permissions.*',
'permission_role.created_at as pivot_created_at',
'permission_role.updated_at as pivot_updated_at',
])
->join('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
->join('roles', 'roles.id', '=', 'permission_role.role_id')
->whereIn('roles.id', $this->getRoles()->pluck('id')->toArray())
->orWhere('roles.level', '<', $this->level())
->groupBy(['permissions.id', 'pivot_created_at', 'pivot_updated_at']);
}

/**
Expand All @@ -187,14 +197,16 @@ public function userPermissions()
*/
public function getPermissions()
{
return (!$this->permissions) ? $this->permissions = $this->rolePermissions()->get()->merge($this->userPermissions()->get()) : $this->permissions;
return (! $this->permissions) ? $this->permissions = $this->rolePermissions()
->get()
->merge($this->userPermissions()->get()) : $this->permissions;
}

/**
* Check if the user has a permission or permissions.
*
* @param int|string|array $permission
* @param bool $all
* @param bool $all
* @return bool
*/
public function can($permission, $all = false)
Expand Down Expand Up @@ -232,7 +244,7 @@ public function canOne($permission)
public function canAll($permission)
{
foreach ($this->getArrayFrom($permission) as $permission) {
if (!$this->hasPermission($permission)) {
if (! $this->hasPermission($permission)) {
return false;
}
}
Expand All @@ -249,20 +261,29 @@ public function canAll($permission)
public function hasPermission($permission)
{
return $this->getPermissions()->contains(function ($key, $value) use ($permission) {
if ($permission instanceof Model) {
return $value->id == $permission->id;
}

return $permission == $value->id || Str::is($permission, $value->slug);
});
}

/**
* Check if the user is allowed to manipulate with entity.
*
* @param string $providedPermission
* @param string $providedPermission
* @param \Illuminate\Database\Eloquent\Model $entity
* @param bool $owner
* @param string $ownerColumn
* @param bool $owner
* @param string $ownerColumn
* @return bool
*/
public function allowed($providedPermission, Model $entity, $owner = true, $ownerColumn = 'user_id')
public function allowed(
$providedPermission,
Model $entity,
$owner = true,
$ownerColumn = 'user_id'
)
{
if ($this->isPretendEnabled()) {
return $this->pretend('allowed');
Expand All @@ -278,16 +299,14 @@ public function allowed($providedPermission, Model $entity, $owner = true, $owne
/**
* Check if the user is allowed to manipulate with provided entity.
*
* @param string $providedPermission
* @param string $providedPermission
* @param \Illuminate\Database\Eloquent\Model $entity
* @return bool
*/
protected function isAllowed($providedPermission, Model $entity)
{
foreach ($this->getPermissions() as $permission) {
if ($permission->model != '' && get_class($entity) == $permission->model
&& ($permission->id == $providedPermission || $permission->slug === $providedPermission)
) {
if ($permission->model != '' && get_class($entity) == $permission->model && ($permission->id == $providedPermission || $permission->slug === $providedPermission)) {
return true;
}
}
Expand All @@ -303,7 +322,8 @@ protected function isAllowed($providedPermission, Model $entity)
*/
public function attachPermission($permission)
{
return (!$this->getPermissions()->contains($permission)) ? $this->userPermissions()->attach($permission) : true;
return (! $this->getPermissions()->contains($permission)) ? $this->userPermissions()
->attach($permission) : true;
}

/**
Expand All @@ -327,7 +347,7 @@ public function detachPermission($permission)
public function detachAllPermissions()
{
$this->permissions = null;

return $this->userPermissions()->detach();
}

Expand Down Expand Up @@ -356,7 +376,7 @@ private function pretend($option)
* Get method name.
*
* @param string $methodName
* @param bool $all
* @param bool $all
* @return string
*/
private function getMethodName($methodName, $all)
Expand All @@ -372,14 +392,22 @@ private function getMethodName($methodName, $all)
*/
private function getArrayFrom($argument)
{
return (!is_array($argument)) ? preg_split('/ ?[,|] ?/', $argument) : $argument;
if (is_array($argument)) {
return $argument;
}

if (is_string($argument)) {
return preg_split('/ ?[,|] ?/', $argument);
}

return [$argument];
}

/**
* Handle dynamic method calls.
*
* @param string $method
* @param array $parameters
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
Expand All @@ -389,7 +417,9 @@ public function __call($method, $parameters)
} elseif (starts_with($method, 'can')) {
return $this->can(snake_case(substr($method, 3), config('roles.separator')));
} elseif (starts_with($method, 'allowed')) {
return $this->allowed(snake_case(substr($method, 7), config('roles.separator')), $parameters[0], (isset($parameters[1])) ? $parameters[1] : true, (isset($parameters[2])) ? $parameters[2] : 'user_id');
return $this->allowed(snake_case(substr($method, 7), config('roles.separator')),
$parameters[0], (isset($parameters[1])) ? $parameters[1] : true,
(isset($parameters[2])) ? $parameters[2] : 'user_id');
}

return parent::__call($method, $parameters);
Expand Down