Exemple #1
0
 /**
  * Create role
  *
  * @param $name
  * @return Role|null
  * @throws RoleExistsException
  */
 public function create($name)
 {
     $role = $this->roleRepo->findOneByName($name);
     if ($role instanceof Role) {
         throw new RoleExistsException($role->getName());
     }
     $role = (new Role())->setName($name);
     $this->getManager()->persist($role);
     $this->getManager()->flush();
     return $role;
 }
Exemple #2
0
 /**
  * @param $username
  * @param $password
  * @param $roleName
  * @return User|null
  * @throws RoleNotFoundException
  * @throws UserExistsException
  */
 public function create($username, $password, $roleName)
 {
     $role = $this->roleRepo->findOneByName($roleName);
     if (!$role instanceof Role) {
         throw new RoleNotFoundException($roleName);
     }
     $user = $this->userRepo->findOneByUsername($username);
     if ($user instanceof User) {
         throw new UserExistsException($username);
     }
     $salt = Generator::generateSalt();
     $encryptedPassword = Generator::hash($salt, $this->secret, $password);
     $user = (new User())->setUsername($username)->setPassword($encryptedPassword)->setRole($role)->setSalt($salt)->setDeleted(false)->setActive(true);
     $this->getManager()->persist($user);
     $this->tokenService->create($user);
     $this->getManager()->flush();
     return $user;
 }
 /**
  * Create access rule. To create access successfully $role or $username should be declared
  *
  * @param $route
  * @param $type - should be "role" or "user"
  * @param $value - should be role name or user name
  * @return Access|null
  * @throws RoleNotFoundException
  * @throws UserNotFoundException
  */
 public function create($route, $type, $value)
 {
     if ($type === self::ACCESS_BY_ROLE) {
         $role = $this->roleRepo->findOneByName($value);
         if ($role instanceof Role) {
             $access = (new Access())->setRole($role)->setRoute($route);
             $this->getManager()->persist($access);
             $this->getManager()->flush();
             return $access;
         }
         throw new RoleNotFoundException($value);
     }
     if ($type === self::ACCESS_BY_USER) {
         $user = $this->userRepo->findOneByUsername($value);
         if ($user instanceof User) {
             $access = (new Access())->setUser($user)->setRoute($route);
             $this->getManager()->persist($access);
             $this->getManager()->flush();
             return $access;
         }
         throw new UserNotFoundException($value);
     }
     return null;
 }