diff options
author | n-peugnet <n.peugnet@free.fr> | 2019-11-04 23:31:31 +0100 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2019-11-05 19:06:40 +0100 |
commit | e802d5204b96d645ec3d40b81b4a8bdc6e0ee675 (patch) | |
tree | 8e6db5e36ad8f247b442583e1e9e5da2934f4b52 /app/class/Modeluser.php | |
parent | f1f63f556c41c99d45cd610186b0982383eff375 (diff) | |
download | wcms-e802d5204b96d645ec3d40b81b4a8bdc6e0ee675.tar.gz wcms-e802d5204b96d645ec3d40b81b4a8bdc6e0ee675.zip |
refactor: switch to psr-4 autoloading
Diffstat (limited to 'app/class/Modeluser.php')
-rw-r--r-- | app/class/Modeluser.php | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/app/class/Modeluser.php b/app/class/Modeluser.php new file mode 100644 index 0000000..c921b4c --- /dev/null +++ b/app/class/Modeluser.php @@ -0,0 +1,196 @@ +<?php + +namespace Wcms; + +use JamesMoss\Flywheel\Document; + +class Modeluser extends Modeldb +{ + const ADMIN = 10; + const SUPEREDITOR = 4; + const EDITOR = 3; + const INVITE = 2; + const READ = 1; + const FREE = 0; + + const USER_REPO_NAME = 'user'; + + public function __construct() + { + parent::__construct(); + $this->storeinit(self::USER_REPO_NAME); + } + + public function writesession(User $user) + { + $_SESSION['user' . Config::basepath()] = ['level' => $user->level(), 'id' => $user->id(), 'columns' =>$user->columns()]; + } + + public function writecookie(User $user) + { + $cookiehash = + $cookie = ['level' => $user->level(), 'id' => $user->id()]; + setcookie('user ' . Config::basepath(), $cookie, time() + $user->cookie()*24*3600, null, null, false, true); + } + + public function readsession() + { + $userdatas = []; + if (array_key_exists('user' . Config::basepath(), $_SESSION) && isset($_SESSION['user' . Config::basepath()]['id'])) { + $userdatas = $_SESSION['user' . Config::basepath()]; + $user = new User($userdatas); + $user = $this->get($user); + return $user; + } else { + return new User(['id' => '', 'level' => 0]); + } + } + + + public function logout() + { + $user = new User(['level' => self::FREE]); + return $user; + } + + + + /** + * @return array list of User objects + */ + public function getlister() + { + $userlist = []; + $list = $this->repo->findAll(); + foreach ($list as $userdata) { + $userlist[$userdata->id] = new User($userdata); + } + return $userlist; + } + + + public function getlisterid(array $idlist = []) + { + $userdatalist = $this->repo->query() + ->where('__id', 'IN', $idlist) + ->execute(); + + $userlist = []; + foreach ($userdatalist as $id => $userdata) { + $userlist[$id] = new User($userdata); + } + return $userlist; + } + + public function admincount() + { + $userdatalist = $this->repo->query() + ->where('level', '==', 10) + ->execute(); + + return $userdatalist->total(); + } + + public function getlisterbylevel(int $level, $comp = '==') + { + $userdatalist = $this->repo->query() + ->where('level', $comp, $level) + ->execute(); + + $userlist = []; + foreach ($userdatalist as $user) { + $userlist[] = $user->id; + } + + return $userlist; + } + + /** + * Check if the password is used, and return by who + * + * @param string $pass password clear + * + * @return mixed User or false + */ + public function passwordcheck(string $pass) + { + $userdatalist = $this->getlister(); + foreach ($userdatalist as $user) { + if ($user->passwordhashed()) { + if (password_verify($pass, $user->password())) { + return $user; + } + } else { + if ($user->password() === $pass) { + return $user; + } + } + } + return false; + } + + /** + * Return information if the password is already used or not + * + * @param string $pass password clear + * + * @return bool password exist or not + */ + public function passwordexist(string $pass) : bool + { + if ($this->passwordcheck($pass) !== false) { + return true; + } else { + return false; + } + } + + /** + * @param User $user + * + * @return bool depending on success + */ + public function add(User $user) : bool + { + $userdata = new Document($user->dry()); + $userdata->setId($user->id()); + return $this->repo->store($userdata); + } + + + /** + * @param string|User $id + * + * @return User|false User object or false in case of error + */ + public function get($id) + { + if ($id instanceof User) { + $id = $id->id(); + } + if (is_string($id)) { + $userdata = $this->repo->findById($id); + if ($userdata !== false) { + return new User($userdata); + } else { + return false; + } + } else { + return false; + } + } + + public function delete(User $user) + { + $this->repo->delete($user->id()); + } + + +} + + + + + + +?>
\ No newline at end of file |