All changes to users should happen through this interface. The class also contains ACL annotations which will only work if you have the SecurityExtraBundle installed, otherwise they will simply be ignored.
Author: Gordon Franke (info@nevalon.de)
Author: Thibault Duplessis (thibault.duplessis@gmail.com)
Author: Johannes M. Schmitt (schmittjoh@gmail.com)
Example #1
0
 /**
  * Creates a user and returns it.
  *
  * @param string $username
  * @param string $password
  * @param string $email
  * @param bool   $active
  * @param bool   $superadmin
  *
  * @return \FOS\UserBundle\Model\UserInterface
  */
 public function create($username, $password, $email, $active, $superadmin)
 {
     $discriminator = $this->discriminator;
     switch ($this->type) {
         case 'staff':
             $class = 'Truckee\\MatchBundle\\Entity\\Staff';
             break;
         case 'admin':
             $class = 'Truckee\\MatchBundle\\Entity\\Admin';
             break;
         case 'volunteer':
             $class = 'Truckee\\MatchBundle\\Entity\\Volunteer';
             break;
         default:
             break;
     }
     $discriminator->setClass($class);
     $user = $this->userManager->createUser();
     $user->setUsername($username);
     $user->setFirstname($this->firstname);
     $user->setLastname($this->lastname);
     $user->setEmail($email);
     $user->setPlainPassword($password);
     $user->setEnabled((bool) $active);
     $this->userManager->updateUser($user, true);
     return $user;
 }
 public function postBind(DataEvent $event)
 {
     $user = $event->getForm()->getData();
     if ($user instanceof UserInterface) {
         $this->userManager->updateCanonicalFields($user);
     }
 }
 public function __invoke(Request $request)
 {
     if ($this->container->hasParameter('partkeepr.auth.allow_password_change') && $this->container->getParameter('partkeepr.auth.allow_password_change') === false) {
         throw new PasswordChangeNotAllowedException();
     }
     $user = $this->userService->getUser();
     if (!$request->request->has('oldpassword') && !$request->request->has('newpassword')) {
         throw new \Exception('old password and new password need to be specified');
     }
     $FOSUser = $this->userManager->findUserByUsername($user->getUsername());
     if ($FOSUser !== null) {
         $encoder = $this->encoderFactory->getEncoder($FOSUser);
         $encoded_pass = $encoder->encodePassword($request->request->get('oldpassword'), $FOSUser->getSalt());
         if ($FOSUser->getPassword() != $encoded_pass) {
             throw new OldPasswordWrongException();
         }
         $this->userManipulator->changePassword($user->getUsername(), $request->request->get('newpassword'));
     } else {
         if ($user->isLegacy()) {
             if ($user->getPassword() !== md5($request->request->get('oldpassword'))) {
                 throw new OldPasswordWrongException();
             }
             $user->setNewPassword($request->request->get('newpassword'));
             $this->userService->syncData($user);
         } else {
             throw new \Exception('Cannot change password for LDAP users');
         }
     }
     $user->setPassword('');
     $user->setNewPassword('');
     return $user;
 }
 /**
  * @param UserInterface $user
  */
 protected function onSuccess(UserInterface $user)
 {
     if ($this->getNewPassword() != "") {
         $user->setPlainPassword($this->getNewPassword());
     }
     $this->userManager->updateUser($user);
 }
Example #5
0
 /**
  * This must be called on prePersist and preUpdate if the event is about a
  * user.
  *
  * @param UserInterface $user
  */
 protected function updateUserFields(UserInterface $user)
 {
     if (null === $this->userManager) {
         $this->userManager = $this->container->get('fos_user.user_manager');
     }
     $this->userManager->updateCanonicalFields($user);
     $this->userManager->updatePassword($user);
 }
 /**
  * Finds a user by his username and throws an exception if we can't find it.
  *
  * @param string $username
  *
  * @throws \InvalidArgumentException When user does not exist
  *
  * @return UserInterface
  */
 private function findUserByUsernameOrThrowException($username)
 {
     $user = $this->userManager->findUserByUsername($username);
     if (!$user) {
         throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
     }
     return $user;
 }
 /**
  * Removes the given user object.
  *
  * @param DataEvent $event
  */
 public function deleteObject(DataEvent $event)
 {
     $object = $event->getData();
     if ($object instanceof UserInterface) {
         $this->userManager->deleteUser($object);
         $this->eventDispatcher->dispatch(Events::POST_DELETE, $event);
         $event->stopPropagation();
     }
 }
 /**
  * Create user from response
  *
  * @param UserResponseInterface $response
  *
  * @return User
  */
 private function createUserFromResponse(UserResponseInterface $response)
 {
     /** @var User $user User */
     $user = $this->userManager->createUser();
     $user->setUsername($response->getUsername())->setFullName($response->getRealName())->setEmail($response->getEmail())->setEnabled(true)->setPlainPassword(uniqid())->setFacebookId($response->getUsername())->setFacebookAccessToken($response->getAccessToken());
     $this->eventDispatcher->dispatch(AppEvents::FACEBOOK_USER_CONNECTED, new FacebookUserConnectedEvent($user));
     $this->userManager->updateUser($user);
     return $user;
 }
