/
edit_profile.php
174 lines (150 loc) · 6.08 KB
/
edit_profile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace Concrete\Controller\SinglePage\Account;
use Concrete\Core\Attribute\Category\CategoryService;
use Concrete\Core\Attribute\Key\UserKey as UserAttributeKey;
use Concrete\Core\Authentication\AuthenticationType;
use Concrete\Core\Authentication\AuthenticationTypeFailureException;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\Localization\Localization;
use Concrete\Core\Page\Controller\AccountPageController;
use Exception;
class EditProfile extends AccountPageController
{
public $helpers = ['form', 'date'];
protected $displayUserName = true;
public function view()
{
$profile = $this->get('profile');
if (!is_object($profile)) {
throw new UserMessageException(t('You must be logged in to access this page.'));
}
if (!$this->displayUserName) {
// something has overridden this controller and we want to honor that
$displayUserName = false;
} else {
$displayUserName = $this->app->make(Repository::class)->get('concrete.user.edit_profile.display_username_field');
}
$this->set('displayUserName', $displayUserName);
$locales = [];
$languages = Localization::getAvailableInterfaceLanguages();
if (count($languages) > 0) {
array_unshift($languages, Localization::BASE_LOCALE);
}
if (count($languages) > 0) {
foreach ($languages as $lang) {
$locales[$lang] = \Punic\Language::getName($lang, $lang);
}
asort($locales);
$locales = array_merge(['' => tc('Default locale', '** Default')], $locales);
}
$this->set('locales', $locales);
$service = $this->app->make(CategoryService::class);
$categoryEntity = $service->getByHandle('user');
$category = $categoryEntity->getController();
$setManager = $category->getSetManager();
$attributeSets = [];
foreach ($setManager->getAttributeSets() as $set) {
foreach ($set->getAttributeKeys() as $ak) {
if ($ak->isAttributeKeyEditableOnProfile()) {
$attributeSets[$set->getAttributeSetDisplayName()][] = $ak;
}
}
}
$this->set('attributeSets', $attributeSets);
$unassignedAttributes = [];
foreach ($setManager->getUnassignedAttributeKeys() as $ak) {
if ($ak->isAttributeKeyEditableOnProfile()) {
$unassignedAttributes[] = $ak;
}
}
$this->set('unassignedAttributes', $unassignedAttributes);
}
public function save_complete()
{
$this->set('success', t('Profile updated successfully.'));
$this->view();
}
public function callback($type, $method = 'callback')
{
$at = AuthenticationType::getByHandle($type);
$this->view();
if (!method_exists($at->controller, $method)) {
throw new UserMessageException('Invalid method.');
}
if ($method != 'callback') {
if (!is_array($at->controller->apiMethods) || !in_array($method, $at->controller->apiMethods)) {
throw new UserMessageException('Invalid method.');
}
}
try {
$message = call_user_func([$at->controller, $method]);
if (trim($message)) {
$this->set('message', $message);
}
} catch (Exception $e) {
if ($e instanceof AuthenticationTypeFailureException) {
// Throw again if this is a big`n
throw $e;
}
$this->error->add($e->getMessage());
}
}
public function save()
{
$this->view();
$ui = $this->get('profile');
/* @var \Concrete\Core\User\UserInfo $ui */
$app = $this->app;
$valt = $app->make('token');
$data = $this->post();
if (!$valt->validate('profile_edit')) {
$this->error->add($valt->getErrorMessage());
}
// validate the user's email
$email = $this->post('uEmail');
$app->make('validator/user/email')->isValidFor($email, $ui, $this->error);
// Username validation
$username = $this->post('uName');
if ($username) {
$app->make('validator/user/name')->isValidFor($username, $ui, $this->error);
}
// password
if (strlen($data['uPasswordNew'])) {
$passwordCurrent = (string) $data['uPasswordCurrent'];
$passwordNew = $data['uPasswordNew'];
$passwordNewConfirm = $data['uPasswordNewConfirm'];
$app->make('validator/password')->isValidFor($passwordNew, $ui, $this->error);
if ($passwordNew) {
if (!$ui->passwordMatches($passwordCurrent)) {
$this->error->add(t('Invalid existing password provided.'));
}
if ($passwordNew != $passwordNewConfirm) {
$this->error->add(t('The two passwords provided do not match.'));
}
}
$data['uPasswordConfirm'] = $passwordNew;
$data['uPassword'] = $passwordNew;
}
$aks = UserAttributeKey::getEditableInProfileList();
foreach ($aks as $uak) {
$controller = $uak->getController();
$validator = $controller->getValidator();
$response = $validator->validateSaveValueRequest($controller, $this->request, $uak->isAttributeKeyRequiredOnProfile());
if (!$response->isValid()) {
$error = $response->getErrorObject();
$this->error->add($error);
}
}
if (!$this->error->has()) {
$data['uEmail'] = $email;
$config = $this->app->make('config');
if ($config->get('concrete.misc.user_timezones')) {
$data['uTimezone'] = $this->post('uTimezone');
}
$ui->saveUserAttributesForm($aks);
$ui->update($data);
$this->redirect('/account/edit_profile', 'save_complete');
}
}
}