Пример #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
 /**
  * @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;
 }