Search in sources :

Example 1 with AutoLoginException

use of com.liferay.portal.kernel.security.auto.login.AutoLoginException in project sw360 by eclipse.

the class UserPortlet method updateUserObjectFromRequest.

private boolean updateUserObjectFromRequest(ActionRequest request, ActionResponse response, UserCSV userCSV, org.eclipse.sw360.datahandler.thrift.users.User userByEmailFromCouchDB, boolean isCouchDBUserNew, boolean isLiferayUserNew, User liferayUserByEmailAddress) throws TException, SystemException, PortalException, IOException {
    org.eclipse.sw360.datahandler.thrift.users.User requestingUser = UserCacheHolder.getUserFromRequest(request);
    UserService.Iface userClient = thriftClients.makeUserClient();
    String givenNameFromReq = request.getParameter(org.eclipse.sw360.datahandler.thrift.users.User._Fields.GIVENNAME.toString());
    String lastNameFromReq = request.getParameter(org.eclipse.sw360.datahandler.thrift.users.User._Fields.LASTNAME.toString());
    String emailFromReq = request.getParameter(org.eclipse.sw360.datahandler.thrift.users.User._Fields.EMAIL.toString());
    String departmentFromReq = request.getParameter(org.eclipse.sw360.datahandler.thrift.users.User._Fields.DEPARTMENT.toString());
    String externalIdFromReq = request.getParameter(org.eclipse.sw360.datahandler.thrift.users.User._Fields.EXTERNALID.toString());
    String primaryRoleFromReq = request.getParameter(org.eclipse.sw360.datahandler.thrift.users.User._Fields.USER_GROUP.toString());
    String pwdFromReq = request.getParameter(PortalConstants.PASSWORD);
    if (emailFromReq != null) {
        emailFromReq = emailFromReq.toLowerCase();
    }
    if (externalIdFromReq != null) {
        externalIdFromReq = externalIdFromReq.toLowerCase();
    }
    Map<String, Set<UserGroup>> secondaryDepartmentAndRolesMapFromRequest = PortletUtils.getSecondaryDepartmentAndRolesMapFromRequest(request, departmentFromReq);
    String originalEmail = CommonUtils.nullToEmptyString(userByEmailFromCouchDB.getEmail());
    org.eclipse.sw360.datahandler.thrift.users.User updatedUserForDisplay = userByEmailFromCouchDB.deepCopy();
    updatedUserForDisplay.setGivenname(givenNameFromReq).setLastname(lastNameFromReq).setDepartment(departmentFromReq).setExternalid(externalIdFromReq).setUserGroup(UserGroup.valueOf(primaryRoleFromReq)).setPrimaryRoles(null).setSecondaryDepartmentsAndRoles(secondaryDepartmentAndRolesMapFromRequest);
    request.setAttribute(PortalConstants.USER_OBJ, updatedUserForDisplay);
    if (isLiferayUserNew) {
        if (!emailFromReq.equals(originalEmail)) {
            try {
                org.eclipse.sw360.datahandler.thrift.users.User userByEmail = userClient.getByEmail(emailFromReq);
                if (userByEmail != null) {
                    log.error("Another user with same email id exists:" + emailFromReq);
                    throw new SW360Exception("Another user with same email id exists in couch db:" + emailFromReq);
                }
            } catch (TException exp) {
                log.debug("No user with same email id found " + emailFromReq);
            }
            if (!externalIdFromReq.equals(userByEmailFromCouchDB.getExternalid())) {
                try {
                    org.eclipse.sw360.datahandler.thrift.users.User userByExtId = userClient.getByEmailOrExternalId("dummy@dummy.org", externalIdFromReq);
                    if (userByExtId != null) {
                        log.error("Another user with same external id exists in couch db:" + externalIdFromReq);
                        throw new SW360Exception("Another user with same external id exists:" + externalIdFromReq);
                    }
                } catch (TException exp) {
                    log.debug("No user with same external id found " + externalIdFromReq);
                }
            }
        }
    }
    departmentFromReq = orgHelper.mapOrganizationName(departmentFromReq);
    User liferayCreatedOrUpdated = null;
    if (PortalConstants.SSO_LOGIN_ENABLED) {
        long companyId = PortalUtil.getCompanyId(request);
        HttpServletRequest httpRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));
        if (isLiferayUserNew) {
            SSOAutoLogin sso = new SSOAutoLogin();
            orgHelper.addOrGetOrganization(departmentFromReq, companyId);
            try {
                liferayCreatedOrUpdated = sso.createLiferayUser(httpRequest, emailFromReq, externalIdFromReq, givenNameFromReq, lastNameFromReq, companyId, departmentFromReq);
            } catch (AutoLoginException ale) {
                log.error("Error while creating user in Liferay: ", ale);
                throw new SW360Exception(ale.getMessage());
            }
            if (liferayCreatedOrUpdated == null) {
                log.error("Error while creating new liferay user, SSO login enabled : " + emailFromReq);
                throw new SW360Exception("Error while creating new liferay user, SSO login enabled : " + emailFromReq);
            }
            if (!primaryRoleFromReq.equalsIgnoreCase(UserGroup.USER.name())) {
                liferayCreatedOrUpdated = updateLiferayUser(request, liferayCreatedOrUpdated, givenNameFromReq, lastNameFromReq, emailFromReq, departmentFromReq, externalIdFromReq, pwdFromReq, primaryRoleFromReq);
            }
        } else {
            liferayCreatedOrUpdated = updateLiferayUser(request, liferayUserByEmailAddress, givenNameFromReq, lastNameFromReq, emailFromReq, departmentFromReq, externalIdFromReq, pwdFromReq, primaryRoleFromReq);
        }
    } else {
        try {
            ArrayList<UserCSV> users = new ArrayList<UserCSV>();
            UserCSV userCSVToCreateOrg = new UserCSV();
            userCSVToCreateOrg.setDepartment(departmentFromReq);
            users.add(userCSVToCreateOrg);
            createOrganizations(request, users);
        } catch (SystemException | PortalException e) {
            log.error("Error creating organizations " + departmentFromReq, e);
            throw new SW360Exception("Error creating organizations");
        }
        if (isLiferayUserNew) {
            userCSV.setGivenname(givenNameFromReq);
            userCSV.setDepartment(departmentFromReq);
            userCSV.setEmail(emailFromReq);
            userCSV.setGid(externalIdFromReq);
            userCSV.setGroup(primaryRoleFromReq);
            userCSV.setLastname(lastNameFromReq);
            if (!primaryRoleFromReq.equalsIgnoreCase(UserGroup.ADMIN.name()) && !primaryRoleFromReq.equalsIgnoreCase(UserGroup.USER.name())) {
                userCSV.setGroup(UserGroup.USER.name());
            }
            if (CommonUtils.isNotNullEmptyOrWhitespace(pwdFromReq)) {
                String encryptedPwd = PasswordEncryptorUtil.encrypt(pwdFromReq);
                userCSV.setHash(encryptedPwd);
            }
            userCSV.setMale(false);
            liferayCreatedOrUpdated = dealWithUser(request, userCSV);
            if (liferayCreatedOrUpdated == null) {
                log.error("Error while creating the user : " + emailFromReq);
                throw new SW360Exception("Error while creating the user : " + emailFromReq);
            }
            if (!primaryRoleFromReq.equalsIgnoreCase(UserGroup.ADMIN.name()) && !primaryRoleFromReq.equalsIgnoreCase(UserGroup.USER.name())) {
                liferayCreatedOrUpdated = updateLiferayUser(request, liferayCreatedOrUpdated, givenNameFromReq, lastNameFromReq, emailFromReq, departmentFromReq, externalIdFromReq, pwdFromReq, primaryRoleFromReq);
            }
            org.eclipse.sw360.datahandler.thrift.users.User newlyCreatedUser = userClient.getByEmail(emailFromReq);
            newlyCreatedUser.setSecondaryDepartmentsAndRoles(secondaryDepartmentAndRolesMapFromRequest).setFullname(liferayCreatedOrUpdated.getFullName()).setPrimaryRoles(null).setUserGroup(userGroupFromString(primaryRoleFromReq));
            userClient.updateUser(newlyCreatedUser);
            response.setRenderParameter(PortalConstants.USER_EMAIL, emailFromReq);
            return false;
        } else {
            liferayCreatedOrUpdated = updateLiferayUser(request, liferayUserByEmailAddress, givenNameFromReq, lastNameFromReq, emailFromReq, departmentFromReq, externalIdFromReq, pwdFromReq, primaryRoleFromReq);
        }
    }
    userByEmailFromCouchDB.setGivenname(givenNameFromReq).setLastname(lastNameFromReq).setDepartment(departmentFromReq).setExternalid(externalIdFromReq).setFullname(liferayCreatedOrUpdated.getFullName()).setUserGroup(UserGroup.valueOf(primaryRoleFromReq)).setPrimaryRoles(null);
    userByEmailFromCouchDB.setSecondaryDepartmentsAndRoles(secondaryDepartmentAndRolesMapFromRequest);
    if (isCouchDBUserNew || userByEmailFromCouchDB.getId() == null) {
        userClient.addUser(userByEmailFromCouchDB.setEmail(emailFromReq));
    } else {
        if (!userByEmailFromCouchDB.getEmail().equals(emailFromReq)) {
            userByEmailFromCouchDB.setFormerEmailAddresses(UserUtils.prepareFormerEmailAddresses(userByEmailFromCouchDB, emailFromReq));
        }
        userClient.updateUser(userByEmailFromCouchDB.setEmail(emailFromReq));
    }
    if (originalEmail.equals(requestingUser.getEmail())) {
        HttpServletRequest httprequest = PortalUtil.getHttpServletRequest(request);
        httprequest.getSession().invalidate();
        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
        response.sendRedirect(themeDisplay.getURLSignOut());
        return true;
    } else {
        response.setRenderParameter(PortalConstants.USER_EMAIL, emailFromReq);
    }
    return false;
}
Also used : TException(org.apache.thrift.TException) ImmutableSet(com.google.common.collect.ImmutableSet) UserService(org.eclipse.sw360.datahandler.thrift.users.UserService) UserCSV(org.eclipse.sw360.portal.users.UserCSV) UserUtils.userGroupFromString(org.eclipse.sw360.portal.users.UserUtils.userGroupFromString) HttpServletRequest(javax.servlet.http.HttpServletRequest) AutoLoginException(com.liferay.portal.kernel.security.auto.login.AutoLoginException) SystemException(com.liferay.portal.kernel.exception.SystemException) SSOAutoLogin(org.eclipse.sw360.portal.users.SSOAutoLogin) PortalException(com.liferay.portal.kernel.exception.PortalException) ThemeDisplay(com.liferay.portal.kernel.theme.ThemeDisplay) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 2 with AutoLoginException

use of com.liferay.portal.kernel.security.auto.login.AutoLoginException in project sw360 by eclipse.

the class SSOAutoLogin method login.

@Override
public String[] login(HttpServletRequest request, HttpServletResponse response) throws AutoLoginException {
    dumpHeadersToLog(request);
    String email = request.getHeader(AUTH_EMAIL_HEADER);
    String extId = request.getHeader(AUTH_EXTID_HEADER);
    String givenName = request.getHeader(AUTH_GIVEN_NAME_HEADER);
    String surname = request.getHeader(AUTH_SURNAME_HEADER);
    String department = request.getHeader(AUTH_DEPARTMENT_HEADER);
    log.info(String.format("Attempting auto login for email: '%s', external ID: '%s', given name: '%s', surname: '%s', group: %s", email, extId, givenName, surname, department));
    if (isNullEmptyOrWhitespace(email)) {
        log.error("Empty credentials, auto login impossible.");
        return new String[] {};
    }
    long companyId = PortalUtil.getCompanyId(request);
    try {
        String organizationName = orgHelper.mapOrganizationName(department);
        Organization organization = orgHelper.addOrGetOrganization(organizationName, companyId);
        log.info(String.format("Mapped orgcode %s to %s", department, organizationName));
        User user = findOrCreateLiferayUser(request, email, extId, givenName, surname, companyId, organizationName);
        user = updateLiferayUserEmailIfNecessary(email, user);
        orgHelper.reassignUserToOrganizationIfNecessary(user, organization);
        // Create a return credentials object
        return new String[] { String.valueOf(user.getUserId()), // Encrypted Liferay password
        user.getPassword(), // True: password is encrypted
        Boolean.TRUE.toString() };
    } catch (SystemException | PortalException e) {
        log.error("Exception during login of user: '" + email + "' and company id: '" + companyId + "'", e);
        throw new AutoLoginException(e);
    }
}
Also used : AutoLoginException(com.liferay.portal.kernel.security.auto.login.AutoLoginException) Organization(com.liferay.portal.kernel.model.Organization) User(com.liferay.portal.kernel.model.User) SystemException(com.liferay.portal.kernel.exception.SystemException) PortalException(com.liferay.portal.kernel.exception.PortalException)

