1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Users;
13
14 15 16
17 class SaveOperation extends \Icybee\Operation\Constructor\Save
18 {
19 protected function lazy_get_properties()
20 {
21 global $core;
22
23 $properties = parent::lazy_get_properties();
24 $request = $this->request;
25
26 if ($request[User::PASSWORD])
27 {
28 $properties[User::PASSWORD] = $request[User::PASSWORD];
29 }
30
31 if ($core->user->has_permission(Module::PERMISSION_ADMINISTER, $this->module))
32 {
33
34
35
36
37
38 $roles = [];
39
40 if ($request[User::ROLES])
41 {
42 foreach ($request[User::ROLES] as $rid => $value)
43 {
44 $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
45
46 if (!$value)
47 {
48 continue;
49 }
50
51 $roles[] = (int) $rid;
52 }
53 }
54
55 $properties[User::ROLES] = $roles;
56
57
58
59
60
61
62 $sites = [];
63
64 if ($request[User::RESTRICTED_SITES])
65 {
66 foreach ($request[User::RESTRICTED_SITES] as $siteid => $value)
67 {
68 $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
69
70 if (!$value)
71 {
72 continue;
73 }
74
75 $sites[] = (int) $siteid;
76 }
77 }
78
79 $properties[User::RESTRICTED_SITES] = $sites;
80 }
81 else
82 {
83 unset($properties[User::IS_ACTIVATED]);
84 }
85
86 return $properties;
87 }
88
89 90 91 92 93
94 protected function control_permission($permission=Module::PERMISSION_CREATE)
95 {
96 global $core;
97
98 $user = $core->user;
99
100 if ($user->uid == $this->key && $user->has_permission('modify own profile'))
101 {
102 return true;
103 }
104
105 return parent::control_permission($permission);
106 }
107
108 protected function control_ownership()
109 {
110 global $core;
111
112 $user = $core->user;
113
114 if ($user->uid == $this->key && $user->has_permission('modify own profile'))
115 {
116
117
118 $this->record = $user;
119
120 return true;
121 }
122
123 return parent::control_ownership();
124 }
125
126 127 128 129 130
131 protected function control_form()
132 {
133 $this->request->params[User::ROLES][2] = 'on';
134
135 return parent::control_form($this);
136 }
137
138 protected function validate(\ICanboogie\Errors $errors)
139 {
140 global $core;
141
142 $properties = $this->properties;
143
144 if (!empty($properties[User::PASSWORD]))
145 {
146 if (!$this->request[User::PASSWORD . '-verify'])
147 {
148 $errors[User::PASSWORD . '-verify'] = $errors->format('Password verify is empty.');
149 }
150
151 if ($properties[User::PASSWORD] != $this->request[User::PASSWORD . '-verify'])
152 {
153 $errors[User::PASSWORD . '-verify'] = $errors->format("Password and password verify don't match.");
154 }
155 }
156
157 $uid = $this->key ? $this->key : 0;
158 $model = $core->models['users'];
159
160
161
162
163
164 if (isset($properties[User::USERNAME]))
165 {
166 $username = $properties[User::USERNAME];
167 $used = $model->select('uid')->where('username = ? AND uid != ?', $username, $uid)->rc;
168
169 if ($used)
170 {
171 $errors[User::USERNAME] = $errors->format("L'identifiant %username est déjà utilisé.", [
172
173 '%username' => $username
174
175 ]);
176 }
177 }
178
179
180
181
182
183 if (isset($properties[User::EMAIL]))
184 {
185 $email = $properties[User::EMAIL];
186 $used = $model->select('uid')->where('email = ? AND uid != ?', $email, $uid)->rc;
187
188 if ($used)
189 {
190 $errors[User::EMAIL] = $errors->format("L'adresse email %email est déjà utilisée.", [
191
192 '%email' => $email
193
194 ]);
195 }
196 }
197
198 return count($errors) == 0 && parent::validate($errors);
199 }
200
201 protected function process()
202 {
203 global $core;
204
205 $previous_uid = $core->user_id;
206
207 $rc = parent::process();
208
209 $uid = $rc['key'];
210
211 if (!$previous_uid)
212 {
213 $this->response->message = $errors->format("Your profile has been created.");
214 }
215 else if ($core->user_id == $uid)
216 {
217 $this->response->message = $errors->format($rc['mode'] == 'update' ? "Your profile has been updated." : "Your profile has been created.");
218 }
219 else
220 {
221 $record = $this->module->model[$uid];
222
223 $this->response->message = $errors->format($rc['mode'] == 'update' ? "%name's profile has been updated." : "%name's profile has been created.", [ 'name' => $record->name ]);
224 }
225
226 return $rc;
227 }
228 }