diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index 541a51e2..eade2271 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -67,6 +67,38 @@ ); + /** + * Validation settings for user input + * + * @static + * @var array + */ + public static $validation = array( + 'username' => array( + 'minlength' => 5, + 'maxlength' => 32, + 'regex' => '/^\w*$/' + ), + 'email' => array( + 'regex' => '/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU' + ), + 'prename' => array( + 'minlength' => 2, + 'maxlength' => 128, + 'regex' => '/^\S*$/' + ), + 'surname' => array( + 'minlength' => 2, + 'maxlength' => 128, + 'regex' => '/^\S*$/' + ), + 'password' => array( + 'minlength' => 5, + 'maxlength' => 64 + ) + ); + + /** * Routes * @@ -76,7 +108,7 @@ public static $routes = array( array('css/?(.*)', 'css/$1?layout=stylesheet', true), array('users/([^/]+)/(edit|delete)', 'users/$2/$1', true), - array('users/(?!(index|login|logout|create|edit|delete))', 'users/user/$1', true), + array('users/(?!(index|login|register|logout|create|edit|delete))', 'users/user/$1', true), array('seminaries/([^/]+)/(edit|delete)', 'seminaries/$2/$1', true), array('seminaries/(?!(index|create|edit|delete))', 'seminaries/seminary/$1', true), /*// z/ ⇒ z/seminaries/seminary/ diff --git a/controllers/UsersController.inc b/controllers/UsersController.inc index f6203cc6..04d1f016 100644 --- a/controllers/UsersController.inc +++ b/controllers/UsersController.inc @@ -37,6 +37,12 @@ * @var array */ public $models = array('users', 'characters'); + /** + * Required components + * + * @var array + */ + public $components = array('validation'); @@ -102,12 +108,12 @@ $username, $this->request->getPostParam('password') ); - + if(!is_null($userId)) { $this->Auth->setUserId($userId); $user = $this->Users->getUserById($userId); - + $this->redirect($this->linker->link(array($user['url']), 1)); } } @@ -119,6 +125,68 @@ } + /** + * Action: register. + * + * Register a new user. + */ + public function register() + { + $username = ''; + $prename = ''; + $surname = ''; + $email = ''; + + $fields = array('username', 'prename', 'surname', 'email', 'password'); + $validation = array(); + + // Register a new user + if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('register'))) + { + // Get params and validate them + $validation = $this->Validation->validateParams($this->request->getPostParams(), $fields); + $username = $this->request->getPostParam('username'); + $prename = $this->request->getPostParam('prename'); + $surname = $this->request->getPostParam('surname'); + $email = $this->request->getPostParam('email'); + + // Register + if($validation === true) + { + $userId = $this->Users->createUser( + $username, + $prename, + $surname, + $email, + $this->request->getPostParam('password') + ); + + // Login + $this->Auth->setUserId($userId); + $user = $this->Users->getUserById($userId); + + // Redirect to user page + $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); + } + + /** * Action: logout. * @@ -146,6 +214,8 @@ // 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') ); @@ -180,6 +250,8 @@ $this->Users->editUser( $user['id'], $this->request->getPostParam('username'), + $this->request->getPostParam('prename'), + $this->request->getPostParam('surname'), $this->request->getPostParam('email'), $this->request->getPostParam('password') ); diff --git a/models/UsersModel.inc b/models/UsersModel.inc index d42db7c7..88a7ed2d 100644 --- a/models/UsersModel.inc +++ b/models/UsersModel.inc @@ -132,22 +132,48 @@ * @param string $password Password of the user to create * @return int ID of the newly created user */ - public function createUser($username, $email, $password) + public function createUser($username, $prename, $surname, $email, $password) { - $this->db->query( - 'INSERT INTO users '. - '(username, url, email, password) '. - 'VALUES '. - '(?, ?, ?, ?)', - 'ssss', - $username, - \nre\core\Linker::createLinkParam($username), - $email, - $this->hash($password) - ); + $userId = null; + $this->db->setAutocommit(false); + try { + // Create user + $this->db->query( + 'INSERT INTO users '. + '(username, url, surname, prename, email, password) '. + 'VALUES '. + '(?, ?, ?, ?, ?, ?)', + 'ssssss', + $username, + \nre\core\Linker::createLinkParam($username), + $surname, + $prename, + $email, + $this->hash($password) + ); + $userId = $this->db->getInsertId(); + + // Add role “user” + $this->db->query( + 'INSERT INTO users_userroles '. + '(user_id, userrole_id) '. + 'SELECT ?, userroles.id '. + 'FROM userroles '. + 'WHERE userroles.name = ?', + 'is', + $userId, + 'user' + ); + } + catch(Exception $e) { + $this->db->rollback(); + $this->db->setAutocommit(true); + throw $e; + } + $this->db->setAutocommit(true); - return $this->db->getInsertId(); + return $userId; } @@ -160,17 +186,20 @@ * @param string $email Changed e‑mail-address of user * @param string $password Changed plaintext password of user */ - public function editUser($userId, $username, $email, $password) + public function editUser($userId, $username, $prename, $surname, $email, $password) { + $this->db->setAutocommit(false); try { // Update user data $this->db->query( 'UPDATE users '. - 'SET username = ?, url = ?, email = ? '. + 'SET username = ?, url = ?, prename = ?, surname = ?, email = ? '. 'WHERE id = ?', - 'sssi', + 'sssssi', $username, \nre\core\Linker::createLinkParam($username), + $prename, + $surname, $email, $userId ); diff --git a/views/html/users/create.tpl b/views/html/users/create.tpl index 8536804d..d4f38bb7 100644 --- a/views/html/users/create.tpl +++ b/views/html/users/create.tpl @@ -5,8 +5,12 @@

- -
+ +
+ +
+ +

diff --git a/views/html/users/edit.tpl b/views/html/users/edit.tpl index ef47268c..56ba5d7b 100644 --- a/views/html/users/edit.tpl +++ b/views/html/users/edit.tpl @@ -5,8 +5,12 @@

- -
+ +
+ +
+ +

diff --git a/views/html/users/login.tpl b/views/html/users/login.tpl index 36c13da4..932b4c20 100644 --- a/views/html/users/login.tpl +++ b/views/html/users/login.tpl @@ -1,12 +1,15 @@

-

+

+ +

.

+
- -
- -
+ +
+ +
diff --git a/views/html/users/register.tpl b/views/html/users/register.tpl new file mode 100644 index 00000000..5ba5671e --- /dev/null +++ b/views/html/users/register.tpl @@ -0,0 +1,88 @@ +

+ +

+ + + +
+
+ + />
+ + />
+ + />
+ + />
+ + />
+
+ +