Remove some circular references

This commit is contained in:
Romain Neutron
2014-02-20 19:14:35 +01:00
parent 9e650a7954
commit 862fe696cb
18 changed files with 52 additions and 47 deletions

View File

@@ -86,7 +86,7 @@ class Authenticator
private function populateSession(Session $session) private function populateSession(Session $session)
{ {
$user = $session->getUser($this->app); $user = $session->getUser();
$rights = []; $rights = [];
if ($this->app['acl']->get($user)->has_right('taskmanager')) { if ($this->app['acl']->get($user)->has_right('taskmanager')) {

View File

@@ -16,14 +16,15 @@ use Alchemy\Phrasea\Authentication\Exception\NotAuthenticatedException;
use Alchemy\Phrasea\Authentication\Provider\Token\Token; use Alchemy\Phrasea\Authentication\Provider\Token\Token;
use Alchemy\Phrasea\Authentication\Provider\Token\Identity; use Alchemy\Phrasea\Authentication\Provider\Token\Identity;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Doctrine\Common\Persistence\ObjectRepository;
class SuggestionFinder class SuggestionFinder
{ {
private $app; private $repository;
public function __construct(Application $app) public function __construct(ObjectRepository $repository)
{ {
$this->app = $app; $this->repository = $repository;
} }
/** /**
@@ -40,7 +41,7 @@ class SuggestionFinder
$infos = $token->getIdentity(); $infos = $token->getIdentity();
if ($infos->has(Identity::PROPERTY_EMAIL)) { if ($infos->has(Identity::PROPERTY_EMAIL)) {
return $this->app['manipulator.user']->getRepository()->findByEmail($infos->get(Identity::PROPERTY_EMAIL)); return $this->repository->findByEmail($infos->get(Identity::PROPERTY_EMAIL));
} }
return null; return null;

View File

@@ -109,7 +109,7 @@ class Oauth2 implements ControllerProviderInterface
} }
} }
$account = $oauth2_adapter->updateAccount($app['authentication']->getUser()->getId()); $account = $oauth2_adapter->updateAccount($app['authentication']->getUser());
$params['account_id'] = $account->get_id(); $params['account_id'] = $account->get_id();

View File

@@ -608,7 +608,7 @@ class Login implements ControllerProviderInterface
$app->abort(401, 'A token is required'); $app->abort(401, 'A token is required');
} }
$form = $app->form(new PhraseaRecoverPasswordForm($app)); $form = $app->form(new PhraseaRecoverPasswordForm($app['tokens']));
$form->setData(['token' => $token]); $form->setData(['token' => $token]);
if ('POST' === $request->getMethod()) { if ('POST' === $request->getMethod()) {

View File

@@ -45,7 +45,7 @@ class AuthenticationManagerServiceProvider implements ServiceProviderInterface
}); });
$app['authentication.suggestion-finder'] = $app->share(function (Application $app) { $app['authentication.suggestion-finder'] = $app->share(function (Application $app) {
return new SuggestionFinder($app); return new SuggestionFinder($app['manipulator.user']->getRepository());
}); });
$app['authentication.providers.factory'] = $app->share(function (Application $app) { $app['authentication.providers.factory'] = $app->share(function (Application $app) {

View File

@@ -48,7 +48,7 @@ class RegistrationServiceProvider implements ServiceProviderInterface
'type' => 'text', 'type' => 'text',
'constraints' => [ 'constraints' => [
new Assert\NotBlank(), new Assert\NotBlank(),
new NewLogin($app), NewLogin::create($app),
] ]
], ],
'gender' => [ 'gender' => [

View File

@@ -12,26 +12,27 @@
namespace Alchemy\Phrasea\Form\Constraint; namespace Alchemy\Phrasea\Form\Constraint;
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Doctrine\Common\Persistence\ObjectRepository;
use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraint;
class NewEmail extends Constraint class NewEmail extends Constraint
{ {
public $message = 'This email is already bound to an account'; public $message = 'This email is already bound to an account';
private $app; private $repository;
public function __construct(Application $app) public function __construct(ObjectRepository $repository)
{ {
$this->app = $app; $this->repository = $repository;
parent::__construct(); parent::__construct();
} }
public function isAlreadyRegistered($email) public function isAlreadyRegistered($email)
{ {
return (Boolean) $this->app['manipulator.user']->getRepository()->findByEmail($email); return (Boolean) $this->repository->findByEmail($email);
} }
public static function create(Application $app) public static function create(Application $app)
{ {
return new static($app); return new static($app['manipulator.user']->getRepository());
} }
} }

View File

@@ -12,26 +12,27 @@
namespace Alchemy\Phrasea\Form\Constraint; namespace Alchemy\Phrasea\Form\Constraint;
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Doctrine\Common\Persistence\ObjectRepository;
use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraint;
class NewLogin extends Constraint class NewLogin extends Constraint
{ {
public $message = 'This login is already registered'; public $message = 'This login is already registered';
private $app; private $repository;
public function __construct(Application $app) public function __construct(ObjectRepository $repository)
{ {
$this->app = $app; $this->repository = $repository;
parent::__construct(); parent::__construct();
} }
public function isAlreadyRegistered($login) public function isAlreadyRegistered($login)
{ {
return (Boolean) $this->app['manipulator.user']->getRepository()->findByLogin($login); return (Boolean) $this->repository->findByLogin($login);
} }
public static function create(Application $app) public static function create(Application $app)
{ {
return new static($app); return new static($app['manipulator.user']->getRepository());
} }
} }

View File

@@ -18,12 +18,10 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class PasswordToken extends Constraint class PasswordToken extends Constraint
{ {
public $message = 'The token provided is not valid anymore'; public $message = 'The token provided is not valid anymore';
private $app;
private $random; private $random;
public function __construct(Application $app, \random $random) public function __construct(\random $random)
{ {
$this->app = $app;
$this->random = $random; $this->random = $random;
parent::__construct(); parent::__construct();
} }
@@ -41,6 +39,6 @@ class PasswordToken extends Constraint
public static function create(Application $app) public static function create(Application $app)
{ {
return new static($app, $app['tokens']); return new static($app['tokens']);
} }
} }

View File

@@ -22,11 +22,11 @@ use Symfony\Component\Validator\Constraints as Assert;
*/ */
class PhraseaRecoverPasswordForm extends AbstractType class PhraseaRecoverPasswordForm extends AbstractType
{ {
private $app; private $tokens;
public function __construct(Application $app) public function __construct(\random $tokens)
{ {
$this->app = $app; $this->tokens = $tokens;
} }
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
@@ -34,7 +34,7 @@ class PhraseaRecoverPasswordForm extends AbstractType
$builder->add('token', 'hidden', [ $builder->add('token', 'hidden', [
'required' => true, 'required' => true,
'constraints' => [ 'constraints' => [
new PasswordToken($this->app, $this->app['tokens']) new PasswordToken($this->tokens)
] ]
]); ]);

View File

@@ -12,6 +12,7 @@
namespace Alchemy\Phrasea\Form\Login; namespace Alchemy\Phrasea\Form\Login;
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Form\Constraint\NewEmail;
use Alchemy\Phrasea\Utilities\String\Camelizer; use Alchemy\Phrasea\Utilities\String\Camelizer;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
@@ -40,7 +41,7 @@ class PhraseaRegisterForm extends AbstractType
'constraints' => [ 'constraints' => [
new Assert\NotBlank(), new Assert\NotBlank(),
new Assert\Email(), new Assert\Email(),
new \Alchemy\Phrasea\Form\Constraint\NewEmail($this->app), NewEmail::create($this->app),
], ],
]); ]);

View File

@@ -497,19 +497,18 @@ class API_OAuth2_Adapter extends OAuth2
/** /**
* *
* @param usr_id $usr_id * @param User $user
* @return API_OAuth2_Account * @return API_OAuth2_Account
*/ */
public function updateAccount($usr_id) public function updateAccount(User $user)
{ {
if ($this->client === null) if ($this->client === null)
throw new logicalException("Client property must be set before update an account"); throw new logicalException("Client property must be set before update an account");
try { try {
$user = $this->app['manipulator.user']->getRepository()->find($usr_id);
$account = API_OAuth2_Account::load_with_user($this->app, $this->client, $user); $account = API_OAuth2_Account::load_with_user($this->app, $this->client, $user);
} catch (\Exception $e) { } catch (\Exception $e) {
$account = $this->createAccount($usr_id); $account = $this->createAccount($user->getId());
} }
return $account; return $account;
@@ -795,7 +794,11 @@ class API_OAuth2_Adapter extends OAuth2
return false; return false;
} }
$account = $this->updateAccount($usr_id); if (null === $user = $this->app['manipulator.user']->getRepository()->find($usr_id)) {
return false;
}
$account = $this->updateAccount($user);
return [ return [
'redirect_uri' => $this->client->get_redirect_uri() 'redirect_uri' => $this->client->get_redirect_uri()

View File

@@ -285,7 +285,7 @@
<p>{{ "Date" | trans }} : <span class="info">{{ app['date-formatter'].getPrettyString(file.getCreated()) }}</span></p> <p>{{ "Date" | trans }} : <span class="info">{{ app['date-formatter'].getPrettyString(file.getCreated()) }}</span></p>
{% if file.getSession().getUser() is not none %} {% if file.getSession().getUser() is not none %}
<p> <p>
{% set username = '<a href="#" class="username userTips" tooltipsrc="' ~ path('prod_tooltip_user', { 'usr_id' : file.getSession().getUser(app).getId() }) ~ '/">' ~ file.getSession().getUser().getDisplayName() ~ '</a>' %} {% set username = '<a href="#" class="username userTips" tooltipsrc="' ~ path('prod_tooltip_user', { 'usr_id' : file.getSession().getUser().getId() }) ~ '/">' ~ file.getSession().getUser().getDisplayName() ~ '</a>' %}
{% trans with {'%username%' : username} %}Uploaded by : %username%{% endtrans %} {% trans with {'%username%' : username} %}Uploaded by : %username%{% endtrans %}
</p> </p>
{% endif %} {% endif %}

View File

@@ -11,7 +11,7 @@ class SuggestionFinderTest extends \PhraseanetTestCase
{ {
$token = $this->getToken(self::$DI['user']->getEmail()); $token = $this->getToken(self::$DI['user']->getEmail());
$finder = new SuggestionFinder(self::$DI['app']); $finder = new SuggestionFinder(self::$DI['app']['manipulator.user']->getRepository());
$user = $finder->find($token); $user = $finder->find($token);
$this->assertInstanceOf('Alchemy\Phrasea\Model\Entities\User', $user); $this->assertInstanceOf('Alchemy\Phrasea\Model\Entities\User', $user);
@@ -22,7 +22,7 @@ class SuggestionFinderTest extends \PhraseanetTestCase
{ {
$token = $this->getToken(sprintf('%srandom%s@%srandom.com', uniqid(mt_rand(), true), uniqid(mt_rand(), true), uniqid(mt_rand(), true))); $token = $this->getToken(sprintf('%srandom%s@%srandom.com', uniqid(mt_rand(), true), uniqid(mt_rand(), true), uniqid(mt_rand(), true)));
$finder = new SuggestionFinder(self::$DI['app']); $finder = new SuggestionFinder(self::$DI['app']['manipulator.user']->getRepository());
$user = $finder->find($token); $user = $finder->find($token);
$this->assertNull($user); $this->assertNull($user);

View File

@@ -8,25 +8,25 @@ class NewEmailTest extends \PhraseanetTestCase
{ {
public function testAnUnknownAddressIsNotAlreadyRegistered() public function testAnUnknownAddressIsNotAlreadyRegistered()
{ {
$constraint = new NewEmail(self::$DI['app']); $constraint = NewEmail::create(self::$DI['app']);
$this->assertFalse($constraint->isAlreadyRegistered('nonehere')); $this->assertFalse($constraint->isAlreadyRegistered('nonehere'));
} }
public function testARegisteredAddressIsAlreadyRegistered() public function testARegisteredAddressIsAlreadyRegistered()
{ {
$constraint = new NewEmail(self::$DI['app']); $constraint = NewEmail::create(self::$DI['app']);
$this->assertTrue($constraint->isAlreadyRegistered(self::$DI['user']->getEmail())); $this->assertTrue($constraint->isAlreadyRegistered(self::$DI['user']->getEmail()));
} }
public function testNullIsNotAlreadyRegistered() public function testNullIsNotAlreadyRegistered()
{ {
$constraint = new NewEmail(self::$DI['app']); $constraint = NewEmail::create(self::$DI['app']);
$this->assertFalse($constraint->isAlreadyRegistered(null)); $this->assertFalse($constraint->isAlreadyRegistered(null));
} }
public function testBlankIsNotAlreadyRegistered() public function testBlankIsNotAlreadyRegistered()
{ {
$constraint = new NewEmail(self::$DI['app']); $constraint = NewEmail::create(self::$DI['app']);
$this->assertFalse($constraint->isAlreadyRegistered('')); $this->assertFalse($constraint->isAlreadyRegistered(''));
} }
} }

View File

@@ -8,25 +8,25 @@ class NewLoginTest extends \PhraseanetTestCase
{ {
public function testAnUnknownLoginIsNotAlreadyRegistered() public function testAnUnknownLoginIsNotAlreadyRegistered()
{ {
$constraint = new NewLogin(self::$DI['app']); $constraint = NewLogin::create(self::$DI['app']);
$this->assertFalse($constraint->isAlreadyRegistered('nonehere@test.com')); $this->assertFalse($constraint->isAlreadyRegistered('nonehere@test.com'));
} }
public function testARegisteredLoginIsAlreadyRegistered() public function testARegisteredLoginIsAlreadyRegistered()
{ {
$constraint = new NewLogin(self::$DI['app']); $constraint = NewLogin::create(self::$DI['app']);
$this->assertTrue($constraint->isAlreadyRegistered(self::$DI['user']->getLogin())); $this->assertTrue($constraint->isAlreadyRegistered(self::$DI['user']->getLogin()));
} }
public function testNullIsNotAlreadyRegistered() public function testNullIsNotAlreadyRegistered()
{ {
$constraint = new NewLogin(self::$DI['app']); $constraint = NewLogin::create(self::$DI['app']);
$this->assertFalse($constraint->isAlreadyRegistered(null)); $this->assertFalse($constraint->isAlreadyRegistered(null));
} }
public function testBlankIsNotAlreadyRegistered() public function testBlankIsNotAlreadyRegistered()
{ {
$constraint = new NewLogin(self::$DI['app']); $constraint = NewLogin::create(self::$DI['app']);
$this->assertFalse($constraint->isAlreadyRegistered('')); $this->assertFalse($constraint->isAlreadyRegistered(''));
} }
} }

View File

@@ -23,7 +23,7 @@ class PasswordTokenTest extends \PhraseanetTestCase
->with($token) ->with($token)
->will($this->throwException(new NotFoundHttpException('Token not found'))); ->will($this->throwException(new NotFoundHttpException('Token not found')));
$constraint = new PasswordToken(self::$DI['app'], $random); $constraint = new PasswordToken($random);
$this->assertFalse($constraint->isValid($token)); $this->assertFalse($constraint->isValid($token));
} }
@@ -43,7 +43,7 @@ class PasswordTokenTest extends \PhraseanetTestCase
->with($token) ->with($token)
->will($this->returnValue(['usr_id' => mt_rand(), 'type' => \random::TYPE_PASSWORD])); ->will($this->returnValue(['usr_id' => mt_rand(), 'type' => \random::TYPE_PASSWORD]));
$constraint = new PasswordToken(self::$DI['app'], $random); $constraint = new PasswordToken($random);
$this->assertTrue($constraint->isValid($token)); $this->assertTrue($constraint->isValid($token));
} }
} }

View File

@@ -9,6 +9,6 @@ class PhraseaRecoverPasswordFormTest extends FormTestCase
{ {
protected function getForm() protected function getForm()
{ {
return new PhraseaRecoverPasswordForm(self::$DI['app']); return new PhraseaRecoverPasswordForm(self::$DI['app']['tokens']);
} }
} }