Search in sources :

Example 1 with ApplicationEntity

use of io.gravitee.rest.api.model.ApplicationEntity in project gravitee-management-rest-api by gravitee-io.

the class PermissionsFilter method filter.

protected void filter(Permissions permissions, ContainerRequestContext requestContext) {
    if (permissions != null && permissions.value().length > 0) {
        Principal principal = securityContext.getUserPrincipal();
        if (principal != null) {
            String username = principal.getName();
            for (Permission permission : permissions.value()) {
                Map<String, char[]> memberPermissions;
                switch(permission.value().getScope()) {
                    case ORGANIZATION:
                        memberPermissions = membershipService.getUserMemberPermissions(MembershipReferenceType.ORGANIZATION, GraviteeContext.getCurrentOrganization(), username);
                        if (roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls())) {
                            return;
                        }
                        break;
                    case ENVIRONMENT:
                        memberPermissions = membershipService.getUserMemberPermissions(MembershipReferenceType.ENVIRONMENT, GraviteeContext.getCurrentEnvironment(), username);
                        if (roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls())) {
                            return;
                        }
                        break;
                    case APPLICATION:
                        ApplicationEntity application = getApplication(requestContext);
                        memberPermissions = membershipService.getUserMemberPermissions(application, username);
                        if (roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls())) {
                            return;
                        }
                        break;
                    case API:
                        ApiEntity api = getApi(requestContext);
                        memberPermissions = membershipService.getUserMemberPermissions(api, username);
                        if (roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls())) {
                            return;
                        }
                        break;
                    case GROUP:
                        GroupEntity group = getGroup(requestContext);
                        memberPermissions = membershipService.getUserMemberPermissions(group, username);
                        if (roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls())) {
                            return;
                        }
                        break;
                    default:
                        sendSecurityError();
                }
            }
        }
        sendSecurityError();
    }
}
Also used : ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) GroupEntity(io.gravitee.rest.api.model.GroupEntity) Permission(io.gravitee.rest.api.management.rest.security.Permission) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Principal(java.security.Principal)

Example 2 with ApplicationEntity

use of io.gravitee.rest.api.model.ApplicationEntity in project gravitee-management-rest-api by gravitee-io.

the class ApplicationResourceAdminTest method shouldRestoreApplication.

@Test
public void shouldRestoreApplication() {
    reset(applicationService);
    ApplicationEntity restored = new ApplicationEntity();
    restored.setId("my-beautiful-application");
    doReturn(restored).when(applicationService).restore(any());
    final Response response = envTarget("_restore").request().post(null);
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    assertEquals(response.readEntity(ApplicationEntity.class).getId(), "my-beautiful-application");
}
Also used : Response(javax.ws.rs.core.Response) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) Test(org.junit.Test)

Example 3 with ApplicationEntity

use of io.gravitee.rest.api.model.ApplicationEntity in project gravitee-management-rest-api by gravitee-io.

the class ApplicationMapper method convert.

public Application convert(ApplicationEntity applicationEntity, UriInfo uriInfo) {
    final Application application = new Application();
    application.setApplicationType(applicationEntity.getType());
    application.setCreatedAt(applicationEntity.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
    application.setDescription(applicationEntity.getDescription());
    Set<String> groupEntities = applicationEntity.getGroups();
    if (groupEntities != null && !groupEntities.isEmpty()) {
        List<Group> groups = groupEntities.stream().map(groupService::findById).map(groupEntity -> new Group().id(groupEntity.getId()).name(groupEntity.getName())).collect(Collectors.toList());
        application.setGroups(groups);
    }
    application.setId(applicationEntity.getId());
    application.setName(applicationEntity.getName());
    UserEntity primaryOwnerUserEntity = userService.findById(applicationEntity.getPrimaryOwner().getId());
    User owner = userMapper.convert(primaryOwnerUserEntity);
    owner.setLinks(userMapper.computeUserLinks(usersURL(uriInfo.getBaseUriBuilder(), primaryOwnerUserEntity.getId()), primaryOwnerUserEntity.getUpdatedAt()));
    application.setOwner(owner);
    application.setUpdatedAt(applicationEntity.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
    application.setPicture(applicationEntity.getPicture());
    application.setBackground(applicationEntity.getBackground());
    final ApplicationSettings applicationEntitySettings = applicationEntity.getSettings();
    if (applicationEntitySettings != null) {
        io.gravitee.rest.api.portal.rest.model.ApplicationSettings appSettings = new io.gravitee.rest.api.portal.rest.model.ApplicationSettings();
        final SimpleApplicationSettings simpleAppEntitySettings = applicationEntitySettings.getApp();
        if (simpleAppEntitySettings != null) {
            appSettings.app(new io.gravitee.rest.api.portal.rest.model.SimpleApplicationSettings().clientId(simpleAppEntitySettings.getClientId()).type(simpleAppEntitySettings.getType()));
            application.setHasClientId(simpleAppEntitySettings.getClientId() != null);
        } else {
            final OAuthClientSettings oAuthClientEntitySettings = applicationEntitySettings.getoAuthClient();
            appSettings.oauth(new io.gravitee.rest.api.portal.rest.model.OAuthClientSettings().applicationType(oAuthClientEntitySettings.getApplicationType()).clientId(oAuthClientEntitySettings.getClientId()).clientSecret(oAuthClientEntitySettings.getClientSecret()).clientUri(oAuthClientEntitySettings.getClientUri()).logoUri(oAuthClientEntitySettings.getLogoUri()).grantTypes(oAuthClientEntitySettings.getGrantTypes()).redirectUris(oAuthClientEntitySettings.getRedirectUris()).responseTypes(oAuthClientEntitySettings.getResponseTypes()).renewClientSecretSupported(oAuthClientEntitySettings.isRenewClientSecretSupported()));
            application.setHasClientId(oAuthClientEntitySettings.getClientId() != null);
        }
        application.setSettings(appSettings);
    }
    return application;
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) OAuthClientSettings(io.gravitee.rest.api.model.application.OAuthClientSettings) Group(io.gravitee.rest.api.portal.rest.model.Group) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) GroupService(io.gravitee.rest.api.service.GroupService) Set(java.util.Set) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) User(io.gravitee.rest.api.portal.rest.model.User) Collectors(java.util.stream.Collectors) Application(io.gravitee.rest.api.portal.rest.model.Application) List(java.util.List) Component(org.springframework.stereotype.Component) OffsetDateTime(java.time.OffsetDateTime) UserService(io.gravitee.rest.api.service.UserService) ZoneOffset(java.time.ZoneOffset) UriInfo(javax.ws.rs.core.UriInfo) PortalApiLinkHelper.usersURL(io.gravitee.rest.api.portal.rest.utils.PortalApiLinkHelper.usersURL) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) ApplicationLinks(io.gravitee.rest.api.portal.rest.model.ApplicationLinks) Group(io.gravitee.rest.api.portal.rest.model.Group) User(io.gravitee.rest.api.portal.rest.model.User) UserEntity(io.gravitee.rest.api.model.UserEntity) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) OAuthClientSettings(io.gravitee.rest.api.model.application.OAuthClientSettings) Application(io.gravitee.rest.api.portal.rest.model.Application)

