예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function create($username, $password, $email, $active, $superadmin)
 {
     $user = parent::create($username, $password, $email, $active, $superadmin);
     $apiToken = substr($this->tokenGenerator->generateToken(), 0, 20);
     $user->setApiToken($apiToken);
     $this->userManager->updateUser($user);
     return $user;
 }
 /**
  * @inheritdoc
  */
 public function getUserFromOAuthResponse($providerName, array $data)
 {
     $field = $providerName . 'Id';
     if ($user = $this->userManager->findUserBy([$field => $data['id']])) {
         if (isset($data['data']['email'])) {
             $user->setEmail($data['data']['email']);
         }
         return $user;
     }
     if (isset($data['data']['email'])) {
         $user->setEmail($data['data']['email']);
     }
     $user = $this->userManipulator->create($data['data']['name'], 'secret', '', true, false);
     $setter = "set" . ucfirst($providerName) . 'Id';
     $user->{$setter}($data['id']);
     $this->userManager->updateUser($user);
     return $user;
 }
예제 #3
0
 /**
  * Syncronizes the data of the given user with the FOSRestBundle
  *
  * @throws \Exception If the password was not set
  *
  * @param $user
  */
 public function syncData(User $user)
 {
     if ($user->getProvider()->getType() !== self::BUILTIN_PROVIDER) {
         return;
     }
     $FOSUser = $this->userManager->findUserByUsername($user->getUsername());
     if ($FOSUser === null) {
         if ($user->getNewPassword() == "") {
             throw new \Exception("Password must be set");
         }
         $FOSUser = $this->userManipulator->create($user->getUsername(), $user->getNewPassword(), "", true, false);
         $user->setLegacy(false);
     }
     if ($user->getNewPassword() != "") {
         $this->userManipulator->changePassword($user->getUsername(), $user->getNewPassword());
     }
     $FOSUser->setEmail($user->getEmail());
     $FOSUser->setEnabled($user->isActive());
 }
 public function testCreate()
 {
     $userManagerMock = $this->createMock('FOS\\UserBundle\\Model\\UserManagerInterface');
     $user = new TestUser();
     $username = '******';
     $password = '******';
     $email = '*****@*****.**';
     $active = true;
     // it is enabled
     $superadmin = false;
     $userManagerMock->expects($this->once())->method('createUser')->will($this->returnValue($user));
     $userManagerMock->expects($this->once())->method('updateUser')->will($this->returnValue($user))->with($this->isInstanceOf('FOS\\UserBundle\\Tests\\TestUser'));
     $manipulator = new UserManipulator($userManagerMock);
     $manipulator->create($username, $password, $email, $active, $superadmin);
     $this->assertEquals($username, $user->getUsername());
     $this->assertEquals($password, $user->getPlainPassword());
     $this->assertEquals($email, $user->getEmail());
     $this->assertEquals($active, $user->isEnabled());
     $this->assertEquals($superadmin, $user->isSuperAdmin());
 }
예제 #5
0
 /**
  * Displays a form to create a new User.
  *
  * @Route("/user/new", name="donate_admin_user_new")
  */
 public function newAction(Request $request)
 {
     // @since 2.3 we user voters to check authorization instead of being ROLE based
     if (false === $this->get('security.authorization_checker')->isGranted('create users')) {
         throw new AccessDeniedException('Unauthorised access!');
     }
     $form = $this->createForm(new AccountType(), new User(), array('roles' => $this->getAvailabledRoles(), 'action' => 'new'));
     $form->handleRequest($request);
     if ($form->isValid()) {
         $data = $form->getData();
         $userManager = $this->get('fos_user.user_manager');
         if (!$this->userAlreadyExist($userManager, $data->getUsername(), $data->getEmail())) {
             $userManipulator = new UserManipulator($userManager);
             $user = $userManipulator->create($data->getUsername(), $data->getPassword(), $data->getEmail(), true, false);
             $this->get('session')->getFlashBag()->add('notice', "L'utilisateur " . $user->getUsername() . " a été enregistré");
             return $this->redirect($this->generateUrl('donate_admin_users'));
         }
     }
     return $this->render('DonateAdminBundle:Account:new.html.twig', ['form' => $form->createView()]);
 }
예제 #6
0
 /**
  * Displays a form to create a new User.
  *
  * @Route("/user/new", name="donate_admin_user_new")
  * @Security("is_granted('ROLE_ADMIN')")
  * @since 2.4.7 we use ROLE_ADMIN as User Manager
  */
 public function newAction(Request $request)
 {
     $form = $this->createForm(AccountType::class, new User(), array('roles' => $this->getAvailabledRoles(), 'action' => 'new'));
     $form->handleRequest($request);
     if ($form->isValid()) {
         $data = $form->getData();
         $userManager = $this->get('fos_user.user_manager');
         if (!$this->userAlreadyExist($userManager, $data->getUsername(), $data->getEmail())) {
             $userManipulator = new UserManipulator($userManager);
             $user = $userManipulator->create($data->getUsername(), $data->getPassword(), $data->getEmail(), true, false);
             $this->get('session')->getFlashBag()->add('notice', "L'utilisateur " . $user->getUsername() . " a été enregistré");
             return $this->redirect($this->generateUrl('donate_admin_users'));
         }
     }
     return $this->render('DonateAdminBundle:Account:new.html.twig', ['form' => $form->createView()]);
 }