Adapter.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ lcX9DZKUmd5ReaYidJVrAuBwc3jFCYKpWaSAQudG2OQ=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.10 [rev.7.10.01]
  11. */
  12. /**
  13. * authentication adapter
  14. */
  15. namespace Ppb\Authentication;
  16. use Cube\Authentication\Adapter\AdapterInterface,
  17. Cube\Authentication\Result as AuthenticationResult,
  18. Cube\Translate,
  19. Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,
  20. Cube\Controller\Front,
  21. Cube\Db\Expr,
  22. Ppb\Service\Users as UsersService,
  23. Ppb\Service\BlockedUsers as BlockedUsersService,
  24. Ppb\Db\Table\Row\BlockedUser as BlockedUserModel;
  25. class Adapter implements AdapterInterface
  26. {
  27. /**
  28. *
  29. * whether to check old v6.x passwords
  30. */
  31. const V6_HASHES = true;
  32. /**
  33. *
  34. * user id
  35. *
  36. * @var int
  37. */
  38. protected $_id = null;
  39. /**
  40. *
  41. * username
  42. *
  43. * @var string
  44. */
  45. protected $_username = null;
  46. /**
  47. *
  48. * password
  49. *
  50. * @var string
  51. */
  52. protected $_password = null;
  53. /**
  54. *
  55. * email address
  56. *
  57. * @var string
  58. */
  59. protected $_email = null;
  60. /**
  61. *
  62. * allowed roles
  63. *
  64. * @var array
  65. */
  66. protected $_allowedRoles = array();
  67. /**
  68. *
  69. * denied roles
  70. *
  71. * @var array
  72. */
  73. protected $_deniedRoles = array();
  74. /**
  75. *
  76. * check for blocked user / ip
  77. *
  78. * @var bool
  79. */
  80. protected $_checkBlockedUser = true;
  81. /**
  82. *
  83. * translate adapter
  84. *
  85. * @var \Cube\Translate\Adapter\AbstractAdapter
  86. */
  87. protected $_translate;
  88. public function __construct($params = array(), $id = null, $allowedRoles = array(), $deniedRoles = array())
  89. {
  90. if (array_key_exists('username', $params)) {
  91. $this->setUsername(
  92. $params['username']);
  93. }
  94. if (array_key_exists('password', $params)) {
  95. $this->setPassword(
  96. $params['password']);
  97. }
  98. if (array_key_exists('email', $params)) {
  99. $this->setEmail(
  100. $params['email']);
  101. }
  102. $this->setId($id)
  103. ->setAllowedRoles($allowedRoles)
  104. ->setDeniedRoles($deniedRoles);
  105. }
  106. /**
  107. *
  108. * get id
  109. *
  110. * @return int
  111. */
  112. public function getId()
  113. {
  114. return $this->_id;
  115. }
  116. /**
  117. *
  118. * set id
  119. *
  120. * @param int $id
  121. *
  122. * @return $this;
  123. */
  124. public function setId($id)
  125. {
  126. $this->_id = $id;
  127. return $this;
  128. }
  129. /**
  130. *
  131. * get username
  132. *
  133. * @return string
  134. */
  135. public function getUsername()
  136. {
  137. return $this->_username;
  138. }
  139. /**
  140. *
  141. * set username
  142. *
  143. * @param string $username
  144. *
  145. * @return $this
  146. */
  147. public function setUsername($username)
  148. {
  149. $this->_username = $username;
  150. return $this;
  151. }
  152. /**
  153. *
  154. * get password
  155. *
  156. * @return string
  157. */
  158. public function getPassword()
  159. {
  160. return $this->_password;
  161. }
  162. /**
  163. *
  164. * set password
  165. *
  166. * @param string $password
  167. *
  168. * @return $this
  169. */
  170. public function setPassword($password)
  171. {
  172. $this->_password = $password;
  173. return $this;
  174. }
  175. /**
  176. *
  177. * get email
  178. *
  179. * @return string
  180. */
  181. public function getEmail()
  182. {
  183. return $this->_email;
  184. }
  185. /**
  186. *
  187. * set email
  188. *
  189. * @param string $email
  190. *
  191. * @return $this
  192. */
  193. public function setEmail($email)
  194. {
  195. $this->_email = $email;
  196. return $this;
  197. }
  198. /**
  199. *
  200. * get allowed roles
  201. *
  202. * @return array
  203. */
  204. public function getAllowedRoles()
  205. {
  206. return $this->_allowedRoles;
  207. }
  208. /**
  209. *
  210. * set allowed roles
  211. *
  212. * @param array $allowedRoles
  213. *
  214. * @return $this
  215. */
  216. public function setAllowedRoles($allowedRoles)
  217. {
  218. $this->_allowedRoles = $allowedRoles;
  219. return $this;
  220. }
  221. /**
  222. *
  223. * get denied roles
  224. *
  225. * @return array
  226. */
  227. public function getDeniedRoles()
  228. {
  229. return $this->_deniedRoles;
  230. }
  231. /**
  232. *
  233. * set denied roles
  234. *
  235. * @param array $deniedRoles
  236. *
  237. * @return $this
  238. */
  239. public function setDeniedRoles($deniedRoles)
  240. {
  241. $this->_deniedRoles = $deniedRoles;
  242. return $this;
  243. }
  244. /**
  245. * get check blocked user
  246. *
  247. * @return boolean
  248. */
  249. public function isCheckBlockedUser()
  250. {
  251. return $this->_checkBlockedUser;
  252. }
  253. /**
  254. *
  255. * set check blocked user
  256. *
  257. * @param boolean $checkBlockedUser
  258. *
  259. * @return $this
  260. */
  261. public function setCheckBlockedUser($checkBlockedUser)
  262. {
  263. $this->_checkBlockedUser = $checkBlockedUser;
  264. return $this;
  265. }
  266. /**
  267. *
  268. * set translate adapter
  269. *
  270. * @param \Cube\Translate\Adapter\AbstractAdapter $translate
  271. *
  272. * @return $this
  273. */
  274. public function setTranslate(TranslateAdapter $translate)
  275. {
  276. $this->_translate = $translate;
  277. return $this;
  278. }
  279. /**
  280. *
  281. * get translate adapter
  282. *
  283. * @return \Cube\Translate\Adapter\AbstractAdapter
  284. */
  285. public function getTranslate()
  286. {
  287. if (!$this->_translate instanceof TranslateAdapter) {
  288. $translate = Front::getInstance()->getBootstrap()->getResource('translate');
  289. if ($translate instanceof Translate) {
  290. $this->setTranslate(
  291. $translate->getAdapter());
  292. }
  293. }
  294. return $this->_translate;
  295. }
  296. /**
  297. *
  298. * authenticate user by username and password or if id is set, authenticate directly
  299. *
  300. * @return AuthenticationResult
  301. */
  302. public function authenticate()
  303. {
  304. $usersService = new UsersService();
  305. $user = null;
  306. $id = $this->getId();
  307. $username = $this->getUsername();
  308. $email = $this->getEmail();
  309. $password = $this->getPassword();
  310. if ($id !== null) {
  311. $user = $usersService->findBy('id', $id);
  312. }
  313. else if ($username !== null || $email !== null) {
  314. $user = $usersService->findBy('username', $username);
  315. if (!$user && $email !== null) {
  316. $user = $usersService->findBy('email', $email);
  317. }
  318. }
  319. $success = false;
  320. $blockedUser = null;
  321. $translate = $this->getTranslate();
  322. $messages = array(
  323. $translate->_('The login details you have submitted are invalid.'));
  324. if ($this->isCheckBlockedUser()) {
  325. $blockedUsersService = new BlockedUsersService();
  326. $blockedUser = $blockedUsersService->check(
  327. BlockedUserModel::ACTION_REGISTER,
  328. array(
  329. 'ip' => $_SERVER['REMOTE_ADDR'],
  330. 'username' => $username,
  331. 'email' => $email,
  332. ));
  333. }
  334. if ($blockedUser !== null) {
  335. $success = false;
  336. $messages = array($blockedUser->blockMessage());
  337. }
  338. else if (count($user) > 0) {
  339. $allowedRoles = $this->getAllowedRoles();
  340. $deniedRoles = $this->getDeniedRoles();
  341. if ($id !== null) {
  342. $success = true;
  343. }
  344. else if (strcmp($usersService->hashPassword($password, $user['salt']), $user['password']) === 0) {
  345. $success = true;
  346. }
  347. else if (self::V6_HASHES && strcmp(md5(md5($password) . $user['salt']), $user['password']) === 0) {
  348. $success = true;
  349. }
  350. if (count($allowedRoles) > 0 && !array_key_exists($user['role'], $allowedRoles)) {
  351. $success = false;
  352. }
  353. if (array_key_exists($user['role'], $deniedRoles)) {
  354. $success = false;
  355. }
  356. }
  357. if ($success === true) {
  358. $usersService->save(array(
  359. 'last_login' => new Expr('now()'),
  360. 'ip_address' => (!empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '',
  361. ), $user['id']);
  362. return new AuthenticationResult(true, array(
  363. 'id' => $user['id'],
  364. 'username' => $user['username'],
  365. 'role' => $user['role'],
  366. ));
  367. }
  368. else {
  369. return new AuthenticationResult(false, array(), $messages);
  370. }
  371. }
  372. }