Search in sources :

Example 1 with UserAuthorityGroup

use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.

the class UserRoleController method removeUserFromRole.

@RequestMapping(value = "/{id}/users/{userId}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void removeUserFromRole(@PathVariable(value = "id") String pvId, @PathVariable("userId") String pvUserId, HttpServletResponse response) throws WebMessageException {
    UserAuthorityGroup userAuthorityGroup = userService.getUserAuthorityGroup(pvId);
    if (userAuthorityGroup == null) {
        throw new WebMessageException(WebMessageUtils.notFound("UserRole does not exist: " + pvId));
    }
    User user = userService.getUser(pvUserId);
    if (user == null || user.getUserCredentials() == null) {
        throw new WebMessageException(WebMessageUtils.notFound("User does not exist: " + pvId));
    }
    if (!aclService.canUpdate(currentUserService.getCurrentUser(), userAuthorityGroup)) {
        throw new DeleteAccessDeniedException("You don't have the proper permissions to delete this object.");
    }
    if (user.getUserCredentials().getUserAuthorityGroups().contains(userAuthorityGroup)) {
        user.getUserCredentials().getUserAuthorityGroups().remove(userAuthorityGroup);
        userService.updateUserCredentials(user.getUserCredentials());
    }
}
Also used : User(org.hisp.dhis.user.User) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UserAuthorityGroup

use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.

the class DhisConvenienceTest method createUserAuthorityGroup.

public static UserAuthorityGroup createUserAuthorityGroup(char uniqueCharacter) {
    UserAuthorityGroup role = new UserAuthorityGroup();
    role.setAutoFields();
    role.setUid(BASE_UID + uniqueCharacter);
    role.setName("UserAuthorityGroup" + uniqueCharacter);
    return role;
}
Also used : UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup)

Example 3 with UserAuthorityGroup

use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.

the class DhisConvenienceTest method createUserAndInjectSecurityContext.

/**
     * Creates a user and injects into the security context with username
     * "username". Requires <code>identifiableObjectManager</code> and
     * <code>userService</code> to be injected into the test.
     *
     * @param organisationUnits         the organisation units of the user.
     * @param dataViewOrganisationUnits user's data view organisation units.
     * @param allAuth                   whether to grant the ALL authority.
     * @param auths                     authorities to grant to user.
     * @return the user.
     */
