Search in sources :

Example 1 with UserProfileService

use of org.obiba.mica.user.UserProfileService 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 UserProfileService

use of org.obiba.mica.user.UserProfileService in project mica2 by obiba.

the class DataAccessController method getHistory.

@GetMapping("/data-access-history/{id:.+}")
public ModelAndView getHistory(@PathVariable String id) {
    Subject subject = SecurityUtils.getSubject();
    if (subject.isAuthenticated()) {
        Map<String, Object> params = newParameters(id);
        DataAccessRequest dar = (DataAccessRequest) params.get("dar");
        addDataAccessConfiguration(params);
        // merge change history from main form, feasibility and amendment forms
        final List<FormStatusChangeEvent> events = Lists.newArrayList(dar.getStatusChangeHistory().stream().map(e -> new FormStatusChangeEvent(userProfileService, dar, e)).collect(Collectors.toList()));
        getDataAccessFeasibilities(params).forEach(a -> {
            a.getStatusChangeHistory().stream().map(e -> new FormStatusChangeEvent(userProfileService, a, e)).forEach(events::add);
        });
        getDataAccessAmendments(params).forEach(a -> {
            a.getStatusChangeHistory().stream().map(e -> new FormStatusChangeEvent(userProfileService, a, e)).forEach(events::add);
        });
        // order change events
        params.put("statusChangeEvents", events.stream().sorted(new Comparator<FormStatusChangeEvent>() {

            @Override
            public int compare(FormStatusChangeEvent event1, FormStatusChangeEvent event2) {
                return event1.getDate().compareTo(event2.getDate());
            }
        }).collect(Collectors.toList()));
        return new ModelAndView("data-access-history", params);
    } else {
        return new ModelAndView("redirect:../signin?redirect=" + micaConfigService.getContextPath() + "/data-access-history%2F" + id);
    }
}
Also used : Comment(org.obiba.mica.core.domain.Comment) DataAccessForm(org.obiba.mica.micaConfig.domain.DataAccessForm) DataAccessFeasibilityFormService(org.obiba.mica.micaConfig.service.DataAccessFeasibilityFormService) java.util(java.util) Roles(org.obiba.mica.security.Roles) UserProfileService(org.obiba.mica.user.UserProfileService) Controller(org.springframework.stereotype.Controller) DataAccessRequestService(org.obiba.mica.access.service.DataAccessRequestService) DataAccessAmendmentService(org.obiba.mica.access.service.DataAccessAmendmentService) Inject(javax.inject.Inject) Lists(com.google.common.collect.Lists) Subject(org.apache.shiro.subject.Subject) DataAccessFormService(org.obiba.mica.micaConfig.service.DataAccessFormService) DataAccessAmendmentForm(org.obiba.mica.micaConfig.domain.DataAccessAmendmentForm) AbstractAuditableDocument(org.obiba.mica.core.domain.AbstractAuditableDocument) DataAccessFeasibilityService(org.obiba.mica.access.service.DataAccessFeasibilityService) CommentsService(org.obiba.mica.core.service.CommentsService) FormStatusChangeEvent(org.obiba.mica.web.controller.domain.FormStatusChangeEvent) DataAccessConfigBundle(org.obiba.mica.web.controller.domain.DataAccessConfigBundle) SchemaFormConfig(org.obiba.mica.web.controller.domain.SchemaFormConfig) DataAccessRequestReportNotificationService(org.obiba.mica.access.notification.DataAccessRequestReportNotificationService) DataAccessFeasibilityForm(org.obiba.mica.micaConfig.domain.DataAccessFeasibilityForm) Collectors(java.util.stream.Collectors) NoSuchDataAccessRequestException(org.obiba.mica.access.NoSuchDataAccessRequestException) ModelAndView(org.springframework.web.servlet.ModelAndView) DataAccessAmendmentFormService(org.obiba.mica.micaConfig.service.DataAccessAmendmentFormService) org.obiba.mica.access.domain(org.obiba.mica.access.domain) DataAccessConfigService(org.obiba.mica.micaConfig.service.DataAccessConfigService) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Pattern(java.util.regex.Pattern) SecurityUtils(org.apache.shiro.SecurityUtils) ModelAndView(org.springframework.web.servlet.ModelAndView) FormStatusChangeEvent(org.obiba.mica.web.controller.domain.FormStatusChangeEvent) Subject(org.apache.shiro.subject.Subject)

Aggregations

Lists (com.google.common.collect.Lists)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 SecurityUtils (org.apache.shiro.SecurityUtils)2 Subject (org.apache.shiro.subject.Subject)2 Roles (org.obiba.mica.security.Roles)2 UserProfileService (org.obiba.mica.user.UserProfileService)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 java.util (java.util)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 NoSuchDataAccessRequestException (org.obiba.mica.access.NoSuchDataAccessRequestException)1 org.obiba.mica.access.domain (org.obiba.mica.access.domain)1 DataAccessRequestReportNotificationService (org.obiba.mica.access.notification.DataAccessRequestReportNotificationService)1 DataAccessAmendmentService (org.obiba.mica.access.service.DataAccessAmendmentService)1 DataAccessFeasibilityService (org.obiba.mica.access.service.DataAccessFeasibilityService)1