427 lines
10 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Auth;
2022-06-01 10:15:55 +03:00
use Akaunting\Sortable\Traits\Sortable;
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;
2022-06-01 10:15:55 +03:00
use App\Traits\Tenants;
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;
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 = [
2023-04-27 00:18:48 +03:00
'enabled' => 'boolean',
'last_logged_in_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
2020-11-13 15:15:27 +03:00
];
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
2017-10-07 23:52:34 +03:00
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'email', 'enabled'];
public static function boot()
{
parent::boot();
2022-06-01 10:15:55 +03:00
static::retrieved(function ($model) {
$model->setCompanyIds();
});
2020-06-25 00:48:15 +03:00
2022-06-01 10:15:55 +03:00
static::saving(function ($model) {
2020-06-25 00:48:15 +03:00
$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
}
2022-06-29 10:48:57 +03:00
public function invitation()
{
return $this->hasOne('App\Models\Auth\UserInvitation', 'user_id', 'id');
}
2022-07-20 17:22:01 +03:00
public function roles()
{
return $this->belongsToMany('App\Models\Auth\Role', 'App\Models\Auth\UserRole');
}
2017-09-14 22:21:00 +03:00
/**
* Always capitalize the name when we retrieve it
*/
public function getNameAttribute($value)
{
2022-06-01 10:15:55 +03:00
if (empty($value)) {
return trans('general.na');
}
2017-09-14 22:21:00 +03:00
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
2022-06-01 10:15:55 +03:00
$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;
2022-10-31 15:29:45 +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');
}
}
/**
* 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
}
2022-06-01 10:15:55 +03:00
/**
* Send reset link to user via email
*/
public function sendPasswordResetNotification($token)
{
2022-08-18 18:59:00 +03:00
$this->notify(new Reset($token, $this->email));
2022-06-01 10:15:55 +03:00
}
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');
/**
* Modules that use the sort parameter in CRUD operations cause an error,
* so this sort parameter set back to old value after the query is executed.
2023-03-16 16:36:13 +03:00
*
* for Custom Fields module
*/
$request_sort = $request->get('sort');
$query->usingSearchString($search)->sortable($sort);
$request->merge(['sort' => $request_sort]);
// This line disabled because broken sortable issue.
//$request->offsetUnset('direction');
$limit = (int) $request->get('limit', setting('default.list_limit', '25'));
2017-09-14 22:21:00 +03:00
return $query->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');
}
2023-04-27 11:44:40 +03:00
/**
* Scope to only employees.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsEmployee($query)
{
return $query->whereHasRole('employee');
}
/**
* Scope to only users.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotEmployee($query)
{
return $query->wherePermissionIs('read-admin-panel');
}
2023-04-24 11:02:45 +03:00
public function scopeEmail($query, $email)
{
return $query->where('email', '=', $email);
}
/**
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');
}
2023-04-27 11:44:40 +03:00
/**
* Determine if user is a employee.
*
* @return bool
*/
public function isEmployee()
{
return (bool) $this->hasRole('employee');
}
/**
* Determine if user is not a employee.
*
* @return bool
*/
public function isNotEmployee()
{
return (bool) ! $this->hasRole('employee');
}
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;
}
2022-06-01 10:15:55 +03:00
/**
* Get the line actions.
*
* @return array
*/
public function getLineActionsAttribute()
{
$actions = [];
if (user()->id == $this->id) {
return $actions;
}
2022-06-28 21:58:44 +03:00
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route('users.edit', $this->id),
'permission' => 'update-auth-users',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-show-user-' . $this->id,
],
2022-06-28 21:58:44 +03:00
];
2022-06-01 10:15:55 +03:00
if ($this->hasPendingInvitation()) {
$actions[] = [
'title' => trans('general.resend') . ' ' . trans_choice('general.invitations', 1),
'icon' => 'replay',
'url' => route('users.invite', $this->id),
'permission' => 'update-auth-users',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-resend-user-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
}
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
'route' => 'users.destroy',
'permission' => 'delete-auth-users',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-delete-user-' . $this->id,
],
2022-06-01 10:15:55 +03:00
'model' => $this,
];
return $actions;
}
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
}