123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <?php
- /**
- *
- * PHP Pro Bid $Id$ lcX9DZKUmd5ReaYidJVrAuBwc3jFCYKpWaSAQudG2OQ=
- *
- * @link http://www.phpprobid.com
- * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
- * @license http://www.phpprobid.com/license Commercial License
- *
- * @version 7.10 [rev.7.10.01]
- */
- /**
- * authentication adapter
- */
- namespace Ppb\Authentication;
- use Cube\Authentication\Adapter\AdapterInterface,
- Cube\Authentication\Result as AuthenticationResult,
- Cube\Translate,
- Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,
- Cube\Controller\Front,
- Cube\Db\Expr,
- Ppb\Service\Users as UsersService,
- Ppb\Service\BlockedUsers as BlockedUsersService,
- Ppb\Db\Table\Row\BlockedUser as BlockedUserModel;
- class Adapter implements AdapterInterface
- {
- /**
- *
- * whether to check old v6.x passwords
- */
- const V6_HASHES = true;
- /**
- *
- * user id
- *
- * @var int
- */
- protected $_id = null;
- /**
- *
- * username
- *
- * @var string
- */
- protected $_username = null;
- /**
- *
- * password
- *
- * @var string
- */
- protected $_password = null;
- /**
- *
- * email address
- *
- * @var string
- */
- protected $_email = null;
- /**
- *
- * allowed roles
- *
- * @var array
- */
- protected $_allowedRoles = array();
- /**
- *
- * denied roles
- *
- * @var array
- */
- protected $_deniedRoles = array();
- /**
- *
- * check for blocked user / ip
- *
- * @var bool
- */
- protected $_checkBlockedUser = true;
- /**
- *
- * translate adapter
- *
- * @var \Cube\Translate\Adapter\AbstractAdapter
- */
- protected $_translate;
- public function __construct($params = array(), $id = null, $allowedRoles = array(), $deniedRoles = array())
- {
- if (array_key_exists('username', $params)) {
- $this->setUsername(
- $params['username']);
- }
- if (array_key_exists('password', $params)) {
- $this->setPassword(
- $params['password']);
- }
- if (array_key_exists('email', $params)) {
- $this->setEmail(
- $params['email']);
- }
- $this->setId($id)
- ->setAllowedRoles($allowedRoles)
- ->setDeniedRoles($deniedRoles);
- }
- /**
- *
- * get id
- *
- * @return int
- */
- public function getId()
- {
- return $this->_id;
- }
- /**
- *
- * set id
- *
- * @param int $id
- *
- * @return $this;
- */
- public function setId($id)
- {
- $this->_id = $id;
- return $this;
- }
- /**
- *
- * get username
- *
- * @return string
- */
- public function getUsername()
- {
- return $this->_username;
- }
- /**
- *
- * set username
- *
- * @param string $username
- *
- * @return $this
- */
- public function setUsername($username)
- {
- $this->_username = $username;
- return $this;
- }
- /**
- *
- * get password
- *
- * @return string
- */
- public function getPassword()
- {
- return $this->_password;
- }
- /**
- *
- * set password
- *
- * @param string $password
- *
- * @return $this
- */
- public function setPassword($password)
- {
- $this->_password = $password;
- return $this;
- }
- /**
- *
- * get email
- *
- * @return string
- */
- public function getEmail()
- {
- return $this->_email;
- }
- /**
- *
- * set email
- *
- * @param string $email
- *
- * @return $this
- */
- public function setEmail($email)
- {
- $this->_email = $email;
- return $this;
- }
- /**
- *
- * get allowed roles
- *
- * @return array
- */
- public function getAllowedRoles()
- {
- return $this->_allowedRoles;
- }
- /**
- *
- * set allowed roles
- *
- * @param array $allowedRoles
- *
- * @return $this
- */
- public function setAllowedRoles($allowedRoles)
- {
- $this->_allowedRoles = $allowedRoles;
- return $this;
- }
- /**
- *
- * get denied roles
- *
- * @return array
- */
- public function getDeniedRoles()
- {
- return $this->_deniedRoles;
- }
- /**
- *
- * set denied roles
- *
- * @param array $deniedRoles
- *
- * @return $this
- */
- public function setDeniedRoles($deniedRoles)
- {
- $this->_deniedRoles = $deniedRoles;
- return $this;
- }
- /**
- * get check blocked user
- *
- * @return boolean
- */
- public function isCheckBlockedUser()
- {
- return $this->_checkBlockedUser;
- }
- /**
- *
- * set check blocked user
- *
- * @param boolean $checkBlockedUser
- *
- * @return $this
- */
- public function setCheckBlockedUser($checkBlockedUser)
- {
- $this->_checkBlockedUser = $checkBlockedUser;
- return $this;
- }
- /**
- *
- * set translate adapter
- *
- * @param \Cube\Translate\Adapter\AbstractAdapter $translate
- *
- * @return $this
- */
- public function setTranslate(TranslateAdapter $translate)
- {
- $this->_translate = $translate;
- return $this;
- }
- /**
- *
- * get translate adapter
- *
- * @return \Cube\Translate\Adapter\AbstractAdapter
- */
- public function getTranslate()
- {
- if (!$this->_translate instanceof TranslateAdapter) {
- $translate = Front::getInstance()->getBootstrap()->getResource('translate');
- if ($translate instanceof Translate) {
- $this->setTranslate(
- $translate->getAdapter());
- }
- }
- return $this->_translate;
- }
- /**
- *
- * authenticate user by username and password or if id is set, authenticate directly
- *
- * @return AuthenticationResult
- */
- public function authenticate()
- {
- $usersService = new UsersService();
- $user = null;
- $id = $this->getId();
- $username = $this->getUsername();
- $email = $this->getEmail();
- $password = $this->getPassword();
- if ($id !== null) {
- $user = $usersService->findBy('id', $id);
- }
- else if ($username !== null || $email !== null) {
- $user = $usersService->findBy('username', $username);
- if (!$user && $email !== null) {
- $user = $usersService->findBy('email', $email);
- }
- }
- $success = false;
- $blockedUser = null;
- $translate = $this->getTranslate();
- $messages = array(
- $translate->_('The login details you have submitted are invalid.'));
- if ($this->isCheckBlockedUser()) {
- $blockedUsersService = new BlockedUsersService();
- $blockedUser = $blockedUsersService->check(
- BlockedUserModel::ACTION_REGISTER,
- array(
- 'ip' => $_SERVER['REMOTE_ADDR'],
- 'username' => $username,
- 'email' => $email,
- ));
- }
- if ($blockedUser !== null) {
- $success = false;
- $messages = array($blockedUser->blockMessage());
- }
- else if (count($user) > 0) {
- $allowedRoles = $this->getAllowedRoles();
- $deniedRoles = $this->getDeniedRoles();
- if ($id !== null) {
- $success = true;
- }
- else if (strcmp($usersService->hashPassword($password, $user['salt']), $user['password']) === 0) {
- $success = true;
- }
- else if (self::V6_HASHES && strcmp(md5(md5($password) . $user['salt']), $user['password']) === 0) {
- $success = true;
- }
- if (count($allowedRoles) > 0 && !array_key_exists($user['role'], $allowedRoles)) {
- $success = false;
- }
- if (array_key_exists($user['role'], $deniedRoles)) {
- $success = false;
- }
- }
- if ($success === true) {
- $usersService->save(array(
- 'last_login' => new Expr('now()'),
- 'ip_address' => (!empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '',
- ), $user['id']);
- return new AuthenticationResult(true, array(
- 'id' => $user['id'],
- 'username' => $user['username'],
- 'role' => $user['role'],
- ));
- }
- else {
- return new AuthenticationResult(false, array(), $messages);
- }
- }
- }
|