1
|
|
|
<?php namespace Anomaly\UsersModule\Role; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Entry\EntryCollection; |
4
|
|
|
use Anomaly\Streams\Platform\Entry\EntryRepository; |
5
|
|
|
use Anomaly\UsersModule\Role\Contract\RoleInterface; |
6
|
|
|
use Anomaly\UsersModule\Role\Contract\RoleRepositoryInterface; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RoleRepositoryInterface |
11
|
|
|
* |
12
|
|
|
* @link http://pyrocms.com/ |
13
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
14
|
|
|
* @author Ryan Thompson <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class RoleRepository extends EntryRepository implements RoleRepositoryInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The role model. |
21
|
|
|
* |
22
|
|
|
* @var RoleModel |
23
|
|
|
*/ |
24
|
|
|
protected $model; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new RoleRepositoryInterface instance. |
28
|
|
|
* |
29
|
|
|
* @param RoleModel $model |
30
|
|
|
*/ |
31
|
|
|
function __construct(RoleModel $model) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->model = $model; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return all but the admin role. |
38
|
|
|
* |
39
|
|
|
* @return RoleCollection |
40
|
|
|
*/ |
41
|
|
|
public function allButAdmin() |
42
|
|
|
{ |
43
|
|
|
return $this->model->where('slug', '!=', 'admin')->get(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Find a role by it's slug. |
48
|
|
|
* |
49
|
|
|
* @param $slug |
50
|
|
|
* @return null|RoleInterface |
51
|
|
|
*/ |
52
|
|
|
public function findBySlug($slug) |
53
|
|
|
{ |
54
|
|
|
return $this->model->where('slug', $slug)->first(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Find a role by a permission key. |
59
|
|
|
* |
60
|
|
|
* @param $permission |
61
|
|
|
* @return null|EntryCollection |
62
|
|
|
*/ |
63
|
|
|
public function findByPermission($permission) |
64
|
|
|
{ |
65
|
|
|
$query = $this->model->newQuery(); |
66
|
|
|
|
67
|
|
|
foreach ((array)$permission as $key) { |
68
|
|
|
$query->where('permissions', 'LIKE', '%"' . str_replace('*', '%', $key) . '"%'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $query->get(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Update permissions for a role. |
76
|
|
|
* |
77
|
|
|
* @param RoleInterface $role |
78
|
|
|
* @param array $permissions |
79
|
|
|
* @return RoleInterface |
80
|
|
|
*/ |
81
|
|
|
public function updatePermissions(RoleInterface $role, array $permissions) |
82
|
|
|
{ |
83
|
|
|
$role->permissions = $permissions; |
|
|
|
|
84
|
|
|
|
85
|
|
|
$role->save(); |
86
|
|
|
|
87
|
|
|
return $role; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.