PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/www/system/module/identity/library/Identity/User.php

https://bitbucket.org/vmihailenco/vladimirwebdev
PHP | 399 lines | 259 code | 50 blank | 90 comment | 27 complexity | 99d9e9457487f602809b6b91af63b230 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. class Identity_User extends DoctrineX_Record
  3. {
  4. const GUEST_ROLE_ID = 1;
  5. const MEMBER_ROLE_ID = 2;
  6. const ADMIN_ROLE_ID = 3;
  7. /**
  8. * Enter description here...
  9. *
  10. * @var Zend_Auth_Adapter
  11. */
  12. protected static $_authAdapter = null;
  13. /**
  14. *
  15. * @var string
  16. */
  17. private $_secretKey = __CLASS__;
  18. /**
  19. *
  20. * @var Zend_Acl
  21. */
  22. protected $_acl = null;
  23. /**
  24. * Enter description here...
  25. *
  26. * @var int
  27. */
  28. protected $_rememberUntil = 1209600;
  29. public function setSecretKey($value)
  30. {
  31. $this->_secretKey = $value;
  32. return $this;
  33. }
  34. /**
  35. *
  36. * @return string
  37. */
  38. public function getSecretKey()
  39. {
  40. return $this->_secretKey;
  41. }
  42. /**
  43. *
  44. * @param Zend_Acl $acl
  45. * @return Identity_AbstractUser
  46. */
  47. public function setAcl(Zend_Acl $acl)
  48. {
  49. $this->_acl = $acl;
  50. return $this;
  51. }
  52. /**
  53. *
  54. * @return Zend_Acl
  55. */
  56. public function getAcl()
  57. {
  58. if (null === $this->_acl) {
  59. $this->_acl = Zend_Registry::get('zendAcl');
  60. }
  61. return $this->_acl;
  62. }
  63. /**
  64. * Enter description here...
  65. *
  66. * @param int $rememberUntil
  67. * @return Identity_User
  68. */
  69. public function setRememberUntil($rememberUntil)
  70. {
  71. $this->_rememberUntil = $rememberUntil;
  72. return $this;
  73. }
  74. /**
  75. * Enter description here...
  76. *
  77. * @return int
  78. */
  79. public function getRememberUntil()
  80. {
  81. return $this->_rememberUntil;
  82. }
  83. public function setTableDefinition()
  84. {
  85. $this->hasColumn('id', 'integer', null, array('primary' => true, 'autoincrement' => true));
  86. $this->hasColumn('name', 'string', 64);
  87. $this->hasColumn('login', 'string', 64);
  88. $this->hasColumn('password', 'string', 32, array('fixed' => true));
  89. $this->hasColumn('email', 'string', 64);
  90. $this->hasColumn('code', 'string', 32, array('fixed' => true));
  91. $this->hasColumn('roleId', 'integer');
  92. $this->actAs(new DoctrineX_Template_Datetime());
  93. }
  94. public function preInsert()
  95. {
  96. $this->cleanup();
  97. if (empty($this->roleId)) {
  98. $this->roleId = self::GUEST_ROLE_ID;
  99. $this->code = $this->generateCode();
  100. $this->sendConfirmation();
  101. }
  102. }
  103. public function preSave($event)
  104. {
  105. if (in_array('password', $this->_modified)) {
  106. $this->password = md5(md5($this->_secretKey) . $this->password
  107. . md5($this->_secretKey));
  108. }
  109. }
  110. public function postSave()
  111. {
  112. $user = Zend_Registry::get('user');
  113. if ($user->id == $this->id) {
  114. $this->persistAuthResult((object)$this->toArray());
  115. }
  116. }
  117. public function preDelete()
  118. {
  119. IO_File::deleteDir($this->getUploadsPath());
  120. }
  121. public function isLogged()
  122. {
  123. return !$this->isGuest($this);
  124. }
  125. /**
  126. * Enter description here...
  127. *
  128. * @param string $login
  129. * @param string $password
  130. * @param bool $writeResult
  131. * @param bool $rememberMe
  132. * @return Zend_Auth_Result
  133. */
  134. public function login($login, $password = '', $persistResult = true, $rememberMe = true)
  135. {
  136. $password = md5($this->_secretKey) . $password . md5($this->_secretKey);
  137. $authAdapter = $this->getAuthAdapter();
  138. $authAdapter->setIdentity($login);
  139. $authAdapter->setCredential($password);
  140. $result = $authAdapter->authenticate($authAdapter);
  141. if ($result->isValid() && $persistResult) {
  142. $this->persistAuthResult($this->getAuthResult());
  143. if ($rememberMe) {
  144. Zend_Session::rememberMe($this->getRememberUntil());
  145. $saveHandler = Zend_Session::getSaveHandler();
  146. if ($saveHandler) {
  147. $saveHandler
  148. ->setLifetime($this->getRememberUntil())
  149. ->setOverrideLifetime(true);
  150. }
  151. }
  152. }
  153. return $result;
  154. }
  155. /**
  156. * Enter description here...
  157. *
  158. */
  159. public function logout()
  160. {
  161. Zend_Auth::getInstance()->clearIdentity();
  162. }
  163. /**
  164. *
  165. * @param mixed $resource
  166. * @param mixed $privilege
  167. * @return bool
  168. */
  169. public function isAllowed($resource, $privilege = null)
  170. {
  171. $acl = $this->getAcl();
  172. if (!$acl->has($resource)) {
  173. $resource = null;
  174. }
  175. return $acl->isAllowed($this, $resource, $privilege);
  176. }
  177. protected function _getIdentifier($resource)
  178. {
  179. if (is_scalar($resource)) {
  180. return $resource;
  181. }
  182. if (method_exists($resource, 'identifier')) {
  183. return $resource->identifier();
  184. }
  185. if (method_exists($resource, 'toArray')) {
  186. $resource = $resource->toArray();
  187. } else {
  188. $resource = (array) $resource;
  189. }
  190. if (isset($resource['id'])) {
  191. return $resource['id'];
  192. } else {
  193. return $resource;
  194. }
  195. }
  196. public function owner($resource)
  197. {
  198. $this->getSession()->owner[] = $this->_getIdentifier($resource);
  199. }
  200. public function isOwner($resource)
  201. {
  202. if (self::isAdmin($this)) {
  203. return true;
  204. } else if (false !== array_search($this->_getIdentifier($resource), (array) $this->getSession()->owner)) {
  205. return true;
  206. } else if (self::isGuest($this)) {
  207. return false;
  208. } else if (isset($resource['user_id'])) {
  209. return (int) $this->id === (int) $resource['user_id'];
  210. } else {
  211. return (int) $this->id === (int) $resource;
  212. }
  213. }
  214. /**
  215. *
  216. * @return string
  217. */
  218. public function getUploadsPath($id = null, $path = '')
  219. {
  220. if (null === $id) {
  221. $id = $this->id;
  222. }
  223. $uploads = Zend_Registry::get('nenoPath')->uploads . '/' . $id . $path;
  224. if (!IO_File::exists($uploads)) {
  225. IO_File::createDir($uploads);
  226. }
  227. return $uploads;
  228. }
  229. /**
  230. * Enter description here...
  231. *
  232. * @return Zend_Auth_Adapter
  233. */
  234. public static function getAuthAdapterStatic()
  235. {
  236. return self::$_authAdapter;
  237. }
  238. public function getAuthAdapter()
  239. {
  240. if (null === self::$_authAdapter) {
  241. self::$_authAdapter = new Zend_Auth_Adapter_DbTable(
  242. Zend_Registry::get('zendDb'),
  243. $this->getTable()->getTableName(), 'login', 'password', 'MD5(?) AND roleId != ' . self::GUEST_ROLE_ID
  244. );
  245. }
  246. return self::$_authAdapter;
  247. }
  248. /**
  249. * Enter description here...
  250. *
  251. * @return stdClass
  252. */
  253. public function getAuthResult()
  254. {
  255. $identity = $this->getAuthAdapter()->getResultRowObject(null, 'password');
  256. return $identity;
  257. }
  258. public function persistAuthResult($authResult)
  259. {
  260. $auth = Zend_Auth::getInstance();
  261. $auth->getStorage()->write($authResult);
  262. }
  263. public static function isGuest($user)
  264. {
  265. return $user instanceof Identity_User_Guest;
  266. }
  267. public static function isModerator($user)
  268. {
  269. return $user instanceof Identity_User_Moderator;
  270. }
  271. public static function isAdmin($user)
  272. {
  273. return $user instanceof Identity_User_Admin;
  274. }
  275. public function cleanup()
  276. {
  277. Doctrine_Query::create()
  278. ->delete()
  279. ->from('Identity_User User')
  280. ->where('User.roleId = ?', Identity_User::GUEST_ROLE_ID)
  281. ->andWhere('User.createdAt < ?', date(
  282. Zend_Registry::get('dbDatetimeFormat'),
  283. time() - 7 * 24 * 60 * 60)
  284. )
  285. ->execute();
  286. }
  287. public function generateCode()
  288. {
  289. return md5(
  290. mt_rand()
  291. . $this->login
  292. . mt_rand()
  293. );
  294. }
  295. public function sendConfirmation()
  296. {
  297. $view = Zend_Registry::get('zendView');
  298. $config = Zend_Registry::get('persistentConfig');
  299. $view->user = $this->toArray();
  300. $body = $view->render('user/_confirm-registration-email.phtml');
  301. $mail = new Zend_Mail('Utf-8');
  302. $mail->setFrom($config->support->email, $config->support->name);
  303. $mail->addTo($this->email, $this->name);
  304. $mail->setSubject($config->site->title);
  305. $mail->setBodyText($body);
  306. $mail->send();
  307. }
  308. /**
  309. * Enter description here...
  310. *
  311. */
  312. public function sendPasswordResetEmail()
  313. {
  314. $view = Zend_Registry::get('zendView');
  315. $config = Zend_Registry::get('persistentConfig');
  316. $this->code = $this->generateCode();
  317. $this->save();
  318. $view->user = $this->toArray();
  319. $body = $view->render('user/_password-reset-email.phtml');
  320. $mail = new Zend_Mail('Utf-8');
  321. $mail->setFrom($config->support->email, $config->support->name);
  322. $mail->addTo($this->email, $this->name);
  323. $mail->setSubject($config->site->title);
  324. $mail->setBodyText($body);
  325. $mail->send();
  326. }
  327. /**
  328. * Enter description here...
  329. *
  330. * @return stdClass
  331. */
  332. public function toIdentity()
  333. {
  334. $identity = $this->toArray();
  335. return (object) $identity;
  336. }
  337. /**
  338. * Enter description here...
  339. *
  340. * @param stdClass $identity
  341. * @return Identity_User
  342. */
  343. public function fromIdentity(stdClass $identity)
  344. {
  345. $this->fromArray((array) $identity);
  346. $this->assignIdentifier($identity->id);
  347. return $this;
  348. }
  349. }