implement managing of users
This commit is contained in:
parent
0cba4afefb
commit
c856cb7c54
5 changed files with 263 additions and 3 deletions
|
|
@ -170,8 +170,8 @@
|
|||
* @var array
|
||||
*/
|
||||
public static $routes = array(
|
||||
array('^users/([^/]+)/(edit|delete)/?$', 'users/$2/$1', true),
|
||||
array('^users/(?!(index|login|register|logout|create|edit|delete))/?', 'users/user/$1', true),
|
||||
array('^users/([^/]+)/(edit|delete)/?$', 'users/$2/$1', true),
|
||||
array('^users/(?!(index|login|register|logout|manage|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),
|
||||
array('^questgroups/([^/]+)/(create)/?$', 'questgroups/$2/$1', true),
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public $models = array('users', 'characters', 'avatars', 'media', 'characterroles');
|
||||
public $models = array('users', 'userroles', 'characters', 'characterroles', 'avatars', 'media');
|
||||
/**
|
||||
* Required components
|
||||
*
|
||||
|
|
@ -238,6 +238,101 @@
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Action: manage.
|
||||
*
|
||||
* Manage users.
|
||||
*/
|
||||
public function manage()
|
||||
{
|
||||
$selectedUsers = array();
|
||||
global $sortorder;
|
||||
|
||||
if($this->request->getRequestMethod() == 'POST')
|
||||
{
|
||||
// Set sortorder
|
||||
$sortorder = $this->request->getPostParam('sortorder');
|
||||
|
||||
// Do action
|
||||
$selectedUsers = $this->request->getPostParam('users');
|
||||
if(!is_array($selectedUsers)) {
|
||||
$selectedUsers = array();
|
||||
}
|
||||
if(!is_null($this->request->getPostParam('actions')) && count($this->request->getPostParam('actions')) > 0 && !is_null($this->request->getPostParam('users')) && count($this->request->getPostParam('users')) > 0)
|
||||
{
|
||||
$actions = $this->request->getPostParam('actions');
|
||||
$action = array_keys($actions)[0];
|
||||
|
||||
switch($action)
|
||||
{
|
||||
// Add/remove role to/from Characters
|
||||
case 'addrole':
|
||||
case 'removerole':
|
||||
// Determine role and check permissions
|
||||
$role = null;
|
||||
switch($actions[$action])
|
||||
{
|
||||
case _('Admin'):
|
||||
if(!in_array('admin', \hhu\z\controllers\IntermediateController::$user['roles'])) {
|
||||
throw new \nre\exceptions\AccessDeniedException();
|
||||
}
|
||||
$role = 'admin';
|
||||
break;
|
||||
case _('Moderator'):
|
||||
if(!in_array('admin', \hhu\z\controllers\IntermediateController::$user['roles'])) {
|
||||
throw new \nre\exceptions\AccessDeniedException();
|
||||
}
|
||||
$role = 'moderator';
|
||||
break;
|
||||
case _('User'):
|
||||
if(count(array_intersect(array('admin', 'moderator'), \hhu\z\controllers\IntermediateController::$user['roles'])) <= 0) {
|
||||
throw new \nre\exceptions\AccessDeniedException();
|
||||
}
|
||||
$role = 'user';
|
||||
break;
|
||||
}
|
||||
|
||||
// Add role
|
||||
if($action == 'addrole') {
|
||||
foreach($selectedUsers as &$userId) {
|
||||
$this->Userroles->addUserroleToUser($userId, $role);
|
||||
}
|
||||
}
|
||||
// Remove role
|
||||
else {
|
||||
foreach($selectedUsers as &$userId) {
|
||||
$this->Userroles->removeUserroleFromUser($userId, $role);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get registered users
|
||||
$users = $this->Users->getUsers();
|
||||
foreach($users as &$user) {
|
||||
$user['roles'] = array_map(function($r) { return $r['name']; }, $this->Userroles->getUserrolesForUserById($user['id']));
|
||||
}
|
||||
|
||||
// Sort users
|
||||
$sortorder = (!is_null($sortorder)) ? $sortorder : 'username';
|
||||
$sortMethod = 'sortUsersBy'.ucfirst(strtolower($sortorder));
|
||||
if(method_exists($this, $sortMethod)) {
|
||||
usort($users, array($this, $sortMethod));
|
||||
}
|
||||
else {
|
||||
throw new \nre\exceptions\ParamsNotValidException($sortorder);
|
||||
}
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('users', $users);
|
||||
$this->set('selectedUsers', $selectedUsers);
|
||||
$this->set('sortorder', $sortorder);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Action: create.
|
||||
*
|
||||
|
|
@ -451,6 +546,76 @@
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compare two users by their username.
|
||||
*
|
||||
* @param array $a User a
|
||||
* @param array $b User b
|
||||
* @return int Result of comparison
|
||||
*/
|
||||
private function sortUsersByUsername($a, $b)
|
||||
{
|
||||
if($a['username'] == $b['username']) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
return ($a['username'] < $b['username']) ? -1 : 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compare two users by their userroles.
|
||||
*
|
||||
* @param array $a Character a
|
||||
* @param array $b Character b
|
||||
* @return int Result of comparison
|
||||
*/
|
||||
private function sortUsersByRole($a, $b)
|
||||
{
|
||||
if(in_array('admin', $a['roles']))
|
||||
{
|
||||
if(in_array('admin', $b['roles'])) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if(in_array('moderator', $a['roles']))
|
||||
{
|
||||
if(in_array('admin', $b['roles'])) {
|
||||
return 1;
|
||||
}
|
||||
if(in_array('moderator', $b['roles'])) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if(in_array('user', $a['roles']))
|
||||
{
|
||||
if(in_array('admin', $b['roles']) || in_array('moderator', $b['roles'])) {
|
||||
return 1;
|
||||
}
|
||||
if(in_array('user', $b['roles'])) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if(in_array('guest', $a['roles']))
|
||||
{
|
||||
if(in_array('admin', $b['roles']) || in_array('moderator', $b['roles']) || in_array('user', $b['roles'])) {
|
||||
return 1;
|
||||
}
|
||||
if(in_array('guest', $b['roles'])) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -72,6 +72,49 @@
|
|||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a role to a user.
|
||||
*
|
||||
* @param int $userId ID of user to add role to
|
||||
* @param string $userrole Role to add
|
||||
*/
|
||||
public function addUserroleToUser($userId, $userrole)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT IGNORE INTO users_userroles '.
|
||||
'(user_id, userrole_id) '.
|
||||
'SELECT ?, id '.
|
||||
'FROM userroles '.
|
||||
'WHERE name = ?',
|
||||
'is',
|
||||
$userId,
|
||||
$userrole
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove a role from a user.
|
||||
*
|
||||
* @param int $userId ID of user to remove role from
|
||||
* @param string $userrole Role to remove
|
||||
*/
|
||||
public function removeUserroleFromUser($userId, $userrole)
|
||||
{
|
||||
$this->db->query(
|
||||
'DELETE FROM users_userroles '.
|
||||
'WHERE user_id = ? AND userrole_id = ('.
|
||||
'SELECT id '.
|
||||
'FROM userroles '.
|
||||
'WHERE name = ?'.
|
||||
')',
|
||||
'is',
|
||||
$userId,
|
||||
$userrole
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
<h1><?=_('Users')?></h1>
|
||||
<nav class="admin">
|
||||
<li><a href="<?=$linker->link('create', 1)?>"><?=_('Create new user')?></a></li>
|
||||
<li><a href="<?=$linker->link('manage',1)?>"><?=_('Manage')?></a></li>
|
||||
</nav>
|
||||
<ol class="cglist">
|
||||
<?php foreach($users as &$user) : ?>
|
||||
|
|
|
|||
51
views/html/users/manage.tpl
Normal file
51
views/html/users/manage.tpl
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
<div class="moodpic">
|
||||
<img src="<?=$linker->link(array('grafics','questlab.jpg'))?>" />
|
||||
</div>
|
||||
<ul class="breadcrumbs">
|
||||
<li><a href="<?=$linker->link('index',1)?>"><?=_('Users')?></a></li>
|
||||
</ul>
|
||||
|
||||
<h1><?=_('Manage')?></h1>
|
||||
|
||||
<form method="post">
|
||||
<fieldset class="filter">
|
||||
<p><small>Sortierung:</small></p>
|
||||
<select name="sortorder" onchange="this.form.submit();">
|
||||
<option value="username" <?php if($sortorder == 'username') : ?>selected="selected"<?php endif ?>><?=_('Username')?></option>
|
||||
<option value="role" <?php if($sortorder == 'role') : ?>selected="selected"<?php endif ?>><?=_('Role')?></option>
|
||||
</select>
|
||||
<noscript><input type="submit" value="<?=_('Sort list')?>" /></noscript>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<ul class="admnql">
|
||||
<?php foreach($users as &$user) : ?>
|
||||
<li class="cf">
|
||||
<input type="checkbox" id="users-<?=$user['id']?>" name="users[]" value="<?=$user['id']?>" <?php if(in_array($user['id'], $selectedUsers)) : ?>checked="checked"<?php endif ?> <?php if($user['id'] == \hhu\z\controllers\SeminaryController::$user['id']) : ?>disabled="disabled"<?php endif ?>/>
|
||||
<label for="users-<?=$user['id']?>">
|
||||
<p><a href="<?=$linker->link(array('users','user',$user['url']))?>"><?=$user['username']?></a></p>
|
||||
<?php if(in_array('admin', $user['roles'])) : ?><small>(<?=_('Admin')?>)</small><?php endif ?>
|
||||
<?php if(in_array('moderator', $user['roles'])) : ?><small>(<?=_('Moderator')?>)</small><?php endif ?>
|
||||
<?php if(in_array('user', $user['roles'])) : ?><small>(<?=_('User')?>)</small><?php endif ?>
|
||||
</p>
|
||||
</label>
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
</ul>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend><?=_('Add role')?></legend>
|
||||
<?php if(in_array('admin', \hhu\z\controllers\IntermediateController::$user['roles'])) : ?>
|
||||
<input type="submit" name="actions[addrole]" value="<?=_('Admin')?>" />
|
||||
<input type="submit" name="actions[addrole]" value="<?=_('Moderator')?>" />
|
||||
<?php endif ?>
|
||||
<input type="submit" name="actions[addrole]" value="<?=_('User')?>" />
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend><?=_('Remove role')?></legend>
|
||||
<?php if(in_array('admin', \hhu\z\controllers\IntermediateController::$user['roles'])) : ?>
|
||||
<input type="submit" name="actions[removerole]" value="<?=_('Admin')?>" />
|
||||
<input type="submit" name="actions[removerole]" value="<?=_('Moderator')?>" />
|
||||
<?php endif ?>
|
||||
<input type="submit" name="actions[removerole]" value="<?=_('User')?>" />
|
||||
</fieldset>
|
||||
</form>
|
||||
Loading…
Add table
Add a link
Reference in a new issue