123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 7/2/16
- * Time: 10:15 PM
- */
-
- namespace Luticate\Auth\Business;
-
- use Luticate\Auth\DataAccess\LuUserDataAccess;
- use Luticate\Auth\Dbo\Users\LuUsersAddDbo;
- use Luticate\Auth\Dbo\Users\LuUsersDbo;
- use Luticate\Auth\Dbo\Users\LuUsersEditDbo;
- use Luticate\Auth\Dbo\Users\LuUsersLiteDbo;
- use Luticate\Auth\Dbo\Users\LuUsersLoginDbo;
- use Luticate\Auth\Dbo\Users\LuUsersLoginResultDbo;
- use Luticate\Utils\Business\LuBusiness;
- use Luticate\Utils\Dbo\LuPaginatedDbo;
- use Luticate\Utils\Dbo\LuQueryDbo;
-
- class LuUsersBusiness extends LuBusiness
- {
- const KEY_USER_ID = "user_id";
- const KEY_SALT = "salt";
- const KEY_DATA = "data";
-
- /**
- * @return LuUserDataAccess
- */
- protected static function getDataAccess()
- {
- return new LuUserDataAccess();
- }
-
- protected function badPassword()
- {
- static::unauthorized("Bad username/password");
- }
-
- public function hashPassword(string $password)
- {
- return password_hash($password, PASSWORD_BCRYPT);
- }
-
- public function verifyPassword(string $password, string $hash)
- {
- return password_verify($password, $hash);
- }
-
- public function checkPasswordRequirements(string $password)
- {
- if (strlen($password) < 5) { //TODO: add a setting
- self::badInput("Password must have at least 5 characters");
- }
- }
-
- public function getSalt($length = 10)
- {
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- return $randomString;
- }
-
- public function getToken(LuUsersDbo $user, $data = null)
- {
- $session_time = 30;//TODO add a setting
- return JwtHelper::encode(array(
- self::KEY_USER_ID => $user->getId(),
- self::KEY_SALT => $user->getSalt(),
- self::KEY_DATA => $data
- ), $session_time);
- }
-
- public function login(LuUsersLoginDbo $login)
- {
- $user = static::getDataAccess()->getByUsernameOrEmail($login->getUsername());
- if (is_null($user))
- self::badPassword();
- if (!self::verifyPassword($login->getPassword(), $user->getPassword()))
- self::badPassword();
-
- /**
- * @var $result LuUsersLoginResultDbo
- */
- $result = $user->castAs(LuUsersLoginResultDbo::class);
- $result->setToken(self::getToken($user));
- return $result;
- }
-
- public function logout(LuUsersDbo $user)
- {
- if ($user->getId() != 0) {
- $user->setSalt(self::getSalt());
- static::getDataAccess()->editSingleById($user);
- }
- return true;
- }
-
- public function getSingleLiteById($userId)
- {
- /**
- * @var $user LuUsersDbo
- */
- $user = static::getById($userId);
- return $user->toLite();
- }
-
- /**
- * @param LuQueryDbo $query
- * @return LuPaginatedDbo
- */
- public function getAllLite(LuQueryDbo $query)
- {
- return static::getDataAccess()->getAll($query)->map(function($user)
- {
- /**
- * @var $user LuUsersDbo
- */
- return $user->toLite();
- });
- }
-
- public function add(LuUsersAddDbo $user)
- {
- static::checkPasswordRequirements($user->getPassword());
-
- if (filter_var($user->getUsername(), FILTER_VALIDATE_EMAIL)) {
- self::badInput("Username can not be an email");
- }
- if (!preg_match("/^[A-Za-z0-9\\-_\\.]+$/", $user->getUsername())) {
- self::badInput("Username can only contain letters (A-Z a-z), numbers (0-9), hyphen (-), underscore (_) and dot (.)");
- }
- if (!filter_var($user->getEmail(), FILTER_VALIDATE_EMAIL)) {
- self::badInput("Invalid email address");
- }
- $existingUser = static::getDataAccess()->getByUsernameOrEmail($user->getUsername());
- if ($existingUser != null) {
- self::badInput("Username already exists");
- }
- $existingUser = static::getDataAccess()->getByUsernameOrEmail($user->getEmail());
- if ($existingUser != null) {
- self::badInput("Email already used");
- }
- $user->setPassword(self::hashPassword($user->getPassword()));
- $user->setExternalAuth(null);
- $user->setProfileId(null);
-
- /**
- * @var LuUsersDbo $newUser
- */
- $newUser = $user->castAs(LuUsersDbo::class);
- $newUser->setSalt(static::getSalt());
-
- $id = static::getDataAccess()->addSingleId($newUser);
- return self::getById($id);
- }
-
- public function del(int $userId)
- {
- $user = static::getSingleLiteById($userId);
- if ($userId != 0) {
- static::deleteById($user->getId());
- }
- return $user;
- }
-
- public function edit(int $userId, LuUsersEditDbo $user)
- {
- $existingUser = static::getSingleLiteById($userId);
- if (!filter_var($user->getEmail(), FILTER_VALIDATE_EMAIL)) {
- self::badInput("Invalid email address");
- }
- $anotherExistingUser = static::getDataAccess()->getByUsernameOrEmail($user->getEmail());
- if ($anotherExistingUser != null && $anotherExistingUser->getId() != $existingUser->getId()) {
- self::badInput("Email already used");
- }
-
- $existingUser->setEmail($user->getEmail());
- $existingUser->setFirstname($user->getFirstname());
- $existingUser->setLastname($user->getLastname());
-
- static::getDataAccess()->editSingleById($existingUser);
-
- return static::getSingleLiteById($existingUser->getId());
- }
-
- public function setPassword(int $userId, string $password)
- {
- $this->checkPasswordRequirements($password);
-
- /**
- * @var $existingUser LuUsersDbo
- */
- $existingUser = static::getDataAccess()->getSingleById($userId);
- $existingUser->setPassword(static::hashPassword($password));
- $existingUser->setSalt(static::getSalt());
-
- static::getDataAccess()->editSingleById($existingUser);
-
- return true;
- }
-
- public function setPasswordMe(LuUsersDbo $_user, string $password, string $oldPassword)
- {
- $loginDbo = new LuUsersLoginDbo();
- $loginDbo->setUsername($_user->getUsername());
- $loginDbo->setPassword($oldPassword);
- static::login($loginDbo);
- return static::setPassword($_user->getId(), $password);
- }
- }
|