Файл: gapps/vendor/cartalyst/sentinel/src/Users/EloquentUser.php
Строк: 414
<?php
/**
* Part of the Sentinel package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Sentinel
* @version 2.0.15
* @author Cartalyst LLC
* @license BSD License (3-clause)
* @copyright (c) 2011-2017, Cartalyst LLC
* @link http://cartalyst.com
*/
namespace CartalystSentinelUsers;
use CartalystSentinelPermissionsPermissibleInterface;
use CartalystSentinelPermissionsPermissibleTrait;
use CartalystSentinelPersistencesPersistableInterface;
use CartalystSentinelRolesRoleableInterface;
use CartalystSentinelRolesRoleInterface;
use IlluminateDatabaseEloquentModel;
class EloquentUser extends Model implements RoleableInterface, PermissibleInterface, PersistableInterface, UserInterface
{
use PermissibleTrait;
/**
* {@inheritDoc}
*/
protected $table = 'users';
/**
* {@inheritDoc}
*/
protected $fillable = [
'email',
'password',
'last_name',
'first_name',
'permissions',
];
/**
* {@inheritDoc}
*/
protected $hidden = [
'password',
];
/**
* {@inheritDoc}
*/
protected $persistableKey = 'user_id';
/**
* {@inheritDoc}
*/
protected $persistableRelationship = 'persistences';
/**
* Array of login column names.
*
* @var array
*/
protected $loginNames = ['email'];
/**
* The Eloquent roles model name.
*
* @var string
*/
protected static $rolesModel = 'CartalystSentinelRolesEloquentRole';
/**
* The Eloquent persistences model name.
*
* @var string
*/
protected static $persistencesModel = 'CartalystSentinelPersistencesEloquentPersistence';
/**
* The Eloquent activations model name.
*
* @var string
*/
protected static $activationsModel = 'CartalystSentinelActivationsEloquentActivation';
/**
* The Eloquent reminders model name.
*
* @var string
*/
protected static $remindersModel = 'CartalystSentinelRemindersEloquentReminder';
/**
* The Eloquent throttling model name.
*
* @var string
*/
protected static $throttlingModel = 'CartalystSentinelThrottlingEloquentThrottle';
/**
* Returns an array of login column names.
*
* @return array
*/
public function getLoginNames()
{
return $this->loginNames;
}
/**
* Returns the roles relationship.
*
* @return IlluminateDatabaseEloquentRelationsBelongsToMany
*/
public function roles()
{
return $this->belongsToMany(static::$rolesModel, 'role_users', 'user_id', 'role_id')->withTimestamps();
}
/**
* Returns the persistences relationship.
*
* @return IlluminateDatabaseEloquentRelationsHasMany
*/
public function persistences()
{
return $this->hasMany(static::$persistencesModel, 'user_id');
}
/**
* Returns the activations relationship.
*
* @return IlluminateDatabaseEloquentRelationsHasMany
*/
public function activations()
{
return $this->hasMany(static::$activationsModel, 'user_id');
}
/**
* Returns the reminders relationship.
*
* @return IlluminateDatabaseEloquentRelationsHasMany
*/
public function reminders()
{
return $this->hasMany(static::$remindersModel, 'user_id');
}
/**
* Returns the throttle relationship.
*
* @return IlluminateDatabaseEloquentRelationsHasMany
*/
public function throttle()
{
return $this->hasMany(static::$throttlingModel, 'user_id');
}
/**
* Get mutator for the "permissions" attribute.
*
* @param mixed $permissions
* @return array
*/
public function getPermissionsAttribute($permissions)
{
return $permissions ? json_decode($permissions, true) : [];
}
/**
* Set mutator for the "permissions" attribute.
*
* @param mixed $permissions
* @return void
*/
public function setPermissionsAttribute(array $permissions)
{
$this->attributes['permissions'] = $permissions ? json_encode($permissions) : '';
}
/**
* {@inheritDoc}
*/
public function getRoles()
{
return $this->roles;
}
/**
* {@inheritDoc}
*/
public function inRole($role)
{
if ($role instanceof RoleInterface) {
$roleId = $role->getRoleId();
}
foreach ($this->roles as $instance) {
if ($role instanceof RoleInterface) {
if ($instance->getRoleId() === $roleId) {
return true;
}
} else {
if ($instance->getRoleId() == $role || $instance->getRoleSlug() == $role) {
return true;
}
}
}
return false;
}
/**
* {@inheritDoc}
*/
public function generatePersistenceCode()
{
return str_random(32);
}
/**
* {@inheritDoc}
*/
public function getUserId()
{
return $this->getKey();
}
/**
* {@inheritDoc}
*/
public function getPersistableId()
{
return $this->getKey();
}
/**
* {@inheritDoc}
*/
public function getPersistableKey()
{
return $this->persistableKey;
}
/**
* {@inheritDoc}
*/
public function setPersistableKey($key)
{
$this->persistableKey = $key;
}
/**
* {@inheritDoc}
*/
public function setPersistableRelationship($persistableRelationship)
{
$this->persistableRelationship = $persistableRelationship;
}
/**
* {@inheritDoc}
*/
public function getPersistableRelationship()
{
return $this->persistableRelationship;
}
/**
* {@inheritDoc}
*/
public function getUserLogin()
{
return $this->getAttribute($this->getUserLoginName());
}
/**
* {@inheritDoc}
*/
public function getUserLoginName()
{
return reset($this->loginNames);
}
/**
* {@inheritDoc}
*/
public function getUserPassword()
{
return $this->password;
}
/**
* Returns the roles model.
*
* @return string
*/
public static function getRolesModel()
{
return static::$rolesModel;
}
/**
* Sets the roles model.
*
* @param string $rolesModel
* @return void
*/
public static function setRolesModel($rolesModel)
{
static::$rolesModel = $rolesModel;
}
/**
* Returns the persistences model.
*
* @return string
*/
public static function getPersistencesModel()
{
return static::$persistencesModel;
}
/**
* Sets the persistences model.
*
* @param string $persistencesModel
* @return void
*/
public static function setPersistencesModel($persistencesModel)
{
static::$persistencesModel = $persistencesModel;
}
/**
* Returns the activations model.
*
* @return string
*/
public static function getActivationsModel()
{
return static::$activationsModel;
}
/**
* Sets the activations model.
*
* @param string $activationsModel
* @return void
*/
public static function setActivationsModel($activationsModel)
{
static::$activationsModel = $activationsModel;
}
/**
* Returns the reminders model.
*
* @return string
*/
public static function getRemindersModel()
{
return static::$remindersModel;
}
/**
* Sets the reminders model.
*
* @param string $remindersModel
* @return void
*/
public static function setRemindersModel($remindersModel)
{
static::$remindersModel = $remindersModel;
}
/**
* Returns the throttling model.
*
* @return string
*/
public static function getThrottlingModel()
{
return static::$throttlingModel;
}
/**
* Sets the throttling model.
*
* @param string $throttlingModel
* @return void
*/
public static function setThrottlingModel($throttlingModel)
{
static::$throttlingModel = $throttlingModel;
}
/**
* {@inheritDoc}
*/
public function delete()
{
$isSoftDeleted = array_key_exists('IlluminateDatabaseEloquentSoftDeletes', class_uses($this));
if ($this->exists && ! $isSoftDeleted) {
$this->activations()->delete();
$this->persistences()->delete();
$this->reminders()->delete();
$this->roles()->detach();
$this->throttle()->delete();
}
return parent::delete();
}
/**
* Dynamically pass missing methods to the user.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
$methods = ['hasAccess', 'hasAnyAccess'];
if (in_array($method, $methods)) {
$permissions = $this->getPermissionsInstance();
return call_user_func_array([$permissions, $method], $parameters);
}
return parent::__call($method, $parameters);
}
/**
* Creates a permissions object.
*
* @return CartalystSentinelPermissionsPermissionsInterface
*/
protected function createPermissions()
{
$userPermissions = $this->permissions;
$rolePermissions = [];
foreach ($this->roles as $role) {
$rolePermissions[] = $role->permissions;
}
return new static::$permissionsClass($userPermissions, $rolePermissions);
}
}