Example 4 with ApplicationEntity

use of io.gravitee.rest.api.model.ApplicationEntity in project gravitee-management-rest-api by gravitee-io.

the class PermissionsFilter method hasPermission.

protected boolean hasPermission(ContainerRequestContext requestContext, String username, Permission permission) {
    Map<String, char[]> memberPermissions;
    switch(permission.value().getScope()) {
        case ORGANIZATION:
            memberPermissions = membershipService.getUserMemberPermissions(MembershipReferenceType.ORGANIZATION, GraviteeContext.getCurrentOrganization(), username);
            return roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls());
        case ENVIRONMENT:
            memberPermissions = membershipService.getUserMemberPermissions(MembershipReferenceType.ENVIRONMENT, GraviteeContext.getCurrentEnvironment(), username);
            return roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls());
        case APPLICATION:
            ApplicationEntity application = getApplication(requestContext);
            memberPermissions = membershipService.getUserMemberPermissions(application, username);
            return roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls());
        case API:
            ApiEntity api = getApi(requestContext);
            memberPermissions = membershipService.getUserMemberPermissions(api, username);
            return roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls());
        default:
            sendSecurityError();
    }
    return false;
}
Also used : ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity)

Example 5 with ApplicationEntity

use of io.gravitee.rest.api.model.ApplicationEntity in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceImpl method create.

@Override
public AlertTriggerEntity create(String applicationId, NewAlertTriggerEntity alert) {
    final ApplicationEntity application = applicationService.findById(applicationId);
    alert.setName(generateAlertName(application, alert));
    alert.setReferenceType(AlertReferenceType.APPLICATION);
    alert.setReferenceId(application.getId());
    alert.setSource("REQUEST");
    alert.setSeverity(Trigger.Severity.INFO);
    alert.setDampening(Dampening.strictCount(1));
    Filter filter = StringCondition.equals("application", applicationId).build();
    alert.setFilters(singletonList(filter));
    final List<String> recipients = getNotificationRecipients(application.getId(), application.getGroups());
    if (!CollectionUtils.isEmpty(recipients)) {
        alert.setNotifications(createNotification(alert.getType(), recipients));
    }
    return alertService.create(alert);
}
Also used : Filter(io.gravitee.alert.api.condition.Filter) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity)

Aggregations

ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)38 Test (org.junit.Test)19 Response (javax.ws.rs.core.Response)8 ApplicationSettings (io.gravitee.rest.api.model.application.ApplicationSettings)7 SimpleApplicationSettings (io.gravitee.rest.api.model.application.SimpleApplicationSettings)7 NewApplicationEntity (io.gravitee.rest.api.model.NewApplicationEntity)6 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)6 UserEntity (io.gravitee.rest.api.model.UserEntity)5 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)5 Application (io.gravitee.rest.api.portal.rest.model.Application)5 UpdateApplicationEntity (io.gravitee.rest.api.model.UpdateApplicationEntity)4 Before (org.junit.Before)4 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)3 NewAlertTriggerEntity (io.gravitee.rest.api.model.alert.NewAlertTriggerEntity)3 OAuthClientSettings (io.gravitee.rest.api.model.application.OAuthClientSettings)3 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)3 UserService (io.gravitee.rest.api.service.UserService)3 ApplicationNotFoundException (io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException)3 ForbiddenAccessException (io.gravitee.rest.api.service.exceptions.ForbiddenAccessException)3 MediaType (io.gravitee.common.http.MediaType)2