implement user registration and improve user handling

This commit is contained in:
coderkun 2014-04-05 13:29:49 +02:00
commit f3602d7a9e
7 changed files with 260 additions and 28 deletions

View file

@ -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/<Seminary> z/seminaries/seminary/<Seminary>

View file

@ -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')
);

View file

@ -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 email-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
);

View file

@ -5,8 +5,12 @@
<fieldset>
<label for="username"><?=_('Username')?>:</label>
<input type="text" name="username" placeholder="<?=_('Username')?>" /><br />
<label for="email"><?=_('EMail-Address')?>:</label>
<input type="email" name="email" placeholder="<?=_('EMail-Address')?>" /><br />
<label for="prename"><?=_('Prename')?>:</label>
<input name="prename" type="text" placeholder="<?=_('Prename')?>" /><br />
<label for="surname"><?=_('Surname')?>:</label>
<input name="surname" type="text" placeholder="<?=_('Surname')?>" /><br />
<label for="email"><?=_('Email address')?>:</label>
<input type="email" name="email" placeholder="<?=_('Email address')?>" /><br />
<label for="password"><?=_('Password')?>:</label>
<input type="password" name="password" placeholder="<?=_('Password')?>" /><br />
</fieldset>

View file

@ -5,8 +5,12 @@
<fieldset>
<label for="username"><?=_('Username')?>:</label>
<input type="text" name="username" placeholder="<?=_('Username')?>" value="<?=$user['username']?>" /><br />
<label for="email"><?=_('EMail-Address')?>:</label>
<input type="email" name="email" placeholder="<?=_('EMail-Address')?>" value="<?=$user['email']?>" /><br />
<label for="prename"><?=_('Prename')?>:</label>
<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"><?=_('Email address')?>:</label>
<input type="email" name="email" placeholder="<?=_('Email address')?>" value="<?=$user['email']?>" /><br />
<label for="password"><?=_('Password')?>:</label>
<input type="password" name="password" placeholder="<?=_('Password')?>" /><br />
</fieldset>

View file

@ -1,12 +1,15 @@
<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)?>">
<fieldset>
<label for="username"><?=_('Username')?></label>
<input name="username" type="text" placeholder="<?=_('Username')?>" value="<?=$username?>" /><br />
<label for="password"><?=_('Password')?></label>
<input name="password" type="password" placeholder="<?=_('Password')?>" /><br />
<label for="username"><?=_('Username')?>:</label>
<input name="username" type="text" placeholder="<?=_('Username')?>" value="<?=$username?>" required="required" /><br />
<label for="password"><?=_('Password')?>:</label>
<input name="password" type="password" placeholder="<?=_('Password')?>" required="required" /><br />
</fieldset>
<input type="submit" name="login" value="<?=_('Login')?>" />
</form>

View 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 _('Email address invalid');
break;
default: echo _('Email 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"><?=_('Email address')?>:</label>
<input name="email" type="email" placeholder="<?=_('Email address')?>" title="<?=_('Email 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>