1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models\Access\User; |
4
|
|
|
|
5
|
|
|
use App\Models\Access\User\Traits\Attribute\UserAttribute; |
6
|
|
|
use App\Models\Access\User\Traits\Relationship\UserRelationship; |
7
|
|
|
use App\Models\Access\User\Traits\Scope\UserScope; |
8
|
|
|
use App\Models\Access\User\Traits\UserAccess; |
9
|
|
|
use App\Models\Access\User\Traits\UserSendPasswordReset; |
10
|
|
|
use App\Models\Elementset; |
11
|
|
|
use App\Models\ElementsetUser; |
12
|
|
|
use App\Models\Project; |
13
|
|
|
use App\Models\ProjectUser; |
14
|
|
|
use App\Models\Vocabulary; |
15
|
|
|
use App\Models\VocabularyUser; |
16
|
|
|
use Illuminate\Database\Eloquent\Collection; |
17
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
18
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
19
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable; |
20
|
|
|
use Illuminate\Notifications\Notifiable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* App\Models\Access\User\User |
24
|
|
|
* |
25
|
|
|
* @property int $id |
26
|
|
|
* @property \Carbon\Carbon|null $created_at |
27
|
|
|
* @property \Carbon\Carbon|null $updated_at |
28
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
29
|
|
|
* @property string|null $nickname |
30
|
|
|
* @property string|null $salutation |
31
|
|
|
* @property string|null $first_name |
32
|
|
|
* @property string|null $last_name |
33
|
|
|
* @property string|null $email |
34
|
|
|
* @property int|null $is_administrator |
35
|
|
|
* @property string|null $password |
36
|
|
|
* @property int $status |
37
|
|
|
* @property string|null $culture |
38
|
|
|
* @property string|null $confirmation_code |
39
|
|
|
* @property string $name |
40
|
|
|
* @property int $confirmed |
41
|
|
|
* @property string|null $remember_token |
42
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Elementset[] $elementsets |
43
|
|
|
* @property-read string $action_buttons |
44
|
|
|
* @property-read string $change_password_button |
45
|
|
|
* @property-read string $clear_session_button |
46
|
|
|
* @property-read string $confirmed_button |
47
|
|
|
* @property-read string $confirmed_label |
48
|
|
|
* @property-read string $delete_button |
49
|
|
|
* @property-read string $delete_permanently_button |
50
|
|
|
* @property-read string $edit_button |
51
|
|
|
* @property-read string $full_name |
52
|
|
|
* @property-read mixed $github_token |
53
|
|
|
* @property-read string $login_as_button |
54
|
|
|
* @property-read mixed $picture |
55
|
|
|
* @property-read string $restore_button |
56
|
|
|
* @property-read string $show_button |
57
|
|
|
* @property-read string $status_button |
58
|
|
|
* @property-read string $status_label |
59
|
|
|
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications |
60
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Project[] $projects |
61
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Access\User\SocialLogin[] $providers |
62
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Access\Role\Role[] $roles |
63
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\System\Session[] $sessions |
64
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Access\User\SocialLogin[] $socialLogins |
65
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Vocabulary[] $vocabularies |
66
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User active($status = true) |
67
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User confirmed($confirmed = true) |
68
|
|
|
* @method static bool|null forceDelete() |
69
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Access\User\User onlyTrashed() |
70
|
|
|
* @method static bool|null restore() |
71
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereConfirmationCode($value) |
72
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereConfirmed($value) |
73
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereCreatedAt($value) |
74
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereCulture($value) |
75
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereDeletedAt($value) |
76
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereEmail($value) |
77
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereFirstName($value) |
78
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereId($value) |
79
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereIsAdministrator($value) |
80
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereLastName($value) |
81
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereName($value) |
82
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereNickname($value) |
83
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User wherePassword($value) |
84
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereRememberToken($value) |
85
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereSalutation($value) |
86
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereStatus($value) |
87
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Access\User\User whereUpdatedAt($value) |
88
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Access\User\User withTrashed() |
89
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Access\User\User withoutTrashed() |
90
|
|
|
* @mixin \Eloquent |
91
|
|
|
*/ |
92
|
|
|
class User extends Authenticatable |
93
|
|
|
{ |
94
|
|
|
use UserScope, UserAccess, Notifiable, SoftDeletes, UserAttribute, UserRelationship, UserSendPasswordReset; |
95
|
|
|
/** |
96
|
|
|
* The database table used by the model. |
97
|
|
|
* |
98
|
|
|
* @var string |
99
|
|
|
*/ |
100
|
|
|
protected $table = self::TABLE; |
101
|
|
|
public const TABLE = 'users'; |
102
|
|
|
|
103
|
|
|
protected $guarded = ['id', 'is_administrator']; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* The attributes that should be hidden for arrays. |
107
|
|
|
* |
108
|
|
|
* @var array |
109
|
|
|
*/ |
110
|
|
|
protected $hidden = ['password', 'remember_token']; |
111
|
|
|
/** |
112
|
|
|
* @var array |
113
|
|
|
*/ |
114
|
|
|
protected $dates = ['deleted_at']; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $attributes |
118
|
|
|
*/ |
119
|
|
|
public function __construct(array $attributes = []) |
120
|
|
|
{ |
121
|
|
|
parent::__construct($attributes); |
122
|
|
|
$this->table = config('access.users_table'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function isAdminForProjectId(int $project_id): bool |
126
|
|
|
{ |
127
|
|
|
return (bool) ProjectUser::where([ |
128
|
|
|
['user_id', '=', $this->id], |
129
|
|
|
['agent_id', '=', $project_id], |
130
|
|
|
['is_admin_for', '=', true], |
131
|
|
|
])->count(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function isAdminForVocabulary(Vocabulary $vocabulary): bool |
135
|
|
|
{ |
136
|
|
|
return (bool) VocabularyUser::where([ |
137
|
|
|
['user_id', '=', $this->id], |
138
|
|
|
['is_admin_for', '=', true], |
139
|
|
|
])->count() or $this->isAdminForProjectId($vocabulary->agent_id); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function isMaintainerForVocabulary(Vocabulary $vocabulary): bool |
143
|
|
|
{ |
144
|
|
|
return (bool) VocabularyUser::where([ |
145
|
|
|
['user_id', '=', $this->id], |
146
|
|
|
['is_maintainer_for', '=', true], |
147
|
|
|
])->count() or $this->isAdminForVocabulary($vocabulary); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function isAdminForElementSet(Elementset $elementset): bool |
151
|
|
|
{ |
152
|
|
|
return (bool) ElementsetUser::where([ |
153
|
|
|
['user_id', '=', $this->id], |
154
|
|
|
['is_admin_for', '=', true], |
155
|
|
|
])->count() or $this->isAdminForProjectId($elementset->agent_id); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function isMaintainerForElementSet(Elementset $elementset): bool |
159
|
|
|
{ |
160
|
|
|
return (bool) ElementsetUser::where([ |
161
|
|
|
['user_id', '=', $this->id], |
162
|
|
|
['is_maintainer_for', '=', true], |
163
|
|
|
])->count() or $this->isAdminForElementSet($elementset); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function isMemberOfProject(Project $project): bool |
167
|
|
|
{ |
168
|
|
|
return (bool) $this->projects()->wherePivot('agent_id', $project->id)->count(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function projects(): ?BelongsToMany |
172
|
|
|
{ |
173
|
|
|
return $this->belongsToMany(Project::class, ProjectUser::TABLE, 'user_id', 'agent_id') |
174
|
|
|
->withPivot('is_registrar_for', 'is_admin_for', 'is_maintainer_for', 'authorized_as', 'languages') |
175
|
|
|
->withTimestamps(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function vocabularies(): ?BelongsToMany |
179
|
|
|
{ |
180
|
|
|
return $this->belongsToMany(Vocabulary::class, |
181
|
|
|
VocabularyUser::TABLE, |
182
|
|
|
'user_id', |
183
|
|
|
'vocabulary_id')->withPivot('is_maintainer_for', |
184
|
|
|
'is_registrar_for', |
185
|
|
|
'is_admin_for', |
186
|
|
|
'languages', |
187
|
|
|
'default_language', |
188
|
|
|
'current_language')->withTimestamps(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function elementsets(): ?BelongsToMany |
192
|
|
|
{ |
193
|
|
|
return $this->belongsToMany(Elementset::class, |
194
|
|
|
ElementsetUser::TABLE, |
195
|
|
|
'user_id', |
196
|
|
|
'schema_id') |
197
|
|
|
->withPivot('is_registrar_for', 'is_admin_for', 'is_maintainer_for') |
198
|
|
|
->withTimestamps(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function getGithubTokenAttribute(){ |
202
|
|
|
return $this->githubLogin()->token; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function githubLogin(){ |
206
|
|
|
return $this->socialLogins()->where( 'provider', '=', 'github')->first(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function socialLogins() |
210
|
|
|
{ |
211
|
|
|
return $this->hasMany(SocialLogin::class, 'user_id'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param Collection|array $purgeKeys is a collection or array of user ids to purge from the list |
216
|
|
|
* |
217
|
|
|
* @return Collection of id -> nickname (user name) key:value pairs |
218
|
|
|
*/ |
219
|
|
|
public static function getUsersForSelect($purgeKeys = []): Collection |
220
|
|
|
{ |
221
|
|
|
return self::orderBy('nickname')->get(['id', 'nickname', 'first_name', 'last_name'])->mapWithKeys(function ($item) { |
222
|
|
|
return [$item['id'] => self::getCombinedName($item)]; |
223
|
|
|
})->diffKeys($purgeKeys); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public static function getCombinedName($user) |
227
|
|
|
{ |
228
|
|
|
$name = trim($user['first_name'] . ' ' . $user['last_name']); |
229
|
|
|
|
230
|
|
|
return $name ? $user['nickname'] . ' (' . $name . ')' : $user['nickname']; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.