1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use App\Src\UseCases\Domain\Context\Dto\UserDto; |
6
|
|
|
use App\Src\UseCases\Domain\Ports\OrganizationRepository; |
7
|
|
|
use App\Src\UseCases\Infra\Sql\Model\CharacteristicsModel; |
8
|
|
|
use App\Src\UseCases\Infra\Sql\Model\ContextModel; |
9
|
|
|
use App\Src\UseCases\Infra\Sql\Model\UserCharacteristicsModel; |
10
|
|
|
use Illuminate\Auth\MustVerifyEmail; |
11
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
13
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable; |
14
|
|
|
use Illuminate\Notifications\Notifiable; |
15
|
|
|
use Illuminate\Support\Facades\Mail; |
16
|
|
|
use Laravel\Sanctum\HasApiTokens; |
17
|
|
|
use Spatie\Permission\Traits\HasRoles; |
18
|
|
|
|
19
|
|
|
class User extends Authenticatable implements \Illuminate\Contracts\Auth\MustVerifyEmail |
20
|
|
|
{ |
21
|
|
|
use Notifiable, HasRoles, MustVerifyEmail, HasApiTokens, HasFactory; |
|
|
|
|
22
|
|
|
|
23
|
|
|
protected $fillable = [ |
24
|
|
|
'firstname', 'lastname', 'email', 'password', 'uuid', 'organization_id', "path_picture", "providers" |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
protected $hidden = [ |
28
|
|
|
'password', 'remember_token', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
protected $casts = [ |
32
|
|
|
'email_verified_at' => 'datetime', |
33
|
|
|
'providers' => 'json', |
34
|
|
|
'wiki_stats' => 'json', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
public function adminlte_image() |
38
|
|
|
{ |
39
|
|
|
$urlPicture = $this->path_picture != "" ? asset('storage/'.str_replace('app/public/', '', $this->path_picture)) : null; |
40
|
|
|
if(!isset($urlPicture) || $urlPicture === ""){ |
41
|
|
|
$urlPicture = url('').'/'.config('adminlte.logo_img'); |
42
|
|
|
} |
43
|
|
|
return $urlPicture; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function adminlte_desc() |
47
|
|
|
{ |
48
|
|
|
$desc = $this->firstname.' '.$this->lastname; |
49
|
|
|
if($this->organization_id !== null){ |
50
|
|
|
$organization = app(OrganizationRepository::class)->get($this->organization_id); |
51
|
|
|
$desc .= ' - organisme : '.$organization->name(); |
52
|
|
|
} |
53
|
|
|
return $desc; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getAvatarUrlAttribute() |
57
|
|
|
{ |
58
|
|
|
return $this->adminlte_image(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getBioAttribute() |
62
|
|
|
{ |
63
|
|
|
return $this->context->description; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function adminlte_profile_url() |
67
|
|
|
{ |
68
|
|
|
return 'user/edit/profile'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function fullname() |
72
|
|
|
{ |
73
|
|
|
return $this->firstname.' '.$this->lastname; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function sendEmailVerificationNotification() |
77
|
|
|
{ |
78
|
|
|
$callback = ''; |
79
|
|
|
if(session()->has('wiki_callback')){ |
80
|
|
|
$callback = base64_encode(urldecode(session()->get('wiki_callback'))); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if(session()->has('sso')){ |
84
|
|
|
$sso = session()->get('sso'); |
85
|
|
|
$sig = session()->get('sig'); |
86
|
|
|
$callback = base64_encode(url('discourse/sso?sso='.$sso.'&sig='.$sig)); |
87
|
|
|
} |
88
|
|
|
Mail::to($this->email)->send(new \App\Mail\Auth\VerifyEmail($this, $callback)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function characteristics():BelongsToMany |
92
|
|
|
{ |
93
|
|
|
return $this->belongsToMany(CharacteristicsModel::class, |
94
|
|
|
'user_characteristics', |
95
|
|
|
'user_id', |
96
|
|
|
'characteristic_id' |
97
|
|
|
) |
98
|
|
|
->using(UserCharacteristicsModel::class); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function context() |
102
|
|
|
{ |
103
|
|
|
return $this->hasOne(ContextModel::class, 'id', 'context_id'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function addCharacteristics(array $characteristics) |
107
|
|
|
{ |
108
|
|
|
foreach($characteristics as $characteristicUuid){ |
109
|
|
|
$characteristic = CharacteristicsModel::where('uuid', (string)$characteristicUuid)->first(); |
110
|
|
|
if(isset($characteristic)) { |
111
|
|
|
$this->characteristics()->save($characteristic); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function syncCharacteristics(array $characteristics) |
117
|
|
|
{ |
118
|
|
|
$characteristicsToSync = []; |
119
|
|
|
foreach($characteristics as $characteristicUuid){ |
120
|
|
|
$characteristicModel = CharacteristicsModel::where('uuid', (string)$characteristicUuid)->first(); |
121
|
|
|
if(isset($characteristicModel)) { |
122
|
|
|
$characteristicsToSync[] = $characteristicModel->id; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
$this->characteristics()->sync($characteristicsToSync); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function toDto():UserDto |
129
|
|
|
{ |
130
|
|
|
return new UserDto($this->uuid, $this->firstname, $this->lastname); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|