/
add.php
121 lines (102 loc) · 4.71 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
<?php
namespace Concrete\Controller\SinglePage\Dashboard\Users;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Permission\Checker;
use Concrete\Core\User\Group\GroupRepository;
use GroupList;
use Imagine\Image\Box;
use Localization;
use PermissionKey;
use UserAttributeKey;
class Add extends DashboardPageController
{
public function view()
{
$locales = Localization::getAvailableInterfaceLanguageDescriptions();
$attribs = UserAttributeKey::getRegistrationList();
$assignment = PermissionKey::getByHandle('edit_user_properties')->getMyAssignment();
$gl = new GroupList();
$gArray = $gl->getPagination()->setMaxPerPage(10000)->getCurrentPageResults();
$this->set('valc', $this->app->make('helper/concrete/validation'));
$this->set('ih', $this->app->make('helper/concrete/ui'));
$this->set('av', $this->app->make('helper/concrete/avatar'));
$this->set('dtt', $this->app->make('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();
$username = trim($_POST['uName']);
$username = preg_replace("/\s+/", ' ', $username);
$_POST['uName'] = $username;
$password = $_POST['uPassword'];
$this->app->make('validator/user/name')->isValid($username, $this->error);
$this->app->make('validator/password')->isValid($password, $this->error);
$this->app->make('validator/user/email')->isValid($_POST['uEmail'], $this->error);
if (!$this->token->validate('submit')) {
$this->error->add($this->token->getErrorMessage());
}
$aks = UserAttributeKey::getRegistrationList();
foreach ($aks as $uak) {
$controller = $uak->getController();
$validator = $controller->getValidator();
$response = $validator->validateSaveValueRequest(
$controller,
$this->request,
$uak->isAttributeKeyRequiredOnRegister()
);
if (!$response->isValid()) {
$error = $response->getErrorObject();
$this->error->add($error);
}
}
if (!$this->error->has()) {
// do the registration
$data = ['uName' => $username, 'uPassword' => $password, 'uEmail' => $_POST['uEmail'], 'uDefaultLanguage' => $_POST['uDefaultLanguage'], 'uHomeFileManagerFolderID' => ($_POST['uHomeFileManagerFolderID'] == 0 ? '' : $_POST['uHomeFileManagerFolderID'])];
$uo = $this->app['user/registration']->create($data);
if (is_object($uo)) {
if ($assignment->allowEditAvatar()) {
if (!empty($_FILES['uAvatar']) && is_uploaded_file($_FILES['uAvatar']['tmp_name'])) {
$config = $this->app->make('config');
$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 = [];
foreach ($aks as $uak) {
if (in_array($uak->getAttributeKeyID(), $assignment->getAttributesAllowedArray())) {
$saveAttributes[] = $uak;
}
}
if (count($saveAttributes) > 0) {
$uo->saveUserAttributesForm($saveAttributes);
}
$groupRepo = $this->app->make(GroupRepository::class);
$gIDs = [];
if (!empty($_POST['gID']) && is_array($_POST['gID'])) {
foreach ($_POST['gID'] as $gID) {
$gx = $groupRepo->getGroupById($gID);
$gxp = new Checker($gx);
if ($gxp->canAssignGroup()) {
$gIDs[] = $gID;
}
}
}
$uo->updateGroups($gIDs);
$uID = $uo->getUserID();
return $this->buildRedirect(['/dashboard/users/search', 'edit', $uID, 'created']);
}
$this->error->add(t('An error occurred while trying to create the account.'));
$this->set('error', $this->error);
} else {
$this->view();
}
}
}