Add pagination for users page
This commit is contained in:
@@ -121,16 +121,31 @@ class User extends Base
|
|||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$users = $this->user->getAll();
|
$direction = $this->request->getStringParam('direction', 'ASC');
|
||||||
$nb_users = count($users);
|
$order = $this->request->getStringParam('order', 'username');
|
||||||
|
$offset = $this->request->getIntegerParam('offset', 0);
|
||||||
|
$limit = 25;
|
||||||
|
|
||||||
|
$users = $this->user->paginate($offset, $limit, $order, $direction);
|
||||||
|
$nb_users = $this->user->count();
|
||||||
|
|
||||||
$this->response->html(
|
$this->response->html(
|
||||||
$this->template->layout('user_index', array(
|
$this->template->layout('user_index', array(
|
||||||
'projects' => $this->project->getList(),
|
'projects' => $this->project->getList(),
|
||||||
'users' => $users,
|
|
||||||
'nb_users' => $nb_users,
|
'nb_users' => $nb_users,
|
||||||
|
'users' => $users,
|
||||||
'menu' => 'users',
|
'menu' => 'users',
|
||||||
'title' => t('Users').' ('.$nb_users.')'
|
'title' => t('Users').' ('.$nb_users.')',
|
||||||
|
'pagination' => array(
|
||||||
|
'controller' => 'user',
|
||||||
|
'action' => 'index',
|
||||||
|
'direction' => $direction,
|
||||||
|
'order' => $order,
|
||||||
|
'total' => $nb_users,
|
||||||
|
'offset' => $offset,
|
||||||
|
'limit' => $limit,
|
||||||
|
'params' => array(),
|
||||||
|
),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -138,10 +138,64 @@ class User extends Base
|
|||||||
return $this->db
|
return $this->db
|
||||||
->table(self::TABLE)
|
->table(self::TABLE)
|
||||||
->asc('username')
|
->asc('username')
|
||||||
->columns('id', 'username', 'name', 'email', 'is_admin', 'default_project_id', 'is_ldap_user', 'notifications_enabled', 'google_id', 'github_id')
|
->columns(
|
||||||
|
'id',
|
||||||
|
'username',
|
||||||
|
'name',
|
||||||
|
'email',
|
||||||
|
'is_admin',
|
||||||
|
'default_project_id',
|
||||||
|
'is_ldap_user',
|
||||||
|
'notifications_enabled',
|
||||||
|
'google_id',
|
||||||
|
'github_id'
|
||||||
|
)
|
||||||
->findAll();
|
->findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all users with pagination
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param integer $offset Offset
|
||||||
|
* @param integer $limit Limit
|
||||||
|
* @param string $column Sorting column
|
||||||
|
* @param string $direction Sorting direction
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function paginate($offset = 0, $limit = 25, $column = 'username', $direction = 'ASC')
|
||||||
|
{
|
||||||
|
return $this->db
|
||||||
|
->table(self::TABLE)
|
||||||
|
->columns(
|
||||||
|
'id',
|
||||||
|
'username',
|
||||||
|
'name',
|
||||||
|
'email',
|
||||||
|
'is_admin',
|
||||||
|
'default_project_id',
|
||||||
|
'is_ldap_user',
|
||||||
|
'notifications_enabled',
|
||||||
|
'google_id',
|
||||||
|
'github_id'
|
||||||
|
)
|
||||||
|
->offset($offset)
|
||||||
|
->limit($limit)
|
||||||
|
->orderBy($column, $direction)
|
||||||
|
->findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of users
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function count()
|
||||||
|
{
|
||||||
|
return $this->db->table(self::TABLE)->count();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List all users (key-value pairs with id/username)
|
* List all users (key-value pairs with id/username)
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<h2><?= t('Users') ?><span id="page-counter"> (<?= $nb_users ?>)</span></h2>
|
<h2><?= t('Users') ?><span id="page-counter"> (<?= $nb_users ?>)</span></h2>
|
||||||
<?php if (Helper\is_admin()): ?>
|
<?php if (Helper\is_admin()): ?>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="?controller=user&action=create"><?= t('New user') ?></a></li>
|
<li><?= Helper\a(t('New user'), 'user', 'create') ?></li>
|
||||||
</ul>
|
</ul>
|
||||||
<?php endif ?>
|
<?php endif ?>
|
||||||
</div>
|
</div>
|
||||||
@@ -13,29 +13,29 @@
|
|||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th><?= t('Id') ?></th>
|
<th><?= Helper\order(t('Id'), 'id', $pagination) ?></th>
|
||||||
<th><?= t('Username') ?></th>
|
<th><?= Helper\order(t('Username'), 'username', $pagination) ?></th>
|
||||||
<th><?= t('Name') ?></th>
|
<th><?= Helper\order(t('Name'), 'name', $pagination) ?></th>
|
||||||
<th><?= t('Email') ?></th>
|
<th><?= Helper\order(t('Email'), 'email', $pagination) ?></th>
|
||||||
<th><?= t('Administrator') ?></th>
|
<th><?= Helper\order(t('Administrator'), 'is_admin', $pagination) ?></th>
|
||||||
<th><?= t('Default project') ?></th>
|
<th><?= Helper\order(t('Default project'), 'default_project_id', $pagination) ?></th>
|
||||||
<th><?= t('Notifications') ?></th>
|
<th><?= Helper\order(t('Notifications'), 'notifications_enabled', $pagination) ?></th>
|
||||||
<th><?= t('External accounts') ?></th>
|
<th><?= t('External accounts') ?></th>
|
||||||
<th><?= t('Account type') ?></th>
|
<th><?= Helper\order(t('Account type'), 'is_ldap_user', $pagination) ?></th>
|
||||||
</tr>
|
</tr>
|
||||||
<?php foreach ($users as $user): ?>
|
<?php foreach ($users as $user): ?>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<a href="?controller=user&action=show&user_id=<?= $user['id'] ?>">#<?= $user['id'] ?></a>
|
<?= Helper\a('#'.$user['id'], 'user', 'show', array('user_id' => $user['id'])) ?>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="?controller=user&action=show&user_id=<?= $user['id'] ?>"><?= Helper\escape($user['username']) ?></a>
|
<?= Helper\a(Helper\escape($user['username']), 'user', 'show', array('user_id' => $user['id'])) ?>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<?= Helper\escape($user['name']) ?>
|
<?= Helper\escape($user['name']) ?>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<?= Helper\escape($user['email']) ?>
|
<a href="mailto:<?= Helper\escape($user['email']) ?>"><?= Helper\escape($user['email']) ?></a>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<?= $user['is_admin'] ? t('Yes') : t('No') ?>
|
<?= $user['is_admin'] ? t('Yes') : t('No') ?>
|
||||||
@@ -66,6 +66,8 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<?php endforeach ?>
|
<?php endforeach ?>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
<?= Helper\paginate($pagination) ?>
|
||||||
<?php endif ?>
|
<?php endif ?>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
17
scripts/create-random-users.php
Executable file
17
scripts/create-random-users.php
Executable file
@@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require __DIR__.'/../app/common.php';
|
||||||
|
|
||||||
|
use Model\User;
|
||||||
|
|
||||||
|
$userModel = new User($registry);
|
||||||
|
|
||||||
|
for ($i = 0; $i < 500; $i++) {
|
||||||
|
$userModel->create(array(
|
||||||
|
'username' => 'user'.$i,
|
||||||
|
'password' => 'password'.$i,
|
||||||
|
'name' => 'User #'.$i,
|
||||||
|
'email' => 'user'.$i.'@localhost',
|
||||||
|
));
|
||||||
|
}
|
||||||
@@ -9,6 +9,15 @@ use Model\Project;
|
|||||||
|
|
||||||
class UserTest extends Base
|
class UserTest extends Base
|
||||||
{
|
{
|
||||||
|
public function testPassword()
|
||||||
|
{
|
||||||
|
$password = 'test123';
|
||||||
|
$hash = password_hash($password, PASSWORD_BCRYPT);
|
||||||
|
|
||||||
|
$this->assertNotEmpty($hash);
|
||||||
|
$this->assertTrue(password_verify($password, $hash));
|
||||||
|
}
|
||||||
|
|
||||||
public function testPrepare()
|
public function testPrepare()
|
||||||
{
|
{
|
||||||
$u = new User($this->registry);
|
$u = new User($this->registry);
|
||||||
|
|||||||
Reference in New Issue
Block a user