Fix user ajax find by role + add test

pull/4014/head^2
Julio 3 years ago
parent d3ba7d062d
commit 67b3bd7953
  1. 24
      public/main/inc/ajax/user_manager.ajax.php
  2. 4
      public/main/mySpace/tc_report.php
  3. 1
      src/CoreBundle/Repository/Node/UserRepository.php
  4. 14
      tests/CoreBundle/Repository/Node/UserRepositoryTest.php

@ -2,6 +2,7 @@
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Framework\Container;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Query\Expr\Join;
@ -246,38 +247,19 @@ switch ($action) {
break;
case 'user_by_role':
api_block_anonymous_users(false);
$status = isset($_REQUEST['status']) ? (int) $_REQUEST['status'] : DRH;
$active = isset($_REQUEST['active']) ? (int) $_REQUEST['active'] : null;
$role = User::getRoleFromStatus($status);
$criteria = new Criteria();
$criteria
->where(
Criteria::expr()->orX(
Criteria::expr()->contains('username', $_REQUEST['q']),
Criteria::expr()->contains('firstname', $_REQUEST['q']),
Criteria::expr()->contains('lastname', $_REQUEST['q'])
)
)
->andWhere(
Criteria::expr()->in('roles', [$role])
);
if (null !== $active) {
$criteria->andWhere(Criteria::expr()->eq('active', $active));
}
$users = UserManager::getRepository()->matching($criteria);
$users = Container::getUserRepository()->findByRole($role, $_REQUEST['q'], api_get_current_access_url_id());
if (!$users->count()) {
if (empty($users)) {
echo json_encode([]);
break;
}
$items = [];
/** @var User $user */
foreach ($users as $user) {
$items[] = [
'id' => $user->getId(),

@ -35,7 +35,7 @@ switch ($action) {
get_lang('User'),
[],
[
'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=user_by_role&active=1&status='.STUDENT,
'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=user_by_role&status='.STUDENT,
]
);
$form->addButtonSave(get_lang('Add'));
@ -165,7 +165,7 @@ if ('add_user' !== $action) {
[],
[
'width' => '200px',
'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=user_by_role&active=1&status='.STUDENT,
'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=user_by_role&status='.STUDENT,
]
);
$addUserForm->addButtonSave(get_lang('Add'));

@ -205,6 +205,7 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter
{
$qb = $this->createQueryBuilder('u');
$this->addActiveAndNotAnonUserQueryBuilder($qb);
$this->addAccessUrlQueryBuilder($accessUrlId, $qb);
$this->addRoleQueryBuilder($role, $qb);
$this->addSearchByKeywordQueryBuilder($keyword, $qb);

@ -213,9 +213,21 @@ class UserRepositoryTest extends AbstractApiTest
$this->assertSame(1, $user->getPortals()->count());
}
public function testFindByRole(): void
{
$userRepo = self::getContainer()->get(UserRepository::class);
$user = $this->createUser('user', 'user');
$user->addRole('ROLE_SESSION_MANAGER');
$userRepo->update($user);
$urlId = $this->getAccessUrl()->getId();
$users = $userRepo->findByRole('ROLE_SESSION_MANAGER', '', $urlId);
$this->assertNotNull($users);
$this->assertCount(1, $users);
}
public function testAddFriendToUser(): void
{
self::bootKernel();
$em = $this->getEntityManager();
$user = $this->createUser('user', 'user');

Loading…
Cancel
Save