1
|
|
|
<?php namespace App\Modules\Acl\Repositories\V1; |
2
|
|
|
|
3
|
|
|
use App\Modules\Core\AbstractRepositories\AbstractRepository; |
4
|
|
|
|
5
|
|
|
class UserRepository extends AbstractRepository |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Return the model full namespace. |
9
|
|
|
* |
10
|
|
|
* @return string |
11
|
|
|
*/ |
12
|
|
|
protected function getModel() |
13
|
|
|
{ |
14
|
|
|
return 'App\Modules\Acl\AclUser'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Check if the logged in user or the given user |
19
|
|
|
* has the given permissions on the given model. |
20
|
|
|
* |
21
|
|
|
* @param string $nameOfPermission |
22
|
|
|
* @param string $model |
23
|
|
|
* @param boolean $user |
24
|
|
|
* @return boolean |
25
|
|
|
*/ |
26
|
|
|
public function can($nameOfPermission, $model, $user = false ) |
27
|
|
|
{ |
28
|
|
|
$user = $user ?: \Auth::user(); |
29
|
|
|
$permissions = []; |
30
|
|
|
\Core::users()->find($user->id, ['groups.permissions'])->groups->lists('permissions')->each(function ($permission) use (&$permissions, $model){ |
31
|
|
|
$permissions = array_merge($permissions, $permission->where('model', $model)->lists('name')->toArray()); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
return in_array($nameOfPermission, $permissions); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check if the logged in user has the given group. |
39
|
|
|
* |
40
|
|
|
* @param string $groupName |
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
|
|
public function hasGroup($groupName) |
44
|
|
|
{ |
45
|
|
|
$groups = \Core::users()->find(\Auth::user()->id)->groups; |
46
|
|
|
return $groups->lists('name')->search($groupName, true); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Assign the given group ids to the given user. |
51
|
|
|
* |
52
|
|
|
* @param integer $user_id |
53
|
|
|
* @param array $group_ids |
54
|
|
|
* @return object |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function assignGroups($user_id, $group_ids) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
\DB::transaction(function () use ($user_id, $group_ids) { |
59
|
|
|
$user = \Core::users()->find($user_id); |
60
|
|
|
$user->groups()->detach(); |
61
|
|
|
$user->groups()->attach($group_ids); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
return \Core::users()->find($user_id); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.