Example #9
0
 public function getNbUnreadByUsername($username)
 {
     $nb = apc_fetch('nbm.' . $username);
     if (false === $nb) {
         $user = $this->userManager->findUserByUsername($username);
         $nb = $this->updateNbUnread($participant);
     }
     return $nb;
 }
 /**
  * handle user Ip log to users table, after login, with security.interactive_login kernel event.
  *
  * @param InteractiveLoginEvent $event
  */
 public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
 {
     $user = $event->getAuthenticationToken()->getUser();
     if ($user instanceof UserInterface) {
         $ipAddress = $this->requestStack->getCurrentRequest()->server->get('REMOTE_ADDR');
         $user->setLastloginClientIp($ipAddress);
         $this->userManager->updateUser($user);
     }
 }
 protected function createModelInstance()
 {
     $message = parent::createModelInstance();
     if ($to = $this->request->query->get('to')) {
         if ($recipient = $this->userManager->findUserByUsername($to)) {
             $message->setRecipient($recipient);
         }
     }
     return $message;
 }
 /**
  * Transforms a username string into a UserInterface instance.
  *
  * @param string $value Username
  *
  * @return UserInterface the corresponding UserInterface instance
  *
  * @throws UnexpectedTypeException if the given value is not a string
  */
 public function reverseTransform($value)
 {
     if (null === $value || '' === $value) {
         return null;
     }
     if (!is_string($value)) {
         throw new UnexpectedTypeException($value, 'string');
     }
     return $this->userManager->findUserByUsername($value);
 }
 /**
  * @param string $email
  * @param string $pw
  * @return \FOS\UserBundle\Model\UserInterface|null
  */
 function getUserByEmailPw($email, $pw)
 {
     $result = null;
     if ($email && $pw) {
         $user = $this->_fosUserManager->findUserByEmail($email);
         if ($user && $this->isPasswordValid($user, $pw)) {
             $result = $user;
         }
     }
     return $result;
 }
Example #14
0
 /**
  * Checks if the passed value is valid.
  *
  * @param mixed      $value      The value that should be validated
  * @param Constraint $constraint The constrain for the validation
  *
  * @return Boolean Whether or not the value is valid
  *
  * @throws UnexpectedTypeException if $value is not instance of \FOS\UserBundle\Model\UserInterface
  */
 public function isValid($value, Constraint $constraint)
 {
     if (!$value instanceof UserInterface) {
         throw new UnexpectedTypeException($value, 'FOS\\UserBundle\\Model\\UserInterface');
     }
     if (!$this->userManager->validateUnique($value, $constraint)) {
         $this->setMessage($constraint->message, array('%property%' => $constraint->property));
         return false;
     }
     return true;
 }
Example #15
0
 private function handleEvent(LifecycleEventArgs $args)
 {
     if (null === $this->userManager) {
         $this->userManager = $this->container->get('fos_user.user_manager');
     }
     $entity = $args->getEntity();
     if ($entity instanceof UserInterface) {
         $this->userManager->updateCanonicalFields($entity);
         $this->userManager->updatePassword($entity);
     }
 }
Example #16
0
 public function closeAccount(Response $response)
 {
     $user = $this->securityContext->getToken()->getUser();
     $user->setEnabled(false);
     $this->userManager->updateUser($user);
     $cookieHandler = new CookieClearingLogoutHandler($this->request->cookies->all());
     $cookieHandler->logout($this->request, $response, $this->securityContext->getToken());
     $sessionHandler = new SessionLogoutHandler();
     $sessionHandler->logout($this->request, $response, $this->securityContext->getToken());
     $this->securityContext->setToken(null);
 }
