| Code Coverage | ||||||||||
| Classes and Traits | Functions and Methods | Lines | ||||||||
| Total |  | 0.00% | 0 / 1 |  | 66.67% | 4 / 6 | CRAP |  | 51.85% | 14 / 27 | 
| Role |  | 0.00% | 0 / 1 |  | 66.67% | 4 / 6 | 44.57 |  | 51.85% | 14 / 27 | 
| users |  | 100.00% | 1 / 1 | 1 |  | 100.00% | 1 / 1 | |||
| permissions |  | 100.00% | 1 / 1 | 1 |  | 100.00% | 1 / 1 | |||
| group |  | 100.00% | 1 / 1 | 1 |  | 100.00% | 1 / 1 | |||
| needed |  | 0.00% | 0 / 1 | 9.83 |  | 28.57% | 2 / 7 | |||
| userHas |  | 0.00% | 0 / 1 | 16.00 |  | 50.00% | 8 / 16 | |||
| 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 Qmp\Laravel\Acls\Exceptions\RolePermissionException; | 
| 7 | use Illuminate\Database\Eloquent\Model; | 
| 8 | use Illuminate\Support\Facades\Log; | 
| 9 | |
| 10 | class Role extends Model | 
| 11 | { | 
| 12 | public function users() | 
| 13 | { | 
| 14 | return $this->belongsToMany(User::class); | 
| 15 | } | 
| 16 | |
| 17 | public function permissions() | 
| 18 | { | 
| 19 | return $this->belongsToMany(Permission::class); | 
| 20 | } | 
| 21 | |
| 22 | public function group() | 
| 23 | { | 
| 24 | return $this->belongsTo(RoleGroup::class, 'group_id'); | 
| 25 | } | 
| 26 | |
| 27 | /** | 
| 28 | * @param array|string $roles | 
| 29 | * @throws RolePermissionException | 
| 30 | */ | 
| 31 | public function needed($userId, $roles, $all = false) | 
| 32 | { | 
| 33 | if (!$this->userHas($userId, $roles, !$all)) { | 
| 34 | if (!is_array($roles)) { | 
| 35 | $roles = [$roles]; | 
| 36 | } | 
| 37 | $separator = $all ? ' and ' : ' or '; | 
| 38 | Log::debug('Unauthorized action : role needed -> ' . implode($separator, $roles)); | 
| 39 | throw new RolePermissionException('The roles ' . implode($separator, $roles) . ' are needed for this action'); | 
| 40 | } | 
| 41 | } | 
| 42 | |
| 43 | private function userHas($userId, $roles, $or = true) | 
| 44 | { | 
| 45 | try { | 
| 46 | $user = User::find($userId); | 
| 47 | if (!$user) { | 
| 48 | throw new \Exception('user not exist!' . $userId); | 
| 49 | } | 
| 50 | if (!is_array($roles)) { | 
| 51 | $roles = [$roles]; | 
| 52 | } | 
| 53 | $userRoles = $user->roles()->get(); | 
| 54 | |
| 55 | foreach ($roles as $role) { | 
| 56 | if ($or === true) { | 
| 57 | if($userRoles->contains('name', $role)) { | 
| 58 | return true; | 
| 59 | } | 
| 60 | } else { | 
| 61 | if(!$userRoles->contains('name', $role)) { | 
| 62 | return false; | 
| 63 | } | 
| 64 | } | 
| 65 | } | 
| 66 | |
| 67 | return !$or; | 
| 68 | } catch (\Exception $e) { | 
| 69 | Log::debug($e->getMessage()); | 
| 70 | return false; | 
| 71 | } | 
| 72 | } | 
| 73 | |
| 74 | /** | 
| 75 | * Prepare a date for array / JSON serialization. | 
| 76 | * | 
| 77 | * @param \DateTimeInterface $date | 
| 78 | * @return string | 
| 79 | */ | 
| 80 | protected function serializeDate(\DateTimeInterface $date) | 
| 81 | { | 
| 82 | return $date->format('Y-m-d H:i:s'); | 
| 83 | } | 
| 84 | |
| 85 | } |