Search in sources :

Example 1 with SubjectAclService

use of org.obiba.mica.security.service.SubjectAclService in project mica2 by obiba.

the class SessionInterceptor method populateUserEntries.

public static void populateUserEntries(ModelAndView modelAndView, UserProfileService userProfileService, VariableSetService variableSetService, StudySetService studySetService, NetworkSetService networkSetService, SubjectAclService subjectAclService) {
    Subject subject = SecurityUtils.getSubject();
    if (subject.isAuthenticated()) {
        String username = subject.getPrincipal().toString();
        try {
            Map<String, Object> params = userProfileService.getProfileMap(username, true);
            List<String> roles = Lists.newArrayList(Roles.MICA_ADMIN, Roles.MICA_REVIEWER, Roles.MICA_EDITOR, Roles.MICA_DAO, Roles.MICA_USER);
            boolean[] result = subject.hasRoles(roles);
            for (int i = result.length - 1; i >= 0; i--) {
                if (!result[i])
                    roles.remove(i);
            }
            params.put("roles", roles);
            params.put("hasPermissionOnAnyDraftDocument", subjectAclService.findBySubject(subject.getPrincipal().toString(), SubjectAcl.Type.USER).stream().anyMatch(acl -> Arrays.stream(ALL_DRAFT_RESOURCES).anyMatch(res -> res.equals(acl.getResource()))));
            params.put("variablesCart", new Cart(variableSetService.getCartCurrentUser()));
            params.put("variablesLists", variableSetService.getAllCurrentUser().stream().filter(DocumentSet::hasName).collect(Collectors.toList()));
            params.put("studiesCart", new Cart(studySetService.getCartCurrentUser()));
            params.put("networksCart", new Cart(networkSetService.getCartCurrentUser()));
            modelAndView.getModel().put("user", params);
        } catch (Exception e) {
            log.warn("Cannot retrieve profile of user {}", username, e);
        }
    }
}
Also used : Arrays(java.util.Arrays) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Logger(org.slf4j.Logger) VariableSetService(org.obiba.mica.dataset.service.VariableSetService) Roles(org.obiba.mica.security.Roles) LoggerFactory(org.slf4j.LoggerFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) UserProfileService(org.obiba.mica.user.UserProfileService) Collectors(java.util.stream.Collectors) HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter) NetworkSetService(org.obiba.mica.network.service.NetworkSetService) SubjectAcl(org.obiba.mica.security.domain.SubjectAcl) Inject(javax.inject.Inject) ModelAndView(org.springframework.web.servlet.ModelAndView) DocumentSet(org.obiba.mica.core.domain.DocumentSet) Component(org.springframework.stereotype.Component) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) Lists(com.google.common.collect.Lists) Subject(org.apache.shiro.subject.Subject) Map(java.util.Map) Cart(org.obiba.mica.web.controller.domain.Cart) SecurityUtils(org.apache.shiro.SecurityUtils) StudySetService(org.obiba.mica.study.service.StudySetService) DocumentSet(org.obiba.mica.core.domain.DocumentSet) Subject(org.apache.shiro.subject.Subject) Cart(org.obiba.mica.web.controller.domain.Cart)

Example 2 with SubjectAclService

use of org.obiba.mica.security.service.SubjectAclService in project mica2 by obiba.

the class DraftCollectedDatasetsResource method list.

/**
 * Get all {@link org.obiba.mica.dataset.domain.StudyDataset}, optionally filtered by study.
 *
 * @param studyId can be null, in which case all datasets are returned
 * @return
 */
