PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/Database/Entities/User.php

https://gitlab.com/gothcon/cthulhu
PHP | 217 lines | 146 code | 28 blank | 43 comment | 19 complexity | 90bed5cb83f10a8a3023050ca23f7334 MD5 | raw file
  1. <?php
  2. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "ReadWriteEntity.php");
  3. /**
  4. * @property string $username
  5. * @property string $password
  6. * @property string $salt
  7. * @property int $level
  8. * @property string $person_id
  9. * @property int $is_active
  10. * @property string $most_recent_login
  11. * @property string reset_password_token
  12. */
  13. class User extends ReadWriteEntity{
  14. protected $newPasswordIsEmpty = false;
  15. protected $passwordRepeat = null;
  16. protected $newPassword = null;
  17. public function __construct(){
  18. parent::__construct();
  19. $this->persistedProperties["username"] = "";
  20. $this->persistedProperties["password"] = "";
  21. $this->persistedProperties["old_password"] = "";
  22. $this->persistedProperties["reset_password_token"] = "";
  23. $this->persistedProperties["salt"] = "";
  24. $this->persistedProperties["level"] = 0;
  25. $this->persistedProperties["person_id"] = 0;
  26. $this->persistedProperties["is_active"] = 0;
  27. $this->persistedProperties["most_recent_login"] = "0000-00-00 00:00:00";
  28. }
  29. /**
  30. * @return UserRepository
  31. */
  32. public static function getRepository() {
  33. return parent::getRepository();
  34. }
  35. /**
  36. *
  37. * @param type $username
  38. * @return type User
  39. */
  40. public static function getUserByUsername($username){
  41. return static::getRepository()->getUserByUsername($username);
  42. }
  43. /**
  44. *
  45. * @param string $token
  46. * @return User
  47. */
  48. public static function getUserByResetPasswordToken($token){
  49. return static::getRepository()->getUserByResetEmailToken($token);
  50. }
  51. /**
  52. *
  53. * @param string $emailAddress
  54. * @return array
  55. */
  56. public static function getUsersByEmailAddress($emailAddress){
  57. return static::getRepository()->getUsersByEmailAddress($emailAddress);
  58. }
  59. /**
  60. *
  61. * @param type $username
  62. * @return bool
  63. */
  64. public static function usernameIsTaken($username,$id=0){
  65. return static::getRepository()->usernameIsTaken($username,$id);
  66. }
  67. /**
  68. * @param type $id
  69. * @return User
  70. */
  71. public static function loadByPersonId($id){
  72. return static::getRepository()->loadByPersonId($id);
  73. }
  74. public function __toString() {
  75. return "{$this->id}. {$this->username}";
  76. }
  77. public static function getUserByUsernameAndOldPassword($username,$password){
  78. return static::getRepository()->getUserByUsernameAndOldPassword($username, $password);
  79. }
  80. /**
  81. *
  82. * @param string $username
  83. * @param string $password
  84. * @return User
  85. */
  86. public static function getUserByUsernameAndPassword($username,$password){
  87. if($password == "")
  88. return null;
  89. $user = User::getUserByUsername($username);
  90. if($user && static::encryptPassword($password,$user->salt) == $user->password)
  91. return $user;
  92. else{
  93. if($password == "")
  94. $user = static::getUserByUsernameAndOldPassword ($username, $password);
  95. if($user){
  96. $user->setNewPassword($password);
  97. $user->old_password = "";
  98. $user->save();
  99. }
  100. }
  101. return $user;
  102. }
  103. public function setNewPassword($password,$passwordRepeat = null){
  104. if($password == ""){
  105. $this->newPasswordIsEmpty = true;
  106. }
  107. $this->newPassword = $password;
  108. $this->passwordRepeat = $passwordRepeat;
  109. $this->persistedProperties["salt"] = static::createSalt();
  110. $this->persistedProperties["password"] = static::encryptPassword($password,$this->salt);
  111. $this->is_modified = true;
  112. }
  113. static public function generateRandomString($length = 12 ,$allchar = "abcdefghijkmnoprstuzABCDFKLMRSTUVWX1235690#!?"){
  114. mt_srand ((double) microtime() * 1000000);
  115. $allCharCount=strlen($allchar);
  116. $string = "";
  117. for($i = 0; $i < $length;$i++)
  118. {
  119. $string .= $allchar[mt_rand(0,$allCharCount-1)];
  120. }
  121. return $string;
  122. }
  123. static public function createSalt(){
  124. return static::generateRandomString(12);
  125. }
  126. static public function encryptPassword($password,$salt){
  127. $hash = $salt . $password;
  128. for($i=0; $i < 1000; $i++){
  129. $hash = md5($hash);
  130. }
  131. return $hash;
  132. }
  133. public function userHasAccess($level){
  134. return $this->level >= $level;
  135. }
  136. protected function validateProperties() {
  137. $isValid = true;
  138. if($this->username == ""){
  139. $this->setValidationError("username","usernameMustBeSet");
  140. $isValid = false;
  141. }
  142. if(User::usernameIsTaken($this->username,$this->id)){
  143. $this->setValidationError("username","usernameExists");
  144. $isValid = false;
  145. }
  146. if($this->newPasswordIsEmpty){
  147. $this->setValidationError("password","passwordIsEmpty");
  148. $isValid = false;
  149. }else if($this->newPassword != $this->passwordRepeat && !is_null($this->passwordRepeat)){
  150. $this->setValidationError("password", "passwordsDiffer");
  151. $isValid = false;
  152. }
  153. return $isValid;
  154. }
  155. }
  156. class UserLevel{
  157. public static function toString($level){
  158. switch($level){
  159. case UserLevel::USER:
  160. return "user";
  161. break;
  162. case UserLevel::ORGANIZER:
  163. return "organizer";
  164. break;
  165. case UserLevel::WORKER:
  166. return "worker";
  167. break;
  168. case UserLevel::STAFF:
  169. return "staff";
  170. break;
  171. case UserLevel::ADMIN:
  172. return "administrator";
  173. break;
  174. case UserLevel::ABOVEADMIN:
  175. return "administrator";
  176. break;
  177. default:
  178. return "visitor";
  179. break;
  180. }
  181. }
  182. const VISITOR = 0;
  183. const USER = 1;
  184. const ORGANIZER = 20;
  185. const WORKER = 30;
  186. const STAFF = 40;
  187. const ADMIN = 100;
  188. const ABOVEADMIN= 1000;
  189. }