|
|
@@ -21,92 +21,98 @@ use DB\SQL\Mapper;
|
|
|
*/
|
|
|
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;
|
|
|
- }
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ public function reset()
|
|
|
+ {
|
|
|
+ $this->userRoles = [];
|
|
|
+ parent::reset(); // TODO: Change the autogenerated stub
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getRoles(): array
|
|
|
+ {
|
|
|
+ return $this->userRoles;
|
|
|
+ }
|
|
|
}
|