Example 3 with AutoLoginException

use of com.liferay.portal.kernel.security.auto.login.AutoLoginException in project sw360 by eclipse.

the class SSOAutoLogin method createLiferayUser.

@NotNull
public User createLiferayUser(HttpServletRequest request, String emailId, String extid, String givenName, String surname, long companyId, String organizationName) throws SystemException, PortalException {
    User user;
    String password = UUID.randomUUID().toString();
    user = UserPortletUtils.addLiferayUser(request, givenName, surname, emailId, organizationName, RoleConstants.USER, false, extid, password, false, true);
    if (user == null) {
        throw new AutoLoginException("Couldn't create user for '" + emailId + "' and company id: '" + companyId + "'");
    }
    log.info("Created user %s", user);
    return user;
}
Also used : AutoLoginException(com.liferay.portal.kernel.security.auto.login.AutoLoginException) User(com.liferay.portal.kernel.model.User) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AutoLoginException (com.liferay.portal.kernel.security.auto.login.AutoLoginException)3 PortalException (com.liferay.portal.kernel.exception.PortalException)2 SystemException (com.liferay.portal.kernel.exception.SystemException)2 User (com.liferay.portal.kernel.model.User)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Organization (com.liferay.portal.kernel.model.Organization)1 ThemeDisplay (com.liferay.portal.kernel.theme.ThemeDisplay)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 TException (org.apache.thrift.TException)1 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)1 UserService (org.eclipse.sw360.datahandler.thrift.users.UserService)1 SSOAutoLogin (org.eclipse.sw360.portal.users.SSOAutoLogin)1 UserCSV (org.eclipse.sw360.portal.users.UserCSV)1 UserUtils.userGroupFromString (org.eclipse.sw360.portal.users.UserUtils.userGroupFromString)1 NotNull (org.jetbrains.annotations.NotNull)1