Exemple #1
0
 /**
  * Automatic post-registration user authentication
  */
 protected function authenticateAccount(Netizen $account)
 {
     $cred = $account->getCredential();
     $token = new Token('secured_area', $cred->getProviderKey(), $cred->getUid(), $account->getRoles());
     $token->setUser($account);
     $this->get('security.context')->setToken($token);
 }
 /**
  * @test
  */
 public function initialize()
 {
     $this->collection->drop();
     $authorList = [];
     foreach (['kirk', 'spock', 'mccoy'] as $nick) {
         $authorList[] = new Author($nick);
     }
     $target = $authorList[0];
     foreach ($authorList as $author) {
         $user = new Netizen($author);
         $user->setProfile(new Profile());
         $this->repository->persist($user);
         if ($target === $author) {
             $targetUser = $user;
         }
     }
     $source = new SmallTalk($target);
     $commentary = new Commentary($target);
     $source->attachCommentary($commentary);
     foreach ($authorList as $other) {
         if ($other === $target) {
             continue;
         }
         $source->addFan($other);
         $commentary->addFan($other);
     }
     $this->repository->batchPersist([$source, $source, $source]);
     $this->assertCount(6, $this->collection->find());
     return (string) $targetUser->getId();
 }
Exemple #3
0
 /**
  * Creates a new Netizen from mandatory datas
  *
  * @param string $nick
  * @param string $OauthProviderKey the key of the OAuth provider (github,facebook,twitter...)
  * @param string $uniqueUserId the unique id of the user given by the OAuth provider
  *
  * @return \Trismegiste\SocialBundle\Security\Netizen
  */
 public function create($nick, $OauthProviderKey, $uniqueUserId)
 {
     $author = new Author($nick);
     $user = new Netizen($author);
     $strat = new Credential\OAuth($uniqueUserId, $OauthProviderKey);
     $user->setCredential($strat);
     $user->setProfile(new Profile());
     $user->setGroup('ROLE_USER');
     return $user;
 }
 /**
  * @test
  */
 public function initialize()
 {
     $this->collection->drop();
     $author = new Author('kirk');
     $user = new Netizen($author);
     $user->setProfile(new \Trismegiste\SocialBundle\Security\Profile());
     $this->repository->persist($user);
     $source = new SmallTalk($author);
     $this->repository->batchPersist([$source, $source, $source]);
     $this->assertCount(4, $this->collection->find());
     return (string) $user->getId();
 }
Exemple #5
0
 protected function benchmark(Netizen $user, $counter)
 {
     $key = $user->getUsername();
     $stopwatch = microtime(true);
     for ($k = 0; $k < $counter; $k++) {
         $this->repository->persist($user);
     }
     printf(" write %s %.1f ms\n", $key, (microtime(true) - $stopwatch) * 1000 / $counter);
     $stopwatch = microtime(true);
     for ($k = 0; $k < $counter; $k++) {
         $user = $this->repository->findByNickname($key);
     }
     printf(" read %s %.1f ms\n", $user->getUsername(), (microtime(true) - $stopwatch) * 1000 / $counter);
 }
 /**
  * @dataProvider getUser
  */
 public function testRedirect($granted, $path)
 {
     $default = new Netizen(new Author('kirk'));
     $default->setCredential(new OAuth('1701', 'ufp'));
     $request = new Request();
     $token = new Token('secured_area', 'ufp', '1701');
     $token->setUser($default);
     $this->security->expects($this->atLeast(1))->method('isGranted')->will($this->returnCallback(function ($role) use($granted) {
         return $role == $granted;
     }));
     $this->urlGenerator->expects($this->once())->method('generate')->with($path)->willReturn('ok');
     $response = $this->sut->onAuthenticationSuccess($request, $token);
     $cookie = $response->headers->getCookies()[0];
     $this->assertEquals('oauth_provider', $cookie->getName());
     $this->assertEquals('ufp', $cookie->getValue());
 }
Exemple #7
0
 private function hasFreeAccess(Netizen $user)
 {
     $roleGroup = $user->getGroup();
     return isset($this->role_hierarchy[$roleGroup]) && in_array(self::ROLE_FREEPASS, $this->role_hierarchy[$roleGroup]);
 }
 public function getValidInputs()
 {
     $promoted = new Netizen(new Author('kirk'));
     $promoted->setGroup('ROLE_ADMIN');
     return [[['group' => 'ROLE_ADMIN'], $promoted]];
 }
 /**
  * @inheritdoc
  */
 public function promote(Netizen $user, SecurityContextInterface $ctx)
 {
     // we only check the equality on author because, roles are just changing now
     if ($user->getAuthor()->isEqual($ctx->getToken()->getUser()->getAuthor())) {
         throw new AccessDeniedException("You can't promote yourself");
     }
     if ($ctx->isGranted('ROLE_PROMOTE')) {
         $this->repository->persist($user);
     } else {
         throw new AccessDeniedException("You have no right to promote someone");
     }
 }
Exemple #10
0
 /**
  * @dataProvider getComparableUser
  */
 public function testIsEqual(Netizen $user1, Netizen $user2, $yesOrNo)
 {
     $this->assertEquals($yesOrNo, $user1->isEqualTo($user2));
 }
 private function createNetizen($nick)
 {
     $mock = $this->getMock("Trismegiste\\Socialist\\AuthorInterface");
     $mock->expects($this->any())->method('getNickname')->will($this->returnValue($nick));
     $user = new Netizen($mock);
     $user->setId(new \MongoId());
     return $user;
 }
 /**
  * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
  * @expectedExceptionMessage You can't promote yourself
  */
 public function testNoPromoteNetizenOnHimself()
 {
     $user = new Netizen(new Author('spock'));
     $secu = $this->createSecurityContextMockFromUser(clone $user);
     $user->setGroup('ROLE_MANAGER');
     $this->sut->promote($user, $secu);
 }