PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/controllers/single_page/account/edit_profile.php

http://github.com/concrete5/concrete5
PHP | 158 lines | 132 code | 21 blank | 5 comment | 21 complexity | ce580d80961ae0a905723a3497b9422a MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. namespace Concrete\Controller\SinglePage\Account;
  3. use Concrete\Core\Attribute\Category\CategoryService;
  4. use Concrete\Core\Attribute\Key\UserKey as UserAttributeKey;
  5. use Concrete\Core\Authentication\AuthenticationType;
  6. use Concrete\Core\Authentication\AuthenticationTypeFailureException;
  7. use Concrete\Core\Error\UserMessageException;
  8. use Concrete\Core\Localization\Localization;
  9. use Concrete\Core\Page\Controller\AccountPageController;
  10. use Exception;
  11. class EditProfile extends AccountPageController
  12. {
  13. public $helpers = ['form', 'date'];
  14. public function view()
  15. {
  16. $profile = $this->get('profile');
  17. if (!is_object($profile)) {
  18. throw new UserMessageException(t('You must be logged in to access this page.'));
  19. }
  20. $locales = [];
  21. $languages = Localization::getAvailableInterfaceLanguages();
  22. if (count($languages) > 0) {
  23. array_unshift($languages, Localization::BASE_LOCALE);
  24. }
  25. if (count($languages) > 0) {
  26. foreach ($languages as $lang) {
  27. $locales[$lang] = \Punic\Language::getName($lang, $lang);
  28. }
  29. asort($locales);
  30. $locales = array_merge(['' => tc('Default locale', '** Default')], $locales);
  31. }
  32. $this->set('locales', $locales);
  33. $service = $this->app->make(CategoryService::class);
  34. $categoryEntity = $service->getByHandle('user');
  35. $category = $categoryEntity->getController();
  36. $setManager = $category->getSetManager();
  37. $attributeSets = [];
  38. foreach ($setManager->getAttributeSets() as $set) {
  39. foreach ($set->getAttributeKeys() as $ak) {
  40. if ($ak->isAttributeKeyEditableOnProfile()) {
  41. $attributeSets[$set->getAttributeSetDisplayName()][] = $ak;
  42. }
  43. }
  44. }
  45. $this->set('attributeSets', $attributeSets);
  46. $unassignedAttributes = [];
  47. foreach ($setManager->getUnassignedAttributeKeys() as $ak) {
  48. if ($ak->isAttributeKeyEditableOnProfile()) {
  49. $unassignedAttributes[] = $ak;
  50. }
  51. }
  52. $this->set('unassignedAttributes', $unassignedAttributes);
  53. }
  54. public function save_complete()
  55. {
  56. $this->set('success', t('Profile updated successfully.'));
  57. $this->view();
  58. }
  59. public function callback($type, $method = 'callback')
  60. {
  61. $at = AuthenticationType::getByHandle($type);
  62. $this->view();
  63. if (!method_exists($at->controller, $method)) {
  64. throw new UserMessageException('Invalid method.');
  65. }
  66. if ($method != 'callback') {
  67. if (!is_array($at->controller->apiMethods) || !in_array($method, $at->controller->apiMethods)) {
  68. throw new UserMessageException('Invalid method.');
  69. }
  70. }
  71. try {
  72. $message = call_user_func([$at->controller, $method]);
  73. if (trim($message)) {
  74. $this->set('message', $message);
  75. }
  76. } catch (Exception $e) {
  77. if ($e instanceof AuthenticationTypeFailureException) {
  78. // Throw again if this is a big`n
  79. throw $e;
  80. }
  81. $this->error->add($e->getMessage());
  82. }
  83. }
  84. public function save()
  85. {
  86. $this->view();
  87. $ui = $this->get('profile');
  88. /* @var \Concrete\Core\User\UserInfo $ui */
  89. $app = $this->app;
  90. $valt = $app->make('token');
  91. $data = $this->post();
  92. if (!$valt->validate('profile_edit')) {
  93. $this->error->add($valt->getErrorMessage());
  94. }
  95. // validate the user's email
  96. $email = $this->post('uEmail');
  97. $app->make('validator/user/email')->isValidFor($email, $ui, $this->error);
  98. // Username validation
  99. $username = $this->post('uName');
  100. if ($username) {
  101. $app->make('validator/user/name')->isValidFor($username, $ui, $this->error);
  102. }
  103. // password
  104. if (strlen($data['uPasswordNew'])) {
  105. $passwordNew = $data['uPasswordNew'];
  106. $passwordNewConfirm = $data['uPasswordNewConfirm'];
  107. $app->make('validator/password')->isValidFor($passwordNew, $ui, $this->error);
  108. if ($passwordNew) {
  109. if ($passwordNew != $passwordNewConfirm) {
  110. $this->error->add(t('The two passwords provided do not match.'));
  111. }
  112. }
  113. $data['uPasswordConfirm'] = $passwordNew;
  114. $data['uPassword'] = $passwordNew;
  115. }
  116. $aks = UserAttributeKey::getEditableInProfileList();
  117. foreach ($aks as $uak) {
  118. $controller = $uak->getController();
  119. $validator = $controller->getValidator();
  120. $response = $validator->validateSaveValueRequest($controller, $this->request, $uak->isAttributeKeyRequiredOnProfile());
  121. if (!$response->isValid()) {
  122. $error = $response->getErrorObject();
  123. $this->error->add($error);
  124. }
  125. }
  126. if (!$this->error->has()) {
  127. $data['uEmail'] = $email;
  128. $config = $this->app->make('config');
  129. if ($config->get('concrete.misc.user_timezones')) {
  130. $data['uTimezone'] = $this->post('uTimezone');
  131. }
  132. $ui->saveUserAttributesForm($aks);
  133. $ui->update($data);
  134. $this->redirect('/account/edit_profile', 'save_complete');
  135. }
  136. }
  137. }