All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.allegro.tech.hermes.management.domain.group.GroupValidator Maven / Gradle / Ivy

There is a newer version: 2.8.0
Show newest version
package pl.allegro.tech.hermes.management.domain.group;

import org.springframework.stereotype.Component;
import pl.allegro.tech.hermes.api.Group;
import pl.allegro.tech.hermes.domain.group.GroupAlreadyExistsException;
import pl.allegro.tech.hermes.domain.group.GroupRepository;
import pl.allegro.tech.hermes.management.api.auth.CreatorRights;
import pl.allegro.tech.hermes.management.config.GroupProperties;
import pl.allegro.tech.hermes.management.domain.PermissionDeniedException;

@Component
public class GroupValidator {

  private final GroupRepository repository;

  private final GroupNameValidator groupNameValidator;

  public GroupValidator(GroupRepository repository, GroupProperties groupProperties) {
    this.repository = repository;
    this.groupNameValidator = new GroupNameValidator(groupProperties.getAllowedGroupNameRegex());
  }

  public void checkCreation(Group toCheck, CreatorRights creatorRights) {
    groupNameValidator.requireValid(toCheck.getGroupName());

    if (!creatorRights.allowedToCreate(toCheck)) {
      throw new PermissionDeniedException("You are not allowed to create groups");
    }

    if (repository.groupExists(toCheck.getGroupName())) {
      throw new GroupAlreadyExistsException(toCheck.getGroupName());
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy