/
add.php
156 lines (135 loc) · 6.28 KB
/
add.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
<?php
namespace Concrete\Controller\SinglePage\Dashboard\Users;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Validation\ResponseInterface;
use Config;
use Imagine\Image\Box;
use Loader;
use UserInfo;
use PermissionKey;
use Permissions;
use UserAttributeKey;
use Group;
use Localization;
use GroupList;
class Add extends DashboardPageController
{
public function view()
{
$loc = Localization::getInstance();
$locales = Localization::getAvailableInterfaceLanguageDescriptions();
$attribs = UserAttributeKey::getRegistrationList();
$assignment = PermissionKey::getByHandle('edit_user_properties')->getMyAssignment();
$gl = new GroupList();
$gArray = $gl->getPagination()->setMaxPerPage(10000)->getCurrentPageResults();
$this->set('form', Loader::helper('form'));
$this->set('valt', Loader::helper('validation/token'));
$this->set('valc', Loader::helper('concrete/validation'));
$this->set('ih', Loader::helper('concrete/ui'));
$this->set('av', Loader::helper('concrete/avatar'));
$this->set('dtt', Loader::helper('form/date_time'));
$this->set('gArray', $gArray);
$this->set('assignment', $assignment);
$this->set('locales', $locales);
$this->set('attribs', $attribs);
}
public function submit()
{
$assignment = PermissionKey::getByHandle('edit_user_properties')->getMyAssignment();
$vals = Loader::helper('validation/strings');
$valt = Loader::helper('validation/token');
$valc = Loader::helper('concrete/validation');
$username = trim($_POST['uName']);
$username = preg_replace("/\s+/", " ", $username);
$_POST['uName'] = $username;
$password = $_POST['uPassword'];
if (!$vals->email($_POST['uEmail'])) {
$this->error->add(t('Invalid email address provided.'));
} elseif (!$valc->isUniqueEmail($_POST['uEmail'])) {
$this->error->add(t("The email address '%s' is already in use. Please choose another.", $_POST['uEmail']));
}
if (strlen($username) < Config::get('concrete.user.username.minimum')) {
$this->error->add(t('A username must be at least %s characters long.', Config::get('concrete.user.username.minimum')));
}
if (strlen($username) > Config::get('concrete.user.username.maximum')) {
$this->error->add(t('A username cannot be more than %s characters long.', Config::get('concrete.user.username.maximum')));
}
if (strlen($username) >= Config::get('concrete.user.username.minimum') && !$valc->username($username)) {
if (Config::get('concrete.user.username.allow_spaces')) {
$this->error->add(t('A username may only contain letters, numbers, spaces (not at the beginning/end), dots (not at the beginning/end), underscores (not at the beginning/end).'));
} else {
$this->error->add(t('A username may only contain letters, numbers, dots (not at the beginning/end), underscores (not at the beginning/end).'));
}
}
if (!$valc->isUniqueUsername($username)) {
$this->error->add(t("The username '%s' already exists. Please choose another", $username));
}
if ($username == USER_SUPER) {
$this->error->add(t('Invalid Username'));
}
\Core::make('validator/password')->isValid($password, $this->error);
if (!$valt->validate('submit')) {
$this->error->add($valt->getErrorMessage());
}
$aks = UserAttributeKey::getRegistrationList();
foreach ($aks as $uak) {
$controller = $uak->getController();
$validator = $controller->getValidator();
$response = $validator->validateSaveValueRequest(
$controller, $this->request, $uak->isAttributeKeyRequiredOnRegister()
);
/**
* @var $response ResponseInterface
*/
if (!$response->isValid()) {
$error = $response->getErrorObject();
$this->error->add($error);
}
}
if (!$this->error->has()) {
// do the registration
$data = array('uName' => $username, 'uPassword' => $password, 'uEmail' => $_POST['uEmail'], 'uDefaultLanguage' => $_POST['uDefaultLanguage']);
$uo = UserInfo::add($data);
if (is_object($uo)) {
$av = Loader::helper('concrete/avatar');
if ($assignment->allowEditAvatar()) {
if (is_uploaded_file($_FILES['uAvatar']['tmp_name'])) {
$image = \Image::open($_FILES['uAvatar']['tmp_name']);
$image = $image->thumbnail(new Box(
Config::get('concrete.icons.user_avatar.width'),
Config::get('concrete.icons.user_avatar.height')
));
$uo->updateUserAvatar($image);
}
}
$saveAttributes = array();
foreach ($aks as $uak) {
if (in_array($uak->getAttributeKeyID(), $assignment->getAttributesAllowedArray())) {
$saveAttributes[] = $uak;
}
}
if (count($saveAttributes) > 0) {
$uo->saveUserAttributesForm($saveAttributes);
}
$gIDs = array();
if (is_array($_POST['gID'])) {
foreach ($_POST['gID'] as $gID) {
$gx = Group::getByID($gID);
$gxp = new Permissions($gx);
if ($gxp->canAssignGroup()) {
$gIDs[] = $gID;
}
}
}
$uo->updateGroups($gIDs);
$uID = $uo->getUserID();
$this->redirect('/dashboard/users/search', 'view', $uID, 'created');
} else {
$this->error->add(t('An error occurred while trying to create the account.'));
$this->set('error', $this->error);
}
} else {
$this->view();
}
}
}