protected User createUserAndInjectSecurityContext(Set<OrganisationUnit> organisationUnits, Set<OrganisationUnit> dataViewOrganisationUnits, boolean allAuth, String... auths) {
    Assert.notNull(userService, "UserService must be injected in test");
    Set<String> authorities = new HashSet<>();
    if (allAuth) {
        authorities.add(UserAuthorityGroup.AUTHORITY_ALL);
    }
    if (auths != null) {
        authorities.addAll(Lists.newArrayList(auths));
    }
    UserAuthorityGroup userAuthorityGroup = new UserAuthorityGroup();
    userAuthorityGroup.setName("Superuser");
    userAuthorityGroup.getAuthorities().addAll(authorities);
    userService.addUserAuthorityGroup(userAuthorityGroup);
    User user = createUser('A');
    if (organisationUnits != null) {
        user.setOrganisationUnits(organisationUnits);
    }
    if (dataViewOrganisationUnits != null) {
        user.setDataViewOrganisationUnits(dataViewOrganisationUnits);
    }
    user.getUserCredentials().getUserAuthorityGroups().add(userAuthorityGroup);
    userService.addUser(user);
    user.getUserCredentials().setUserInfo(user);
    userService.addUserCredentials(user.getUserCredentials());
    Set<GrantedAuthority> grantedAuths = authorities.stream().map(a -> new SimpleGrantedAuthority(a)).collect(Collectors.toSet());
    UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getUserCredentials().getUsername(), user.getUserCredentials().getPassword(), grantedAuths);
    Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, "", grantedAuths);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return user;
}
Also used : UniqunessType(org.hisp.dhis.program.UniqunessType) AopUtils(org.springframework.aop.support.AopUtils) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ProgramMessage(org.hisp.dhis.program.message.ProgramMessage) SqlView(org.hisp.dhis.sqlview.SqlView) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ProgramRuleVariableSourceType(org.hisp.dhis.programrule.ProgramRuleVariableSourceType) UserCredentials(org.hisp.dhis.user.UserCredentials) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) ValidationRuleGroup(org.hisp.dhis.validation.ValidationRuleGroup) NamespaceContext(javax.xml.namespace.NamespaceContext) DataElementCategoryService(org.hisp.dhis.dataelement.DataElementCategoryService) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) PrintWriter(java.io.PrintWriter) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) UserGroup(org.hisp.dhis.user.UserGroup) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) CacheStrategy(org.hisp.dhis.common.cache.CacheStrategy) Set(java.util.Set) ProgramRuleActionType(org.hisp.dhis.programrule.ProgramRuleActionType) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) Operator(org.hisp.dhis.expression.Operator) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Predictor(org.hisp.dhis.predictor.Predictor) ProgramType(org.hisp.dhis.program.ProgramType) LogFactory(org.apache.commons.logging.LogFactory) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Legend(org.hisp.dhis.legend.Legend) DataDimensionType(org.hisp.dhis.common.DataDimensionType) XPath(javax.xml.xpath.XPath) Advised(org.springframework.aop.framework.Advised) DataSet(org.hisp.dhis.dataset.DataSet) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Attribute(org.hisp.dhis.attribute.Attribute) Lists(com.google.common.collect.Lists) ProgramNotificationRecipient(org.hisp.dhis.program.notification.ProgramNotificationRecipient) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) LegendSet(org.hisp.dhis.legend.LegendSet) Indicator(org.hisp.dhis.indicator.Indicator) DataElementGroupSet(org.hisp.dhis.dataelement.DataElementGroupSet) IndicatorType(org.hisp.dhis.indicator.IndicatorType) NotificationTrigger(org.hisp.dhis.program.notification.NotificationTrigger) CategoryOptionGroupSet(org.hisp.dhis.dataelement.CategoryOptionGroupSet) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) StringWriter(java.io.StringWriter) AggregationType(org.hisp.dhis.analytics.AggregationType) CategoryOptionGroup(org.hisp.dhis.dataelement.CategoryOptionGroup) IOException(java.io.IOException) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet) SqlViewType(org.hisp.dhis.sqlview.SqlViewType) File(java.io.File) OptionSet(org.hisp.dhis.option.OptionSet) StringReader(java.io.StringReader) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) DataValue(org.hisp.dhis.datavalue.DataValue) PeriodType(org.hisp.dhis.period.PeriodType) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Expression(org.hisp.dhis.expression.Expression) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) IndicatorGroupSet(org.hisp.dhis.indicator.IndicatorGroupSet) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ProgramTrackedEntityAttributeGroup(org.hisp.dhis.program.ProgramTrackedEntityAttributeGroup) ValueType(org.hisp.dhis.common.ValueType) Date(java.util.Date) RenderService(org.hisp.dhis.render.RenderService) Constant(org.hisp.dhis.constant.Constant) Method(java.lang.reflect.Method) Period(org.hisp.dhis.period.Period) DataEntryForm(org.hisp.dhis.dataentryform.DataEntryForm) UserService(org.hisp.dhis.user.UserService) Chart(org.hisp.dhis.chart.Chart) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ProgramRule(org.hisp.dhis.programrule.ProgramRule) List(java.util.List) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) PostConstruct(javax.annotation.PostConstruct) ValidationCriteria(org.hisp.dhis.validation.ValidationCriteria) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) Authentication(org.springframework.security.core.Authentication) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramMessageStatus(org.hisp.dhis.program.message.ProgramMessageStatus) AttributeValue(org.hisp.dhis.attribute.AttributeValue) LocationManager(org.hisp.dhis.external.location.LocationManager) ValidationNotificationTemplate(org.hisp.dhis.validation.notification.ValidationNotificationTemplate) ClassPathResource(org.springframework.core.io.ClassPathResource) ProgramRuleVariable(org.hisp.dhis.programrule.ProgramRuleVariable) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) ProgramRuleAction(org.hisp.dhis.programrule.ProgramRuleAction) Program(org.hisp.dhis.program.Program) DataElement(org.hisp.dhis.dataelement.DataElement) HashSet(java.util.HashSet) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) RelationshipType(org.hisp.dhis.relationship.RelationshipType) User(org.hisp.dhis.user.User) UserDetails(org.springframework.security.core.userdetails.UserDetails) XMLConstants(javax.xml.XMLConstants) InputSource(org.xml.sax.InputSource) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Iterator(java.util.Iterator) DataElementDomain(org.hisp.dhis.dataelement.DataElementDomain) DateTime(org.joda.time.DateTime) ValidationRule(org.hisp.dhis.validation.ValidationRule) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageSection(org.hisp.dhis.program.ProgramStageSection) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) XPathFactory(javax.xml.xpath.XPathFactory) Option(org.hisp.dhis.option.Option) DeliveryChannel(org.hisp.dhis.common.DeliveryChannel) Log(org.apache.commons.logging.Log) Collections(java.util.Collections) ChartType(org.hisp.dhis.chart.ChartType) Assert(org.springframework.util.Assert) User(org.hisp.dhis.user.User) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetails(org.springframework.security.core.userdetails.UserDetails) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) Authentication(org.springframework.security.core.Authentication) HashSet(java.util.HashSet)

