307 lines
7.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;
2021-09-07 10:33:34 +03:00
use App\Traits\Owners;
use App\Traits\Sources;
2021-04-16 00:59:43 +03:00
use App\Traits\Users;
2021-06-17 10:59:07 +03:00
use App\Utilities\Date;
2021-04-16 00:59:43 +03:00
use Illuminate\Contracts\Translation\HasLocalePreference;
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
2021-04-16 00:59:43 +03:00
class User extends Authenticatable implements HasLocalePreference
2017-09-14 22:21:00 +03:00
{
2021-09-07 10:33:34 +03:00
use HasFactory, LaratrustUserTrait, Media, Notifiable, Owners, SearchString, SoftDeletes, Sortable, Sources, Tenants, Users;
2017-09-14 22:21:00 +03:00
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
2021-09-07 10:33:34 +03:00
protected $fillable = ['name', 'email', 'password', 'locale', 'enabled', 'landing_page', 'created_from', 'created_by'];
2017-09-14 22:21:00 +03:00
/**
2020-11-13 15:15:27 +03:00
* The attributes that should be cast.
2017-09-14 22:21:00 +03:00
*
* @var array
*/
2020-11-13 15:15:27 +03:00
protected $casts = [
'enabled' => 'boolean',
];
2017-09-14 22:21:00 +03:00
/**
2020-11-13 15:15:27 +03:00
* The attributes that should be hidden for arrays.
2017-09-14 22:21:00 +03:00
*
* @var array
*/
2020-11-13 15:15:27 +03:00
protected $hidden = ['password', 'remember_token'];
2017-09-14 22:21:00 +03:00
2020-07-27 13:08:26 +03:00
/**
2020-11-13 15:15:27 +03:00
* The attributes that should be mutated to dates.
2020-07-27 13:08:26 +03:00
*
* @var array
*/
2020-11-13 15:15:27 +03:00
protected $dates = ['last_logged_in_at', 'created_at', 'updated_at', 'deleted_at'];
2020-07-27 13:08:26 +03:00
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()
{
2022-03-02 12:19:46 +03:00
return $this->belongsToMany('App\Models\Common\Company', 'App\Models\Auth\UserCompany');
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
{
2022-03-02 12:19:46 +03:00
return $this->belongsToMany('App\Models\Common\Dashboard', 'App\Models\Auth\UserDashboard');
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 = (int) $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
/**
2021-09-01 23:16:41 +03:00
* Scope to only include active users.
2017-10-07 23:52:34 +03:00
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeEnabled($query)
{
return $query->where('enabled', 1);
}
2021-09-01 23:16:41 +03:00
/**
* Scope to only customers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsCustomer($query)
{
return $query->wherePermissionIs('read-client-portal');
}
/**
* Scope to only users.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotCustomer($query)
{
return $query->wherePermissionIs('read-admin-panel');
}
/**
2021-04-16 00:59:43 +03:00
* Attach company_ids attribute to model.
*
* @return void
*/
public function setCompanyIds()
{
2021-04-16 00:59:43 +03:00
$company_ids = $this->withoutEvents(function () {
return $this->companies->pluck('id')->toArray();
});
$this->setAttribute('company_ids', $company_ids);
}
2020-06-25 00:48:15 +03:00
2021-04-16 00:59:43 +03:00
/**
* Detach company_ids attribute from model.
*
* @return void
*/
2020-06-25 00:48:15 +03:00
public function unsetCompanyIds()
{
$this->offsetUnset('company_ids');
}
2020-10-14 17:07:59 +03:00
2021-09-01 23:16:41 +03:00
/**
* Determine if user is a customer.
*
* @return bool
*/
public function isCustomer()
{
return (bool) $this->can('read-client-portal');
}
/**
* Determine if user is not a customer.
*
* @return bool
*/
public function isNotCustomer()
{
return (bool) $this->can('read-admin-panel');
}
2021-09-07 10:33:34 +03:00
public function scopeSource($query, $source)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('created_from'), $source);
2021-09-07 10:33:34 +03:00
}
public function scopeIsOwner($query)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('created_by'), user_id());
2021-09-07 10:33:34 +03:00
}
public function scopeIsNotOwner($query)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('created_by'), '<>', user_id());
2021-09-07 10:33:34 +03:00
}
public function ownerKey($owner)
{
if ($this->isNotOwnable()) {
return 0;
}
return $this->created_by;
}
2021-04-16 00:59:43 +03:00
/**
* Get the user's preferred locale.
*
* @return string
*/
public function preferredLocale()
{
return $this->locale;
}
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
}