Search in sources :

Example 6 with MidPointPrincipal

use of com.evolveum.midpoint.security.api.MidPointPrincipal in project midpoint by Evolveum.

the class WebModelServiceUtils method createSimpleTask.

public static Task createSimpleTask(String operation, PrismObject<UserType> owner, TaskManager manager) {
    Task task = manager.createTaskInstance(operation);
    if (owner == null) {
        MidPointPrincipal user = SecurityUtils.getPrincipalUser();
        if (user == null) {
            throw new RestartResponseException(PageLogin.class);
        } else {
            owner = user.getUser().asPrismObject();
        }
    }
    task.setOwner(owner);
    task.setChannel(SchemaConstants.CHANNEL_GUI_USER_URI);
    return task;
}
Also used : Task(com.evolveum.midpoint.task.api.Task) RestartResponseException(org.apache.wicket.RestartResponseException) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal)

Example 7 with MidPointPrincipal

use of com.evolveum.midpoint.security.api.MidPointPrincipal in project midpoint by Evolveum.

the class WebModelServiceUtils method getLocale.

public static Locale getLocale(UserType user) {
    MidPointPrincipal principal = SecurityUtils.getPrincipalUser();
    Locale locale = null;
    if (principal != null) {
        if (user == null) {
            PrismObject<UserType> userPrismObject = principal.getUser().asPrismObject();
            user = userPrismObject == null ? null : userPrismObject.asObjectable();
        }
        if (user != null && user.getPreferredLanguage() != null && !user.getPreferredLanguage().trim().equals("")) {
            try {
                locale = LocaleUtils.toLocale(user.getPreferredLanguage());
            } catch (Exception ex) {
                LOGGER.debug("Error occurred while getting user locale, " + ex.getMessage());
            }
        }
        if (locale != null && MidPointApplication.containsLocale(locale)) {
            return locale;
        } else {
            String userLocale = user != null ? user.getLocale() : null;
            try {
                locale = userLocale == null ? null : LocaleUtils.toLocale(userLocale);
            } catch (Exception ex) {
                LOGGER.debug("Error occurred while getting user locale, " + ex.getMessage());
            }
            if (locale != null && MidPointApplication.containsLocale(locale)) {
                return locale;
            } else {
                locale = Session.get().getLocale();
                if (locale == null || !MidPointApplication.containsLocale(locale)) {
                    //default locale for web application
                    return MidPointApplication.getDefaultLocale();
                }
                return locale;
            }
        }
    }
    return null;
}
Also used : PolicyViolationException(com.evolveum.midpoint.util.exception.PolicyViolationException) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) AuthorizationException(com.evolveum.midpoint.util.exception.AuthorizationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) RestartResponseException(org.apache.wicket.RestartResponseException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal)

Example 8 with MidPointPrincipal

use of com.evolveum.midpoint.security.api.MidPointPrincipal in project midpoint by Evolveum.

the class InitialDataImport method init.

public void init() throws SchemaException {
    LOGGER.info("Starting initial object import (if necessary).");
    OperationResult mainResult = new OperationResult(OPERATION_INITIAL_OBJECTS_IMPORT);
    Task task = taskManager.createTaskInstance(OPERATION_INITIAL_OBJECTS_IMPORT);
    task.setChannel(SchemaConstants.CHANNEL_GUI_INIT_URI);
    int count = 0;
    int errors = 0;
    File[] files = getInitialImportObjects();
    LOGGER.debug("Files to be imported: {}.", Arrays.toString(files));
    // We need to provide a fake Spring security context here.
    // We have to fake it because we do not have anything in the repository yet. And to get
    // something to the repository we need a context. Chicken and egg. So we fake the egg.
    SecurityContext securityContext = SecurityContextHolder.getContext();
    UserType userAdministrator = new UserType();
    prismContext.adopt(userAdministrator);
    userAdministrator.setName(new PolyStringType(new PolyString("initAdmin", "initAdmin")));
    MidPointPrincipal principal = new MidPointPrincipal(userAdministrator);
    AuthorizationType superAutzType = new AuthorizationType();
    prismContext.adopt(superAutzType, RoleType.class, new ItemPath(RoleType.F_AUTHORIZATION));
    superAutzType.getAction().add(AuthorizationConstants.AUTZ_ALL_URL);
    Authorization superAutz = new Authorization(superAutzType);
    Collection<Authorization> authorities = principal.getAuthorities();
    authorities.add(superAutz);
    Authentication authentication = new PreAuthenticatedAuthenticationToken(principal, null);
    securityContext.setAuthentication(authentication);
    for (File file : files) {
        try {
            LOGGER.debug("Considering initial import of file {}.", file.getName());
            PrismObject object = prismContext.parseObject(file);
            if (ReportType.class.equals(object.getCompileTimeClass())) {
                ReportTypeUtil.applyDefinition(object, prismContext);
            }
            Boolean importObject = importObject(object, file, task, mainResult);
            if (importObject == null) {
                continue;
            }
            if (importObject) {
                count++;
            } else {
                errors++;
            }
        } catch (Exception ex) {
            LoggingUtils.logUnexpectedException(LOGGER, "Couldn't import file {}", ex, file.getName());
            mainResult.recordFatalError("Couldn't import file '" + file.getName() + "'", ex);
        }
    }
    securityContext.setAuthentication(null);
    mainResult.recomputeStatus("Couldn't import objects.");
    LOGGER.info("Initial object import finished ({} objects imported, {} errors)", count, errors);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Initialization status:\n" + mainResult.debugDump());
    }
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) Task(com.evolveum.midpoint.task.api.Task) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) URISyntaxException(java.net.URISyntaxException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) IOException(java.io.IOException) Authorization(com.evolveum.midpoint.security.api.Authorization) PrismObject(com.evolveum.midpoint.prism.PrismObject) Authentication(org.springframework.security.core.Authentication) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) SecurityContext(org.springframework.security.core.context.SecurityContext) AuthorizationType(com.evolveum.midpoint.xml.ns._public.common.common_3.AuthorizationType) File(java.io.File) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 9 with MidPointPrincipal

