minor fix

master
Константин 2023-12-18 09:51:22 +03:00
parent 5ea95c78ff
commit 3560965ffc
2 changed files with 11 additions and 5 deletions

View File

@ -115,15 +115,17 @@ class User extends Authenticatable {
public static function getByData($data, $triggerEvent = false) {
$result = false;
if ($email = trim($data['email'] ?? null)) {
$result = self::query()->where(['email' => $email])->first();
$result = self::query()->where(['email' => $email])->withTrashed()->first();
if (!$result) {
$password = $data['password'] ?? (App::environment('local') ? 'Qwerty1!' : Str::random(8));
$password = $data['password'] ?? self::makeDefaultPassword();
$result = self::create(['email' => $email, 'password' => $password]);
$result->update(['name' => trim($data['name'] ?? null), 'phone' => trim($data['phone'] ?? null)]);
$result->assignRole($data['role'] ?? 'User');
$result->update(['name' => trim($data['name'] ?? null), 'phone' => trim($data['phone'] ?? null), 'position' => $data['position'] ?? null]);
$result->syncRoles($data['role'] ?? 'User');
if ($triggerEvent) event(new UserRegistered($result, $password));
} else {
if ($val = trim($data['phone'] ?? null)) $result->update(['phone' => $val]);
if ($val = trim($data['position'] ?? null)) $result->update(['position' => $val]);
if ($result->trashed()) $result->restore();
}
if ($val = $data['avatar'] ?? null) $result->setAvatar($val);
}
@ -153,6 +155,11 @@ class User extends Authenticatable {
return Hash::check($password, $this->password);
}
public static function makeDefaultPassword(): string {
return App::environment('production') ? Str::random(8) : 'Qwerty1!';
}
public function sendPasswordResetNotification($token) {
Mail::to($this->email)->send(new PasswordResetRequested($this, $token));
}

View File

@ -76,7 +76,6 @@ class UserForms extends FormsService {
public function store(array $data): ?JsonResponse {
$model = User::getByData($data, true);
$model->syncRoles($data['role'] ?? null);
return fractal($model, new UserTransformer())->respond();
}