diff --git a/controllers/UsersController.inc b/controllers/UsersController.inc index 70ab8904..03b9c4c0 100644 --- a/controllers/UsersController.inc +++ b/controllers/UsersController.inc @@ -28,7 +28,7 @@ 'index' => array('admin', 'moderator'), 'user' => array('admin', 'moderator', 'user'), 'create' => array('admin', 'moderator'), - 'edit' => array('admin', 'moderator'), + 'edit' => array('admin', 'moderator', 'user'), 'delete' => array('admin') ); /** @@ -245,21 +245,61 @@ */ public function create() { + // Values + $username = ''; + $prename = ''; + $surname = ''; + $email = ''; + $fields = array('username', 'prename', 'surname', 'email', 'password'); + $validation = array(); + + // Create new user if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('create'))) { - // Create new user - $userId = $this->Users->createUser( - $this->request->getPostParam('username'), - $this->request->getPostParam('prename'), - $this->request->getPostParam('surname'), - $this->request->getPostParam('email'), - $this->request->getPostParam('password') - ); + // Get params and validate them + $validation = $this->Validation->validateParams($this->request->getPostParams(), $fields); + $username = $this->request->getPostParam('username'); + if($this->Users->usernameExists($username)) { + $validation = $this->Validation->addValidationResult($validation, 'username', 'exist', true); + } + $prename = $this->request->getPostParam('prename'); + $surname = $this->request->getPostParam('surname'); + $email = $this->request->getPostParam('email'); + if($this->Users->emailExists($email)) { + $validation = $this->Validation->addValidationResult($validation, 'email', 'exist', true); + } - // Redirect to user - $user = $this->Users->getUserById($userId); - $this->redirect($this->linker->link(array($user['url']), 1)); + // Create + if($validation === true) + { + $userId = $this->Users->createUser( + $this->request->getPostParam('username'), + $this->request->getPostParam('prename'), + $this->request->getPostParam('surname'), + $this->request->getPostParam('email'), + $this->request->getPostParam('password') + ); + + // Redirect to user + $user = $this->Users->getUserById($userId); + $this->redirect($this->linker->link(array($user['url']), 1)); + } } + + // Get validation settings + $validationSettings = array(); + foreach($fields as &$field) { + $validationSettings[$field] = \nre\configs\AppConfig::$validation[$field]; + } + + + // Pass data to view + $this->set('username', $username); + $this->set('prename', $prename); + $this->set('surname', $surname); + $this->set('email', $email); + $this->set('validation', $validation); + $this->set('validationSettings', $validationSettings); } @@ -276,32 +316,77 @@ // User $user = $this->Users->getUserByUrl($userUrl); - // Check request method - if($this->request->getRequestMethod() == 'POST') + // Check permissions + if(count(array_intersect(array('admin','moderator'), \hhu\z\controllers\IntermediateController::$user['roles'])) == 0 && $user['id'] != \hhu\z\controllers\IntermediateController::$user['id']) { + throw new \nre\exceptions\AccessDeniedException(); + } + + // Values + $username = $user['username']; + $prename = $user['prename']; + $surname = $user['surname']; + $email = $user['email']; + $fields = array('username', 'prename', 'surname', 'email'); + $validation = array(); + + // Edit user + if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('save'))) { + // Get params and validate them + $validation = $this->Validation->validateParams($this->request->getPostParams(), $fields); + $username = $this->request->getPostParam('username'); + if($this->Users->usernameExists($username, $user['id'])) { + $validation = $this->Validation->addValidationResult($validation, 'username', 'exist', true); + } + if(!empty($this->request->getPostParam('password'))) { + $validation = $this->Validation->addValidationResults($validation, + 'password', + $this->Validation->validateParam( + $this->request->getPostParams(), + 'password' + ) + ); + } + $prename = $this->request->getPostParam('prename'); + $surname = $this->request->getPostParam('surname'); + $email = $this->request->getPostParam('email'); + if($this->Users->emailExists($email, $user['id'])) { + $validation = $this->Validation->addValidationResult($validation, 'email', 'exist', true); + } + // Save changes - if(!is_null($this->request->getPostParam('save'))) + if($validation === true) { // Edit user $this->Users->editUser( $user['id'], - $this->request->getPostParam('username'), + (count(array_intersect(array('admin','moderator'),\hhu\z\controllers\IntermediateController::$user['roles'])) > 0) ? $this->request->getPostParam('username') : $user['username'], $this->request->getPostParam('prename'), $this->request->getPostParam('surname'), $this->request->getPostParam('email'), $this->request->getPostParam('password') ); + + // Redirect to entry $user = $this->Users->getUserById($user['id']); + $this->redirect($this->linker->link(array('user', $user['url']), 1)); } - - - // Redirect to entry - $this->redirect($this->linker->link(array($user['url']), 1)); + } + + // Get validation settings + $validationSettings = array(); + foreach($fields as &$field) { + $validationSettings[$field] = \nre\configs\AppConfig::$validation[$field]; } // Pass data to view - $this->set('user', $user); + $this->set('username', $username); + $this->set('prename', $prename); + $this->set('surname', $surname); + $this->set('email', $email); + $this->set('validation', $validation); + $this->set('validationSettings', $validationSettings); } diff --git a/models/UsersModel.inc b/models/UsersModel.inc index d2a69003..2181586a 100644 --- a/models/UsersModel.inc +++ b/models/UsersModel.inc @@ -149,24 +149,22 @@ * Check if an username already exists. * * @param string $username Username to check + * @param int $userId Do not check this ID (for editing) * @return boolean Whether username exists or not */ - public function usernameExists($username) + public function usernameExists($username, $userId=null) { $data = $this->db->query( - 'SELECT count(id) AS c '. + 'SELECT id '. 'FROM users '. 'WHERE username = ? OR url = ?', 'ss', $username, \nre\core\Linker::createLinkParam($username) ); - if(!empty($data)) { - return ($data[0]['c'] > 0); - } - return false; + return (!empty($data) && (is_null($userId) || $userId != $data[0]['id'])); } @@ -174,23 +172,21 @@ * Check if an e‑mail address already exists. * * @param string $email E‑mail address to check + * @param int $userId Do not check this ID (for editing) * @return boolean Whether e‑mail address exists or not */ - public function emailExists($email) + public function emailExists($email, $userId=null) { $data = $this->db->query( - 'SELECT count(id) AS c '. + 'SELECT id '. 'FROM users '. 'WHERE email = ?', 's', $email ); - if(!empty($data)) { - return ($data[0]['c'] > 0); - } - return false; + return (!empty($data) && (is_null($userId) || $userId != $data[0]['id'])); } diff --git a/views/html/users/create.tpl b/views/html/users/create.tpl index ba7baed6..590dff3b 100644 --- a/views/html/users/create.tpl +++ b/views/html/users/create.tpl @@ -1,21 +1,97 @@
-

-

+ -
+

+ + + +
+
-
+ />
-
+ />
-
+ />
-
+ />
-
+ />
+
diff --git a/views/html/users/delete.tpl b/views/html/users/delete.tpl index af0631b7..ca28f4fc 100644 --- a/views/html/users/delete.tpl +++ b/views/html/users/delete.tpl @@ -1,9 +1,11 @@
-

-

+ +

diff --git a/views/html/users/edit.tpl b/views/html/users/edit.tpl index 6d25f014..7a6bf041 100644 --- a/views/html/users/edit.tpl +++ b/views/html/users/edit.tpl @@ -1,20 +1,104 @@
-

-

+ +

+ + +
-
-
+ 0) : ?> + /> + + + +
+ + />
-
+ />
-
+ />
-
+ />
diff --git a/views/html/users/login.tpl b/views/html/users/login.tpl index 7b88b50b..71b5fa4c 100644 --- a/views/html/users/login.tpl +++ b/views/html/users/login.tpl @@ -7,7 +7,7 @@

.

-
+

diff --git a/views/html/users/register.tpl b/views/html/users/register.tpl index 1b2b1db0..48fe6f47 100644 --- a/views/html/users/register.tpl +++ b/views/html/users/register.tpl @@ -1,9 +1,11 @@
-

+ -

+

- +
/>
diff --git a/views/html/users/user.tpl b/views/html/users/user.tpl index d3b8b5ad..31c4907b 100644 --- a/views/html/users/user.tpl +++ b/views/html/users/user.tpl @@ -1,14 +1,23 @@
-

- 0) : ?> + + +

- -

format(new \DateTime($user['created'])))?>
: