Search in sources :

Example 6 with CommonException

use of com.evolveum.midpoint.util.exception.CommonException in project midpoint by Evolveum.

the class ConsistencyTest method test999Shutdown.

@Test
public void test999Shutdown() throws Exception {
    taskManager.shutdown();
    waitFor("waiting for task manager shutdown", new Checker() {

        @Override
        public boolean check() throws CommonException {
            try {
                return taskManager.getLocallyRunningTasks(new OperationResult("dummy")).isEmpty();
            } catch (TaskManagerException e) {
                throw new SystemException(e);
            }
        }

        @Override
        public void timeout() {
        // No reaction, the test will fail right after return from this
        }
    }, 10000);
    AssertJUnit.assertEquals("Some tasks left running after shutdown", new HashSet<Task>(), taskManager.getLocallyRunningTasks(new OperationResult("dummy")));
}
Also used : Checker(com.evolveum.midpoint.test.Checker) Task(com.evolveum.midpoint.task.api.Task) SystemException(com.evolveum.midpoint.util.exception.SystemException) TaskManagerException(com.evolveum.midpoint.task.api.TaskManagerException) CommonException(com.evolveum.midpoint.util.exception.CommonException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Test(org.testng.annotations.Test) AbstractModelIntegrationTest(com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)

Example 7 with CommonException

use of com.evolveum.midpoint.util.exception.CommonException in project midpoint by Evolveum.

the class PageLogin method initCustomLayer.

@Override
protected void initCustomLayer() {
    MidpointForm form = new MidpointForm(ID_FORM);
    form.add(AttributeModifier.replace("action", new IModel<String>() {

        @Override
        public String getObject() {
            return getUrlProcessingLogin();
        }
    }));
    add(form);
    BookmarkablePageLink<String> link = new BookmarkablePageLink<>(ID_FORGET_PASSWORD, PageForgotPassword.class);
    Task task = createAnonymousTask(OPERATION_LOAD_RESET_PASSWORD_POLICY);
    OperationResult parentResult = new OperationResult(OPERATION_LOAD_RESET_PASSWORD_POLICY);
    SecurityPolicyType securityPolicy = null;
    try {
        securityPolicy = getModelInteractionService().getSecurityPolicy((PrismObject<? extends FocusType>) null, task, parentResult);
    } catch (CommonException e) {
        LOGGER.warn("Cannot read credentials policy: " + e.getMessage(), e);
    }
    SecurityPolicyType finalSecurityPolicy = securityPolicy;
    link.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            if (finalSecurityPolicy == null) {
                return false;
            }
            if (finalSecurityPolicy != null && finalSecurityPolicy.getCredentialsReset() != null && StringUtils.isNotBlank(finalSecurityPolicy.getCredentialsReset().getAuthenticationSequenceName())) {
                AuthenticationSequenceType sequence = SecurityUtils.getSequenceByName(finalSecurityPolicy.getCredentialsReset().getAuthenticationSequenceName(), finalSecurityPolicy.getAuthentication());
                if (sequence != null && (sequence.getChannel() == null || StringUtils.isBlank(sequence.getChannel().getUrlSuffix()))) {
                    return false;
                }
            }
            CredentialsPolicyType creds = finalSecurityPolicy.getCredentials();
            // TODO: Not entirely correct. This means we have reset somehow configured, but not necessarily enabled.
            if (creds != null && ((creds.getSecurityQuestions() != null && creds.getSecurityQuestions().getQuestionNumber() != null) || (finalSecurityPolicy.getCredentialsReset() != null))) {
                return true;
            }
            return false;
        }
    });
    if (securityPolicy != null && securityPolicy.getCredentialsReset() != null && StringUtils.isNotBlank(securityPolicy.getCredentialsReset().getAuthenticationSequenceName())) {
        AuthenticationSequenceType sequence = SecurityUtils.getSequenceByName(securityPolicy.getCredentialsReset().getAuthenticationSequenceName(), securityPolicy.getAuthentication());
        if (sequence != null) {
            if (sequence.getChannel() == null || StringUtils.isBlank(sequence.getChannel().getUrlSuffix())) {
                String message = "Sequence with name " + securityPolicy.getCredentialsReset().getAuthenticationSequenceName() + " doesn't contain urlSuffix";
                LOGGER.error(message, new IllegalArgumentException(message));
                error(message);
            }
            link.add(AttributeModifier.replace("href", new IModel<String>() {

                @Override
                public String getObject() {
                    return "./" + ModuleWebSecurityConfiguration.DEFAULT_PREFIX_OF_MODULE + "/" + sequence.getChannel().getUrlSuffix();
                }
            }));
        }
    }
    form.add(link);
    BookmarkablePageLink<String> registration = new BookmarkablePageLink<>(ID_SELF_REGISTRATION, PageSelfRegistration.class);
    registration.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            OperationResult parentResult = new OperationResult(OPERATION_LOAD_REGISTRATION_POLICY);
            RegistrationsPolicyType registrationPolicies = null;
            try {
                Task task = createAnonymousTask(OPERATION_LOAD_REGISTRATION_POLICY);
                registrationPolicies = getModelInteractionService().getFlowPolicy(null, task, parentResult);
            } catch (CommonException e) {
                LOGGER.warn("Cannot read credentials policy: " + e.getMessage(), e);
            }
            boolean linkIsVisible = false;
            if (registrationPolicies != null && registrationPolicies.getSelfRegistration() != null) {
                linkIsVisible = true;
            }
            return linkIsVisible;
        }
    });
    if (securityPolicy != null) {
        SelfRegistrationPolicyType policy = SecurityPolicyUtil.getSelfRegistrationPolicy(securityPolicy);
        if (policy != null) {
            String sequenceName = policy.getAdditionalAuthenticationSequence() == null ? policy.getAdditionalAuthenticationName() : policy.getAdditionalAuthenticationSequence();
            if (StringUtils.isNotBlank(sequenceName)) {
                AuthenticationSequenceType sequence = SecurityUtils.getSequenceByName(sequenceName, securityPolicy.getAuthentication());
                if (sequence != null) {
                    registration.add(AttributeModifier.replace("href", () -> "./" + ModuleWebSecurityConfiguration.DEFAULT_PREFIX_OF_MODULE + "/" + sequence.getChannel().getUrlSuffix()));
                }
            }
        }
    }
    form.add(registration);
    WebMarkupContainer csrfField = SecurityUtils.createHiddenInputForCsrf(ID_CSRF_FIELD);
    form.add(csrfField);
}
Also used : IModel(org.apache.wicket.model.IModel) Task(com.evolveum.midpoint.task.api.Task) MidpointForm(com.evolveum.midpoint.web.component.form.MidpointForm) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) BookmarkablePageLink(org.apache.wicket.markup.html.link.BookmarkablePageLink) PrismObject(com.evolveum.midpoint.prism.PrismObject) CommonException(com.evolveum.midpoint.util.exception.CommonException) VisibleEnableBehaviour(com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour)

