7 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
8 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
13 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
24 protected $fillable = [
'first_name',
'last_name',
'email',
'password',
'username'];
34 'first_name' =>
'required|string|min:1',
35 'last_name' =>
'required|string|min:1',
36 'username' =>
'required|string|min:2|unique:users,username,NULL,deleted_at',
38 'password' =>
'required|min:6',
45 $user_permissions = json_decode($this->permissions,
true);
46 $user_groups = $this->
groups();
48 if (((array_key_exists($section, $user_permissions)) && ($user_permissions[$section]==
'1')) ||
49 ((array_key_exists(
'admin', $user_permissions)) && ($user_permissions[
'admin']==
'1'))) {
53 foreach ($user_groups as $user_group) {
54 $group_permissions = json_decode($user_group->permissions,
true);
55 if (((array_key_exists($section, $group_permissions)) && ($group_permissions[$section]==
'1')) ||
56 ((array_key_exists(
'admin', $group_permissions)) && ($group_permissions[
'admin']==
'1'))) {
65 $user_permissions = json_decode($this->permissions,
true);
66 $user_groups = $this->
groups();
68 if ((array_key_exists(
'superuser', $user_permissions)) && ($user_permissions[
'superuser']==
'1')) {
80 return $this->belongsTo(
'\App\Models\Company',
'company_id');
85 if ($this->activated == 1) {
101 return "{$this->first_name} {$this->last_name}";
114 return config(
'app.url').
'/uploads/avatars/'.$this->avatar;
119 $gravatar = md5(strtolower(trim($this->email)));
121 return "//gravatar.com/avatar/".$gravatar;
133 return $this->hasMany(
'\App\Models\Asset',
'assigned_to')->withTrashed();
141 return $this->belongsToMany(
'\App\Models\Accessory',
'accessories_users',
'assigned_to',
'accessory_id')->withPivot(
'id')->withTrashed();
149 return $this->belongsToMany(
'\App\Models\Consumable',
'consumables_users',
'assigned_to',
'consumable_id')->withPivot(
'id')->withTrashed();
157 return $this->belongsToMany(
'\App\Models\License',
'license_seats',
'assigned_to',
'license_id')->withPivot(
'id');
165 return $this->hasMany(
'\App\Models\Actionlog',
'checkedout_to')->orderBy(
'created_at',
'DESC')->withTrashed();
173 return $this->belongsTo(
'\App\Models\Location',
'location_id')->withTrashed();
181 return $this->belongsTo(
'\App\Models\User',
'manager_id')->withTrashed();
189 return $this->belongsToMany(
'\App\Models\Group',
'users_groups');
210 return $this->hasMany(
'\App\Models\Asset',
'id')->withTrashed();
218 return $this->hasMany(
'\App\Models\Actionlog',
'asset_id')
219 ->where(
'asset_type',
'=',
'user')
220 ->where(
'action_type',
'=',
'uploaded')
221 ->whereNotNull(
'filename')
222 ->orderBy(
'created_at',
'desc');
227 return $this->hasOne(
'\App\Models\Throttle');
232 return $query->withTrashed()->whereNotNull(
'deleted_at');
237 return $query->whereNull(
'deleted_at');
247 if (!config(
'session.multi_login') || (!$this->persist_code)) {
248 $this->persist_code = $this->getRandomString();
251 $persistCode = $this->persist_code;
255 return $this->persist_code;
260 return $query->where(
'email',
'=', $user_email)
261 ->orWhere(
'username',
'=', $user_username)
262 ->orWhere(
'username',
'=', $user_email);
268 $name = explode(
" ", $users_name);
269 $name = str_replace(
"'",
'', $name);
270 $first_name = $name[0];
271 $email_last_name =
'';
272 $email_prefix = $first_name;
275 if (!array_key_exists(1, $name)) {
277 $email_last_name = $last_name;
278 $user_username = $first_name;
283 $last_name = str_replace($first_name,
'', $users_name);
285 if ($format==
'filastname') {
286 $email_last_name.=str_replace(
' ',
'', $last_name);
287 $email_prefix = $first_name[0].$email_last_name;
289 } elseif ($format==
'firstname.lastname') {
290 $email_last_name.=str_replace(
' ',
'', $last_name);
291 $email_prefix = $first_name.
'.'.$email_last_name;
293 } elseif ($format==
'firstname') {
294 $email_last_name.=str_replace(
' ',
'', $last_name);
295 $email_prefix = $first_name;
300 $user_username = $email_prefix;
301 $user[
'first_name'] = $first_name;
302 $user[
'last_name'] = $last_name;
303 $user[
'username'] = strtolower($user_username);
321 return $query->where(
function ($query) use ($search) {
322 $query->where(
'users.first_name',
'LIKE',
"%$search%")
323 ->orWhere(
'users.last_name',
'LIKE',
"%$search%")
324 ->orWhere(
'users.email',
'LIKE',
"%$search%")
325 ->orWhere(
'users.username',
'LIKE',
"%$search%")
326 ->orWhere(
'users.notes',
'LIKE',
"%$search%")
327 ->orWhere(
'users.employee_num',
'LIKE',
"%$search%")
328 ->orWhere(
function ($query) use ($search) {
329 $query->whereHas(
'userloc',
function ($query) use ($search) {
330 $query->where(
'locations.name',
'LIKE',
'%'.$search.
'%');
335 ->orWhere(
function ($query) use ($search) {
336 $query->whereRaw(
"users.manager_id IN (select id from users where first_name LIKE '%".$search.
"%' OR last_name LIKE '%".$search.
"%') ");
353 return $query->whereNotNull(
'deleted_at');
368 return $query->leftJoin(
'users as manager',
'users.manager_id',
'=',
'manager.id')->orderBy(
'manager.first_name', $order)->orderBy(
'manager.last_name', $order);
381 return $query->leftJoin(
'locations',
'users.location_id',
'=',
'locations.id')->orderBy(
'locations.name', $order);
scopeDeleted($query)
Query builder scope for Deleted users.
scopeMatchEmailOrUsername($query, $user_username, $user_email)
scopeGetNotDeleted($query)
gravatar()
Returns the user Gravatar image url.
consumables()
Get consumables assigned to this user.
assets()
Get assets assigned to this user.
getPersistCode()
Override the SentryUser getPersistCode method for multiple logins at one time.
licenses()
Get licenses assigned to this user.
fullName()
Returns the user full name, it simply concatenates the user first and last name.
uploads()
Get uploads for this asset.
static generateFormattedNameFromFullName($format= 'filastname', $users_name)
manager()
Get the user's manager based on the assigned user.
scopeTextsearch($query, $search)
Query builder scope to search on text.
userlog()
Get action logs for this user.
userloc()
Get the asset's location based on the assigned user.
scopeOrderManager($query, $order)
Query builder scope to order on manager.
accessories()
Get accessories assigned to this user.
scopeOrderLocation($query, $order)
Query builder scope to order on company.