mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-13 04:53:26 +00:00
Add user repositories functions
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
namespace Repositories;
|
namespace Repositories;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityRepository;
|
use Doctrine\ORM\EntityRepository;
|
||||||
|
use Entities\User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User
|
* User
|
||||||
@@ -21,4 +22,89 @@ use Doctrine\ORM\EntityRepository;
|
|||||||
*/
|
*/
|
||||||
class UserRepository extends EntityRepository
|
class UserRepository extends EntityRepository
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Finds admins
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function findAdmins()
|
||||||
|
{
|
||||||
|
$qb = $this->createQueryBuilder('u');
|
||||||
|
|
||||||
|
$qb->where($qb->expr()->eq('u.admin', $qb->expr()->literal(true)))
|
||||||
|
->andWhere($qb->expr()->isNull('u.modelOf'))
|
||||||
|
->andWhere($qb->expr()->eq('u.deleted', $qb->expr()->literal(false)));
|
||||||
|
|
||||||
|
return $qb->getQuery()->getResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a selection of user
|
||||||
|
*
|
||||||
|
* @param array $users An array of user
|
||||||
|
*
|
||||||
|
* @return integer The number of processed rows
|
||||||
|
*/
|
||||||
|
public function setAdmins(array $users)
|
||||||
|
{
|
||||||
|
$qb = $this->createQueryBuilder('u');
|
||||||
|
|
||||||
|
$qb->update('Entities\User', 'u')
|
||||||
|
->set('u.admin', $qb->expr()->literal(true))
|
||||||
|
->where($qb->expr()->in('u.id', array_map(function($value) {
|
||||||
|
if ($value instanceof User) {
|
||||||
|
return $value->getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int) $value;
|
||||||
|
}, $users)));
|
||||||
|
|
||||||
|
return $qb->getQuery()->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets all admins
|
||||||
|
*
|
||||||
|
* @return integer The number of processed rows
|
||||||
|
*/
|
||||||
|
public function resetAdmins()
|
||||||
|
{
|
||||||
|
$qb = $this->createQueryBuilder('u');
|
||||||
|
|
||||||
|
$qb->update('Entities\User', 'u')
|
||||||
|
->set('u.admin', $qb->expr()->literal(false))
|
||||||
|
->where($qb->expr()->eq('u.admin', $qb->expr()->literal(true)));
|
||||||
|
|
||||||
|
return $qb->getQuery()->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a user by login
|
||||||
|
*
|
||||||
|
* @param string $login
|
||||||
|
*
|
||||||
|
* @return null|User
|
||||||
|
*/
|
||||||
|
public function findByLogin($login)
|
||||||
|
{
|
||||||
|
return $this->findOneBy(array('login' => $login));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a user by email
|
||||||
|
*
|
||||||
|
* @param string $email
|
||||||
|
*
|
||||||
|
* @return null|User
|
||||||
|
*/
|
||||||
|
public function findByEmail($email)
|
||||||
|
{
|
||||||
|
$qb = $this->createQueryBuilder('u');
|
||||||
|
|
||||||
|
$qb->where($qb->expr()->eq('u.email', $email))
|
||||||
|
->andWhere($qb->expr()->isNotNull('u.email'))
|
||||||
|
->andWhere($qb->expr()->eq('u.deleted', $qb->expr()->literal(false)));
|
||||||
|
|
||||||
|
return $qb->getQuery()->getOneOrNullResult();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
104
tests/Doctrine/Tests/Repositories/UserRepositoryTest.php
Normal file
104
tests/Doctrine/Tests/Repositories/UserRepositoryTest.php
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2013 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Doctrine\Tests\Repositories;
|
||||||
|
|
||||||
|
use Entities\User;
|
||||||
|
|
||||||
|
class UserRepositoryTest extends \PhraseanetPHPUnitAbstract
|
||||||
|
{
|
||||||
|
public function testFindAdmins()
|
||||||
|
{
|
||||||
|
$this->markTestSkipped('missing deleted field');
|
||||||
|
$user = new User();
|
||||||
|
$user->setLogin('login');
|
||||||
|
$user->setPassword('toto');
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$users = self::$DI['app']['EM']->getRepository('Entities\User')->findAdmins();
|
||||||
|
$this->assertEquals(0, count($users));
|
||||||
|
|
||||||
|
$user->setAdmin(true);
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$users = self::$DI['app']['EM']->getRepository('Entities\User')->findAdmins();
|
||||||
|
$this->assertEquals(1, count($users));
|
||||||
|
|
||||||
|
$user->setModelOf(1);
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$users = self::$DI['app']['EM']->getRepository('Entities\User')->findAdmins();
|
||||||
|
$this->assertEquals(0, count($users));
|
||||||
|
|
||||||
|
$user->setModelOf(null);
|
||||||
|
$user->setModelOf(true);
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$users = self::$DI['app']['EM']->getRepository('Entities\User')->findAdmins();
|
||||||
|
$this->assertEquals(0, count($users));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetAdmins()
|
||||||
|
{
|
||||||
|
$user = new User();
|
||||||
|
$user->setLogin('login');
|
||||||
|
$user->setPassword('toto');
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$this->assertFalse($user->isAdmin());
|
||||||
|
self::$DI['app']['EM']->getRepository('Entities\User')->setAdmins(array($user));
|
||||||
|
$user = self::$DI['app']['EM']->getReference('Entities\User', $user->getId());
|
||||||
|
self::$DI['app']['EM']->refresh($user);
|
||||||
|
$this->assertTrue($user->isAdmin());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testResetAdmins()
|
||||||
|
{
|
||||||
|
$user = new User();
|
||||||
|
$user->setLogin('login');
|
||||||
|
$user->setPassword('toto');
|
||||||
|
$user->setAdmin(true);
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$this->assertTrue($user->isAdmin());
|
||||||
|
self::$DI['app']['EM']->getRepository('Entities\User')->resetAdmins();
|
||||||
|
$user = self::$DI['app']['EM']->getReference('Entities\User', $user->getId());
|
||||||
|
self::$DI['app']['EM']->refresh($user);
|
||||||
|
$this->assertFalse($user->isAdmin());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindByLogin()
|
||||||
|
{
|
||||||
|
$user = new User();
|
||||||
|
$user->setLogin('login');
|
||||||
|
$user->setPassword('toto');
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$user = self::$DI['app']['EM']->getRepository('Entities\User')->findByLogin('login');
|
||||||
|
$this->assertInstanceOf('Entities\User', $user);
|
||||||
|
$this->assertNull(self::$DI['app']['EM']->getRepository('Entities\User')->findByLogin('wrong-login'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindByEmail()
|
||||||
|
{
|
||||||
|
$this->markTestSkipped('missing deleted field');
|
||||||
|
$user = new User();
|
||||||
|
$user->setLogin('login');
|
||||||
|
$user->setPassword('toto');
|
||||||
|
$user->setEmail('toto@toto.to');
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$userFound = self::$DI['app']['EM']->getRepository('Entities\User')->findByEmail('toto@toto.to');
|
||||||
|
$this->assertInstanceOf('Entities\User', $userFound);
|
||||||
|
|
||||||
|
$user->setDeleted(true);
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$userFound = self::$DI['app']['EM']->getRepository('Entities\User')->findByEmail('toto@toto.to');
|
||||||
|
$this->assertNull($userFound);
|
||||||
|
|
||||||
|
$user->setEmail(null);
|
||||||
|
$this->insertOneUser($user);
|
||||||
|
$userFound = self::$DI['app']['EM']->getRepository('Entities\User')->findByEmail(null);
|
||||||
|
$this->assertNull($userFound);
|
||||||
|
}
|
||||||
|
}
|
@@ -4,6 +4,7 @@ use Alchemy\Phrasea\CLI;
|
|||||||
use Alchemy\Phrasea\Application;
|
use Alchemy\Phrasea\Application;
|
||||||
use Alchemy\Phrasea\Border\File;
|
use Alchemy\Phrasea\Border\File;
|
||||||
use Doctrine\Common\DataFixtures\Loader;
|
use Doctrine\Common\DataFixtures\Loader;
|
||||||
|
use Entities\User;
|
||||||
use Silex\WebTestCase;
|
use Silex\WebTestCase;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpKernel\Client;
|
use Symfony\Component\HttpKernel\Client;
|
||||||
@@ -568,6 +569,22 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function insertOneUser(User $user)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$userFixture = new PhraseaFixture\User\LoadOneUser();
|
||||||
|
$userFixture->setUser($user);
|
||||||
|
|
||||||
|
$loader = new Loader();
|
||||||
|
$loader->addFixture($userFixture);
|
||||||
|
|
||||||
|
$this->insertFixtureInDatabase($loader);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->fail('Fail load one Basket : ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls a URI as XMLHTTP request.
|
* Calls a URI as XMLHTTP request.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user