You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuticateUsersBusiness.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Luticate\Auth\Business;
  3. use Illuminate\Support\Facades\DB;
  4. use Luticate\Auth\DBO\LuticatePermissions;
  5. use Luticate\Auth\DBO\LuticateUsersLightDbo;
  6. use Luticate\Auth\DBO\LuticateUsersLoginDbo;
  7. use Luticate\Utils\LuBusiness;
  8. use Luticate\Auth\DataAccess\LuticateUsersDataAccess;
  9. use Luticate\Auth\DBO\LuticateUsersDbo;
  10. use Luticate\Utils\LuMultipleDbo;
  11. class LuticateUsersBusiness extends LuBusiness {
  12. const KEY_USER_ID = "user_id";
  13. const KEY_SALT = "salt";
  14. const KEY_DATA = "data";
  15. protected static function getDataAccess()
  16. {
  17. return new LuticateUsersDataAccess();
  18. }
  19. protected static function badPassword()
  20. {
  21. abort(401, "Bad username/password");
  22. }
  23. public static function hashPassword($password)
  24. {
  25. return password_hash($password, PASSWORD_BCRYPT);
  26. }
  27. public static function verifyPassword($password, $hash)
  28. {
  29. return password_verify($password, $hash);
  30. }
  31. public static function getSalt($length = 10)
  32. {
  33. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  34. $charactersLength = strlen($characters);
  35. $randomString = '';
  36. for ($i = 0; $i < $length; $i++) {
  37. $randomString .= $characters[rand(0, $charactersLength - 1)];
  38. }
  39. return $randomString;
  40. }
  41. /**
  42. * @param $user LuticateUsersDbo
  43. * @param $data mixed
  44. * @return string
  45. */
  46. public static function getToken($user, $data = null)
  47. {
  48. return JwtHelper::encode(array(
  49. self::KEY_USER_ID => $user->getId(),
  50. self::KEY_SALT => $user->getSalt(),
  51. self::KEY_DATA => $data
  52. ));
  53. }
  54. /**
  55. * @param $user_id
  56. * @return LuticateUsersDbo|null
  57. */
  58. public static function checkUserId($user_id)
  59. {
  60. $user = LuticateUsersDataAccess::getById($user_id);
  61. if (is_null($user)) {
  62. self::notFound("User not found");
  63. }
  64. return $user;
  65. }
  66. public static function login($username, $password)
  67. {
  68. $user = LuticateUsersDataAccess::getByUsernameOrEmail($username);
  69. if (is_null($user))
  70. self::badPassword();
  71. if (!self::verifyPassword($password, $user->getPassword()))
  72. self::badPassword();
  73. $user = LuticateUsersLoginDbo::fromUserDbo($user);
  74. $user->setToken(self::getToken($user));
  75. return $user;
  76. }
  77. /**
  78. * @param $user LuticateUsersDbo
  79. * @return bool
  80. */
  81. public static function logout($user)
  82. {
  83. if ($user->getId() != 0) {
  84. $user->setSalt(self::getSalt());
  85. LuticateUsersDataAccess::editById($user->getId(), $user);
  86. }
  87. return true;
  88. }
  89. public static function getAllLight($page = 0, $perPage = PHP_INT_MAX, $query = "")
  90. {
  91. $users = self::getAll($page, $perPage, $query);
  92. return $users->map(function($user)
  93. {
  94. return LuticateUsersLightDbo::fromUserDbo($user);
  95. });
  96. }
  97. public static function getLightById($user_id)
  98. {
  99. $user = self::getById($user_id);
  100. if (is_null($user)) {
  101. self::notFound(self::getResourceName() . " not found");
  102. }
  103. return LuticateUsersLightDbo::fromUserDbo($user);
  104. }
  105. public static function deleteById($id)
  106. {
  107. if ($id != 0) {
  108. $res = static::getDataAccess()->deleteById($id);
  109. if (!$res)
  110. self::notFound(static::getResourceName() . " not found");
  111. }
  112. return true;
  113. }
  114. public static function add($username, $email, $firstname, $lastname, $password)
  115. {
  116. if (strlen($password) < 5) {
  117. self::badInput("Password must have at least 5 characters");
  118. }
  119. $hash = self::hashPassword($password);
  120. if (filter_var($username, FILTER_VALIDATE_EMAIL))
  121. self::badInput("Username can not be an email");
  122. if (!filter_var($email, FILTER_VALIDATE_EMAIL))
  123. self::badInput("Invalid email address");
  124. $user = LuticateUsersDataAccess::getByUsernameOrEmail($username);
  125. if (!is_null($user))
  126. self::badInput("Username already exists");
  127. $user = LuticateUsersDataAccess::getByUsernameOrEmail($email);
  128. if (!is_null($user))
  129. self::badInput("Email already used");
  130. $user = new LuticateUsersDbo();
  131. $user->setEmail($email);
  132. $user->setPassword($hash);
  133. $user->setUsername($username);
  134. $user->setFirstname($firstname);
  135. $user->setLastname($lastname);
  136. $user->setSalt(self::getSalt());
  137. return LuticateUsersDataAccess::addId($user);
  138. }
  139. public static function edit($user_id, $firstname, $lastname, $email)
  140. {
  141. $user = self::checkUserId($user_id);
  142. if (!filter_var($email, FILTER_VALIDATE_EMAIL))
  143. self::badInput("Invalid email address");
  144. $user->setEmail($email);
  145. $user->setFirstname($firstname);
  146. $user->setLastname($lastname);
  147. LuticateUsersDataAccess::editById($user_id, $user);
  148. return true;
  149. }
  150. public static function setPassword($user_id, $password)
  151. {
  152. if ($user_id != 0) {
  153. $user = self::checkUserId($user_id);
  154. $user->setPassword(self::hashPassword($password));
  155. $user->setSalt(self::getSalt());
  156. LuticateUsersDataAccess::editById($user_id, $user);
  157. }
  158. return true;
  159. }
  160. public static function setPasswordMe($password, $oldPassword, $user)
  161. {
  162. self::login($user->getUsername(), $oldPassword);
  163. return self::setPassword($user->getId(), $password);
  164. }
  165. }