127 lines
3.1 KiB
PHP
127 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laratrust\Traits\LaratrustUserTrait;
|
|
use Mpociot\Teamwork\Traits\UserHasTeams;
|
|
use Sebastienheyd\Boilerplate\Notifications\NewUser as NewUserNotification;
|
|
use Sebastienheyd\Boilerplate\Notifications\ResetPassword as ResetPasswordNotification;
|
|
use Yadahan\AuthenticationLog\AuthenticationLogable;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use AuthenticationLogable;
|
|
use LaratrustUserTrait;
|
|
use Notifiable;
|
|
use SoftDeletes;
|
|
use UserHasTeams;
|
|
|
|
protected $fillable = ['active', 'last_name', 'first_name', 'email', 'password', 'remember_token', 'last_login'];
|
|
|
|
protected $hidden = ['password', 'remember_token'];
|
|
|
|
protected $casts = [
|
|
'email_verified_at' => 'datetime',
|
|
];
|
|
|
|
public function teams()
|
|
{
|
|
return $this->hasManyThrough(
|
|
'App\Models\Core\Auth\Team',
|
|
'App\Models\Core\Auth\TeamUser',
|
|
'user_id',
|
|
'id',
|
|
'id',
|
|
'team_id'
|
|
);
|
|
}
|
|
|
|
public function scopeByTeam($query, $id)
|
|
{
|
|
return $query->whereHas('teams', function ($query) use ($id) {
|
|
$query->where('id', $id);
|
|
});
|
|
}
|
|
|
|
public function scopeByUniqueTeam($query)
|
|
{
|
|
return $query->has('teams', '=', 1);
|
|
}
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('active', 1);
|
|
}
|
|
|
|
public function sendPasswordResetNotification($token)
|
|
{
|
|
$this->notify(new ResetPasswordNotification($token));
|
|
}
|
|
|
|
public function sendNewUserNotification($token)
|
|
{
|
|
$this->notify(new NewUserNotification($token, $this));
|
|
}
|
|
|
|
public function getLastNameAttribute($value)
|
|
{
|
|
return mb_strtoupper($value);
|
|
}
|
|
|
|
public function getFirstNameAttribute($value)
|
|
{
|
|
return mb_convert_case($value, MB_CASE_TITLE);
|
|
}
|
|
|
|
public function getNameAttribute($value)
|
|
{
|
|
if ($value ?? false) {
|
|
return $value;
|
|
}
|
|
|
|
return $this->first_name.' '.$this->last_name;
|
|
}
|
|
|
|
public function getLastLogin($format = 'YYYY-MM-DD HH:mm:ss', $default = '')
|
|
{
|
|
if ($this->last_login === null) {
|
|
return $default;
|
|
}
|
|
|
|
return Carbon::createFromTimeString($this->last_login)->isoFormat($format);
|
|
}
|
|
|
|
public function getRolesList()
|
|
{
|
|
$res = [];
|
|
foreach ($this->roles as $role) {
|
|
$res[] = __($role->display_name);
|
|
}
|
|
if (! $res ?? true) {
|
|
return '-';
|
|
}
|
|
|
|
return implode(', ', $res);
|
|
}
|
|
|
|
public function getAvatarPathAttribute()
|
|
{
|
|
return public_path('images/avatars/'.md5($this->id.$this->email).'.jpg');
|
|
}
|
|
|
|
public function getAvatarUrlAttribute()
|
|
{
|
|
if (is_file($this->avatar_path)) {
|
|
$ts = filemtime($this->avatar_path);
|
|
|
|
return asset('images/avatars/'.md5($this->id.$this->email).'.jpg?t='.$ts);
|
|
}
|
|
|
|
return asset('/assets/vendor/boilerplate/images/default-user.png');
|
|
}
|
|
}
|