220 lines
5.4 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Auth;
2020-05-02 14:53:06 +03:00
use App\Traits\Tenants;
2017-09-14 22:21:00 +03:00
use App\Notifications\Auth\Reset;
2019-11-16 10:21:14 +03:00
use App\Traits\Media;
2017-09-14 22:21:00 +03:00
use Date;
2020-10-14 17:07:59 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2017-09-14 22:21:00 +03:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Kyslik\ColumnSortable\Sortable;
2020-06-29 23:28:57 +03:00
use Laratrust\Traits\LaratrustUserTrait;
2019-11-16 10:21:14 +03:00
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
2017-09-14 22:21:00 +03:00
class User extends Authenticatable
{
2020-10-14 17:07:59 +03:00
use HasFactory, LaratrustUserTrait, Notifiable, SearchString, SoftDeletes, Sortable, Media, Tenants;
2017-09-14 22:21:00 +03:00
protected $table = 'users';
2020-05-02 14:33:41 +03:00
protected $tenantable = false;
2017-09-14 22:21:00 +03:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
2020-01-21 11:59:52 +03:00
protected $fillable = ['name', 'email', 'password', 'locale', 'enabled', 'landing_page'];
2017-09-14 22:21:00 +03:00
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['last_logged_in_at', 'created_at', 'updated_at', 'deleted_at'];
2020-07-27 13:08:26 +03:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'enabled' => 'boolean',
];
2017-10-07 23:52:34 +03:00
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'email', 'enabled'];
public static function boot()
{
parent::boot();
static::retrieved(function($model) {
$model->setCompanyIds();
});
2020-06-25 00:48:15 +03:00
static::saving(function($model) {
$model->unsetCompanyIds();
});
}
2017-09-14 22:21:00 +03:00
public function companies()
{
2018-06-10 02:48:51 +03:00
return $this->morphToMany('App\Models\Common\Company', 'user', 'user_companies', 'user_id', 'company_id');
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
public function contact()
{
return $this->hasOne('App\Models\Common\Contact', 'user_id', 'id');
}
public function dashboards()
2017-09-14 22:21:00 +03:00
{
2020-01-07 17:15:00 +03:00
return $this->morphToMany('App\Models\Common\Dashboard', 'user', 'user_dashboards', 'user_id', 'dashboard_id');
2019-12-22 15:58:48 +03:00
}
2017-09-14 22:21:00 +03:00
/**
* Always capitalize the name when we retrieve it
*/
public function getNameAttribute($value)
{
return ucfirst($value);
}
/**
* Always return a valid picture when we retrieve it
*/
public function getPictureAttribute($value)
{
2017-09-28 18:10:13 +03:00
// Check if we should use gravatar
2019-11-16 10:21:14 +03:00
if (setting('default.use_gravatar', '0') == '1') {
2017-11-21 18:33:59 +03:00
try {
// Check for gravatar
$url = 'https://www.gravatar.com/avatar/' . md5(strtolower($this->getAttribute('email'))).'?size=90&d=404';
2017-09-14 22:21:00 +03:00
2017-11-21 18:33:59 +03:00
$client = new \GuzzleHttp\Client(['verify' => false]);
2017-09-14 22:21:00 +03:00
2017-11-21 18:33:59 +03:00
$client->request('GET', $url)->getBody()->getContents();
$value = $url;
2020-06-29 23:28:57 +03:00
} catch (\GuzzleHttp\Exception\RequestException $e) {
2017-09-14 22:21:00 +03:00
// 404 Not Found
}
}
if (!empty($value)) {
return $value;
2018-01-10 18:21:12 +03:00
} elseif (!$this->hasMedia('picture')) {
return false;
}
return $this->getMedia('picture')->last();
2017-09-14 22:21:00 +03:00
}
/**
* Always return a valid picture when we retrieve it
*/
2020-10-20 18:27:26 +03:00
public function getLastLoggedAttribute($value)
2017-09-14 22:21:00 +03:00
{
// Date::setLocale('tr');
if (!empty($value)) {
return Date::parse($value)->diffForHumans();
} else {
return trans('auth.never');
}
}
/**
* Send reset link to user via email
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new Reset($token));
}
/**
* Always capitalize the name when we save it to the database
*/
public function setNameAttribute($value)
{
$this->attributes['name'] = ucfirst($value);
}
/**
* Always hash the password when we save it to the database
*/
public function setPasswordAttribute($value)
{
2018-01-03 19:07:29 +03:00
$this->attributes['password'] = bcrypt($value);
2017-09-14 22:21:00 +03:00
}
/**
* Scope to get all rows filtered, sorted and paginated.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $sort
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCollect($query, $sort = 'name')
{
$request = request();
2019-11-16 10:21:14 +03:00
$search = $request->get('search');
$limit = $request->get('limit', setting('default.list_limit', '25'));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return $query->usingSearchString($search)->sortable($sort)->paginate($limit);
2017-09-14 22:21:00 +03:00
}
2017-10-07 23:52:34 +03:00
/**
* Scope to only include active currencies.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeEnabled($query)
{
return $query->where('enabled', 1);
}
/**
* Convert tax to Array.
*
* @return void
*/
public function setCompanyIds()
{
2020-06-29 23:28:57 +03:00
$this->setAttribute('company_ids', $this->companies->pluck('id')->toArray());
}
2020-06-25 00:48:15 +03:00
public function unsetCompanyIds()
{
$this->offsetUnset('company_ids');
}
2020-10-14 17:07:59 +03:00
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\User::new();
}
2017-09-14 22:21:00 +03:00
}