use of io.gravitee.am.common.event.Type in project gravitee-access-management by gravitee-io.
the class ScopeServiceImpl method delete.
@Override
public Completable delete(String scopeId, boolean force, User principal) {
LOGGER.debug("Delete scope {}", scopeId);
return scopeRepository.findById(scopeId).switchIfEmpty(Maybe.error(new ScopeNotFoundException(scopeId))).flatMapSingle(scope -> {
if (scope.isSystem() && !force) {
throw new SystemScopeDeleteException(scopeId);
}
return Single.just(scope);
}).flatMapCompletable(scope -> Completable.fromSingle(// 1_ Remove permissions from role
roleService.findByDomain(scope.getDomain()).flatMapObservable(roles -> Observable.fromIterable(roles.stream().filter(role -> role.getOauthScopes() != null && role.getOauthScopes().contains(scope.getKey())).collect(Collectors.toList()))).flatMapSingle(role -> {
role.getOauthScopes().remove(scope.getKey());
UpdateRole updatedRole = new UpdateRole();
updatedRole.setName(role.getName());
updatedRole.setDescription(role.getDescription());
updatedRole.setPermissions(role.getOauthScopes());
// Save role
return roleService.update(scope.getDomain(), role.getId(), updatedRole);
}).toList()).andThen(// 2_ Remove scopes from application
applicationService.findByDomain(scope.getDomain()).flatMapObservable(applications -> Observable.fromIterable(applications.stream().filter(application -> {
if (application.getSettings() == null) {
return false;
}
if (application.getSettings().getOauth() == null) {
return false;
}
ApplicationOAuthSettings oAuthSettings = application.getSettings().getOauth();
return oAuthSettings.getScopeSettings() != null && !oAuthSettings.getScopeSettings().stream().filter(s -> s.getScope().equals(scope.getKey())).findFirst().isEmpty();
}).collect(Collectors.toList()))).flatMapSingle(application -> {
// Remove scope from application
final List<ApplicationScopeSettings> cleanScopes = application.getSettings().getOauth().getScopeSettings().stream().filter(s -> !s.getScope().equals(scope.getKey())).collect(Collectors.toList());
application.getSettings().getOauth().setScopeSettings(cleanScopes);
// Then update
return applicationService.update(application);
}).toList()).toCompletable().andThen(scopeApprovalRepository.deleteByDomainAndScopeKey(scope.getDomain(), scope.getKey())).andThen(scopeRepository.delete(scopeId)).andThen(Completable.fromSingle(eventService.create(new Event(Type.SCOPE, new Payload(scope.getId(), ReferenceType.DOMAIN, scope.getDomain(), Action.DELETE))))).doOnComplete(() -> auditService.report(AuditBuilder.builder(ScopeAuditBuilder.class).principal(principal).type(EventType.SCOPE_DELETED).scope(scope))).doOnError(throwable -> auditService.report(AuditBuilder.builder(ScopeAuditBuilder.class).principal(principal).type(EventType.SCOPE_DELETED).throwable(throwable)))).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Completable.error(ex);
}
LOGGER.error("An error occurs while trying to delete scope: {}", scopeId, ex);
return Completable.error(new TechnicalManagementException(String.format("An error occurs while trying to delete scope: %s", scopeId), ex));
});
}
use of io.gravitee.am.common.event.Type in project gravitee-access-management by gravitee-io.
the class RoleServiceImpl method upsert.
private Completable upsert(Role role) {
return roleRepository.findByNameAndAssignableType(role.getReferenceType(), role.getReferenceId(), role.getName(), role.getAssignableType()).map(Optional::ofNullable).defaultIfEmpty(Optional.empty()).flatMapCompletable(optRole -> {
if (!optRole.isPresent()) {
LOGGER.debug("Create a system role {}", role.getAssignableType() + ":" + role.getName());
role.setCreatedAt(new Date());
role.setUpdatedAt(role.getCreatedAt());
return roleRepository.create(role).flatMap(role1 -> {
Event event = new Event(Type.ROLE, new Payload(role1.getId(), role1.getReferenceType(), role1.getReferenceId(), Action.CREATE));
return eventService.create(event).flatMap(__ -> Single.just(role1));
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
}
LOGGER.error("An error occurs while trying to create a system role {}", role.getAssignableType() + ":" + role.getName(), ex);
return Single.error(new TechnicalManagementException("An error occurs while trying to create a role", ex));
}).doOnSuccess(role1 -> auditService.report(AuditBuilder.builder(RoleAuditBuilder.class).type(EventType.ROLE_CREATED).role(role1))).doOnError(throwable -> auditService.report(AuditBuilder.builder(RoleAuditBuilder.class).type(EventType.ROLE_CREATED).throwable(throwable))).toCompletable();
} else {
// check if permission set has changed
Role currentRole = optRole.get();
if (permissionsAreEquals(currentRole, role)) {
return Completable.complete();
}
LOGGER.debug("Update a system role {}", role.getAssignableType() + ":" + role.getName());
// update the role
role.setId(currentRole.getId());
role.setPermissionAcls(role.getPermissionAcls());
role.setUpdatedAt(new Date());
return roleRepository.update(role).flatMap(role1 -> {
Event event = new Event(Type.ROLE, new Payload(role1.getId(), role1.getReferenceType(), role1.getReferenceId(), Action.UPDATE));
return eventService.create(event).flatMap(__ -> Single.just(role1));
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
}
LOGGER.error("An error occurs while trying to update a system role {}", role.getAssignableType() + ":" + role.getName(), ex);
return Single.error(new TechnicalManagementException("An error occurs while trying to update a role", ex));
}).doOnSuccess(role1 -> auditService.report(AuditBuilder.builder(RoleAuditBuilder.class).type(EventType.ROLE_UPDATED).oldValue(currentRole).role(role1))).doOnError(throwable -> auditService.report(AuditBuilder.builder(RoleAuditBuilder.class).type(EventType.ROLE_UPDATED).throwable(throwable))).toCompletable();
}
});
}
use of io.gravitee.am.common.event.Type in project gravitee-access-management by gravitee-io.
the class EmailTemplateServiceImpl method create0.
private Single<Email> create0(ReferenceType referenceType, String referenceId, String client, NewEmail newEmail, User principal) {
String emailId = RandomString.generate();
// check if email is unique
return checkEmailUniqueness(referenceType, referenceId, client, newEmail.getTemplate().template()).flatMap(irrelevant -> {
Email email = new Email();
email.setId(emailId);
email.setReferenceType(referenceType);
email.setReferenceId(referenceId);
email.setClient(client);
email.setEnabled(newEmail.isEnabled());
email.setTemplate(newEmail.getTemplate().template());
email.setFrom(newEmail.getFrom());
email.setFromName(newEmail.getFromName());
email.setSubject(newEmail.getSubject());
email.setContent(newEmail.getContent());
email.setExpiresAfter(newEmail.getExpiresAfter());
email.setCreatedAt(new Date());
email.setUpdatedAt(email.getCreatedAt());
return emailRepository.create(email);
}).flatMap(email -> {
// create event for sync process
Event event = new Event(Type.EMAIL, new Payload(email.getId(), email.getReferenceType(), email.getReferenceId(), Action.CREATE));
return eventService.create(event).flatMap(__ -> Single.just(email));
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
}
LOGGER.error("An error occurs while trying to create a email", ex);
return Single.error(new TechnicalManagementException("An error occurs while trying to create a email", ex));
}).doOnSuccess(email -> auditService.report(AuditBuilder.builder(EmailTemplateAuditBuilder.class).principal(principal).type(EventType.EMAIL_TEMPLATE_CREATED).email(email))).doOnError(throwable -> auditService.report(AuditBuilder.builder(EmailTemplateAuditBuilder.class).principal(principal).type(EventType.EMAIL_TEMPLATE_CREATED).throwable(throwable)));
}
use of io.gravitee.am.common.event.Type in project gravitee-access-management by gravitee-io.
the class GroupServiceImpl method create.
@Override
public Single<Group> create(ReferenceType referenceType, String referenceId, NewGroup newGroup, io.gravitee.am.identityprovider.api.User principal) {
LOGGER.debug("Create a new group {} for {} {}", newGroup.getName(), referenceType, referenceId);
return findByName(referenceType, referenceId, newGroup.getName()).isEmpty().map(isEmpty -> {
if (!isEmpty) {
throw new GroupAlreadyExistsException(newGroup.getName());
} else {
String groupId = RandomString.generate();
Group group = new Group();
group.setId(groupId);
group.setReferenceType(referenceType);
group.setReferenceId(referenceId);
group.setName(newGroup.getName());
group.setDescription(newGroup.getDescription());
group.setMembers(newGroup.getMembers());
group.setCreatedAt(new Date());
group.setUpdatedAt(group.getCreatedAt());
return group;
}
}).flatMap(this::setMembers).flatMap(group -> groupRepository.create(group)).flatMap(group -> {
Event event = new Event(Type.GROUP, new Payload(group.getId(), group.getReferenceType(), group.getReferenceId(), Action.CREATE));
return eventService.create(event).flatMap(__ -> Single.just(group));
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
} else {
LOGGER.error("An error occurs while trying to create a group", ex);
return Single.error(new TechnicalManagementException("An error occurs while trying to create a group", ex));
}
}).doOnSuccess(group -> auditService.report(AuditBuilder.builder(GroupAuditBuilder.class).principal(principal).type(EventType.GROUP_CREATED).group(group))).doOnError(throwable -> auditService.report(AuditBuilder.builder(GroupAuditBuilder.class).principal(principal).type(EventType.GROUP_CREATED).throwable(throwable)));
}
use of io.gravitee.am.common.event.Type in project gravitee-access-management by gravitee-io.
the class DeviceIdentifierServiceImpl method create.
@Override
public Single<DeviceIdentifier> create(String domain, NewDeviceIdentifier newDeviceIdentifier, User principal) {
LOGGER.debug("Create a new device identifier {} for domain {}", newDeviceIdentifier, domain);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(newDeviceIdentifier.getId() == null ? RandomString.generate() : newDeviceIdentifier.getId());
deviceIdentifier.setReferenceId(domain);
deviceIdentifier.setReferenceType(ReferenceType.DOMAIN);
deviceIdentifier.setName(newDeviceIdentifier.getName());
deviceIdentifier.setType(newDeviceIdentifier.getType());
deviceIdentifier.setConfiguration(newDeviceIdentifier.getConfiguration());
deviceIdentifier.setCreatedAt(new Date());
deviceIdentifier.setUpdatedAt(deviceIdentifier.getCreatedAt());
return deviceIdentifierRepository.create(deviceIdentifier).flatMap(rd -> {
// create event for sync process
Event event = new Event(Type.DEVICE_IDENTIFIER, new Payload(rd.getId(), rd.getReferenceType(), rd.getReferenceId(), Action.CREATE));
return eventService.create(event).flatMap(__ -> Single.just(rd));
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
}
LOGGER.error("An error occurs while trying to create a device identifier", ex);
return Single.error(new TechnicalManagementException("An error occurs while trying to create a device identifier", ex));
}).doOnSuccess(detection -> auditService.report(AuditBuilder.builder(DeviceIdentifierAuditBuilder.class).principal(principal).type(EventType.DEVICE_IDENTIFIER_CREATED).deviceIdentifier(detection))).doOnError(throwable -> auditService.report(AuditBuilder.builder(DeviceIdentifierAuditBuilder.class).principal(principal).type(EventType.DEVICE_IDENTIFIER_CREATED).throwable(throwable)));
}
Aggregations