1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace BristolSU\ControlDB\Traits; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use BristolSU\ControlDB\Contracts\Models\DataRole as DataRoleModel; |
8
|
|
|
use BristolSU\ControlDB\Contracts\Models\User; |
9
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\DataRole as DataRoleRepository; |
10
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Pivots\Tags\RoleRoleTag; |
11
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Pivots\UserRole; |
12
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Role; |
13
|
|
|
use BristolSU\ControlDB\Models\Group; |
14
|
|
|
use BristolSU\ControlDB\Models\Position; |
15
|
|
|
use Illuminate\Support\Collection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Implements common methods using repositories required by the role model interface |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
trait RoleTrait |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the data attributes of the role |
25
|
|
|
* |
26
|
|
|
* @return DataRoleModel |
27
|
|
|
*/ |
28
|
16 |
|
public function data(): DataRoleModel |
29
|
|
|
{ |
30
|
16 |
|
return app(DataRoleRepository::class)->getById($this->dataProviderId()); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the ID of the data provider |
35
|
|
|
* |
36
|
|
|
* @param int $dataProviderId |
|
|
|
|
37
|
|
|
*/ |
|
|
|
|
38
|
2 |
|
public function setDataProviderId(int $dataProviderId): void |
39
|
|
|
{ |
40
|
2 |
|
app(Role::class)->update($this->id(), $this->positionId(), $this->groupId(), $dataProviderId); |
|
|
|
|
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set a group ID |
45
|
|
|
* |
46
|
|
|
* @param int $groupId |
|
|
|
|
47
|
|
|
*/ |
|
|
|
|
48
|
2 |
|
public function setGroupId(int $groupId): void |
49
|
|
|
{ |
50
|
2 |
|
app(Role::class)->update($this->id(), $this->positionId(), $groupId, $this->dataProviderId()); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set a position ID |
55
|
|
|
* |
56
|
|
|
* @param int $positionId |
|
|
|
|
57
|
|
|
*/ |
|
|
|
|
58
|
2 |
|
public function setPositionId(int $positionId): void |
59
|
|
|
{ |
60
|
2 |
|
app(Role::class)->update($this->id(), $positionId, $this->groupId(), $this->dataProviderId()); |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Position belonging to the role |
65
|
|
|
* |
66
|
|
|
* @return Position |
67
|
|
|
*/ |
68
|
2 |
|
public function position(): \BristolSU\ControlDB\Contracts\Models\Position |
69
|
|
|
{ |
70
|
2 |
|
return app(\BristolSU\ControlDB\Contracts\Repositories\Position::class)->getById($this->positionId()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Group belonging to the role |
75
|
|
|
* |
76
|
|
|
* @return Group |
77
|
|
|
*/ |
78
|
2 |
|
public function group(): \BristolSU\ControlDB\Contracts\Models\Group |
79
|
|
|
{ |
80
|
2 |
|
return app(\BristolSU\ControlDB\Contracts\Repositories\Group::class)->getById($this->groupId()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Users who occupy the role |
85
|
|
|
* |
86
|
|
|
* @return Collection |
87
|
|
|
*/ |
88
|
3 |
|
public function users(): Collection |
89
|
|
|
{ |
90
|
3 |
|
return app(UserRole::class)->getUsersThroughRole($this); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Tags the role is tagged with |
95
|
|
|
* |
96
|
|
|
* @return Collection |
97
|
|
|
*/ |
98
|
2 |
|
public function tags(): Collection |
99
|
|
|
{ |
100
|
2 |
|
return app(RoleRoleTag::class)->getTagsThroughRole($this); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add a tag to the role |
105
|
|
|
* |
106
|
|
|
* @param \BristolSU\ControlDB\Contracts\Models\Tags\RoleTag $roleTag |
|
|
|
|
107
|
|
|
*/ |
|
|
|
|
108
|
6 |
|
public function addTag(\BristolSU\ControlDB\Contracts\Models\Tags\RoleTag $roleTag): void |
109
|
|
|
{ |
110
|
6 |
|
app(RoleRoleTag::class)->addTagToRole($roleTag, $this); |
|
|
|
|
111
|
6 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Remove a tag from the role |
115
|
|
|
* |
116
|
|
|
* @param \BristolSU\ControlDB\Contracts\Models\Tags\RoleTag $roleTag |
|
|
|
|
117
|
|
|
*/ |
|
|
|
|
118
|
2 |
|
public function removeTag(\BristolSU\ControlDB\Contracts\Models\Tags\RoleTag $roleTag): void |
119
|
|
|
{ |
120
|
2 |
|
app(RoleRoleTag::class)->removeTagFromRole($roleTag, $this); |
|
|
|
|
121
|
2 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Add a user to the role |
125
|
|
|
* |
126
|
|
|
* @param User $user |
|
|
|
|
127
|
|
|
*/ |
|
|
|
|
128
|
5 |
|
public function addUser(User $user): void |
129
|
|
|
{ |
130
|
5 |
|
app(UserRole::class)->addUserToRole($user, $this); |
|
|
|
|
131
|
5 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Remove a user from the role |
135
|
|
|
* |
136
|
|
|
* @param User $user |
|
|
|
|
137
|
|
|
*/ |
|
|
|
|
138
|
2 |
|
public function removeUser(User $user): void |
139
|
|
|
{ |
140
|
2 |
|
app(UserRole::class)->removeUserFromRole($user, $this); |
|
|
|
|
141
|
2 |
|
} |
142
|
|
|
|
143
|
|
|
} |