|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace BristolSU\ControlDB\Traits; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use BristolSU\ControlDB\Contracts\Models\DataUser; |
|
8
|
|
|
use BristolSU\ControlDB\Contracts\Models\Role; |
|
9
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Pivots\Tags\UserUserTag; |
|
10
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Pivots\UserGroup; |
|
11
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Pivots\UserRole; |
|
12
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\User; |
|
13
|
|
|
use BristolSU\ControlDB\Models\Group; |
|
14
|
|
|
use BristolSU\ControlDB\Models\Tags\UserTag; |
|
15
|
|
|
use Illuminate\Support\Collection; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Implements common methods using repositories required by the user model interface |
|
19
|
|
|
*/ |
|
|
|
|
|
|
20
|
|
|
trait UserTrait |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get the attributes for this user |
|
25
|
|
|
* |
|
26
|
|
|
* @return DataUser |
|
27
|
|
|
*/ |
|
28
|
15 |
|
public function data(): DataUser { |
|
|
|
|
|
|
29
|
15 |
|
return app(\BristolSU\ControlDB\Contracts\Repositories\DataUser::class)->getById($this->dataProviderId()); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Set the ID of the data provider |
|
34
|
|
|
* |
|
35
|
|
|
* @param int $dataProviderId |
|
|
|
|
|
|
36
|
|
|
*/ |
|
|
|
|
|
|
37
|
2 |
|
public function setDataProviderId(int $dataProviderId): void |
|
38
|
|
|
{ |
|
39
|
2 |
|
app(User::class)->update($this->id(), $dataProviderId); |
|
|
|
|
|
|
40
|
2 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Tags the user is tagged with |
|
44
|
|
|
* |
|
45
|
|
|
* @return Collection |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function tags(): Collection |
|
48
|
|
|
{ |
|
49
|
2 |
|
return app(UserUserTag::class)->getTagsThroughUser($this); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Roles the user owns |
|
54
|
|
|
* |
|
55
|
|
|
* @return Collection |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function roles(): Collection |
|
58
|
|
|
{ |
|
59
|
2 |
|
return app(UserRole::class)->getRolesThroughUser($this); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Groups the user is a member of |
|
64
|
|
|
* |
|
65
|
|
|
* @return Collection |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function groups(): Collection |
|
68
|
|
|
{ |
|
69
|
2 |
|
return app(UserGroup::class)->getGroupsThroughUser($this); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add a tag to the user |
|
74
|
|
|
* |
|
75
|
|
|
* @param \BristolSU\ControlDB\Contracts\Models\Tags\UserTag $userTag |
|
|
|
|
|
|
76
|
|
|
*/ |
|
|
|
|
|
|
77
|
6 |
|
public function addTag(\BristolSU\ControlDB\Contracts\Models\Tags\UserTag $userTag): void |
|
78
|
|
|
{ |
|
79
|
6 |
|
app(UserUserTag::class)->addTagToUser($userTag, $this); |
|
|
|
|
|
|
80
|
6 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Remove a tag from the user |
|
84
|
|
|
* |
|
85
|
|
|
* @param \BristolSU\ControlDB\Contracts\Models\Tags\UserTag $userTag |
|
|
|
|
|
|
86
|
|
|
*/ |
|
|
|
|
|
|
87
|
2 |
|
public function removeTag(\BristolSU\ControlDB\Contracts\Models\Tags\UserTag $userTag): void |
|
88
|
|
|
{ |
|
89
|
2 |
|
app(UserUserTag::class)->removeTagFromUser($userTag, $this); |
|
|
|
|
|
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Add a role to the user |
|
94
|
|
|
* |
|
95
|
|
|
* @param Role $role |
|
|
|
|
|
|
96
|
|
|
*/ |
|
|
|
|
|
|
97
|
5 |
|
public function addRole(Role $role): void |
|
98
|
|
|
{ |
|
99
|
5 |
|
app(UserRole::class)->addUserToRole($this, $role); |
|
|
|
|
|
|
100
|
5 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Remove a role from the user |
|
104
|
|
|
* |
|
105
|
|
|
* @param Role $role |
|
|
|
|
|
|
106
|
|
|
*/ |
|
|
|
|
|
|
107
|
2 |
|
public function removeRole(Role $role): void |
|
108
|
|
|
{ |
|
109
|
2 |
|
app(UserRole::class)->removeUserFromRole($this, $role); |
|
|
|
|
|
|
110
|
2 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Add a group to the user |
|
114
|
|
|
* |
|
115
|
|
|
* @param \BristolSU\ControlDB\Contracts\Models\Group $group |
|
|
|
|
|
|
116
|
|
|
*/ |
|
|
|
|
|
|
117
|
5 |
|
public function addGroup(\BristolSU\ControlDB\Contracts\Models\Group $group): void |
|
118
|
|
|
{ |
|
119
|
5 |
|
app(UserGroup::class)->addUserToGroup($this, $group); |
|
|
|
|
|
|
120
|
5 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Remove a group from the user |
|
124
|
|
|
* |
|
125
|
|
|
* @param \BristolSU\ControlDB\Contracts\Models\Group $group |
|
|
|
|
|
|
126
|
|
|
*/ |
|
|
|
|
|
|
127
|
2 |
|
public function removeGroup(\BristolSU\ControlDB\Contracts\Models\Group $group): void |
|
128
|
|
|
{ |
|
129
|
2 |
|
app(UserGroup::class)->removeUserFromGroup($this, $group); |
|
|
|
|
|
|
130
|
2 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
} |