예제 #1
0
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $user = $this->model->getByNameOrEmail($username);
     if (!$user) {
         throw new AuthenticationException('User not found.', self::IDENTITY_NOT_FOUND);
     }
     if (!$this->passwordStrategy->matchPassword($user, $password)) {
         throw new AuthenticationException('Invalid password.', self::INVALID_CREDENTIAL);
     }
     return new Identity($user->getId(), $this->model->getRoles($user));
 }
예제 #2
0
파일: UserFactory.php 프로젝트: rixxi/user
 /**
  * @param string $name
  * @param string $email
  * @param string $password
  * @param Role[]|string[] $roles
  */
 public function create($name, $email, $password, $roles = [])
 {
     $user = new User();
     $user->name = $name;
     $user->email = $email;
     $this->passwordStrategy->setPassword($user, $password);
     $roles = (array) $roles;
     foreach ($roles as $role) {
         if (is_string($role)) {
             if (!($role = $this->rolesRepository->findBy(['name' => $role]))) {
                 throw new \RuntimeException("Unknown role '{$role}'.");
             }
         } elseif (!$role instanceof Role) {
             throw new \UnexpectedValueException("Role must be string or instance of Rixxi\\User\\Entities\\Role");
         }
         $user->roles->add($role);
     }
     $this->repository->save($user);
     return $user;
 }