@GET
@Path("/collected-datasets")
@Timed
public List<Mica.DatasetDto> list(@QueryParam("study") String studyId, @QueryParam("query") String query, @QueryParam("from") @DefaultValue("0") Integer from, @QueryParam("limit") Integer limit, @QueryParam("sort") @DefaultValue("id") String sort, @QueryParam("order") @DefaultValue("asc") String order, @QueryParam("filter") @DefaultValue("ALL") String filter, @Context HttpServletResponse response) {
    long totalCount;
    EntityStateFilter entityStateFilter = EntityStateFilter.valueOf(filter);
    List<String> filteredIds = datasetService.getIdsByStateFilter(entityStateFilter);
    Searcher.IdFilter accessibleIdFilter = AccessibleIdFilterBuilder.newBuilder().aclService(subjectAclService).resources(Lists.newArrayList("/draft/collected-dataset")).ids(filteredIds).build();
    if (limit == null)
        limit = MAX_LIMIT;
    if (limit < 0)
        throw new IllegalArgumentException("limit cannot be negative");
    DocumentService.Documents<StudyDataset> datasets = draftCollectedDatasetService.find(from, limit, sort, order, studyId, query, null, null, accessibleIdFilter);
    totalCount = datasets.getTotal();
    response.addHeader("X-Total-Count", Long.toString(totalCount));
    return datasets.getList().stream().map(dataset -> dtos.asDto(dataset, true)).collect(toList());
}
Also used : EntityStateFilter(org.obiba.mica.core.domain.EntityStateFilter) Async(org.springframework.scheduling.annotation.Async) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) DraftCollectedDatasetService(org.obiba.mica.dataset.service.DraftCollectedDatasetService) Scope(org.springframework.context.annotation.Scope) Inject(javax.inject.Inject) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) QueryParam(javax.ws.rs.QueryParam) Lists(com.google.common.collect.Lists) Mica(org.obiba.mica.web.model.Mica) DefaultValue(javax.ws.rs.DefaultValue) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Dtos(org.obiba.mica.web.model.Dtos) Nullable(javax.annotation.Nullable) AccessibleIdFilterBuilder(org.obiba.mica.search.AccessibleIdFilterBuilder) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Searcher(org.obiba.mica.spi.search.Searcher) HttpServletResponse(javax.servlet.http.HttpServletResponse) CollectedDatasetService(org.obiba.mica.dataset.service.CollectedDatasetService) Dataset(org.obiba.mica.dataset.domain.Dataset) ApplicationContext(org.springframework.context.ApplicationContext) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) DocumentService(org.obiba.mica.core.service.DocumentService) PUT(javax.ws.rs.PUT) UriInfo(javax.ws.rs.core.UriInfo) EntityStateFilter(org.obiba.mica.core.domain.EntityStateFilter) Searcher(org.obiba.mica.spi.search.Searcher) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) DocumentService(org.obiba.mica.core.service.DocumentService) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 3 with SubjectAclService

use of org.obiba.mica.security.service.SubjectAclService in project mica2 by obiba.

the class StudyStatesResource method listCollectionStudyStates.

@GET
@Path("/study-states")
@Timed
public List<Mica.StudySummaryDto> listCollectionStudyStates(@QueryParam("query") String query, @QueryParam("from") @DefaultValue("0") Integer from, @QueryParam("limit") Integer limit, @QueryParam("sort") @DefaultValue("id") String sort, @QueryParam("order") @DefaultValue("asc") String order, @QueryParam("type") String type, @QueryParam("exclude") List<String> excludes, @QueryParam("filter") @DefaultValue("ALL") String filter, @Context HttpServletResponse response) {
    Stream<? extends EntityState> result;
    long totalCount;
    EntityStateFilter entityStateFilter = EntityStateFilter.valueOf(filter);
    List<String> filteredIds = Strings.isNullOrEmpty(type) ? studyService.getIdsByStateFilter(entityStateFilter) : getStudyServiceByType(type).getIdsByStateFilter(entityStateFilter);
    Searcher.IdFilter accessibleIdFilter = AccessibleIdFilterBuilder.newBuilder().aclService(subjectAclService).resources(getPermissionResources(type)).ids(filteredIds).build();
    String ids = excludes.stream().map(s -> "id:" + s).collect(Collectors.joining(" "));
    if (!Strings.isNullOrEmpty(ids)) {
        if (Strings.isNullOrEmpty(query))
            query = String.format("NOT(%s)", ids);
        else
            query += String.format(" AND NOT(%s)", ids);
    }
    if (limit == null)
        limit = MAX_LIMIT;
    if (limit < 0)
        throw new IllegalArgumentException("limit cannot be negative");
    DocumentService.Documents<Study> studyDocuments = draftStudyService.find(from, limit, sort, order, null, query, null, null, accessibleIdFilter);
    totalCount = studyDocuments.getTotal();
    response.addHeader("X-Total-Count", Long.toString(totalCount));
    return studyDocuments.getList().stream().map(study -> dtos.asDto(study, studyService.getEntityState(study.getId()))).collect(toList());
}
Also used : EntityStateFilter(org.obiba.mica.core.domain.EntityStateFilter) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) EntityState(org.obiba.mica.core.domain.EntityState) QueryParam(javax.ws.rs.QueryParam) Mica(org.obiba.mica.web.model.Mica) DefaultValue(javax.ws.rs.DefaultValue) Dtos(org.obiba.mica.web.model.Dtos) AccessibleIdFilterBuilder(org.obiba.mica.search.AccessibleIdFilterBuilder) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Context(javax.ws.rs.core.Context) Searcher(org.obiba.mica.spi.search.Searcher) HarmonizationStudyService(org.obiba.mica.study.service.HarmonizationStudyService) BaseStudy(org.obiba.mica.study.domain.BaseStudy) HttpServletResponse(javax.servlet.http.HttpServletResponse) StudyService(org.obiba.mica.study.service.StudyService) NotNull(javax.validation.constraints.NotNull) AbstractStudyService(org.obiba.mica.study.service.AbstractStudyService) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) Timed(com.codahale.metrics.annotation.Timed) IndividualStudyService(org.obiba.mica.study.service.IndividualStudyService) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Study(org.obiba.mica.study.domain.Study) Stream(java.util.stream.Stream) DocumentService(org.obiba.mica.core.service.DocumentService) DraftStudyService(org.obiba.mica.study.service.DraftStudyService) EntityStateFilter(org.obiba.mica.core.domain.EntityStateFilter) BaseStudy(org.obiba.mica.study.domain.BaseStudy) Study(org.obiba.mica.study.domain.Study) Searcher(org.obiba.mica.spi.search.Searcher) DocumentService(org.obiba.mica.core.service.DocumentService) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 4 with SubjectAclService

