2019-03-05 18:10:11 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2019-03-18 08:53:00 -03:00
|
|
|
use Illuminate\Auth\Authenticatable;
|
2020-07-29 11:58:25 -03:00
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
2019-03-18 08:53:00 -03:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable;
|
2020-07-29 11:58:25 -03:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
use League\Glide\Server;
|
2019-03-05 18:10:11 -03:00
|
|
|
|
2019-03-18 08:53:00 -03:00
|
|
|
class User extends Model implements AuthenticatableContract, AuthorizableContract
|
2019-03-05 18:10:11 -03:00
|
|
|
{
|
2019-03-18 08:53:00 -03:00
|
|
|
use SoftDeletes, Authenticatable, Authorizable;
|
|
|
|
|
2019-04-03 06:24:11 -03:00
|
|
|
protected $casts = [
|
|
|
|
'owner' => 'boolean',
|
|
|
|
];
|
|
|
|
|
2019-03-18 08:53:00 -03:00
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Account::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNameAttribute()
|
|
|
|
{
|
|
|
|
return $this->first_name.' '.$this->last_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPasswordAttribute($password)
|
|
|
|
{
|
2020-03-21 01:58:34 -03:00
|
|
|
$this->attributes['password'] = Hash::needsRehash($password) ? Hash::make($password) : $password;
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:33:47 -03:00
|
|
|
public function photoUrl(array $attributes)
|
|
|
|
{
|
2019-08-09 13:50:45 -03:00
|
|
|
if ($this->photo_path) {
|
|
|
|
return URL::to(App::make(Server::class)->fromPath($this->photo_path, $attributes));
|
2019-08-09 12:33:47 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 19:33:46 -03:00
|
|
|
public function isDemoUser()
|
|
|
|
{
|
2019-12-19 10:50:26 -03:00
|
|
|
return $this->email === 'johndoe@example.com';
|
2019-12-18 19:33:46 -03:00
|
|
|
}
|
|
|
|
|
2019-03-18 08:53:00 -03:00
|
|
|
public function scopeOrderByName($query)
|
|
|
|
{
|
|
|
|
$query->orderBy('last_name')->orderBy('first_name');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeWhereRole($query, $role)
|
|
|
|
{
|
|
|
|
switch ($role) {
|
|
|
|
case 'user': return $query->where('owner', false);
|
|
|
|
case 'owner': return $query->where('owner', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeFilter($query, array $filters)
|
|
|
|
{
|
|
|
|
$query->when($filters['search'] ?? null, function ($query, $search) {
|
|
|
|
$query->where(function ($query) use ($search) {
|
2019-04-02 14:05:17 -03:00
|
|
|
$query->where('first_name', 'like', '%'.$search.'%')
|
|
|
|
->orWhere('last_name', 'like', '%'.$search.'%')
|
|
|
|
->orWhere('email', 'like', '%'.$search.'%');
|
2019-03-18 08:53:00 -03:00
|
|
|
});
|
|
|
|
})->when($filters['role'] ?? null, function ($query, $role) {
|
|
|
|
$query->whereRole($role);
|
|
|
|
})->when($filters['trashed'] ?? null, function ($query, $trashed) {
|
|
|
|
if ($trashed === 'with') {
|
|
|
|
$query->withTrashed();
|
|
|
|
} elseif ($trashed === 'only') {
|
|
|
|
$query->onlyTrashed();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-03-05 18:10:11 -03:00
|
|
|
}
|