PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/Doctrine/Entities/User.php

https://gitlab.com/gothcon/cthulhu
PHP | 496 lines | 212 code | 64 blank | 220 comment | 20 complexity | dea2064b3e17efbc08a77b4cf6af7429 MD5 | raw file
  1. <?php
  2. namespace Entities;
  3. use Doctrine\ORM\Mapping as ORM;
  4. /**
  5. * User
  6. */
  7. class User
  8. {
  9. /**
  10. * @var \DateTime
  11. */
  12. private $createdAt;
  13. /**
  14. * @var string
  15. */
  16. private $createdBy;
  17. /**
  18. * @var \DateTime
  19. */
  20. private $modifiedAt;
  21. /**
  22. * @var string
  23. */
  24. private $modifiedBy;
  25. /**
  26. * @var boolean
  27. */
  28. private $isDeleted;
  29. /**
  30. * @var string
  31. */
  32. private $username;
  33. /**
  34. * @var string
  35. */
  36. private $password;
  37. /**
  38. * @var string
  39. */
  40. private $resetPasswordToken;
  41. /**
  42. * @var string
  43. */
  44. private $salt;
  45. /**
  46. * @var integer
  47. */
  48. private $level;
  49. /**
  50. * @var boolean
  51. */
  52. private $isActive;
  53. /**
  54. * @var \DateTime
  55. */
  56. private $mostRecentLogin;
  57. /**
  58. * @var string
  59. */
  60. private $oldPassword;
  61. /**
  62. * @var integer
  63. */
  64. private $id;
  65. /**
  66. * @var \Entities\Person
  67. */
  68. private $person;
  69. /**
  70. * Set createdAt
  71. *
  72. * @param \DateTime $createdAt
  73. * @return User
  74. */
  75. public function setCreatedAt($createdAt)
  76. {
  77. $this->createdAt = $createdAt;
  78. return $this;
  79. }
  80. /**
  81. * Get createdAt
  82. *
  83. * @return \DateTime
  84. */
  85. public function getCreatedAt()
  86. {
  87. return $this->createdAt;
  88. }
  89. /**
  90. * Set createdBy
  91. *
  92. * @param string $createdBy
  93. * @return User
  94. */
  95. public function setCreatedBy($createdBy)
  96. {
  97. $this->createdBy = $createdBy;
  98. return $this;
  99. }
  100. /**
  101. * Get createdBy
  102. *
  103. * @return string
  104. */
  105. public function getCreatedBy()
  106. {
  107. return $this->createdBy;
  108. }
  109. /**
  110. * Set modifiedAt
  111. *
  112. * @param \DateTime $modifiedAt
  113. * @return User
  114. */
  115. public function setModifiedAt($modifiedAt)
  116. {
  117. $this->modifiedAt = $modifiedAt;
  118. return $this;
  119. }
  120. /**
  121. * Get modifiedAt
  122. *
  123. * @return \DateTime
  124. */
  125. public function getModifiedAt()
  126. {
  127. return $this->modifiedAt;
  128. }
  129. /**
  130. * Set modifiedBy
  131. *
  132. * @param string $modifiedBy
  133. * @return User
  134. */
  135. public function setModifiedBy($modifiedBy)
  136. {
  137. $this->modifiedBy = $modifiedBy;
  138. return $this;
  139. }
  140. /**
  141. * Get modifiedBy
  142. *
  143. * @return string
  144. */
  145. public function getModifiedBy()
  146. {
  147. return $this->modifiedBy;
  148. }
  149. /**
  150. * Set isDeleted
  151. *
  152. * @param boolean $isDeleted
  153. * @return User
  154. */
  155. public function setIsDeleted($isDeleted)
  156. {
  157. $this->isDeleted = $isDeleted;
  158. return $this;
  159. }
  160. /**
  161. * Get isDeleted
  162. *
  163. * @return boolean
  164. */
  165. public function getIsDeleted()
  166. {
  167. return $this->isDeleted;
  168. }
  169. /**
  170. * Set username
  171. *
  172. * @param string $username
  173. * @return User
  174. */
  175. public function setUsername($username)
  176. {
  177. if($username == ""){
  178. }
  179. if(\UserAccessor::currentUserHasAccess(\UserLevel::ADMIN)){
  180. $this->username = $username;
  181. }
  182. else if($this->username == ""){
  183. $this->username = $username;
  184. }else{
  185. throw new Exception("User not authorized to set usernname", $code, $previous);
  186. }
  187. return $this;
  188. }
  189. /**
  190. * Get username
  191. *
  192. * @return string
  193. */
  194. public function getUsername()
  195. {
  196. return $this->username;
  197. }
  198. /**
  199. * Set password
  200. *
  201. * @param string $password
  202. * @return User
  203. */
  204. public function setPassword($password)
  205. {
  206. if($password != ""){
  207. $this->salt = static::createSalt();
  208. $this->password = static::encryptPassword($password,$this->salt);
  209. }
  210. return $this;
  211. }
  212. /**
  213. * Get password
  214. *
  215. * @return string
  216. */
  217. public function getPassword()
  218. {
  219. return "";
  220. // return $this->password;
  221. }
  222. /**
  223. * Set resetPasswordToken
  224. *
  225. * @param string $resetPasswordToken
  226. * @return User
  227. */
  228. public function setResetPasswordToken($resetPasswordToken)
  229. {
  230. $this->resetPasswordToken = $resetPasswordToken;
  231. return $this;
  232. }
  233. /**
  234. * Get resetPasswordToken
  235. *
  236. * @return string
  237. */
  238. public function getResetPasswordToken()
  239. {
  240. return "";
  241. // return $this->resetPasswordToken;
  242. }
  243. /**
  244. * Set salt
  245. *
  246. * @param string $salt
  247. * @return User
  248. */
  249. private function setSalt($salt)
  250. {
  251. $this->salt = $salt;
  252. return $this;
  253. }
  254. /**
  255. * Get salt
  256. *
  257. * @return string
  258. */
  259. public function getSalt()
  260. {
  261. return "";
  262. // return $this->salt;
  263. }
  264. /**
  265. * Set level
  266. *
  267. * @param integer $level
  268. * @return User
  269. */
  270. public function setLevel($level)
  271. {
  272. if(\UserAccessor::currentUserHasAccess(\UserLevel::ADMIN)){
  273. $this->level = $level;
  274. }
  275. else if(\UserAccessor::currentUserHasAccess(\UserLevel::STAFF) && $level < \UserLevel::STAFF){
  276. $this->level = $level;
  277. }
  278. else{
  279. return $this;
  280. }
  281. }
  282. /**
  283. * Get level
  284. *
  285. * @return integer
  286. */
  287. public function getLevel()
  288. {
  289. return $this->level;
  290. }
  291. /**
  292. * Set isActive
  293. *
  294. * @param boolean $isActive
  295. * @return User
  296. */
  297. public function setIsActive($isActive)
  298. {
  299. $this->isActive = $isActive;
  300. return $this;
  301. }
  302. /**
  303. * Get isActive
  304. *
  305. * @return boolean
  306. */
  307. public function getIsActive()
  308. {
  309. return $this->isActive;
  310. }
  311. /**
  312. * Set mostRecentLogin
  313. *
  314. * @param \DateTime $mostRecentLogin
  315. * @return User
  316. */
  317. public function setMostRecentLogin($mostRecentLogin)
  318. {
  319. $this->mostRecentLogin = $mostRecentLogin;
  320. return $this;
  321. }
  322. /**
  323. * Get mostRecentLogin
  324. *
  325. * @return \DateTime
  326. */
  327. public function getMostRecentLogin()
  328. {
  329. return $this->mostRecentLogin;
  330. }
  331. /**
  332. * Set oldPassword
  333. *
  334. * @param string $oldPassword
  335. * @return User
  336. */
  337. public function setOldPassword($oldPassword)
  338. {
  339. $this->oldPassword = $oldPassword;
  340. return $this;
  341. }
  342. /**
  343. * Get oldPassword
  344. *
  345. * @return string
  346. */
  347. public function getOldPassword()
  348. {
  349. return $this->oldPassword;
  350. }
  351. /**
  352. * Get id
  353. *
  354. * @return integer
  355. */
  356. public function getId()
  357. {
  358. return $this->id;
  359. }
  360. /**
  361. * Set person
  362. *
  363. * @param \Entities\Person $person
  364. * @return User
  365. */
  366. public function setPerson(\Entities\Person $person = null)
  367. {
  368. $this->person = $person;
  369. return $this;
  370. }
  371. /**
  372. * Get person
  373. *
  374. * @return \Entities\Person
  375. */
  376. public function getPerson()
  377. {
  378. return $this->person;
  379. }
  380. /**
  381. * @ORM\PrePersist
  382. */
  383. public function onPrePersist()
  384. {
  385. // Add your code here
  386. }
  387. /**
  388. * @ORM\PreUpdate
  389. */
  390. public function onPreUpdate(\Doctrine\ORM\Event\PreUpdateEventArgs $eventArgs)
  391. {
  392. if($eventArgs->hasChangedField("password") || $eventArgs->hasChangedField("salt")){
  393. switch(true){
  394. case \UserAccessor::currentUserHasAccess(\UserLevel::STAFF) :
  395. case \UserAccessor::getCurrentUser()->id == $this->id :
  396. // everything went fine
  397. break;
  398. default:
  399. // reset to the old values
  400. $this->salt = $eventArgs->getOldValue("salt");
  401. $this->password = $eventArgs->getOldValue("password");
  402. break;
  403. }
  404. }
  405. if($eventArgs->hasChangedField("level") || $eventArgs->hasChangedField("username")){
  406. if(! \UserAccessor::currentUserHasAccess(\UserLevel::STAFF)){
  407. throw new Exception("User is not authorized to update level or username");
  408. }
  409. }
  410. // Add your code here
  411. }
  412. static public function generateRandomString($length = 12 ,$allchar = "abcdefghijkmnoprstuzABCDFKLMRSTUVWX1235690#!?"){
  413. mt_srand ((double) microtime() * 1000000);
  414. $allCharCount=strlen($allchar);
  415. $string = "";
  416. for($i = 0; $i < $length;$i++)
  417. {
  418. $string .= $allchar[mt_rand(0,$allCharCount-1)];
  419. }
  420. return $string;
  421. }
  422. static public function createSalt(){
  423. return static::generateRandomString(12);
  424. }
  425. static public function encryptPassword($password,$salt){
  426. $hash = $salt . $password;
  427. for($i=0; $i < 1000; $i++){
  428. $hash = md5($hash);
  429. }
  430. return $hash;
  431. }
  432. }