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)
コード例 #1
0
ファイル: UserManipulator.php プロジェクト: truckee/match
 /**
  * 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;
 }
コード例 #2
0
 public function postBind(DataEvent $event)
 {
     $user = $event->getForm()->getData();
     if ($user instanceof UserInterface) {
         $this->userManager->updateCanonicalFields($user);
     }
 }
コード例 #3
0
 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;
 }
コード例 #4
0
 /**
  * @param UserInterface $user
  */
 protected function onSuccess(UserInterface $user)
 {
     if ($this->getNewPassword() != "") {
         $user->setPlainPassword($this->getNewPassword());
     }
     $this->userManager->updateUser($user);
 }
コード例 #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);
 }
コード例 #6
0
 /**
  * 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;
 }
コード例 #7
0
 /**
  * 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();
     }
 }
コード例 #8
0
 /**
  * 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;
 }
コード例 #9
0
ファイル: Cache.php プロジェクト: hotfics/lichess-old
 public function getNbUnreadByUsername($username)
 {
     $nb = apc_fetch('nbm.' . $username);
     if (false === $nb) {
         $user = $this->userManager->findUserByUsername($username);
         $nb = $this->updateNbUnread($participant);
     }
     return $nb;
 }
コード例 #10
0
 /**
  * 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);
     }
 }
コード例 #11
0
 protected function createModelInstance()
 {
     $message = parent::createModelInstance();
     if ($to = $this->request->query->get('to')) {
         if ($recipient = $this->userManager->findUserByUsername($to)) {
             $message->setRecipient($recipient);
         }
     }
     return $message;
 }
コード例 #12
0
 /**
  * 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);
 }
コード例 #13
0
 /**
  * @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;
 }
コード例 #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;
 }
コード例 #15
0
ファイル: UserListener.php プロジェクト: renegare/UserBundle
 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);
     }
 }
コード例 #16
0
ファイル: AccountCloser.php プロジェクト: hotfics/lichess-old
 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);
 }
コード例 #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;
 }
コード例 #18
0
 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;
 }
コード例 #19
0
 /**
  * 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;
 }
コード例 #20
0
 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);
 }
コード例 #21
0
 /**
  * @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;
 }
コード例 #22
0
ファイル: UserProvider.php プロジェクト: igui-br/packagist
 /**
  * {@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;
 }
コード例 #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;
 }
コード例 #24
0
 /**
  * @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);
 }
コード例 #25
0
 /**
  * {@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);
 }
コード例 #26
0
ファイル: UserManipulator.php プロジェクト: tkuska/user
 /**
  * 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;
 }
コード例 #27
0
ファイル: RegistrationHandler.php プロジェクト: rogoit/angsym
 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;
 }
コード例 #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;
 }
コード例 #29
0
 /**
  * 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;
     }
 }
コード例 #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);
 }