PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/controllers/single_page/login.php

https://gitlab.com/koodersmiikka/operaatio-terveys
PHP | 402 lines | 318 code | 43 blank | 41 comment | 69 complexity | 3275e388e770d29d821651cef3d5e01c MD5 | raw file
  1. <?php
  2. namespace Concrete\Controller\SinglePage;
  3. use Concrete\Core\Authentication\AuthenticationType;
  4. use Concrete\Core\Authentication\AuthenticationTypeFailureException;
  5. use Concrete\Core\Routing\RedirectResponse;
  6. use Localization;
  7. use Page;
  8. use PageController;
  9. use Permissions;
  10. use User;
  11. use UserAttributeKey;
  12. use UserInfo;
  13. use View;
  14. class Login extends PageController
  15. {
  16. public $helpers = array('form');
  17. protected $locales = array();
  18. public function on_before_render()
  19. {
  20. if ($this->error->has()) {
  21. $this->set('error', $this->error);
  22. }
  23. }
  24. /* automagically run by the controller once we're done with the current method */
  25. /* method is passed to this method, the method that we were just finished running */
  26. public function account_deactivated()
  27. {
  28. $this->error->add(t('This user is inactive. Please contact us regarding this account.'));
  29. }
  30. public function session_invalidated()
  31. {
  32. $this->error->add(t('Your session has expired. Please sign in again.'));
  33. }
  34. /**
  35. * Concrete5_Controller_Login::callback
  36. * Call an AuthenticationTypeController method throw a uri.
  37. * Use: /login/TYPE/METHOD/PARAM1/.../PARAM10.
  38. *
  39. * @param string $type
  40. * @param string $method
  41. * @param null $a
  42. * @param null $b
  43. * @param null $c
  44. * @param null $d
  45. * @param null $e
  46. * @param null $f
  47. * @param null $g
  48. * @param null $h
  49. * @param null $i
  50. * @param null $j
  51. *
  52. * @throws \Concrete\Core\Authentication\AuthenticationTypeFailureException
  53. * @throws \Exception
  54. */
  55. public function callback($type = null, $method = 'callback', $a = null, $b = null, $c = null, $d = null, $e = null, $f = null, $g = null, $h = null, $i = null, $j = null)
  56. {
  57. if (!$type) {
  58. return $this->view();
  59. }
  60. $at = AuthenticationType::getByHandle($type);
  61. if ($at) {
  62. $this->set('authType', $at);
  63. }
  64. if (!method_exists($at->controller, $method)) {
  65. return $this->view();
  66. }
  67. if ($method != 'callback') {
  68. if (!is_array($at->controller->apiMethods) || !in_array($method, $at->controller->apiMethods)) {
  69. return $this->view();
  70. }
  71. }
  72. try {
  73. $params = func_get_args();
  74. array_shift($params);
  75. array_shift($params);
  76. $this->view();
  77. $this->set('authTypeParams', $params);
  78. $this->set('authTypeElement', $method);
  79. } catch (\exception $e) {
  80. if ($e instanceof AuthenticationTypeFailureException) {
  81. // Throw again if this is a big`n
  82. throw $e;
  83. }
  84. $this->error->add($e->getMessage());
  85. }
  86. }
  87. /**
  88. * Concrete5_Controller_Login::authenticate
  89. * Authenticate the user using a specific authentication type.
  90. *
  91. * @param $type AuthenticationType handle
  92. */
  93. public function authenticate($type = '')
  94. {
  95. $valt = $this->app->make('token');
  96. if (!$valt->validate('login_' . $type)) {
  97. $this->error->add($valt->getErrorMessage());
  98. } else {
  99. try {
  100. $at = AuthenticationType::getByHandle($type);
  101. $user = $at->controller->authenticate();
  102. if ($user && $user->isLoggedIn()) {
  103. $this->finishAuthentication($at);
  104. }
  105. } catch (\exception $e) {
  106. $this->error->add($e->getMessage());
  107. }
  108. }
  109. if (isset($at)) {
  110. $this->set('lastAuthType', $at);
  111. }
  112. $this->view();
  113. }
  114. /**
  115. * @param AuthenticationType $type Required
  116. *
  117. * @throws \Exception
  118. */
  119. public function finishAuthentication(/* AuthenticationType */
  120. $type = null
  121. ) {
  122. if (!$type || !($type instanceof AuthenticationType)) {
  123. return $this->view();
  124. }
  125. $u = new User();
  126. $config = $this->app->make('config');
  127. if ($config->get('concrete.i18n.choose_language_login')) {
  128. $userLocale = $this->post('USER_LOCALE');
  129. if (is_string($userLocale) && ($userLocale !== '')) {
  130. if ($userLocale !== 'en_US') {
  131. $availableLocales = Localization::getAvailableInterfaceLanguages();
  132. if (!in_array($userLocale, $availableLocales)) {
  133. $userLocale = '';
  134. }
  135. }
  136. if ($userLocale !== '') {
  137. if (Localization::activeLocale() !== $userLocale) {
  138. Localization::changeLocale($userLocale);
  139. }
  140. $u->setUserDefaultLanguage($userLocale);
  141. }
  142. }
  143. }
  144. $ui = UserInfo::getByID($u->getUserID());
  145. $aks = UserAttributeKey::getRegistrationList();
  146. $unfilled = array_values(
  147. array_filter(
  148. $aks,
  149. function ($ak) use ($ui) {
  150. return $ak->isAttributeKeyRequiredOnRegister() && !is_object($ui->getAttributeValueObject($ak));
  151. }));
  152. if (count($unfilled)) {
  153. $u->logout(false);
  154. if (!$this->error) {
  155. $this->on_start();
  156. }
  157. $this->set('required_attributes', $unfilled);
  158. $this->set('u', $u);
  159. $session = $this->app->make('session');
  160. $session->set('uRequiredAttributeUser', $u->getUserID());
  161. $session->set('uRequiredAttributeUserAuthenticationType', $type->getAuthenticationTypeHandle());
  162. $this->view();
  163. echo $this->getViewObject()->render();
  164. exit;
  165. }
  166. $u->setLastAuthType($type);
  167. $ue = new \Concrete\Core\User\Event\User($u);
  168. $this->app->make('director')->dispatch('on_user_login', $ue);
  169. $this->chooseRedirect();
  170. }
  171. public function on_start()
  172. {
  173. $config = $this->app->make('config');
  174. $this->error = $this->app->make('helper/validation/error');
  175. $this->set('valt', $this->app->make('helper/validation/token'));
  176. if ($config->get('concrete.user.registration.email_registration')) {
  177. $this->set('uNameLabel', t('Email Address'));
  178. } else {
  179. $this->set('uNameLabel', t('Username'));
  180. }
  181. $txt = $this->app->make('helper/text');
  182. if (isset($_GET['uName']) && strlen($_GET['uName'])
  183. ) { // pre-populate the username if supplied, if its an email address with special characters the email needs to be urlencoded first,
  184. $this->set("uName", trim($txt->email($_GET['uName'])));
  185. }
  186. $languages = array();
  187. $locales = array();
  188. if ($config->get('concrete.i18n.choose_language_login')) {
  189. $languages = Localization::getAvailableInterfaceLanguages();
  190. if (count($languages) > 0) {
  191. array_unshift($languages, 'en_US');
  192. }
  193. $locales = array();
  194. foreach ($languages as $lang) {
  195. $locales[$lang] = \Punic\Language::getName($lang, $lang);
  196. }
  197. asort($locales);
  198. $locales = array_merge(array('' => tc('Default locale', '** Default')), $locales);
  199. }
  200. $this->locales = $locales;
  201. $this->set('locales', $locales);
  202. }
  203. public function chooseRedirect()
  204. {
  205. $config = $this->app->make('config');
  206. $session = $this->app->make('session');
  207. if (!$this->error) {
  208. $this->error = $this->app->make('helper/validation/error');
  209. }
  210. $nh = $this->app->make('helper/validation/numbers');
  211. $navigation = $this->app->make('helper/navigation');
  212. $rUrl = false;
  213. $u = new User(); // added for the required registration attribute change above. We recalc the user and make sure they're still logged in
  214. if ($u->isRegistered()) {
  215. if ($u->config('NEWSFLOW_LAST_VIEWED') == 'FIRSTRUN') {
  216. $u->saveConfig('NEWSFLOW_LAST_VIEWED', 0);
  217. }
  218. do {
  219. // redirect to original destination
  220. if ($session->has('rUri')) {
  221. $rUrl = $session->get('rUri');
  222. $session->remove('rUri');
  223. if ($rUrl) {
  224. break;
  225. }
  226. }
  227. if ($session->has('rcID')) {
  228. $rcID = $session->get('rcID');
  229. if ($nh->integer($rcID)) {
  230. $rc = Page::getByID($rcID);
  231. } elseif (strlen($rcID)) {
  232. $rcID = trim($rcID, '/');
  233. $rc = Page::getByPath('/' . $rcID);
  234. }
  235. if ($rc instanceof Page && !$rc->isError()) {
  236. $rUrl = $navigation->getLinkToCollection($rc);
  237. break;
  238. }
  239. }
  240. // admin to dashboard?
  241. $dash = Page::getByPath("/dashboard", "RECENT");
  242. $dbp = new Permissions($dash);
  243. //should administrator be redirected to dashboard? defaults to yes if not set.
  244. $adminToDash = intval($config->get('concrete.misc.login_admin_to_dashboard'));
  245. if ($dbp->canRead() && $adminToDash) {
  246. if (!$rc instanceof Page || $rc->isError()) {
  247. $rc = $dash;
  248. }
  249. $rUrl = $navigation->getLinkToCollection($rc);
  250. break;
  251. }
  252. //options set in dashboard/users/registration
  253. $login_redirect_mode = $config->get('concrete.misc.login_redirect');
  254. //redirect to user profile
  255. if ($login_redirect_mode == 'PROFILE') {
  256. $profileURL = $u->getUserInfoObject()->getUserPublicProfileUrl();
  257. if ($profileURL) {
  258. $rUrl = $profileURL;
  259. }
  260. break;
  261. }
  262. //redirect to custom page
  263. $login_redirect_cid = intval($config->get('concrete.misc.login_redirect_cid'));
  264. if ($login_redirect_mode == 'CUSTOM' && $login_redirect_cid > 0) {
  265. $rc = Page::getByID($login_redirect_cid);
  266. if ($rc instanceof Page && !$rc->isError()) {
  267. $rUrl = $navigation->getLinkToCollection($rc);
  268. break;
  269. }
  270. }
  271. break;
  272. } while (false);
  273. if ($rUrl) {
  274. $r = new RedirectResponse($rUrl);
  275. $r->send();
  276. exit;
  277. } else {
  278. $this->redirect('/');
  279. }
  280. } else {
  281. $this->error->add(t('User is not registered. Check your authentication controller.'));
  282. $u->logout();
  283. }
  284. }
  285. public function view($type = null, $element = 'form')
  286. {
  287. $this->requireAsset('javascript', 'backstretch');
  288. $this->set('authTypeParams', $this->getSets());
  289. if (strlen($type)) {
  290. $at = AuthenticationType::getByHandle($type);
  291. $this->set('authType', $at);
  292. $this->set('authTypeElement', $element);
  293. }
  294. }
  295. public function fill_attributes()
  296. {
  297. try {
  298. $session = $this->app->make('session');
  299. if (!$session->has('uRequiredAttributeUser') ||
  300. intval($session->get('uRequiredAttributeUser')) < 1 ||
  301. !$session->has('uRequiredAttributeUserAuthenticationType') ||
  302. !$session->get('uRequiredAttributeUserAuthenticationType')
  303. ) {
  304. $session->remove('uRequiredAttributeUser');
  305. $session->remove('uRequiredAttributeUserAuthenticationType');
  306. throw new \Exception(t('Invalid Request, please attempt login again.'));
  307. }
  308. User::loginByUserID($session->get('uRequiredAttributeUser'));
  309. $session->remove('uRequiredAttributeUser');
  310. $u = new User();
  311. $at = AuthenticationType::getByHandle($session->get('uRequiredAttributeUserAuthenticationType'));
  312. $session->remove('uRequiredAttributeUserAuthenticationType');
  313. if (!$at) {
  314. throw new \Exception(t("Invalid Authentication Type"));
  315. }
  316. $ui = UserInfo::getByID($u->getUserID());
  317. $aks = UserAttributeKey::getRegistrationList();
  318. $unfilled = array_values(
  319. array_filter(
  320. $aks,
  321. function ($ak) use ($ui) {
  322. return $ak->isAttributeKeyRequiredOnRegister() && !is_object($ui->getAttributeValueObject($ak));
  323. }));
  324. $saveAttributes = array();
  325. foreach ($unfilled as $attribute) {
  326. $err = $attribute->validateAttributeForm();
  327. if ($err == false) {
  328. $this->error->add(t('The field "%s" is required', $attribute->getAttributeKeyDisplayName()));
  329. } elseif ($err instanceof \Concrete\Core\Error\Error) {
  330. $this->error->add($err);
  331. } else {
  332. $saveAttributes[] = $attribute;
  333. }
  334. }
  335. if (count($saveAttributes) > 0) {
  336. $ui->saveUserAttributesForm($saveAttributes);
  337. }
  338. $this->finishAuthentication($at);
  339. } catch (\Exception $e) {
  340. $this->error->add($e->getMessage());
  341. }
  342. }
  343. public function logout($token = false)
  344. {
  345. if ($this->app->make('token')->validate('logout', $token)) {
  346. $u = new User();
  347. $u->logout();
  348. $this->redirect('/');
  349. }
  350. }
  351. public function forward($cID = 0)
  352. {
  353. $nh = $this->app->make('helper/validation/numbers');
  354. if ($nh->integer($cID) && intval($cID) > 0) {
  355. $this->set('rcID', intval($cID));
  356. $this->app->make('session')->set('rcID', intval($cID));
  357. }
  358. }
  359. }