Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
60.00% |
3 / 5 |
CRAP | |
10.00% |
3 / 30 |
| Permission | |
0.00% |
0 / 1 |
|
60.00% |
3 / 5 |
227.68 | |
10.00% |
3 / 30 |
| users | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| roles | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| needed | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 7 |
|||
| userHas | |
0.00% |
0 / 1 |
110 | |
0.00% |
0 / 20 |
|||
| serializeDate | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Acls\Models; |
| 4 | |
| 5 | use App\User; |
| 6 | use Illuminate\Database\Eloquent\Collection; |
| 7 | use Qmp\Laravel\Acls\Exceptions\RolePermissionException; |
| 8 | use Illuminate\Database\Eloquent\Model; |
| 9 | use Illuminate\Support\Facades\Log; |
| 10 | |
| 11 | class Permission extends Model |
| 12 | { |
| 13 | |
| 14 | protected $fillable = ['name']; |
| 15 | |
| 16 | public function users() |
| 17 | { |
| 18 | return $this->belongsToMany(User::class); |
| 19 | } |
| 20 | |
| 21 | public function roles() |
| 22 | { |
| 23 | return $this->belongsToMany(Role::class); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @param array|string $permissions |
| 28 | * @param bool $all |
| 29 | * @throws RolePermissionException |
| 30 | */ |
| 31 | public function needed($userId, $permissions, $all = false) |
| 32 | { |
| 33 | if (!$this->userHas($userId, $permissions, !$all)) { |
| 34 | if (!is_array($permissions)) { |
| 35 | $permissions = [$permissions]; |
| 36 | } |
| 37 | $separator = $all ? ' and ' : ' or '; |
| 38 | Log::debug('Unauthorized action : permissions needed -> ' . implode($separator, $permissions)); |
| 39 | throw new RolePermissionException('The permissions ' . implode($separator, $permissions) . ' are needed for this action'); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | private function userHas($userId, $permissions, $or = true) |
| 44 | { |
| 45 | $user = User::find($userId); |
| 46 | $userRoles = $user->roles()->with('permissions')->get(); |
| 47 | $havingPermissions = collect(); |
| 48 | foreach ($userRoles as $role) { |
| 49 | if (!empty($role->getRelations()['permissions'])) { |
| 50 | $rolePermissions = $role->getRelations()['permissions']; |
| 51 | foreach ($rolePermissions as $permission) { |
| 52 | $havingPermissions->push($permission); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | $userPermissions = $user->permissions()->get(); |
| 58 | |
| 59 | foreach ($userPermissions as $permission) { |
| 60 | $havingPermissions->push($permission); |
| 61 | } |
| 62 | |
| 63 | if (!is_array($permissions)) { |
| 64 | $permissions = [$permissions]; |
| 65 | } |
| 66 | |
| 67 | foreach ($permissions as $permission) { |
| 68 | if ($or === true) { |
| 69 | if($havingPermissions->contains('name', $permission)) { |
| 70 | return true; |
| 71 | } |
| 72 | } else { |
| 73 | if(!$havingPermissions->contains('name', $permission)) { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return !$or; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Prepare a date for array / JSON serialization. |
| 84 | * |
| 85 | * @param \DateTimeInterface $date |
| 86 | * @return string |
| 87 | */ |
| 88 | protected function serializeDate(\DateTimeInterface $date) |
| 89 | { |
| 90 | return $date->format('Y-m-d H:i:s'); |
| 91 | } |
| 92 | |
| 93 | } |