/www/system/modules/identity/library/User.php
PHP | 136 lines | 92 code | 18 blank | 26 comment | 10 complexity | e7f656bde469a45dd4fcf0389efc2623 MD5 | raw file
- <?php
- class Identity_User extends Identity_Base_User implements Zend_Acl_Role_Interface
- {
- protected $acl = null;
- protected $logger = null;
-
- /**
- *
- * @param Zend_Acl $acl
- * @return Identity_AbstractUser
- */
- public function setAcl(Zend_Acl $acl)
- {
- $this->acl = $acl;
- return $this;
- }
-
- /**
- *
- * @return Zend_Acl
- */
- public function getAcl()
- {
- if (null === $this->acl) {
- $this->acl = Zend_Registry::get('zendAcl');
- }
- return $this->acl;
- }
-
- public function setLogger(Zend_Log $logger)
- {
- $this->logger = $logger;
- return $this;
- }
-
- public static function factory($identity)
- {
- $user = new self();
- $user->fromIdentity($identity);
- return $user;
- }
-
- public function preSave($event)
- {
- if (in_array('password', $this->_modified)) {
- $this->password = md5($this->password);
- }
- }
-
- public function preInsert($event)
- {
- $this->secret = md5(
- mt_rand()
- . $this->getLogin()
- . mt_rand()
- );
- }
-
- /**
- *
- * @param mixed $resource
- * @param mixed $privilege
- * @return bool
- */
- public function isAllowed($resource, $privilege = null)
- {
- $acl = $this->getAcl();
- if (!$acl->hasRole($this)) {
- $acl->addRole($this);
- }
- $isAllowed = $acl->isAllowed($this, $resource, $privilege);
-
- if ($this->logger) {
- if ($isAllowed) {
- $this->logger->debug("Access allowed for {$this->getRoleId()}/{$resource}/{$privilege}");
- } else {
- $this->logger->debug("Access denied for {$this->getRoleId()}/{$resource}/{$privilege}");
- }
- }
-
- return $isAllowed;
- }
-
- public function isOwner($resource)
- {
- if (isset($resource['user_id'])) {
- return (int) $this->getId() === (int) $resource['user_id'];
- }
-
- if (isset($resource['User'])) {
- return (int) $this->getId() === (int) $resource['User']['id'];
- }
-
- return false;
- }
-
- public function isLogged()
- {
- return !empty($this->id);
- }
-
- public function logout()
- {
- Zend_Auth::getInstance()->clearIdentity();
- }
-
- /**
- * Enter description here...
- *
- * @return stdClass
- */
- public function toIdentity()
- {
- $identity = $this->toArray();
- return (object) $identity;
- }
-
- /**
- * Enter description here...
- *
- * @param stdClass $identity
- * @return Identity_User
- */
- public function fromIdentity(stdClass $identity)
- {
- $this->fromArray((array) $identity);
- $this->assignIdentifier($identity->id);
-
- return $this;
- }
-
- public function getRoleId()
- {
- return $this->_get('role_id');
- }
- }