Example 8 with CommonException

use of com.evolveum.midpoint.util.exception.CommonException in project midpoint by Evolveum.

the class PageRegistrationFinish method assignDefaultRoles.

private void assignDefaultRoles(String userOid, PrismObject<UserType> administrator, OperationResult parentResult) throws CommonException {
    List<ObjectReferenceType> rolesToAssign = getSelfRegistrationConfiguration().getDefaultRoles();
    if (CollectionUtils.isEmpty(rolesToAssign)) {
        return;
    }
    OperationResult result = parentResult.createSubresult(OPERATION_ASSIGN_DEFAULT_ROLES);
    try {
        PrismContext prismContext = getPrismContext();
        List<AssignmentType> assignmentsToCreate = rolesToAssign.stream().map(ref -> ObjectTypeUtil.createAssignmentTo(ref, prismContext)).collect(Collectors.toList());
        ObjectDelta<Objectable> delta = prismContext.deltaFor(UserType.class).item(UserType.F_ASSIGNMENT).addRealValues(assignmentsToCreate).asObjectDelta(userOid);
        runAsChecked(() -> {
            Task task = createSimpleTask(OPERATION_ASSIGN_DEFAULT_ROLES);
            WebModelServiceUtils.save(delta, result, task, PageRegistrationFinish.this);
            return null;
        }, administrator);
    } catch (CommonException | RuntimeException e) {
        result.recordFatalError(getString("PageRegistrationConfirmation.message.assignDefaultRoles.fatalError"), e);
        throw e;
    } finally {
        result.computeStatusIfUnknown();
    }
}
Also used : com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) PageDescriptor(com.evolveum.midpoint.authentication.api.authorization.PageDescriptor) SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) AuthorizationConstants(com.evolveum.midpoint.security.api.AuthorizationConstants) Trace(com.evolveum.midpoint.util.logging.Trace) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) OperationResultStatus(com.evolveum.midpoint.schema.result.OperationResultStatus) ArrayList(java.util.ArrayList) WebModelServiceUtils(com.evolveum.midpoint.gui.api.util.WebModelServiceUtils) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) PrismContext(com.evolveum.midpoint.prism.PrismContext) RestartResponseException(org.apache.wicket.RestartResponseException) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) AuthenticationException(org.springframework.security.core.AuthenticationException) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Label(org.apache.wicket.markup.html.basic.Label) Url(com.evolveum.midpoint.authentication.api.authorization.Url) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) Objectable(com.evolveum.midpoint.prism.Objectable) AjaxLink(org.apache.wicket.ajax.markup.html.AjaxLink) PrismObject(com.evolveum.midpoint.prism.PrismObject) Task(com.evolveum.midpoint.task.api.Task) Collectors(java.util.stream.Collectors) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) CommonException(com.evolveum.midpoint.util.exception.CommonException) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal) List(java.util.List) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) AuthorizationAction(com.evolveum.midpoint.authentication.api.authorization.AuthorizationAction) VisibleEnableBehaviour(com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour) Authentication(org.springframework.security.core.Authentication) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) Task(com.evolveum.midpoint.task.api.Task) PrismContext(com.evolveum.midpoint.prism.PrismContext) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Objectable(com.evolveum.midpoint.prism.Objectable) CommonException(com.evolveum.midpoint.util.exception.CommonException)

