Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
feat(groups): restrict pages and documentation to a set of groups
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasGeraud authored and brasseld committed Oct 10, 2017
1 parent bac1a5c commit c088a55
Show file tree
Hide file tree
Showing 22 changed files with 1,007 additions and 365 deletions.
Expand Up @@ -15,8 +15,11 @@
*/
package io.gravitee.management.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
* @author Titouan COMPIEGNE (titouan.compiegne at graviteesource.com)
Expand Down Expand Up @@ -45,6 +48,9 @@ public class NewPageEntity {

private boolean homepage;

@JsonProperty("excluded_groups")
private List<String> excludedGroups;

public String getName() {
return name;
}
Expand Down Expand Up @@ -117,6 +123,14 @@ public void setHomepage(boolean homepage) {
this.homepage = homepage;
}

public List<String> getExcludedGroups() {
return excludedGroups;
}

public void setExcludedGroups(List<String> excludedGroups) {
this.excludedGroups = excludedGroups;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Page{");
Expand Down
Expand Up @@ -54,6 +54,9 @@ public class NewPlanEntity {

private List<String> characteristics;

@JsonProperty("excluded_groups")
private List<String> excludedGroups;

public String getName() {
return name;
}
Expand Down Expand Up @@ -126,6 +129,14 @@ public void setSecurity(PlanSecurityType security) {
this.security = security;
}

public List<String> getExcludedGroups() {
return excludedGroups;
}

public void setExcludedGroups(List<String> excludedGroups) {
this.excludedGroups = excludedGroups;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Expand Up @@ -15,7 +15,10 @@
*/
package io.gravitee.management.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -47,6 +50,9 @@ public class PageEntity {

private boolean homepage;

@JsonProperty("excluded_groups")
private List<String> excludedGroups;

public String getId() {
return id;
}
Expand Down Expand Up @@ -143,6 +149,14 @@ public void setHomepage(boolean homepage) {
this.homepage = homepage;
}

public List<String> getExcludedGroups() {
return excludedGroups;
}

public void setExcludedGroups(List<String> excludedGroups) {
this.excludedGroups = excludedGroups;
}

public boolean equals(Object o) {
if (this == o) {
return true;
Expand All @@ -169,6 +183,7 @@ public String toString() {
sb.append(", lastContributor='").append(lastContributor).append('\'');
sb.append(", published='").append(published).append('\'');
sb.append(", lastModificationDate='").append(lastModificationDate).append('\'');
sb.append(", excludedGroups='").append(excludedGroups).append('\'');
sb.append('}');
return sb.toString();
}
Expand Down
Expand Up @@ -15,6 +15,9 @@
*/
package io.gravitee.management.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;
import java.util.Objects;

/**
Expand All @@ -40,6 +43,9 @@ public class PageListItem {

private boolean homepage;

@JsonProperty("excluded_groups")
private List<String> excludedGroups;

public String getId() {
return id;
}
Expand Down Expand Up @@ -112,6 +118,14 @@ public void setHomepage(boolean homepage) {
this.homepage = homepage;
}

public List<String> getExcludedGroups() {
return excludedGroups;
}

public void setExcludedGroups(List<String> excludedGroups) {
this.excludedGroups = excludedGroups;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Expand Up @@ -76,6 +76,9 @@ public class PlanEntity {

private List<String> characteristics;

@JsonProperty("excluded_groups")
private List<String> excludedGroups;

public String getId() {
return id;
}
Expand Down Expand Up @@ -196,6 +199,14 @@ public void setSecurity(PlanSecurityType security) {
this.security = security;
}

public List<String> getExcludedGroups() {
return excludedGroups;
}

public void setExcludedGroups(List<String> excludedGroups) {
this.excludedGroups = excludedGroups;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Expand Up @@ -15,8 +15,11 @@
*/
package io.gravitee.management.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
* @author Titouan COMPIEGNE
Expand All @@ -41,6 +44,9 @@ public class UpdatePageEntity {

private boolean homepage;

@JsonProperty("excluded_groups")
private List<String> excludedGroups;

public String getName() {
return name;
}
Expand Down Expand Up @@ -104,4 +110,12 @@ public boolean isHomepage() {
public void setHomepage(boolean homepage) {
this.homepage = homepage;
}

public List<String> getExcludedGroups() {
return excludedGroups;
}

public void setExcludedGroups(List<String> excludedGroups) {
this.excludedGroups = excludedGroups;
}
}
Expand Up @@ -48,6 +48,9 @@ public class UpdatePlanEntity {
@NotNull
private int order;

@JsonProperty("excluded_groups")
private List<String> excludedGroups;

public String getName() {
return name;
}
Expand Down Expand Up @@ -104,6 +107,14 @@ public void setOrder(int order) {
this.order = order;
}

public List<String> getExcludedGroups() {
return excludedGroups;
}

public void setExcludedGroups(List<String> excludedGroups) {
this.excludedGroups = excludedGroups;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Expand Up @@ -22,6 +22,7 @@
import io.gravitee.management.rest.security.Permission;
import io.gravitee.management.rest.security.Permissions;
import io.gravitee.management.service.ApiService;
import io.gravitee.management.service.GroupService;
import io.gravitee.management.service.PageService;
import io.gravitee.management.service.exceptions.ForbiddenAccessException;
import io.gravitee.management.service.exceptions.UnauthorizedAccessException;
Expand Down Expand Up @@ -50,6 +51,9 @@ public class ApiPagesResource extends AbstractResource {
@Inject
private PageService pageService;

@Inject
private GroupService groupService;

@GET
@Path("/{page}")
@Produces(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -106,7 +110,9 @@ public List<PageListItem> listPages(
final List<PageListItem> pages = pageService.findApiPagesByApiAndHomepage(api, homepage);

return pages.stream()
.filter(page -> isDisplayable(apiEntity, page.isPublished(), getAuthenticatedUsernameOrNull()))
.filter(page ->
isDisplayable(apiEntity, page.isPublished(), getAuthenticatedUsernameOrNull()) &&
groupService.isUserAuthorizedToAccess(apiEntity, page.getExcludedGroups(), getAuthenticatedUsernameOrNull()))
.collect(Collectors.toList());
}
throw new ForbiddenAccessException();
Expand Down
Expand Up @@ -23,6 +23,7 @@
import io.gravitee.management.rest.security.Permission;
import io.gravitee.management.rest.security.Permissions;
import io.gravitee.management.service.ApiService;
import io.gravitee.management.service.GroupService;
import io.gravitee.management.service.PlanService;
import io.gravitee.management.service.exceptions.ForbiddenAccessException;
import io.swagger.annotations.*;
Expand Down Expand Up @@ -53,6 +54,9 @@ public class ApiPlansResource extends AbstractResource {
@Inject
private ApiService apiService;

@Inject
private GroupService groupService;

@Context
private ResourceContext resourceContext;

Expand All @@ -68,11 +72,14 @@ public List<PlanEntity> listPlans(
@PathParam("api") String api,
@QueryParam("status") @DefaultValue("published") PlanStatusParam status) {

if (Visibility.PUBLIC.equals(apiService.findById(api).getVisibility())
ApiEntity apiEntity = apiService.findById(api);

if (Visibility.PUBLIC.equals(apiEntity.getVisibility())
|| hasPermission(RolePermission.API_PLAN, api, RolePermissionAction.READ)) {

return planService.findByApi(api).stream()
.filter(plan -> status.getStatuses().contains(plan.getStatus()))
.filter(plan -> status.getStatuses().contains(plan.getStatus())
&& groupService.isUserAuthorizedToAccess(apiEntity, plan.getExcludedGroups(), getAuthenticatedUsernameOrNull()))
.sorted(Comparator.comparingInt(PlanEntity::getOrder))
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -43,4 +43,5 @@ public interface GroupService {

void delete(String groupId);

boolean isUserAuthorizedToAccess(ApiEntity api, List<String> excludedGroups, String username);
}
Expand Up @@ -15,9 +15,13 @@
*/
package io.gravitee.management.service;

import io.gravitee.management.model.ApiEntity;
import io.gravitee.management.model.NewPlanEntity;
import io.gravitee.management.model.PlanEntity;
import io.gravitee.management.model.UpdatePlanEntity;
import io.gravitee.repository.management.model.Api;
import io.gravitee.repository.management.model.Plan;
import io.gravitee.repository.management.model.User;

import java.util.Set;

Expand Down
Expand Up @@ -350,20 +350,20 @@ public ApiEntity update(String apiId, UpdateApiEntity updateApiEntity) {
}

@Override
public void delete(String apiName) {
public void delete(String apiId) {
try {
LOGGER.debug("Delete API {}", apiName);
LOGGER.debug("Delete API {}", apiId);

Optional<Api> optApi = apiRepository.findById(apiName);
Optional<Api> optApi = apiRepository.findById(apiId);
if (! optApi.isPresent()) {
throw new ApiNotFoundException(apiName);
throw new ApiNotFoundException(apiId);
}

if (optApi.get().getLifecycleState() == LifecycleState.STARTED) {
throw new ApiRunningStateException(apiName);
throw new ApiRunningStateException(apiId);
} else {
// Delete plans
Set<PlanEntity> plans = planService.findByApi(apiName);
Set<PlanEntity> plans = planService.findByApi(apiId);
Set<String> plansNotClosed = plans.stream()
.filter(plan -> plan.getStatus() == PlanStatus.PUBLISHED)
.map(PlanEntity::getName)
Expand All @@ -377,15 +377,15 @@ public void delete(String apiName) {
plans.stream().forEach(plan -> planService.delete(plan.getId()));

// Delete events
eventService.findByApi(apiName)
eventService.findByApi(apiId)
.forEach(event -> eventService.delete(event.getId()));

// Delete API
apiRepository.delete(apiName);
apiRepository.delete(apiId);
}
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to delete API {}", apiName, ex);
throw new TechnicalManagementException("An error occurs while trying to delete API " + apiName, ex);
LOGGER.error("An error occurs while trying to delete API {}", apiId, ex);
throw new TechnicalManagementException("An error occurs while trying to delete API " + apiId, ex);
}
}

Expand Down

0 comments on commit c088a55

Please sign in to comment.