* added toolbar to user screen # 56 * added filter for user role in user admin toolbar # 56 * added unit tests for queries # 56
This commit is contained in:
@@ -13,6 +13,7 @@ namespace AppBundle\Controller\Admin;
|
||||
|
||||
use AppBundle\Controller\AbstractController;
|
||||
use AppBundle\Entity\User;
|
||||
use AppBundle\Form\Toolbar\UserToolbarForm;
|
||||
use AppBundle\Form\UserCreateType;
|
||||
use AppBundle\Repository\Query\UserQuery;
|
||||
use Pagerfanta\Pagerfanta;
|
||||
@@ -32,23 +33,49 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
*/
|
||||
class UserController extends AbstractController
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return UserQuery
|
||||
*/
|
||||
protected function getQueryForRequest(Request $request)
|
||||
{
|
||||
$visibility = $request->get('visibility', UserQuery::SHOW_VISIBLE);
|
||||
if (strlen($visibility) == 0 || (int)$visibility != $visibility) {
|
||||
$visibility = UserQuery::SHOW_BOTH;
|
||||
}
|
||||
$pageSize = (int) $request->get('pageSize');
|
||||
$userRole = $request->get('role');
|
||||
|
||||
$query = new UserQuery();
|
||||
$query
|
||||
->setPageSize($pageSize)
|
||||
->setVisibility($visibility)
|
||||
->setRole($userRole)
|
||||
;
|
||||
|
||||
return $query ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/", defaults={"page": 1}, name="admin_user")
|
||||
* @Route("/page/{page}", requirements={"page": "[1-9]\d*"}, name="admin_user_paginated")
|
||||
* @Method("GET")
|
||||
* @Cache(smaxage="10")
|
||||
* @Security("is_granted('view_all', user)")
|
||||
*/
|
||||
public function indexAction($page)
|
||||
public function indexAction($page, Request $request)
|
||||
{
|
||||
$query = new UserQuery();
|
||||
$query->setVisibility(UserQuery::SHOW_BOTH);
|
||||
$query = $this->getQueryForRequest($request);
|
||||
$query->setPage($page);
|
||||
|
||||
/* @var $entries Pagerfanta */
|
||||
$entries = $this->getDoctrine()->getRepository(User::class)->findByQuery($query);
|
||||
|
||||
return $this->render('admin/user.html.twig', ['entries' => $entries]);
|
||||
return $this->render('admin/user.html.twig', [
|
||||
'entries' => $entries,
|
||||
'query' => $query,
|
||||
'toolbarForm' => $this->getToolbarForm($query)->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,6 +114,24 @@ class UserController extends AbstractController
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UserQuery $query
|
||||
* @return \Symfony\Component\Form\FormInterface
|
||||
*/
|
||||
protected function getToolbarForm(UserQuery $query)
|
||||
{
|
||||
return $this->createForm(
|
||||
UserToolbarForm::class,
|
||||
$query,
|
||||
[
|
||||
'action' => $this->generateUrl('admin_user_paginated', [
|
||||
'page' => $query->getPage(),
|
||||
]),
|
||||
'method' => 'GET',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @return \Symfony\Component\Form\FormInterface
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Form\Toolbar;
|
||||
namespace AppBundle\Form\Toolbar;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Form\Toolbar;
|
||||
namespace AppBundle\Form\Toolbar;
|
||||
|
||||
use AppBundle\Form\Type\PageSizeType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
51
src/AppBundle/Form/Toolbar/UserToolbarForm.php
Normal file
51
src/AppBundle/Form/Toolbar/UserToolbarForm.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai package.
|
||||
*
|
||||
* (c) Kevin Papst <kevin@kevinpapst.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AppBundle\Form\Toolbar;
|
||||
|
||||
use AppBundle\Form\Type\UserRoleType;
|
||||
use AppBundle\Repository\Query\UserQuery;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Defines the form used for filtering the user.
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class UserToolbarForm extends VisibilityToolbarForm
|
||||
{
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
parent::buildForm($builder, $options);
|
||||
|
||||
$builder
|
||||
->add('role', UserRoleType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => UserQuery::class,
|
||||
'csrf_protection' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Form\Toolbar;
|
||||
namespace AppBundle\Form\Toolbar;
|
||||
|
||||
use AppBundle\Form\Type\VisibilityType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
67
src/AppBundle/Form/Type/UserRoleType.php
Normal file
67
src/AppBundle/Form/Type/UserRoleType.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai package.
|
||||
*
|
||||
* (c) Kevin Papst <kevin@kevinpapst.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AppBundle\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Custom form field type to select a user role.
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class UserRoleType extends AbstractType
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $roles = [];
|
||||
|
||||
/**
|
||||
* UserRolesType constructor.
|
||||
* @param string[] $roles
|
||||
*/
|
||||
public function __construct(array $roles)
|
||||
{
|
||||
$this->roles = $roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$roles = [];
|
||||
|
||||
foreach ($this->roles as $key => $value) {
|
||||
$roles[$key] = $key;
|
||||
foreach ($value as $value2) {
|
||||
$roles[$value2] = $value2;
|
||||
}
|
||||
}
|
||||
|
||||
$resolver->setDefaults([
|
||||
'label' => 'label.roles',
|
||||
'choices' => $roles,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return ChoiceType::class;
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,8 @@
|
||||
namespace AppBundle\Form;
|
||||
|
||||
use AppBundle\Entity\User;
|
||||
use AppBundle\Form\Type\UserRoleType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
@@ -24,40 +24,16 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
*/
|
||||
class UserRolesType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $roles = [];
|
||||
|
||||
/**
|
||||
* UserRolesType constructor.
|
||||
* @param string[] $roles
|
||||
*/
|
||||
public function __construct(array $roles)
|
||||
{
|
||||
$this->roles = $roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$roles = [];
|
||||
|
||||
foreach ($this->roles as $key => $value) {
|
||||
$roles[$key] = $key;
|
||||
foreach ($value as $value2) {
|
||||
$roles[$value2] = $value2;
|
||||
}
|
||||
}
|
||||
|
||||
$builder
|
||||
// string[]
|
||||
->add('roles', ChoiceType::class, [
|
||||
->add('roles', UserRoleType::class, [
|
||||
'label' => 'label.roles',
|
||||
'multiple' => true,
|
||||
'choices' => $roles,
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ class BaseQuery
|
||||
*/
|
||||
public function setPage($page)
|
||||
{
|
||||
$this->page = $page;
|
||||
$this->page = (int)$page;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,4 +19,29 @@ namespace AppBundle\Repository\Query;
|
||||
class UserQuery extends BaseQuery implements VisibilityInterface
|
||||
{
|
||||
use VisibilityTrait;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $role;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRole()
|
||||
{
|
||||
return $this->role;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $role
|
||||
* @return UserQuery
|
||||
*/
|
||||
public function setRole($role)
|
||||
{
|
||||
if (strpos($role, 'ROLE_') !== false || $role === null) {
|
||||
$this->role = $role;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,26 @@ interface VisibilityInterface
|
||||
const SHOW_VISIBLE = 1;
|
||||
const SHOW_HIDDEN = 0;
|
||||
const SHOW_BOTH = 2;
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function getVisibility();
|
||||
|
||||
/**
|
||||
* @param int $visibility
|
||||
* @return $this
|
||||
*/
|
||||
public function setVisibility($visibility);
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isExclusiveVisibility();
|
||||
|
||||
/**
|
||||
* @param bool $exclusiveVisibility
|
||||
* @return $this
|
||||
*/
|
||||
public function setExclusiveVisibility($exclusiveVisibility);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ trait VisibilityTrait
|
||||
*/
|
||||
public function setVisibility($visibility)
|
||||
{
|
||||
if (in_array($visibility, [self::SHOW_BOTH, self::SHOW_VISIBLE, self::SHOW_HIDDEN])) {
|
||||
if (in_array($visibility, [self::SHOW_BOTH, self::SHOW_VISIBLE, self::SHOW_HIDDEN], true)) {
|
||||
$this->visibility = $visibility;
|
||||
}
|
||||
return $this;
|
||||
|
||||
@@ -57,10 +57,14 @@ class UserRepository extends AbstractRepository
|
||||
->from('AppBundle:User', 'u')
|
||||
->orderBy('u.' . $query->getOrderBy(), $query->getOrder());
|
||||
|
||||
if ($query->getVisibility() === UserQuery::SHOW_VISIBLE) {
|
||||
$qb->andWhere('u.visible = 1');
|
||||
} elseif ($query->getVisibility() === UserQuery::SHOW_HIDDEN) {
|
||||
$qb->andWhere('u.visible = 0');
|
||||
if ($query->getVisibility() == UserQuery::SHOW_VISIBLE) {
|
||||
$qb->andWhere('u.active = 1');
|
||||
} elseif ($query->getVisibility() == UserQuery::SHOW_HIDDEN) {
|
||||
$qb->andWhere('u.active = 0');
|
||||
}
|
||||
|
||||
if ($query->getRole() !== null) {
|
||||
$qb->andWhere('u.roles LIKE :role')->setParameter('role', '%' . $query->getRole() . '%');
|
||||
}
|
||||
|
||||
return $this->getPager($qb->getQuery(), $query->getPage(), $query->getPageSize());
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace TimesheetBundle\Form\Toolbar;
|
||||
|
||||
use AppBundle\Form\Toolbar\VisibilityToolbarForm;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use TimesheetBundle\Repository\Query\CustomerQuery;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace TimesheetBundle\Form\Toolbar;
|
||||
|
||||
use AppBundle\Form\Toolbar\VisibilityToolbarForm;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use TimesheetBundle\Form\Type\CustomerType;
|
||||
|
||||
@@ -153,7 +153,7 @@ class TimesheetQuery extends BaseQuery
|
||||
*/
|
||||
public function setState($state)
|
||||
{
|
||||
if (in_array($state, [self::STATE_ALL, self::STATE_RUNNING, self::STATE_STOPPED])) {
|
||||
if (in_array($state, [self::STATE_ALL, self::STATE_RUNNING, self::STATE_STOPPED], true)) {
|
||||
$this->state = $state;
|
||||
}
|
||||
return $this;
|
||||
|
||||
Reference in New Issue
Block a user