|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use LaravelEnso\Comments\App\Traits\Comments; |
|
7
|
|
|
use LaravelEnso\Discussions\App\Traits\Discussions; |
|
8
|
|
|
use LaravelEnso\Discussions\App\Traits\Replies; |
|
9
|
|
|
use Illuminate\Contracts\Translation\HasLocalePreference; |
|
10
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable; |
|
11
|
|
|
use Illuminate\Notifications\Notifiable; |
|
12
|
|
|
use Illuminate\Support\Facades\App; |
|
13
|
|
|
use Illuminate\Support\Facades\DB; |
|
14
|
|
|
use LaravelEnso\ActionLogger\App\Traits\ActionLogs; |
|
15
|
|
|
use App\Traits\enso\avatars\HasAvatar; |
|
16
|
|
|
use LaravelEnso\Core\App\Enums\UserGroups; |
|
17
|
|
|
use LaravelEnso\Core\App\Exceptions\UserConflict; |
|
18
|
|
|
use LaravelEnso\Core\App\Services\DefaultPreferences; |
|
19
|
|
|
use LaravelEnso\Core\App\Traits\HasPassword; |
|
20
|
|
|
use App\Traits\enso\DynamicMethods\Relations; |
|
21
|
|
|
use LaravelEnso\Files\App\Traits\Uploads; |
|
22
|
|
|
use LaravelEnso\Helpers\App\Contracts\Activatable; |
|
23
|
|
|
use LaravelEnso\Helpers\App\Traits\ActiveState; |
|
24
|
|
|
use LaravelEnso\Helpers\App\Traits\AvoidsDeletionConflicts; |
|
25
|
|
|
use LaravelEnso\Helpers\App\Traits\CascadesMorphMap; |
|
26
|
|
|
use LaravelEnso\Impersonate\App\Traits\Impersonates; |
|
27
|
|
|
use LaravelEnso\People\App\Traits\IsPerson; |
|
28
|
|
|
use LaravelEnso\Rememberable\App\Traits\Rememberable; |
|
29
|
|
|
use LaravelEnso\Roles\App\Enums\Roles; |
|
30
|
|
|
use LaravelEnso\Tables\App\Traits\TableCache; |
|
31
|
|
|
use LaravelEnso\Teams\App\Models\Team; |
|
32
|
|
|
use LaravelEnso\Calendar\App\Models\Event; |
|
33
|
|
|
use LaravelEnso\Files\App\Models\File; |
|
34
|
|
|
use App\Person; |
|
35
|
|
|
use App\Models\enso\Roles\Role; |
|
36
|
|
|
use App\Models\enso\core\Preference; |
|
37
|
|
|
use App\Models\enso\core\UserGroup; |
|
38
|
|
|
|
|
39
|
|
|
class User extends Authenticatable implements Activatable, HasLocalePreference |
|
40
|
|
|
{ |
|
41
|
|
|
use Comments, Discussions, Replies, ActionLogs, ActiveState, AvoidsDeletionConflicts, CascadesMorphMap, |
|
|
|
|
|
|
42
|
|
|
HasAvatar, HasPassword, Impersonates, IsPerson, Notifiable, |
|
43
|
|
|
Relations, Rememberable, TableCache, Uploads; |
|
44
|
|
|
|
|
45
|
|
|
protected $hidden = ['password', 'remember_token', 'password_updated_at']; |
|
46
|
|
|
|
|
47
|
|
|
protected $fillable = ['person_id', 'group_id', 'role_id', 'email', 'is_active','email_verified_at','password']; |
|
48
|
|
|
|
|
49
|
|
|
protected $casts = [ |
|
50
|
|
|
'is_active' => 'boolean', 'person_id' => 'int', 'group_id' => 'int', 'role_id' => 'int', |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
protected $dates = ['password_updated_at']; |
|
54
|
|
|
|
|
55
|
|
|
public function person() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->belongsTo(Person::class); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function group() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->belongsTo(UserGroup::class); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function teams() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->belongsToMany(Team::class); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function role() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->belongsTo(Role::class); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function company() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->person->company(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function events() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->belongsToMany(Event::class); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function files() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->hasMany(File::class, 'created_by'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function logins() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->hasMany(Login::class); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function preference() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->hasOne(Preference::class); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function isAdmin() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->role_id === App::make(Roles::class)::Admin; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function isSupervisor() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->role_id === App::make(Roles::class)::Supervisor; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function belongsToAdminGroup() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->group_id === App::make(UserGroups::class)::Admin; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function isPerson(Person $person) |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->person_id === $person->id; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function resetPreferences() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->storePreferences($this->defaultPreferences()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function preferences() |
|
126
|
|
|
{ |
|
127
|
|
|
$preferences = $this->preference |
|
128
|
|
|
? $this->preference->value |
|
129
|
|
|
: $this->defaultPreferences()->value; |
|
130
|
|
|
|
|
131
|
|
|
unset($this->preference); |
|
132
|
|
|
|
|
133
|
|
|
return $preferences; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function preferredLocale() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->lang(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function lang() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->preferences()->global->lang; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function storeGlobalPreferences($global) |
|
147
|
|
|
{ |
|
148
|
|
|
$preferences = $this->preferences(); |
|
149
|
|
|
$preferences->global = $global; |
|
150
|
|
|
|
|
151
|
|
|
$this->storePreferences($preferences); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function storeLocalPreferences($route, $value) |
|
155
|
|
|
{ |
|
156
|
|
|
$preferences = $this->preferences(); |
|
157
|
|
|
$preferences->local->$route = $value; |
|
158
|
|
|
|
|
159
|
|
|
$this->storePreferences($preferences); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function erase(bool $person = false) |
|
163
|
|
|
{ |
|
164
|
|
|
if ($person) { |
|
165
|
|
|
return DB::transaction(fn () => tap($this)->delete()->person->delete()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $this->delete(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function delete() |
|
172
|
|
|
{ |
|
173
|
|
|
if ($this->logins()->exists()) { |
|
174
|
|
|
throw UserConflict::hasActivity(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
try { |
|
178
|
|
|
return parent::delete(); |
|
179
|
|
|
} catch (Exception $exception) { |
|
180
|
|
|
throw UserConflict::hasActivity(); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
private function storePreferences($preferences) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->preference()->updateOrCreate( |
|
187
|
|
|
['user_id' => $this->id], |
|
188
|
|
|
['value' => $preferences] |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
private function defaultPreferences() |
|
193
|
|
|
{ |
|
194
|
|
|
return new Preference([ |
|
195
|
|
|
'value' => DefaultPreferences::data(), |
|
196
|
|
|
]); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|