use of org.obiba.mica.security.service.SubjectAclService in project mica2 by obiba.

the class DraftProjectsResource method list.

@GET
@Path("/projects")
@Timed
public Mica.ProjectsDto list(@QueryParam("query") String query, @QueryParam("from") @DefaultValue("0") Integer from, @QueryParam("limit") Integer limit, @QueryParam("sort") @DefaultValue("id") String sort, @QueryParam("order") @DefaultValue("asc") String order, @QueryParam("filter") @DefaultValue("ALL") String filter, @Context HttpServletResponse response) {
    EntityStateFilter entityStateFilter = EntityStateFilter.valueOf(filter);
    List<String> filteredIds = projectService.getIdsByStateFilter(entityStateFilter);
    Searcher.IdFilter accessibleIdFilter = AccessibleIdFilterBuilder.newBuilder().aclService(subjectAclService).resources(Lists.newArrayList("/draft/project")).ids(filteredIds).build();
    if (limit == null)
        limit = MAX_LIMIT;
    if (limit < 0)
        throw new IllegalArgumentException("limit cannot be negative");
    DocumentService.Documents<Project> projectDocuments = draftProjectService.find(from, limit, sort, order, null, query, null, null, accessibleIdFilter);
    long totalCount = projectDocuments.getTotal();
    List<Mica.ProjectDto> result = projectDocuments.getList().stream().map(n -> dtos.asDto(n, true)).collect(toList());
    Mica.ProjectsDto.Builder builder = Mica.ProjectsDto.newBuilder();
    builder.setFrom(from).setLimit(limit).setTotal(Long.valueOf(totalCount).intValue());
    builder.addAllProjects(result);
    if (subjectAclService.isPermitted("/draft/project", "ADD")) {
        builder.addActions("ADD");
    }
    return builder.build();
}
Also used : PathParam(javax.ws.rs.PathParam) Project(org.obiba.mica.project.domain.Project) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) ProjectService(org.obiba.mica.project.service.ProjectService) AbstractGitPersistable(org.obiba.mica.core.domain.AbstractGitPersistable) DraftProjectService(org.obiba.mica.project.service.DraftProjectService) Scope(org.springframework.context.annotation.Scope) Inject(javax.inject.Inject) EventBus(com.google.common.eventbus.EventBus) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) QueryParam(javax.ws.rs.QueryParam) Lists(com.google.common.collect.Lists) Mica(org.obiba.mica.web.model.Mica) DefaultValue(javax.ws.rs.DefaultValue) Dtos(org.obiba.mica.web.model.Dtos) Nullable(javax.annotation.Nullable) AccessibleIdFilterBuilder(org.obiba.mica.search.AccessibleIdFilterBuilder) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Searcher(org.obiba.mica.spi.search.Searcher) HttpServletResponse(javax.servlet.http.HttpServletResponse) ApplicationContext(org.springframework.context.ApplicationContext) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) DocumentService(org.obiba.mica.core.service.DocumentService) PUT(javax.ws.rs.PUT) UriInfo(javax.ws.rs.core.UriInfo) EntityStateFilter(org.obiba.mica.core.domain.EntityStateFilter) IndexProjectsEvent(org.obiba.mica.project.event.IndexProjectsEvent) Searcher(org.obiba.mica.spi.search.Searcher) DocumentService(org.obiba.mica.core.service.DocumentService) EntityStateFilter(org.obiba.mica.core.domain.EntityStateFilter) Project(org.obiba.mica.project.domain.Project) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 5 with SubjectAclService

