hasMany(SocialProvider::class); } public function avatar(): BelongsTo { return $this->belongsTo(Asset::class, 'asset_id'); } public function companies(): BelongsToMany { return $this->belongsToMany(Company::class, 'company_members'); } public function membership(): HasMany { return $this->hasMany(CompanyMember::class); } public function advisoryMembership(): HasManyThrough { return $this->hasManyThrough(AdvisoryMember::class, CompanyMember::class); } public function applicationConclusions(): HasMany { return $this->hasMany(Conclusion::class, 'author_id'); } public function getInitialsAttribute(): string { return collect(explode(' ', $this->name))->slice(0, 2)->map(function($item) { return Str::upper(Str::substr($item, 0, 1)); })->join(''); } public function getFirstAndMidNameAttribute(): string { return collect(explode(' ', $this->name))->slice(1, 2)->join(' '); } public function getRoleAttribute() { return $this->roles()->first(); } public function getIsAdminAttribute(): bool { return $this->hasRole('Administrator'); } public function getIsEditorAttribute(): bool { return $this->hasRole('Editor'); } public function getPrivilegesAttribute(): array { return [ 'admin' => $this->isAdmin, 'editor' => $this->isEditor ]; } public static function getByData($data, $triggerEvent = false) { $result = false; if ($email = trim($data['email'] ?? null)) { $result = self::query()->where(['email' => $email])->withTrashed()->first(); if (!$result) { $password = $data['password'] ?? self::makeDefaultPassword(); $result = self::create(['email' => $email, 'password' => $password]); $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); } return $result; } public function setAvatar($val) { $asset = Asset::byUuid($val)->first(); $this->update(['asset_id' => $asset->id ?? null]); } public static function create(array $attributes = []) { if (array_key_exists('password', $attributes)) { $attributes['password'] = Hash::make($attributes['password']); } return static::query()->create($attributes); } public function setPassword(string $password): bool { return $this->update(['password' => Hash::make($password)]); } public function checkPassword(string $password): bool { 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)); } }