Refactor User class

This commit is contained in:
Giuseppe Criscione 2018-10-12 12:52:19 +02:00
parent cdc795150a
commit cb1ca10a28
3 changed files with 16 additions and 28 deletions

View File

@ -37,7 +37,6 @@ class Register extends AbstractController
'hash' => Password::hash($data->get('password')),
'email' => $data->get('email'),
'language' => $data->get('language'),
'avatar' => null,
'role' => 'admin'
);

View File

@ -58,9 +58,7 @@ class Users extends AbstractController
'fullname' => $data->get('fullname'),
'hash' => Password::hash($data->get('password')),
'email' => $data->get('email'),
'language' => $data->get('language'),
'avatar' => null,
'role' => 'user'
'language' => $data->get('language')
);
FileSystem::write(ACCOUNTS_PATH . $data->get('username') . '.yml', YAML::encode($userData));

View File

@ -10,48 +10,37 @@ use LogicException;
class User extends DataGetter
{
protected $username;
protected $fullname;
protected $hash;
protected $email;
protected $language;
protected $defaults = array(
'avatar' => null,
'role' => 'user'
);
protected $avatar;
protected $role;
protected $permissions;
protected $lastAccess;
public function __construct($data)
{
$this->data = $data;
foreach (array('username', 'fullname', 'hash', 'email', 'language', 'avatar') as $key) {
$this->$key = $data[$key];
}
$this->role = isset($data['role']) ? $data['role'] : 'user';
$this->permissions = new Permissions($this->role);
$this->avatar = new Avatar($this->avatar);
$this->data = array_merge($this->defaults, $data);
$this->avatar = new Avatar($this->data['avatar']);
$this->permissions = new Permissions($this->data['role']);
}
public function authenticate($password)
{
return Password::verify($password, $this->hash);
return Password::verify($password, $this->data['hash']);
}
public function isLogged()
{
return Session::get('FORMWORK_USERNAME') === $this->username;
return Session::get('FORMWORK_USERNAME') === $this->data['username'];
}
public function isAdmin()
{
return $this->role === 'admin';
return $this->data['role'] === 'admin';
}
public function canDeleteUser(User $user)
@ -79,9 +68,8 @@ class User extends DataGetter
if (!is_null($this->lastAccess)) {
return $this->lastAccess;
}
$lastAccess = Admin::instance()->registry('lastAccess')->get($this->username);
$this->lastAccess = $lastAccess;
return $this->lastAccess;
$lastAccess = Admin::instance()->registry('lastAccess')->get($this->data['username']);
return $this->lastAccess = $lastAccess;
}
public function __call($name, $arguments)
@ -89,6 +77,9 @@ class User extends DataGetter
if (property_exists($this, $name)) {
return $this->$name;
}
if ($this->has($name)) {
return $this->get($name);
}
throw new LogicException('Invalid method ' . static::class . '::' . $name);
}
}