use of org.obiba.mica.security.service.SubjectAclService in project mica2 by obiba.

the class DraftHarmonizedDatasetsResource method list.

/**
 * Get all {@link HarmonizationDataset}, optionally filtered by study.
 *
 * @param studyId can be null, in which case all datasets are returned
 * @return
 */
@GET
@Path("/harmonized-datasets")
@Timed
public List<Mica.DatasetDto> list(@QueryParam("study") String studyId, @QueryParam("query") String query, @QueryParam("from") @DefaultValue("0") Integer from, @QueryParam("limit") Integer limit, @QueryParam("sort") @DefaultValue("id") String sort, @QueryParam("order") @DefaultValue("asc") String order, @QueryParam("filter") @DefaultValue("ALL") String filter, @Context HttpServletResponse response) {
    long totalCount;
    EntityStateFilter entityStateFilter = EntityStateFilter.valueOf(filter);
    List<String> filteredIds = datasetService.getIdsByStateFilter(entityStateFilter);
    Searcher.IdFilter accessibleIdFilter = AccessibleIdFilterBuilder.newBuilder().aclService(subjectAclService).resources(Lists.newArrayList("/draft/harmonized-dataset")).ids(filteredIds).build();
    if (limit == null)
        limit = MAX_LIMIT;
    if (limit < 0)
        throw new IllegalArgumentException("limit cannot be negative");
    DocumentService.Documents<HarmonizationDataset> datasets = draftDatasetService.find(from, limit, sort, order, studyId, query, null, null, accessibleIdFilter);
    totalCount = datasets.getTotal();
    response.addHeader("X-Total-Count", Long.toString(totalCount));
    return datasets.getList().stream().map(dataset -> dtos.asDto(dataset, true)).collect(toList());
}
Also used : EntityStateFilter(org.obiba.mica.core.domain.EntityStateFilter) Async(org.springframework.scheduling.annotation.Async) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) Scope(org.springframework.context.annotation.Scope) Inject(javax.inject.Inject) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) QueryParam(javax.ws.rs.QueryParam) Lists(com.google.common.collect.Lists) Mica(org.obiba.mica.web.model.Mica) DefaultValue(javax.ws.rs.DefaultValue) Dtos(org.obiba.mica.web.model.Dtos) Nullable(javax.annotation.Nullable) AccessibleIdFilterBuilder(org.obiba.mica.search.AccessibleIdFilterBuilder) DraftHarmonizationDatasetService(org.obiba.mica.dataset.service.DraftHarmonizationDatasetService) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Searcher(org.obiba.mica.spi.search.Searcher) HttpServletResponse(javax.servlet.http.HttpServletResponse) Dataset(org.obiba.mica.dataset.domain.Dataset) ApplicationContext(org.springframework.context.ApplicationContext) HarmonizedDatasetService(org.obiba.mica.dataset.service.HarmonizedDatasetService) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) DocumentService(org.obiba.mica.core.service.DocumentService) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) PUT(javax.ws.rs.PUT) UriInfo(javax.ws.rs.core.UriInfo) EntityStateFilter(org.obiba.mica.core.domain.EntityStateFilter) Searcher(org.obiba.mica.spi.search.Searcher) DocumentService(org.obiba.mica.core.service.DocumentService) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Aggregations

Lists (com.google.common.collect.Lists)6 List (java.util.List)6 Inject (javax.inject.Inject)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 SubjectAclService (org.obiba.mica.security.service.SubjectAclService)6 Timed (com.codahale.metrics.annotation.Timed)5 Collectors.toList (java.util.stream.Collectors.toList)5 Stream (java.util.stream.Stream)5 DefaultValue (javax.ws.rs.DefaultValue)5 GET (javax.ws.rs.GET)5 Path (javax.ws.rs.Path)5 PathParam (javax.ws.rs.PathParam)5 QueryParam (javax.ws.rs.QueryParam)5 Context (javax.ws.rs.core.Context)5 EntityStateFilter (org.obiba.mica.core.domain.EntityStateFilter)5 DocumentService (org.obiba.mica.core.service.DocumentService)5 AccessibleIdFilterBuilder (org.obiba.mica.search.AccessibleIdFilterBuilder)5 Searcher (org.obiba.mica.spi.search.Searcher)5 Dtos (org.obiba.mica.web.model.Dtos)5 Mica (org.obiba.mica.web.model.Mica)5