| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * Created by PhpStorm.
- * User: giancarlo
- * Date: 28/06/2018
- * Time: 20:01
- */
- namespace OpenMind\System;
- use DB\SQL\Mapper;
- /**
- * Class User
- *
- * @property int $id
- * @property string $username
- * @property string $saltedpassword
- * @property string $roles
- * @package OpenMind\System
- */
- class User extends Mapper
- {
- private $userRoles;
- /**
- * User constructor.
- * @param string/array $where
- */
- public function __construct($where)
- {
- global $f3;
- $this->onload(function ($obj) {
- $obj->userRoles = unserialize($obj->roles);
- });
- parent::__construct($f3->get('DB'), 'users', NULL, 3600);
- if ($where !== NULL) {
- $this->load($where);
- } else
- $this->userRoles = [];
- }
- public function generatePassword(string $clearPassword)
- {
- return password_hash($this->username . "|$clearPassword", PASSWORD_BCRYPT);
- }
- /**
- * Reload user from $_SESSION
- */
- public static function Restore()
- {
- global $f3;
- $userId = $f3->get('SESSION.user');
- return new User(['id=?', $userId]);
- }
- /**
- * Save user in $_SESSION
- */
- public function Store()
- {
- global $f3;
- $f3->set('SESSION.user', $this->id);
- }
- /**
- * Check if user can do <$permissionName>
- *
- * @param string $permissionName
- * @return bool
- */
- public function hasPermission(string $permissionName): bool
- {
- if ($this->userRoles)
- return in_array($permissionName, $this->userRoles);
- else
- return false;
- }
- public function grantPermission(string $permissionName)
- {
- if (!in_array($permissionName, $this->userRoles))
- array_push($this->userRoles, $permissionName);
- }
- public function removePermission(string $permissionName)
- {
- $i = array_search($permissionName, $this->userRoles);
- if ($i !== false)
- unset($this->userRoles[$i]);
- }
- public function save()
- {
- $this->roles = serialize($this->userRoles);
- return parent::save(); // TODO: Change the autogenerated stub
- }
- /**
- * @return array
- */
- public function getRoles(): array
- {
- return $this->userRoles;
- }
- }
|