use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class GroupServiceImpl method findByName.
@Override
public List<GroupEntity> findByName(String name) {
try {
logger.debug("findByUsername : {}", name);
if (name == null) {
return Collections.emptyList();
}
List<GroupEntity> groupEntities = groupRepository.findAll().stream().filter(group -> group.getName().equals(name)).map(this::map).collect(Collectors.toList());
logger.debug("findByUsername : {} - DONE", name);
return groupEntities;
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to find groups by name", ex);
throw new TechnicalManagementException("An error occurs while trying to find groups by name", ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class GroupServiceImpl method create.
@Override
public GroupEntity create(NewGroupEntity group) {
try {
logger.debug("create {}", group);
if (!this.findByName(group.getName()).isEmpty()) {
throw new GroupNameAlreadyExistsException(group.getName());
}
Group newGroup = this.map(group);
newGroup.setId(UUID.toString(UUID.random()));
newGroup.setCreatedAt(new Date());
newGroup.setUpdatedAt(newGroup.getCreatedAt());
GroupEntity grp = this.map(groupRepository.create(newGroup));
// Audit
auditService.createPortalAuditLog(Collections.singletonMap(GROUP, newGroup.getId()), GROUP_CREATED, newGroup.getCreatedAt(), null, newGroup);
logger.debug("create {} - DONE", grp);
return grp;
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to create a group", ex);
throw new TechnicalManagementException("An error occurs while trying to create a group", ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class MembershipServiceImpl method addOrUpdateMember.
@Override
public MemberEntity addOrUpdateMember(MembershipReference reference, MembershipUser user, MembershipRole role) {
try {
LOGGER.debug("Add a new member for {} {}", reference.getType(), reference.getId());
RoleEntity roleEntity = roleService.findById(role.getScope(), role.getName());
if (API.equals(reference.getType()) && !io.gravitee.management.model.permissions.RoleScope.API.equals(roleEntity.getScope())) {
throw new NotAuthorizedMembershipException(role.getName());
} else if (MembershipReferenceType.APPLICATION.equals(reference.getType()) && !io.gravitee.management.model.permissions.RoleScope.APPLICATION.equals(roleEntity.getScope())) {
throw new NotAuthorizedMembershipException(role.getName());
} else if (MembershipReferenceType.GROUP.equals(reference.getType()) && !io.gravitee.management.model.permissions.RoleScope.APPLICATION.equals(roleEntity.getScope()) && !io.gravitee.management.model.permissions.RoleScope.API.equals(roleEntity.getScope())) {
throw new NotAuthorizedMembershipException(role.getName());
}
UserEntity userEntity;
if (user.getId() != null) {
userEntity = userService.findById(user.getId());
} else {
// We have a user reference, meaning that the user is coming from an external system
// User does not exist so we are looking into defined providers
Optional<io.gravitee.management.model.providers.User> providerUser = identityService.findByReference(user.getReference());
if (providerUser.isPresent()) {
try {
userEntity = userService.findByUsername(providerUser.get().getUsername(), false);
} catch (UserNotFoundException unfe) {
User identityUser = providerUser.get();
// The user is not yet registered in repository
// Information will be updated after the first connection of the user
NewExternalUserEntity newUser = new NewExternalUserEntity();
newUser.setUsername(identityUser.getUsername());
newUser.setFirstname(identityUser.getFirstname());
newUser.setLastname(identityUser.getLastname());
newUser.setSource(identityUser.getSource());
newUser.setEmail(identityUser.getEmail());
newUser.setSourceId(identityUser.getSourceId());
userEntity = userService.create(newUser, true);
}
} else {
throw new UserNotFoundException(user.getReference());
}
}
Optional<Membership> optionalMembership = membershipRepository.findById(userEntity.getId(), reference.getType(), reference.getId());
Date updateDate = new Date();
Membership returnedMembership;
if (optionalMembership.isPresent()) {
Membership updatedMembership = optionalMembership.get();
Membership previousMembership = new Membership(updatedMembership);
updatedMembership.getRoles().put(role.getScope().getId(), role.getName());
updatedMembership.setUpdatedAt(updateDate);
returnedMembership = membershipRepository.update(optionalMembership.get());
createAuditLog(MEMBERSHIP_UPDATED, updatedMembership.getUpdatedAt(), previousMembership, updatedMembership);
} else {
Membership membership = new Membership(userEntity.getId(), reference.getId(), reference.getType());
membership.setRoles(Collections.singletonMap(role.getScope().getId(), role.getName()));
membership.setCreatedAt(updateDate);
membership.setUpdatedAt(updateDate);
returnedMembership = membershipRepository.create(membership);
createAuditLog(MEMBERSHIP_CREATED, membership.getCreatedAt(), null, membership);
if (userEntity.getEmail() != null && !userEntity.getEmail().isEmpty()) {
EmailNotification emailNotification = buildEmailNotification(userEntity, reference.getType(), reference.getId());
if (emailNotification != null) {
emailService.sendAsyncEmailNotification(emailNotification);
}
}
}
return convert(returnedMembership, role.getScope());
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to add member for {} {}", reference.getType(), reference.getId(), ex);
throw new TechnicalManagementException("An error occurs while trying to add member for " + reference.getType() + " " + reference.getId(), ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class MembershipServiceImpl method deleteMember.
@Override
public void deleteMember(MembershipReferenceType referenceType, String referenceId, String userId) {
try {
LOGGER.debug("Delete member {} for {} {}", userId, referenceType, referenceId);
if (!MembershipReferenceType.GROUP.equals(referenceType)) {
RoleScope roleScope = getScopeByMembershipReferenceType(referenceType);
RoleEntity roleEntity = this.getRole(referenceType, referenceId, userId, roleScope);
if (roleEntity != null && PRIMARY_OWNER.name().equals(roleEntity.getName())) {
throw new SinglePrimaryOwnerException(referenceType.equals(API) ? RoleScope.API : RoleScope.APPLICATION);
}
}
Membership membership = new Membership(userId, referenceId, referenceType);
membershipRepository.delete(membership);
createAuditLog(MEMBERSHIP_DELETED, new Date(), membership, null);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to delete member {} for {} {}", userId, referenceType, referenceId, ex);
throw new TechnicalManagementException("An error occurs while trying to delete member " + userId + " for " + referenceType + " " + referenceId, ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class MetadataServiceImpl method update.
@Override
public MetadataEntity update(final UpdateMetadataEntity metadataEntity) {
try {
// First we prevent the duplicate metadata name
final Optional<Metadata> optionalMetadata = metadataRepository.findByReferenceType(MetadataReferenceType.DEFAULT).stream().filter(metadata -> !metadataEntity.getKey().equals(metadata.getKey()) && metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
if (optionalMetadata.isPresent()) {
throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
}
checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
final Metadata metadata = convert(metadataEntity);
final Date now = new Date();
metadata.setUpdatedAt(now);
metadataRepository.update(metadata);
// Audit
auditService.createPortalAuditLog(Collections.singletonMap(METADATA, metadata.getKey()), METADATA_UPDATED, metadata.getCreatedAt(), null, metadata);
return convert(metadata);
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to update metadata {}", metadataEntity.getName(), ex);
throw new TechnicalManagementException("An error occurred while trying to update metadata " + metadataEntity.getName(), ex);
}
}
Aggregations