Example #17
0
 /**
  * Creates a user and returns it.
  *
  * @param string  $username
  * @param string  $password
  * @param string  $email
  * @param Boolean $active
  * @param Boolean $superadmin
  *
  * @return \FOS\UserBundle\Model\UserInterface
  */
 public function create($username, $password, $email, $active, $superadmin)
 {
     $user = $this->userManager->createUser();
     $user->setUsername($username);
     $user->setName($this->name);
     $user->setSurname($this->surname);
     $user->setEmail($email);
     $user->setPlainPassword($password);
     $user->setEnabled((bool) $active);
     $this->userManager->updateUser($user, true);
     return $user;
 }
 private function techRegister($form, $request, \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher, \FOS\UserBundle\Model\UserManagerInterface $userManager, $user)
 {
     $event = new FormEvent($form, $request);
     $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
     $userManager->updateUser($user);
     if (null === ($response = $event->getResponse())) {
         $url = $this->container->get('router')->generate('fos_user_registration_confirmed');
         $response = new RedirectResponse($url);
     }
     $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
     return $response;
 }
 /**
  * Creates a user and returns it.
  *
  * @param string $email
  * @param string $password
  * @param string $fullname
  * @param string $institution
  * @param bool   $active
  * @param bool   $superadmin
  *
  * @return User
  */
 public function create($email, $password, $fullname, $institution, $active, $superadmin)
 {
     $user = $this->userManager->createUser();
     $user->setEmail($email);
     $user->setPlainPassword($password);
     $user->setFullname($fullname);
     $user->setInstitution($institution);
     $user->setEnabled($active);
     $user->setSuperAdmin($superadmin);
     $this->userManager->updateUser($user);
     return $user;
 }
 protected function onSuccess()
 {
     if ($this->confirmation) {
         $user->setEnabled(false);
         $this->mailer->sendConfirmationEmailMessage($this->user);
     } else {
         $user->setConfirmationToken(null);
         $user->setEnabled(true);
     }
     $user->setRoles(array('ROLE_ADMIN'));
     $user->setPermissions(array('VIEW', 'EDIT', 'CREATE', 'DELETE'));
     $this->userManager->updateUser($this->user, true);
 }
 /**
  * @param  HookInterface $apiService
  * @param  string        $entityName
  * @return mixed $entityName instance
  */
 public function hydrate(HookInterface $apiService, $entityName)
 {
     $hydratedEntity = new $entityName();
     if (!$hydratedEntity instanceof HookEventInterface) {
         throw new LogicException("Can't hydrate this event");
     }
     $hydratedEntity->setEmail($apiService->getEmail());
     $hydratedEntity->setName($apiService->getService());
     $hydratedEntity->setMetaData($apiService->getMetaData());
     $user = $this->userManager->findUserByEmail($apiService->getEmail());
     $hydratedEntity->setUser($user);
     return $hydratedEntity;
 }
Example #22
0
 /**
  * {@inheritDoc}
  */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
     $username = $response->getUsername();
     /** @var User $user */
     $user = $this->userManager->findUserBy(array('githubId' => $username));
     if (!$user) {
         throw new AccountNotLinkedException(sprintf('No user with github username "%s" was found.', $username));
     }
     if ($user->getGithubToken() !== $response->getAccessToken()) {
         $user->setGithubToken($response->getAccessToken());
         $this->userManager->updateUser($user);
     }
     return $user;
 }
Example #23
0
 /**
  * Creates a user and returns it.
  *
  * @param string  $username
  * @param string  $password
  * @param string  $email
  * @param Boolean $active
  * @param Boolean $superadmin
  * @param string  $firstName
  * @param string  $lastName
  * @param integer  $phoneNumber
  *
  * @return \AppBundle\Entity\Users
  */
 public function create($username, $password, $email, $active, $superadmin, $firstName, $lastName, $phoneNumber)
 {
     $user = new Users();
     $user->setUsername($username);
     $user->setEmail($email);
     $user->setPlainPassword($password);
     $user->setEnabled((bool) $active);
     $user->setFirstName($firstName);
     $user->setLastName($lastName);
     $user->setPhoneNumber($phoneNumber);
     $user->setSuperAdmin((bool) $superadmin);
     $this->userManager->updateUser($user);
     return $user;
 }
 /**
  * @param $email
  * @param $password
  * @return \FOS\UserBundle\Model\UserInterface
  * @throws ValidationException
  */
 public function registerUser($email, $password)
 {
     $user = $this->userManager->createUser();
     $user->setUsername($email);
     $user->setEmail($email);
     $user->setPlainPassword($password);
     $user->setEnabled(true);
     $errors = $this->validator->validate($user, null, array('registration'));
     if ($errors->count() > 0) {
         throw new ValidationException($errors);
     }
     $this->userManager->updateUser($user);
     return UserDTO::withEntity($user);
 }
 /**
  * {@inheritDoc}
  */
 public function connect($user, UserResponseInterface $response)
 {
     $property = $this->getProperty($response);
     $setter = 'set' . ucfirst($property);
     if (!method_exists($user, $setter)) {
         throw new \RuntimeException(sprintf("Class '%s' should have a method '%s'.", get_class($user), $setter));
     }
     $username = $response->getUsername();
     if (null !== ($previousUser = $this->userManager->findUserBy(array($property => $username)))) {
         $previousUser->{$setter}(null);
         $this->userManager->updateUser($previousUser);
     }
     $user->{$setter}($username);
     $this->userManager->updateUser($user);
 }
Example #26
0
 /**
  * Creates a user and returns it.
  *
  * @param string $username
  * @param string $password
  * @param string $email
  * @param string $name
  * @param string $lastName
  * @param bool   $active
  * @param bool   $superadmin
  *
  * @return \FOS\UserBundle\Model\UserInterface
  */
 public function create($username, $password, $email, $name, $lastName, $active, $superadmin, $changePassword)
 {
     $user = $this->userManager->createUser();
     $user->setUsername($username);
     $user->setEmail($email);
     $user->setPlainPassword($password);
     $user->setName($name);
     $user->setLastName($lastName);
     $user->setEnabled((bool) $active);
     $user->setSuperAdmin((bool) $superadmin);
     if ((bool) $changePassword) {
         $user->setPasswordExpireAt(new \DateTime());
     }
     $this->userManager->updateUser($user);
     return $user;
 }
Example #27
0
 public function unlock(User $user, $username, $password)
 {
     $user->setUsername($username);
     $user->setPlainPassword($password);
     $user->setUnlockToken(null);
     $errors = $this->validator->validate($user, ['Registration']);
     if ($errors->count() === 0) {
         try {
             $this->userManager->updateUser($user);
         } catch (\Exception $e) {
             throw new Exception($e->getMessage());
         }
         return true;
     }
     return $errors;
 }
Example #28
0
 /**
  * Create user from response
  *
  * @param UserResponseInterface $response
  *
  * @return User
  */
 private function createUserFromResponse(UserResponseInterface $response)
 {
     $email = $response->getEmail() ?: $response->getUsername() . '@example.com';
     /** @var User $user */
     $user = $this->userManager->createUser();
     $user->setEmail($email);
     $user->setUsername($response->getNickname());
     $user->setEnabled(true);
     $user->setPlainPassword(uniqid());
     $user->setGithubId($response->getUsername());
     // Move to separate listener
     if (in_array($response->getUsername(), $this->adminGitHubIds)) {
         $user->addRole('ROLE_ADMIN');
     }
     $this->userManager->updateUser($user);
     return $user;
 }
 /**
  * Persists, updates or delete data return by the controller if applicable.
  *
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $user = $event->getControllerResult();
     if (!$user instanceof UserInterface) {
         return;
     }
     switch ($event->getRequest()->getMethod()) {
         case Request::METHOD_POST:
         case Request::METHOD_PUT:
             $this->userManager->updateUser($user, false);
             break;
         case Request::METHOD_DELETE:
             $this->userManager->deleteUser($user);
             $event->setControllerResult(null);
             break;
     }
 }
Example #30
0
 /**
  * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::delete
  */
 public function testDeleteUserFromDatabase()
 {
     $resourceMock = m::mock('Hal\\Resource')->shouldReceive('toArray')->andReturn(array('username' => self::TEST_USERNAME))->getMock();
     $user = $this->createUserMock(self::TEST_USERNAME, self::TEST_EMAIL, null);
     $this->userManagerMock->shouldReceive('findUserByUsername')->with(self::TEST_USERNAME)->andReturn($user);
     $this->userManagerMock->shouldReceive('deleteUser')->with($user);
     $this->fixture->delete($resourceMock);
 }