Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LuticateUsersBusiness.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Luticate\Auth\Business;
  3. use Luticate\Auth\DBO\LuticateUsersLoginDbo;
  4. use Luticate\Utils\LuBusiness;
  5. use Luticate\Auth\DataAccess\LuticateUsersDataAccess;
  6. use Luticate\Auth\DBO\LuticateUsersDbo;
  7. class LuticateUsersBusiness extends LuBusiness {
  8. const KEY_USER_ID = "user_id";
  9. const KEY_SALT = "salt";
  10. protected static function getDataAccess()
  11. {
  12. return new LuticateUsersDataAccess();
  13. }
  14. protected static function badPassword()
  15. {
  16. abort(401, "Bad username/password");
  17. }
  18. public static function hashPassword($password)
  19. {
  20. return password_hash($password, PASSWORD_BCRYPT);
  21. }
  22. public static function verifyPassword($password, $hash)
  23. {
  24. return password_verify($password, $hash);
  25. }
  26. public static function getSalt($length = 10)
  27. {
  28. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  29. $charactersLength = strlen($characters);
  30. $randomString = '';
  31. for ($i = 0; $i < $length; $i++) {
  32. $randomString .= $characters[rand(0, $charactersLength - 1)];
  33. }
  34. return $randomString;
  35. }
  36. /**
  37. * @param $user LuticateUsersDbo
  38. * @return string
  39. */
  40. public static function getToken($user)
  41. {
  42. return JwtHelper::encode(array(
  43. self::KEY_USER_ID => $user->getId(),
  44. self::KEY_SALT => $user->getSalt()
  45. ));
  46. }
  47. public static function login($username, $password)
  48. {
  49. $user = LuticateUsersDataAccess::getByUsernameOrEmail($username);
  50. if (is_null($user))
  51. self::badPassword();
  52. if (!self::verifyPassword($password, $user->getPassword()))
  53. self::badPassword();
  54. $user = LuticateUsersLoginDbo::fromUserDbo($user);
  55. $user->setToken(self::getToken($user));
  56. return $user;
  57. }
  58. /**
  59. * @param $user LuticateUsersDbo
  60. * @return bool
  61. */
  62. public static function logout($user)
  63. {
  64. $user->setSalt(self::getSalt());
  65. LuticateUsersDataAccess::editById($user->getId(), $user);
  66. return true;
  67. }
  68. public static function add($username, $email, $password)
  69. {
  70. $hash = self::hashPassword($password);
  71. if (filter_var($username, FILTER_VALIDATE_EMAIL))
  72. self::badInput("Username can not be an email");
  73. if (!filter_var($email, FILTER_VALIDATE_EMAIL))
  74. self::badInput("Invalid email address");
  75. $user = LuticateUsersDataAccess::getByUsernameOrEmail($username);
  76. if (!is_null($user))
  77. self::badInput("Username already exists");
  78. $user = LuticateUsersDataAccess::getByUsernameOrEmail($email);
  79. if (!is_null($user))
  80. self::badInput("Email already used");
  81. $user = new LuticateUsersDbo();
  82. $user->setEmail($email);
  83. $user->setPassword($hash);
  84. $user->setUsername($username);
  85. $user->setSalt(self::getSalt());
  86. return LuticateUsersDataAccess::addId($user);
  87. }
  88. }