Skip to content

Instantly share code, notes, and snippets.

@waspinator
Created April 17, 2019 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waspinator/646012748f747a59e422f9ed9df62e98 to your computer and use it in GitHub Desktop.
Save waspinator/646012748f747a59e422f9ed9df62e98 to your computer and use it in GitHub Desktop.
<?php
namespace App\Model\Entity;
#use Cake\Auth\DefaultPasswordHasher;
use Authentication\PasswordHasher\DefaultPasswordHasher;
use Authorization\IdentityInterface;
use Cake\ORM\Entity;
/**
* User Entity
*
* @property int $id
* @property string $username
* @property string $password
* @property string $email
* @property string|null $token
* @property \Cake\I18n\FrozenTime|null $token_expires
* @property bool $is_superuser
* @property string|null $role
* @property bool $active
* @property \Cake\I18n\FrozenTime $created
* @property \Cake\I18n\FrozenTime $modified
* @property \Cake\I18n\FrozenTime|null $deleted
* @property int $created_by
* @property int $modified_by
*
* @property \App\Model\Entity\UserGroup[] $user_groups
*/
class User extends Entity implements IdentityInterface
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'username' => true,
'password' => true,
'email' => true,
'first_name' => true,
'last_name' => true,
'token' => true,
'token_expires' => true,
'api_key' => true,
'api_key_plain' => true,
'is_superuser' => true,
'role' => true,
'active' => true,
'created' => true,
'modified' => true,
'deleted' => true,
'created_by' => true,
'modified_by' => true,
'user_groups' => true
];
protected $_virtual = ['full_name'];
/**
* Fields that are excluded from JSON versions of the entity.
*
* @var array
*/
protected $_hidden = [
'password',
'token',
'api_key'
];
protected function _setPassword($value)
{
if (strlen($value)) {
$hasher = new DefaultPasswordHasher();
return $hasher->hash($value);
}
}
protected function _getFullName()
{
return $this->first_name . ' ' . $this->last_name;
}
/**
* Authorization\IdentityInterface method
*/
public function can($action, $resource)
{
return $this->authorization->can($this, $action, $resource);
}
/**
* Authorization\IdentityInterface method
*/
public function applyScope($action, $resource)
{
return $this->authorization->applyScope($this, $action, $resource);
}
/**
* Authorization\IdentityInterface method
*/
public function getOriginalData()
{
return $this;
}
/**
* Setter to be used by the middleware.
*/
public function setAuthorization($service)
{
$this->authorization = $service;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment