1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace App\Models;
use App\Models\Util\Status;
use App\Queries\UserQuery;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
protected $table = "users";
/**
* The attributes that are mass assignable.
* @var array<int, string>
*/
protected $fillable = ['name', 'email', 'password', 'status', 'curso_id', 'campus_id'];
/**
* The attributes that should be hidden for serialization.
* @var array<int, string>
*/
protected $hidden = ['password', 'remember_token'];
/**
* The attributes that should be cast.
* @var array<string, string>
* @var boolean $isUpdate
*/
protected $casts = [
'email_verified_at' => 'datetime'
];
protected $dates = ['deleted_at'];
public static function validator(array $attributes, $id = null)
{
$rules = [
'name' => ['required', 'min:4'],
'email' => ['required', 'email', Rule::unique('users')->ignore($id)],
'curso_id' => ['integer'],
'campus_id' => ['integer'],
'status' => [
Rule::requiredIf ( function() use($id)
{
return (bool) $id;
}),
Rule::in([Status::ATIVO, Status::INATIVO]),
'required_with:id',
'integer',
],
];
$messages = [
//name
'name.min' => 'O campo "Nome" dever ter no mínimo 4 caracteres.',
'name.required' => 'O campo "Nome" é obrigatório.',
//email
'email.required' => 'O campo "E-Mail" é obrigatório.',
'email.email' => 'O campo "E-Mail" deve conter um e-mail valido.',
'email.unique' => 'O "E-Mail" informado já foi cadastrado no sistema.',
//status
'status.required' => 'O campo "Status" é obrigatório.',
'status.in' => 'Selecione uma opção da lista de "Status"!',
'status.integer' => 'O campo "Status" deve cónter um inteiro!',
//curso_id
'curso_id.integer' => 'O campo "Curso" deve cónter um inteiro!',
//campus_id
'campus_id.integer' => 'O campo "Campus" deve cónter um inteiro!',
];
try {
return Validator::make($attributes, $rules, $messages);
} catch(ValidationException $exception) {
}
}
public static function validatorPassword(array $attributes)
{
$rules = [
'password' => ['required', 'min:8', 'max:255', 'confirmed'],
];
$messages = [
'password.required' => 'A "Senha" é obrigatória!',
'password.min' => 'A "Senha" deve contér no minímo 8 caracteres!',
'password.max' => 'A campo "Senha" deve contér no máximo 255 caracteres!',
'password.confirmed' => 'As senhas devem ser iguais!',
];
try{
return Validator::make($attributes, $rules, $messages);
} catch(ValidationException $exception) {
}
}
/**
* Validar os campos de acordo com as regras implementadas
*
*/
// public static function validator($attributes, $rule_password = false) {
// $rules = [
// 'name' => ['required'],
// 'email' => ['required', 'email'],
// ];
// if($rule_password) {
// $rules = [
// 'password' => ['required', 'min:8'],
// 'password_confirmation' => [],
// ];
// }
// $messages = [
// // 'unique' => "O :attribute já está registrado no sistema",
// 'required' => "O :attribute precisa ser preenchido",
// ];
// try {
// return Validator::make($attributes, $rules, $messages);
// } catch(ValidationException $exception) {
// }
// }
/**
* Get Curso with curso.id = user.curso_id
*
* @return Curso|null
*/
public function curso()
{
return $this->belongsTo(Curso::class);
}
/**
* Get Unidade with unidade.id = user.unidade_id
*
* @return Unidade|null
*/
public function unidade()
{
return $this->belongsTo(Unidade::class);
}
public function profile($type_profile)
{
return UserType::initQuery()->whereUser($this->id)->whereType($type_profile)->first();
}
/** @return UserType[]|null */
public function profiles()
{
return $this->hasMany(UserType::class);
}
/**
* @return UserType|Null
*/
public function profileSelected()
{
return $this->profiles()->where('selected', true)->first();
}
/**
* @return bool
*/
public function isTypeAdmin()
{
return $this->profileSelected()->type === UserType::ADMIN;
}
/**
* @return bool
*/
public function isTypeTeacher()
{
return $this->profileSelected()->type === UserType::TEACHER;
}
/**
* @return bool
*/
public function isTypeDirector()
{
return $this->profileSelected()->type === UserType::DIRECTOR;
}
/**
* @return bool
*/
public function isTypeCoordinator()
{
return $this->profileSelected()->type === UserType::COORDINATOR;
}
/**
* @return bool
*/
public function isTypeEvaluator()
{
return $this->profileSelected()->type === UserType::EVALUATOR;
}
public static function initQuery()
{
return new UserQuery(get_called_class());
}
public function dashboardName()
{
$name = $this->name;
$split = explode(' ', $name);
$dashboardName = '';
if(count($split) >= 2) {
$dashboardName = array_shift($split) . ' ' . array_shift($split);
} else {
$dashboardName = array_shift($split);
}
return $dashboardName;
}
/**
* @return string
*/
public function __toString()
{
return $this->name;
}
}