Example 4 with UserAuthorityGroup

use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.

the class AddRoleAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    UserAuthorityGroup group = new UserAuthorityGroup();
    group.setName(StringUtils.trimToNull(name));
    group.setDescription(StringUtils.trimToNull(description));
    for (String id : selectedList) {
        DataSet dataSet = dataSetService.getDataSet(id);
        group.getDataSets().add(dataSet);
    }
    for (String id : selectedProgramList) {
        Program program = programService.getProgram(id);
        group.getPrograms().add(program);
    }
    group.getAuthorities().addAll(selectedListAuthority);
    userService.addUserAuthorityGroup(group);
    return SUCCESS;
}
Also used : Program(org.hisp.dhis.program.Program) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) DataSet(org.hisp.dhis.dataset.DataSet)

Example 5 with UserAuthorityGroup

use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.

the class UpdateRoleAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    UserAuthorityGroup group = userService.getUserAuthorityGroup(id);
    group.setName(StringUtils.trimToNull(name));
    group.setDescription(StringUtils.trimToNull(description));
    group.getDataSets().clear();
    group.getPrograms().clear();
    group.getAuthorities().clear();
    for (String id : selectedList) {
        DataSet dataSet = dataSetService.getDataSet(id);
        group.getDataSets().add(dataSet);
    }
    for (String id : selectedProgramList) {
        Program program = programService.getProgram(id);
        group.getPrograms().add(program);
    }
    group.getAuthorities().addAll(selectedListAuthority);
    userService.updateUserAuthorityGroup(group);
    return SUCCESS;
}
Also used : Program(org.hisp.dhis.program.Program) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) DataSet(org.hisp.dhis.dataset.DataSet)

Aggregations

UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)56 User (org.hisp.dhis.user.User)41 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)19 Test (org.junit.jupiter.api.Test)19 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)17 List (java.util.List)15 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)15 DataSet (org.hisp.dhis.dataset.DataSet)15 DataElement (org.hisp.dhis.dataelement.DataElement)14 ClassPathResource (org.springframework.core.io.ClassPathResource)14 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)11 HashSet (java.util.HashSet)7 Program (org.hisp.dhis.program.Program)7 UserGroup (org.hisp.dhis.user.UserGroup)6 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)5 Section (org.hisp.dhis.dataset.Section)4 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)4 EventVisualization (org.hisp.dhis.eventvisualization.EventVisualization)4 ProgramStage (org.hisp.dhis.program.ProgramStage)4 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)4