use of com.evolveum.midpoint.security.api.MidPointPrincipal in project midpoint by Evolveum.

the class PageAbout method reindexRepositoryObjectsPerformed.

private void reindexRepositoryObjectsPerformed(AjaxRequestTarget target) {
    OperationResult result = new OperationResult(OPERATION_SUBMIT_REINDEX);
    try {
        TaskManager taskManager = getTaskManager();
        Task task = taskManager.createTaskInstance();
        MidPointPrincipal user = SecurityUtils.getPrincipalUser();
        if (user == null) {
            throw new RestartResponseException(PageLogin.class);
        } else {
            task.setOwner(user.getUser().asPrismObject());
        }
        getSecurityEnforcer().authorize(AuthorizationConstants.AUTZ_ALL_URL, null, null, null, null, null, result);
        task.setChannel(SchemaConstants.CHANNEL_GUI_USER_URI);
        task.setHandlerUri(ModelPublicConstants.REINDEX_TASK_HANDLER_URI);
        task.setName("Reindex repository objects");
        taskManager.switchToBackground(task, result);
        result.setBackgroundTaskOid(task.getOid());
    } catch (SecurityViolationException | SchemaException | RuntimeException e) {
        result.recordFatalError(e);
    } finally {
        result.computeStatusIfUnknown();
    }
    showResult(result);
    target.add(getFeedbackPanel());
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Task(com.evolveum.midpoint.task.api.Task) TaskManager(com.evolveum.midpoint.task.api.TaskManager) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) RestartResponseException(org.apache.wicket.RestartResponseException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal)

Example 10 with MidPointPrincipal

use of com.evolveum.midpoint.security.api.MidPointPrincipal in project midpoint by Evolveum.

the class PageSelfDashboard method loadUser.

private PrismObject<UserType> loadUser() {
    MidPointPrincipal principal = SecurityUtils.getPrincipalUser();
    Validate.notNull(principal, "No principal");
    if (principal.getOid() == null) {
        throw new IllegalArgumentException("No OID in principal: " + principal);
    }
    Task task = createSimpleTask(OPERATION_LOAD_USER);
    OperationResult result = task.getResult();
    PrismObject<UserType> user = WebModelServiceUtils.loadObject(UserType.class, principal.getOid(), PageSelfDashboard.this, task, result);
    result.computeStatus();
    if (!WebComponentUtil.isSuccessOrHandledError(result)) {
        showResult(result);
    }
    return user;
}
Also used : Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal)

Aggregations

MidPointPrincipal (com.evolveum.midpoint.security.api.MidPointPrincipal)75 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)35 Task (com.evolveum.midpoint.task.api.Task)35 Test (org.testng.annotations.Test)30 AbstractInitializedModelIntegrationTest (com.evolveum.midpoint.model.intest.AbstractInitializedModelIntegrationTest)18 TestTriggerTask (com.evolveum.midpoint.model.intest.TestTriggerTask)18 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)11 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)10 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)10 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)9 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)8 IdItemPathSegment (com.evolveum.midpoint.prism.path.IdItemPathSegment)6 NameItemPathSegment (com.evolveum.midpoint.prism.path.NameItemPathSegment)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)6 Authentication (org.springframework.security.core.Authentication)6 TestRbac (com.evolveum.midpoint.model.intest.rbac.TestRbac)5 SystemException (com.evolveum.midpoint.util.exception.SystemException)5 Authorization (com.evolveum.midpoint.security.api.Authorization)3 ConnectionEnvironment (com.evolveum.midpoint.security.api.ConnectionEnvironment)3 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)3