PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/www/system/modules/identity/library/User.php

https://bitbucket.org/vmihailenco/zf-blog
PHP | 136 lines | 92 code | 18 blank | 26 comment | 10 complexity | e7f656bde469a45dd4fcf0389efc2623 MD5 | raw file
  1. <?php
  2. class Identity_User extends Identity_Base_User implements Zend_Acl_Role_Interface
  3. {
  4. protected $acl = null;
  5. protected $logger = null;
  6. /**
  7. *
  8. * @param Zend_Acl $acl
  9. * @return Identity_AbstractUser
  10. */
  11. public function setAcl(Zend_Acl $acl)
  12. {
  13. $this->acl = $acl;
  14. return $this;
  15. }
  16. /**
  17. *
  18. * @return Zend_Acl
  19. */
  20. public function getAcl()
  21. {
  22. if (null === $this->acl) {
  23. $this->acl = Zend_Registry::get('zendAcl');
  24. }
  25. return $this->acl;
  26. }
  27. public function setLogger(Zend_Log $logger)
  28. {
  29. $this->logger = $logger;
  30. return $this;
  31. }
  32. public static function factory($identity)
  33. {
  34. $user = new self();
  35. $user->fromIdentity($identity);
  36. return $user;
  37. }
  38. public function preSave($event)
  39. {
  40. if (in_array('password', $this->_modified)) {
  41. $this->password = md5($this->password);
  42. }
  43. }
  44. public function preInsert($event)
  45. {
  46. $this->secret = md5(
  47. mt_rand()
  48. . $this->getLogin()
  49. . mt_rand()
  50. );
  51. }
  52. /**
  53. *
  54. * @param mixed $resource
  55. * @param mixed $privilege
  56. * @return bool
  57. */
  58. public function isAllowed($resource, $privilege = null)
  59. {
  60. $acl = $this->getAcl();
  61. if (!$acl->hasRole($this)) {
  62. $acl->addRole($this);
  63. }
  64. $isAllowed = $acl->isAllowed($this, $resource, $privilege);
  65. if ($this->logger) {
  66. if ($isAllowed) {
  67. $this->logger->debug("Access allowed for {$this->getRoleId()}/{$resource}/{$privilege}");
  68. } else {
  69. $this->logger->debug("Access denied for {$this->getRoleId()}/{$resource}/{$privilege}");
  70. }
  71. }
  72. return $isAllowed;
  73. }
  74. public function isOwner($resource)
  75. {
  76. if (isset($resource['user_id'])) {
  77. return (int) $this->getId() === (int) $resource['user_id'];
  78. }
  79. if (isset($resource['User'])) {
  80. return (int) $this->getId() === (int) $resource['User']['id'];
  81. }
  82. return false;
  83. }
  84. public function isLogged()
  85. {
  86. return !empty($this->id);
  87. }
  88. public function logout()
  89. {
  90. Zend_Auth::getInstance()->clearIdentity();
  91. }
  92. /**
  93. * Enter description here...
  94. *
  95. * @return stdClass
  96. */
  97. public function toIdentity()
  98. {
  99. $identity = $this->toArray();
  100. return (object) $identity;
  101. }
  102. /**
  103. * Enter description here...
  104. *
  105. * @param stdClass $identity
  106. * @return Identity_User
  107. */
  108. public function fromIdentity(stdClass $identity)
  109. {
  110. $this->fromArray((array) $identity);
  111. $this->assignIdentifier($identity->id);
  112. return $this;
  113. }
  114. public function getRoleId()
  115. {
  116. return $this->_get('role_id');
  117. }
  118. }