Files
core/app/Models/User.php

87 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use App\Models\Permissions\Permission;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticableInterface;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Notifications\Notifiable;
/**
* Class User
*/
class User extends Model implements AuthenticableInterface, CanResetPassword
{
use SoftDeletes, Authenticatable, Authorizable, Notifiable;
const STATE_DELETED = 'deleted';
const STATE_BLOCKED = 'blocked';
const STATE_ACTIVE = 'active';
const ROLE_VISITOR = 'visitor';
const ROLE_EDITOR = 'editor';
const ROLE_ADMIN = 'admin';
protected static $abilities;
public $timestamps = true;
protected $fillable = [
'email',
'username',
'password',
'name',
'role'
];
/**
* The attributes excluded from the model's JSON form.
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the e-mail address where password reset links are sent.
* @return string
*/
public function getEmailForPasswordReset()
{
return $this->email;
}
/**
* Send the password reset notification.
*
* @param string $token
*
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
public function permissions()
{
return $this->belongsToMany(Permission::class, 'user_has_permissions');
}
/**
* Check if current user has given ability
*
* @param $ability
*
* @return bool
*/
public function hasAbility($ability)
{
if (!isset(self::$abilities[$this->id])) {
self::$abilities[$this->id] = $this->permissions->pluck('string_id')->toArray();
}
return in_array($ability, self::$abilities[$this->id]);
}
}