/blog/www/system/module/identity/library/Identity/User.php
PHP | 399 lines | 259 code | 50 blank | 90 comment | 27 complexity | 99d9e9457487f602809b6b91af63b230 MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php
- class Identity_User extends DoctrineX_Record
- {
- const GUEST_ROLE_ID = 1;
- const MEMBER_ROLE_ID = 2;
- const ADMIN_ROLE_ID = 3;
- /**
- * Enter description here...
- *
- * @var Zend_Auth_Adapter
- */
- protected static $_authAdapter = null;
- /**
- *
- * @var string
- */
- private $_secretKey = __CLASS__;
- /**
- *
- * @var Zend_Acl
- */
- protected $_acl = null;
- /**
- * Enter description here...
- *
- * @var int
- */
- protected $_rememberUntil = 1209600;
- public function setSecretKey($value)
- {
- $this->_secretKey = $value;
- return $this;
- }
- /**
- *
- * @return string
- */
- public function getSecretKey()
- {
- return $this->_secretKey;
- }
- /**
- *
- * @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;
- }
- /**
- * Enter description here...
- *
- * @param int $rememberUntil
- * @return Identity_User
- */
- public function setRememberUntil($rememberUntil)
- {
- $this->_rememberUntil = $rememberUntil;
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return int
- */
- public function getRememberUntil()
- {
- return $this->_rememberUntil;
- }
- public function setTableDefinition()
- {
- $this->hasColumn('id', 'integer', null, array('primary' => true, 'autoincrement' => true));
- $this->hasColumn('name', 'string', 64);
- $this->hasColumn('login', 'string', 64);
- $this->hasColumn('password', 'string', 32, array('fixed' => true));
- $this->hasColumn('email', 'string', 64);
- $this->hasColumn('code', 'string', 32, array('fixed' => true));
- $this->hasColumn('roleId', 'integer');
- $this->actAs(new DoctrineX_Template_Datetime());
- }
- public function preInsert()
- {
- $this->cleanup();
- if (empty($this->roleId)) {
- $this->roleId = self::GUEST_ROLE_ID;
- $this->code = $this->generateCode();
- $this->sendConfirmation();
- }
- }
- public function preSave($event)
- {
- if (in_array('password', $this->_modified)) {
- $this->password = md5(md5($this->_secretKey) . $this->password
- . md5($this->_secretKey));
- }
- }
- public function postSave()
- {
- $user = Zend_Registry::get('user');
- if ($user->id == $this->id) {
- $this->persistAuthResult((object)$this->toArray());
- }
- }
- public function preDelete()
- {
- IO_File::deleteDir($this->getUploadsPath());
- }
- public function isLogged()
- {
- return !$this->isGuest($this);
- }
- /**
- * Enter description here...
- *
- * @param string $login
- * @param string $password
- * @param bool $writeResult
- * @param bool $rememberMe
- * @return Zend_Auth_Result
- */
- public function login($login, $password = '', $persistResult = true, $rememberMe = true)
- {
- $password = md5($this->_secretKey) . $password . md5($this->_secretKey);
- $authAdapter = $this->getAuthAdapter();
- $authAdapter->setIdentity($login);
- $authAdapter->setCredential($password);
- $result = $authAdapter->authenticate($authAdapter);
- if ($result->isValid() && $persistResult) {
- $this->persistAuthResult($this->getAuthResult());
- if ($rememberMe) {
- Zend_Session::rememberMe($this->getRememberUntil());
- $saveHandler = Zend_Session::getSaveHandler();
- if ($saveHandler) {
- $saveHandler
- ->setLifetime($this->getRememberUntil())
- ->setOverrideLifetime(true);
- }
- }
- }
- return $result;
- }
- /**
- * Enter description here...
- *
- */
- public function logout()
- {
- Zend_Auth::getInstance()->clearIdentity();
- }
- /**
- *
- * @param mixed $resource
- * @param mixed $privilege
- * @return bool
- */
- public function isAllowed($resource, $privilege = null)
- {
- $acl = $this->getAcl();
- if (!$acl->has($resource)) {
- $resource = null;
- }
- return $acl->isAllowed($this, $resource, $privilege);
- }
- protected function _getIdentifier($resource)
- {
- if (is_scalar($resource)) {
- return $resource;
- }
- if (method_exists($resource, 'identifier')) {
- return $resource->identifier();
- }
- if (method_exists($resource, 'toArray')) {
- $resource = $resource->toArray();
- } else {
- $resource = (array) $resource;
- }
- if (isset($resource['id'])) {
- return $resource['id'];
- } else {
- return $resource;
- }
- }
- public function owner($resource)
- {
- $this->getSession()->owner[] = $this->_getIdentifier($resource);
- }
- public function isOwner($resource)
- {
- if (self::isAdmin($this)) {
- return true;
- } else if (false !== array_search($this->_getIdentifier($resource), (array) $this->getSession()->owner)) {
- return true;
- } else if (self::isGuest($this)) {
- return false;
- } else if (isset($resource['user_id'])) {
- return (int) $this->id === (int) $resource['user_id'];
- } else {
- return (int) $this->id === (int) $resource;
- }
- }
- /**
- *
- * @return string
- */
- public function getUploadsPath($id = null, $path = '')
- {
- if (null === $id) {
- $id = $this->id;
- }
- $uploads = Zend_Registry::get('nenoPath')->uploads . '/' . $id . $path;
- if (!IO_File::exists($uploads)) {
- IO_File::createDir($uploads);
- }
- return $uploads;
- }
- /**
- * Enter description here...
- *
- * @return Zend_Auth_Adapter
- */
- public static function getAuthAdapterStatic()
- {
- return self::$_authAdapter;
- }
- public function getAuthAdapter()
- {
- if (null === self::$_authAdapter) {
- self::$_authAdapter = new Zend_Auth_Adapter_DbTable(
- Zend_Registry::get('zendDb'),
- $this->getTable()->getTableName(), 'login', 'password', 'MD5(?) AND roleId != ' . self::GUEST_ROLE_ID
- );
- }
- return self::$_authAdapter;
- }
- /**
- * Enter description here...
- *
- * @return stdClass
- */
- public function getAuthResult()
- {
- $identity = $this->getAuthAdapter()->getResultRowObject(null, 'password');
- return $identity;
- }
- public function persistAuthResult($authResult)
- {
- $auth = Zend_Auth::getInstance();
- $auth->getStorage()->write($authResult);
- }
- public static function isGuest($user)
- {
- return $user instanceof Identity_User_Guest;
- }
- public static function isModerator($user)
- {
- return $user instanceof Identity_User_Moderator;
- }
- public static function isAdmin($user)
- {
- return $user instanceof Identity_User_Admin;
- }
- public function cleanup()
- {
- Doctrine_Query::create()
- ->delete()
- ->from('Identity_User User')
- ->where('User.roleId = ?', Identity_User::GUEST_ROLE_ID)
- ->andWhere('User.createdAt < ?', date(
- Zend_Registry::get('dbDatetimeFormat'),
- time() - 7 * 24 * 60 * 60)
- )
- ->execute();
- }
- public function generateCode()
- {
- return md5(
- mt_rand()
- . $this->login
- . mt_rand()
- );
- }
- public function sendConfirmation()
- {
- $view = Zend_Registry::get('zendView');
- $config = Zend_Registry::get('persistentConfig');
- $view->user = $this->toArray();
- $body = $view->render('user/_confirm-registration-email.phtml');
- $mail = new Zend_Mail('Utf-8');
- $mail->setFrom($config->support->email, $config->support->name);
- $mail->addTo($this->email, $this->name);
- $mail->setSubject($config->site->title);
- $mail->setBodyText($body);
- $mail->send();
- }
- /**
- * Enter description here...
- *
- */
- public function sendPasswordResetEmail()
- {
- $view = Zend_Registry::get('zendView');
- $config = Zend_Registry::get('persistentConfig');
- $this->code = $this->generateCode();
- $this->save();
- $view->user = $this->toArray();
- $body = $view->render('user/_password-reset-email.phtml');
- $mail = new Zend_Mail('Utf-8');
- $mail->setFrom($config->support->email, $config->support->name);
- $mail->addTo($this->email, $this->name);
- $mail->setSubject($config->site->title);
- $mail->setBodyText($body);
- $mail->send();
- }
- /**
- * 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;
- }
- }