Example 9 with CommonException

use of com.evolveum.midpoint.util.exception.CommonException in project midpoint by Evolveum.

the class PageRegistrationFinish method assignAdditionalRoleIfPresent.

private void assignAdditionalRoleIfPresent(String userOid, NonceType nonceType, PrismObject<UserType> administrator, OperationResult parentResult) throws CommonException {
    if (nonceType.getName() == null) {
        return;
    }
    OperationResult result = parentResult.createSubresult(OPERATION_ASSIGN_ADDITIONAL_ROLE);
    try {
        runAsChecked(() -> {
            Task task = createAnonymousTask(OPERATION_ASSIGN_ADDITIONAL_ROLE);
            ObjectDelta<UserType> assignRoleDelta;
            AssignmentType assignment = new AssignmentType();
            assignment.setTargetRef(ObjectTypeUtil.createObjectRef(nonceType.getName(), ObjectTypes.ABSTRACT_ROLE));
            getPrismContext().adopt(assignment);
            List<ItemDelta> userDeltas = new ArrayList<>();
            userDeltas.add(getPrismContext().deltaFactory().container().createModificationAdd(UserType.F_ASSIGNMENT, UserType.class, assignment));
            assignRoleDelta = getPrismContext().deltaFactory().object().createModifyDelta(userOid, userDeltas, UserType.class);
            assignRoleDelta.setPrismContext(getPrismContext());
            WebModelServiceUtils.save(assignRoleDelta, result, task, PageRegistrationFinish.this);
            return null;
        }, administrator);
    } catch (CommonException | RuntimeException e) {
        result.recordFatalError(getString("PageRegistrationConfirmation.message.assignAdditionalRoleIfPresent.fatalError"), e);
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't assign additional role", e);
        throw e;
    } finally {
        result.computeStatusIfUnknown();
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) CommonException(com.evolveum.midpoint.util.exception.CommonException)

Example 10 with CommonException

use of com.evolveum.midpoint.util.exception.CommonException in project midpoint by Evolveum.

the class PageRegistrationConfirmation method init.

private void init(final PageParameters pageParameters) {
    PageParameters params = pageParameters;
    if (params == null) {
        params = getPageParameters();
    }
    OperationResult result = new OperationResult(OPERATION_FINISH_REGISTRATION);
    if (params == null) {
        LOGGER.error("Confirmation link is not valid. No credentials provided in it");
        String msg = createStringResource("PageSelfRegistration.invalid.registration.link").getString();
        getSession().error(createStringResource(msg));
        result.recordFatalError(msg);
        initLayout(result);
        return;
    }
    StringValue userNameValue = params.get(SchemaConstants.USER_ID);
    Validate.notEmpty(userNameValue.toString());
    StringValue tokenValue = params.get(SchemaConstants.TOKEN);
    Validate.notEmpty(tokenValue.toString());
    try {
        UserType user = checkUserCredentials(userNameValue.toString(), tokenValue.toString(), result);
        PrismObject<UserType> administrator = getAdministratorPrivileged(result);
        assignDefaultRoles(user.getOid(), administrator, result);
        result.computeStatus();
        if (result.getStatus() == OperationResultStatus.FATAL_ERROR) {
            LOGGER.error("Failed to assign default roles, {}", result.getMessage());
        } else {
            NonceType nonceClone = user.getCredentials().getNonce().clone();
            removeNonceAndSetLifecycleState(user.getOid(), nonceClone, administrator, result);
            assignAdditionalRoleIfPresent(user.getOid(), nonceClone, administrator, result);
            result.computeStatus();
        }
        initLayout(result);
    } catch (CommonException | AuthenticationException e) {
        result.computeStatus();
        initLayout(result);
    }
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) CommonException(com.evolveum.midpoint.util.exception.CommonException) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) StringValue(org.apache.wicket.util.string.StringValue)

Aggregations

CommonException (com.evolveum.midpoint.util.exception.CommonException)71 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)44 Task (com.evolveum.midpoint.task.api.Task)33 PrismObject (com.evolveum.midpoint.prism.PrismObject)14 SystemException (com.evolveum.midpoint.util.exception.SystemException)14 ArrayList (java.util.ArrayList)12 ModelService (com.evolveum.midpoint.model.api.ModelService)11 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)8 NotNull (org.jetbrains.annotations.NotNull)7 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)6 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)5 ActivityRunException (com.evolveum.midpoint.repo.common.activity.run.ActivityRunException)5 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)5 RestartResponseException (org.apache.wicket.RestartResponseException)5 ModelInteractionService (com.evolveum.midpoint.model.api.ModelInteractionService)4 ModelContext (com.evolveum.midpoint.model.api.context.ModelContext)4 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)4 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)4