implement user registration and improve user handling
This commit is contained in:
parent
5dea059429
commit
f3602d7a9e
7 changed files with 260 additions and 28 deletions
|
|
@ -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
|
* Routes
|
||||||
*
|
*
|
||||||
|
|
@ -76,7 +108,7 @@
|
||||||
public static $routes = array(
|
public static $routes = array(
|
||||||
array('css/?(.*)', 'css/$1?layout=stylesheet', true),
|
array('css/?(.*)', 'css/$1?layout=stylesheet', true),
|
||||||
array('users/([^/]+)/(edit|delete)', 'users/$2/$1', 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/([^/]+)/(edit|delete)', 'seminaries/$2/$1', true),
|
||||||
array('seminaries/(?!(index|create|edit|delete))', 'seminaries/seminary/$1', true),
|
array('seminaries/(?!(index|create|edit|delete))', 'seminaries/seminary/$1', true),
|
||||||
/*// z/<Seminary> ⇒ z/seminaries/seminary/<Seminary>
|
/*// z/<Seminary> ⇒ z/seminaries/seminary/<Seminary>
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,12 @@
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $models = array('users', 'characters');
|
public $models = array('users', 'characters');
|
||||||
|
/**
|
||||||
|
* Required components
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $components = array('validation');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -102,12 +108,12 @@
|
||||||
$username,
|
$username,
|
||||||
$this->request->getPostParam('password')
|
$this->request->getPostParam('password')
|
||||||
);
|
);
|
||||||
|
|
||||||
if(!is_null($userId))
|
if(!is_null($userId))
|
||||||
{
|
{
|
||||||
$this->Auth->setUserId($userId);
|
$this->Auth->setUserId($userId);
|
||||||
$user = $this->Users->getUserById($userId);
|
$user = $this->Users->getUserById($userId);
|
||||||
|
|
||||||
$this->redirect($this->linker->link(array($user['url']), 1));
|
$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.
|
* Action: logout.
|
||||||
*
|
*
|
||||||
|
|
@ -146,6 +214,8 @@
|
||||||
// Create new user
|
// Create new user
|
||||||
$userId = $this->Users->createUser(
|
$userId = $this->Users->createUser(
|
||||||
$this->request->getPostParam('username'),
|
$this->request->getPostParam('username'),
|
||||||
|
$this->request->getPostParam('prename'),
|
||||||
|
$this->request->getPostParam('surname'),
|
||||||
$this->request->getPostParam('email'),
|
$this->request->getPostParam('email'),
|
||||||
$this->request->getPostParam('password')
|
$this->request->getPostParam('password')
|
||||||
);
|
);
|
||||||
|
|
@ -180,6 +250,8 @@
|
||||||
$this->Users->editUser(
|
$this->Users->editUser(
|
||||||
$user['id'],
|
$user['id'],
|
||||||
$this->request->getPostParam('username'),
|
$this->request->getPostParam('username'),
|
||||||
|
$this->request->getPostParam('prename'),
|
||||||
|
$this->request->getPostParam('surname'),
|
||||||
$this->request->getPostParam('email'),
|
$this->request->getPostParam('email'),
|
||||||
$this->request->getPostParam('password')
|
$this->request->getPostParam('password')
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -132,22 +132,48 @@
|
||||||
* @param string $password Password of the user to create
|
* @param string $password Password of the user to create
|
||||||
* @return int ID of the newly created user
|
* @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(
|
$userId = null;
|
||||||
'INSERT INTO users '.
|
$this->db->setAutocommit(false);
|
||||||
'(username, url, email, password) '.
|
try {
|
||||||
'VALUES '.
|
// Create user
|
||||||
'(?, ?, ?, ?)',
|
$this->db->query(
|
||||||
'ssss',
|
'INSERT INTO users '.
|
||||||
$username,
|
'(username, url, surname, prename, email, password) '.
|
||||||
\nre\core\Linker::createLinkParam($username),
|
'VALUES '.
|
||||||
$email,
|
'(?, ?, ?, ?, ?, ?)',
|
||||||
$this->hash($password)
|
'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 $email Changed e‑mail-address of user
|
||||||
* @param string $password Changed plaintext password 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 {
|
try {
|
||||||
// Update user data
|
// Update user data
|
||||||
$this->db->query(
|
$this->db->query(
|
||||||
'UPDATE users '.
|
'UPDATE users '.
|
||||||
'SET username = ?, url = ?, email = ? '.
|
'SET username = ?, url = ?, prename = ?, surname = ?, email = ? '.
|
||||||
'WHERE id = ?',
|
'WHERE id = ?',
|
||||||
'sssi',
|
'sssssi',
|
||||||
$username,
|
$username,
|
||||||
\nre\core\Linker::createLinkParam($username),
|
\nre\core\Linker::createLinkParam($username),
|
||||||
|
$prename,
|
||||||
|
$surname,
|
||||||
$email,
|
$email,
|
||||||
$userId
|
$userId
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,12 @@
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<label for="username"><?=_('Username')?>:</label>
|
<label for="username"><?=_('Username')?>:</label>
|
||||||
<input type="text" name="username" placeholder="<?=_('Username')?>" /><br />
|
<input type="text" name="username" placeholder="<?=_('Username')?>" /><br />
|
||||||
<label for="email"><?=_('E‑Mail-Address')?>:</label>
|
<label for="prename"><?=_('Prename')?>:</label>
|
||||||
<input type="email" name="email" placeholder="<?=_('E‑Mail-Address')?>" /><br />
|
<input name="prename" type="text" placeholder="<?=_('Prename')?>" /><br />
|
||||||
|
<label for="surname"><?=_('Surname')?>:</label>
|
||||||
|
<input name="surname" type="text" placeholder="<?=_('Surname')?>" /><br />
|
||||||
|
<label for="email"><?=_('E‑mail address')?>:</label>
|
||||||
|
<input type="email" name="email" placeholder="<?=_('E‑mail address')?>" /><br />
|
||||||
<label for="password"><?=_('Password')?>:</label>
|
<label for="password"><?=_('Password')?>:</label>
|
||||||
<input type="password" name="password" placeholder="<?=_('Password')?>" /><br />
|
<input type="password" name="password" placeholder="<?=_('Password')?>" /><br />
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,12 @@
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<label for="username"><?=_('Username')?>:</label>
|
<label for="username"><?=_('Username')?>:</label>
|
||||||
<input type="text" name="username" placeholder="<?=_('Username')?>" value="<?=$user['username']?>" /><br />
|
<input type="text" name="username" placeholder="<?=_('Username')?>" value="<?=$user['username']?>" /><br />
|
||||||
<label for="email"><?=_('E‑Mail-Address')?>:</label>
|
<label for="prename"><?=_('Prename')?>:</label>
|
||||||
<input type="email" name="email" placeholder="<?=_('E‑Mail-Address')?>" value="<?=$user['email']?>" /><br />
|
<input name="prename" type="text" placeholder="<?=_('Prename')?>" value="<?=$user['prename']?>" /><br />
|
||||||
|
<label for="surname"><?=_('Surname')?>:</label>
|
||||||
|
<input name="surname" type="text" placeholder="<?=_('Surname')?>" value="<?=$user['surname']?>" /><br />
|
||||||
|
<label for="email"><?=_('E‑mail address')?>:</label>
|
||||||
|
<input type="email" name="email" placeholder="<?=_('E‑mail address')?>" value="<?=$user['email']?>" /><br />
|
||||||
<label for="password"><?=_('Password')?>:</label>
|
<label for="password"><?=_('Password')?>:</label>
|
||||||
<input type="password" name="password" placeholder="<?=_('Password')?>" /><br />
|
<input type="password" name="password" placeholder="<?=_('Password')?>" /><br />
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
<h1><?=_('Users')?></h1>
|
<h1><?=_('Users')?></h1>
|
||||||
<h2><?=_('Login')?></h2>
|
|
||||||
|
|
||||||
|
<h2><?=_('Login')?></h2>
|
||||||
|
<?php if($failed) : ?>
|
||||||
|
<p class="invalid"><?=_('Login failed')?>.</p>
|
||||||
|
<?php endif ?>
|
||||||
<form method="post" action="<?=$linker->link(array(), 2)?>">
|
<form method="post" action="<?=$linker->link(array(), 2)?>">
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<label for="username"><?=_('Username')?></label>
|
<label for="username"><?=_('Username')?>:</label>
|
||||||
<input name="username" type="text" placeholder="<?=_('Username')?>" value="<?=$username?>" /><br />
|
<input name="username" type="text" placeholder="<?=_('Username')?>" value="<?=$username?>" required="required" /><br />
|
||||||
<label for="password"><?=_('Password')?></label>
|
<label for="password"><?=_('Password')?>:</label>
|
||||||
<input name="password" type="password" placeholder="<?=_('Password')?>" /><br />
|
<input name="password" type="password" placeholder="<?=_('Password')?>" required="required" /><br />
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<input type="submit" name="login" value="<?=_('Login')?>" />
|
<input type="submit" name="login" value="<?=_('Login')?>" />
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
88
views/html/users/register.tpl
Normal file
88
views/html/users/register.tpl
Normal file
|
|
@ -0,0 +1,88 @@
|
||||||
|
<h1><?=_('Users')?></h1>
|
||||||
|
|
||||||
|
<h2><?=_('Registration')?></h2>
|
||||||
|
<?php if(!empty($validation)) : ?>
|
||||||
|
<ul>
|
||||||
|
<?php foreach($validation as $field => &$settings) : ?>
|
||||||
|
<?php if($settings !== true) : ?>
|
||||||
|
<li>
|
||||||
|
<ul>
|
||||||
|
<?php foreach($settings as $setting => $value) : ?>
|
||||||
|
<li>
|
||||||
|
<?php switch($field) {
|
||||||
|
case 'username':
|
||||||
|
switch($setting) {
|
||||||
|
case 'minlength': printf(_('Username is too short (min. %d chars)'), $value);
|
||||||
|
break;
|
||||||
|
case 'maxlength': printf(_('Username is too long (max. %d chars)'), $value);
|
||||||
|
break;
|
||||||
|
case 'regex': echo _('Username contains illegal characters');
|
||||||
|
break;
|
||||||
|
default: echo _('Username invalid');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'prename':
|
||||||
|
switch($setting) {
|
||||||
|
case 'minlength': printf(_('Prename is too short (min. %d chars)'), $value);
|
||||||
|
break;
|
||||||
|
case 'maxlength': printf(_('Prename is too long (max. %d chars)'), $value);
|
||||||
|
break;
|
||||||
|
case 'regex': printf(_('Prename contains illegal characters'));
|
||||||
|
break;
|
||||||
|
default: echo _('Prename invalid');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'surname':
|
||||||
|
switch($setting) {
|
||||||
|
case 'minlength': printf(_('Surname is too short (min. %d chars)'), $value);
|
||||||
|
break;
|
||||||
|
case 'maxlength': printf(_('Surname is too long (max. %d chars)'), $value);
|
||||||
|
break;
|
||||||
|
case 'regex': printf(_('Surname contains illegal characters'));
|
||||||
|
break;
|
||||||
|
default: echo _('Surname invalid');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'email':
|
||||||
|
switch($setting) {
|
||||||
|
case 'regex': echo _('E‑mail address invalid');
|
||||||
|
break;
|
||||||
|
default: echo _('E‑mail address invalid');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'password':
|
||||||
|
switch($setting) {
|
||||||
|
case 'minlength': printf(_('Password is too short (min. %d chars)'), $value);
|
||||||
|
break;
|
||||||
|
case 'maxlength': printf(_('Password is too long (max. %d chars)'), $value);
|
||||||
|
break;
|
||||||
|
default: echo _('Password invalid');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
echo $exception->getMessage();
|
||||||
|
break;
|
||||||
|
} ?>
|
||||||
|
</li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<?php endif ?>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
<?php endif ?>
|
||||||
|
<form method="post" action="<?=$linker->link(array(), 2)?>">
|
||||||
|
<fieldset>
|
||||||
|
<label for="username"><?=_('Username')?>:</label>
|
||||||
|
<input name="username" type="text" placeholder="<?=_('Username')?>" title="<?=_('Username')?>" required="required" maxlength="<?=$validationSettings['username']['maxlength']?>" value="<?=$username?>" <?=(array_key_exists('username', $validation)) ? 'class="invalid"' : null?> /><br />
|
||||||
|
<label for="prename"><?=_('Prename')?>:</label>
|
||||||
|
<input name="prename" type="text" placeholder="<?=_('Prename')?>" title="<?=_('Prename')?>" required="required" maxlength="<?=$validationSettings['prename']['maxlength']?>" value="<?=$prename?>" <?=(array_key_exists('prename', $validation)) ? 'class="invalid"' : null?> /><br />
|
||||||
|
<label for="surname"><?=_('Surname')?>:</label>
|
||||||
|
<input name="surname" type="text" placeholder="<?=_('Surname')?>" title="<?=_('Surname')?>" required="required" maxlength="<?=$validationSettings['surname']['maxlength']?>" value="<?=$surname?>" <?=(array_key_exists('surname', $validation)) ? 'class="invalid"' : null?> /><br />
|
||||||
|
<label for="email"><?=_('E‑mail address')?>:</label>
|
||||||
|
<input name="email" type="email" placeholder="<?=_('E‑mail address')?>" title="<?=_('E‑mail address')?>" required="required" value="<?=$email?>" <?=(array_key_exists('email', $validation)) ? 'class="invalid"' : null?> /><br />
|
||||||
|
<label for="password"><?=_('Password')?>:</label>
|
||||||
|
<input name="password" type="password" placeholder="<?=_('Password')?>" title="<?=_('Password')?>" required="required" maxlength="<?=$validationSettings['password']['maxlength']?>" <?=(array_key_exists('password', $validation)) ? 'class="invalid"' : null?> /><br />
|
||||||
|
</fieldset>
|
||||||
|
<input type="submit" name="register" value="<?=_('Register')?>" />
|
||||||
|
</form>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue