Search in sources :

Example 56 with User

use of com.liferay.portal.model.User in project sw360portal by sw360.

the class UserPortletUtils method addLiferayUser.

private static User addLiferayUser(PortletRequest request, String firstName, String lastName, String emailAddress, long organizationId, long roleId, boolean isMale, String externalId, String password, boolean passwordEncrypted, boolean activateImmediately) {
    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
    String screenName = firstName + lastName;
    long companyId = themeDisplay.getCompanyId();
    try {
        if (userAlreadyExists(request, emailAddress, externalId, screenName, companyId)) {
            return null;
        }
    } catch (PortalException | SystemException e) {
        log.error(e);
        // won't try to create user if even checking for existing user failed
        return null;
    }
    try {
        ServiceContext serviceContext = ServiceContextFactory.getInstance(request);
        long[] roleIds = roleId == 0 ? new long[] {} : new long[] { roleId };
        long[] organizationIds = organizationId == 0 ? new long[] {} : new long[] { organizationId };
        long[] userGroupIds = null;
        long currentUserId = themeDisplay.getUserId();
        User user = UserLocalServiceUtil.addUser(currentUserId, /*creator*/
        companyId, false, /*autoPassword*/
        password, password, false, /*autoScreenName*/
        screenName, emailAddress, 0, /*facebookId*/
        externalId, /*openId*/
        themeDisplay.getLocale(), firstName, "", /*middleName*/
        lastName, 0, /*prefixId*/
        0, /*suffixId*/
        isMale, 4, /*birthdayMonth*/
        12, /*birthdayDay*/
        1959, /*birthdayYear*/
        "", /*jobTitle*/
        null, /*groupIds*/
        organizationIds, roleIds, userGroupIds, false, /*sendEmail*/
        serviceContext);
        user.setPasswordReset(false);
        if (passwordEncrypted) {
            user.setPassword(password);
            user.setPasswordEncrypted(true);
        }
        Role role = RoleLocalServiceUtil.getRole(roleId);
        RoleUtil.addUser(role.getRoleId(), user.getUserId());
        UserLocalServiceUtil.updateUser(user);
        RoleLocalServiceUtil.updateRole(role);
        UserLocalServiceUtil.updateStatus(user.getUserId(), activateImmediately ? WorkflowConstants.STATUS_APPROVED : WorkflowConstants.STATUS_INACTIVE);
        Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
        indexer.reindex(user);
        return user;
    } catch (PortalException | SystemException e) {
        log.error(e);
        return null;
    }
}
Also used : Role(com.liferay.portal.model.Role) User(com.liferay.portal.model.User) Indexer(com.liferay.portal.kernel.search.Indexer) SystemException(com.liferay.portal.kernel.exception.SystemException) PortalException(com.liferay.portal.kernel.exception.PortalException) ThemeDisplay(com.liferay.portal.theme.ThemeDisplay)

Example 57 with User

use of com.liferay.portal.model.User in project sw360portal by sw360.

the class SSOAutoLogin method login.

@Override
public String[] login(HttpServletRequest request, HttpServletResponse response) throws AutoLoginException {
    String emailId = request.getHeader(AUTH_EMAIL_VALUE);
    String extid = request.getHeader(AUTH_EXTID_VALUE);
    log.info("Attempting auto login for email: '" + emailId + "' and external id: '" + extid + "'");
    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String key = (String) headerNames.nextElement();
        String value = request.getHeader(key);
        log.debug(key + ":" + value);
    }
    if (emailId == null || emailId.isEmpty() || extid == null || extid.isEmpty()) {
        log.error("Empty credentials, auto login impossible.");
        return new String[] {};
    }
    long companyId = PortalUtil.getCompanyId(request);
    User user = null;
    try {
        user = UserLocalServiceUtil.getUserByEmailAddress(companyId, emailId);
    } catch (SystemException | PortalException e) {
        log.error("Exception during get user by email: '" + emailId + "' and company id: '" + companyId + "'", e);
    }
    // If user was found by liferay
    if (user != null) {
        // Create a return credentials object
        return new String[] { String.valueOf(user.getUserId()), // Encrypted Liferay password
        user.getPassword(), // True: password is encrypted
        Boolean.TRUE.toString() };
    } else {
        log.error("Could not get user with email: '" + emailId + "'.");
        return new String[] {};
    }
}
Also used : Enumeration(java.util.Enumeration) User(com.liferay.portal.model.User) SystemException(com.liferay.portal.kernel.exception.SystemException) PortalException(com.liferay.portal.kernel.exception.PortalException)

Example 58 with User

use of com.liferay.portal.model.User in project sw360portal by sw360.

the class UserPortlet method getCurrentUser.

private User getCurrentUser(PortletRequest request) throws SW360Exception {
    User user;
    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
    if (themeDisplay.isSignedIn())
        user = themeDisplay.getUser();
    else {
        throw new SW360Exception("Broken portlet!");
    }
    return user;
}
Also used : User(com.liferay.portal.model.User) ThemeDisplay(com.liferay.portal.theme.ThemeDisplay) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 59 with User

use of com.liferay.portal.model.User in project sw360portal by sw360.

the class UserPortlet method backUpUsers.

public void backUpUsers(ResourceRequest request, ResourceResponse response) throws PortletException, IOException, SystemException, PortalException {
    List<User> liferayUsers;
    try {
        liferayUsers = UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
    } catch (SystemException e) {
        log.error("Could not get user List from liferay", e);
        liferayUsers = Collections.emptyList();
    }
    final ByteArrayOutputStream outB = new ByteArrayOutputStream();
    Writer out = new BufferedWriter(new OutputStreamWriter(outB));
    CSVPrinter csvPrinter = new CSVPrinter(out, CommonUtils.sw360CsvFormat);
    csvPrinter.printRecord("GivenName", "Lastname", "Email", "Department", "UserGroup", "GID", "isMale", "PasswdHash", "wantsMailNotification");
    for (User liferayUser : liferayUsers) {
        String firstName = liferayUser.getFirstName();
        String lastName = liferayUser.getLastName();
        String emailAddress = liferayUser.getEmailAddress();
        List<Organization> organizations = liferayUser.getOrganizations();
        String department = "";
        if (organizations != null && organizations.size() > 0) {
            department = organizations.get(0).getName();
        }
        String gid = liferayUser.getOpenId();
        boolean isMale = liferayUser.isMale();
        String passwordHash = liferayUser.getPassword();
        if (isNullOrEmpty(emailAddress) || isNullOrEmpty(department)) {
            continue;
        }
        org.eclipse.sw360.datahandler.thrift.users.User sw360user = UserCacheHolder.getUserFromEmail(emailAddress);
        boolean wantsMailNotification = sw360user.isSetWantsMailNotification() ? sw360user.wantsMailNotification : true;
        String userGroup = sw360user.getUserGroup().toString();
        csvPrinter.printRecord(firstName, lastName, emailAddress, department, userGroup, gid, isMale, passwordHash, wantsMailNotification);
    }
    csvPrinter.flush();
    csvPrinter.close();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outB.toByteArray());
    PortletResponseUtil.sendFile(request, response, "Users.csv", byteArrayInputStream, "text/csv");
}
Also used : User(com.liferay.portal.model.User) CSVPrinter(org.apache.commons.csv.CSVPrinter) SystemException(com.liferay.portal.kernel.exception.SystemException)

Example 60 with User

use of com.liferay.portal.model.User in project liferay-ide by liferay.

the class KBArticleLocalServiceImpl method updateStatus.

@Override
public KBArticle updateStatus(long userId, long resourcePrimKey, int status, ServiceContext serviceContext) throws PortalException, SystemException {
    // KB article
    User user = userPersistence.findByPrimaryKey(userId);
    boolean main = false;
    Date now = new Date();
    if (status == WorkflowConstants.STATUS_APPROVED) {
        main = true;
    }
    KBArticle kbArticle = getLatestKBArticle(resourcePrimKey, WorkflowConstants.STATUS_ANY);
    kbArticle.setModifiedDate(serviceContext.getModifiedDate(now));
    kbArticle.setMain(main);
    kbArticle.setStatus(status);
    kbArticle.setStatusByUserId(user.getUserId());
    kbArticle.setStatusByUserName(user.getFullName());
    kbArticle.setStatusDate(serviceContext.getModifiedDate(now));
    kbArticlePersistence.update(kbArticle);
    if (status != WorkflowConstants.STATUS_APPROVED) {
        return kbArticle;
    }
    if (!kbArticle.isFirstVersion()) {
        KBArticle oldKBArticle = kbArticlePersistence.findByR_V(resourcePrimKey, kbArticle.getVersion() - 1);
        oldKBArticle.setMain(false);
        kbArticlePersistence.update(oldKBArticle);
    }
    // Asset
    AssetEntry assetEntry = assetEntryLocalService.getEntry(KBArticle.class.getName(), kbArticle.getKbArticleId());
    List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(assetEntry.getEntryId(), AssetLinkConstants.TYPE_RELATED);
    long[] assetLinkEntryIds = StringUtil.split(ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
    updateKBArticleAsset(userId, kbArticle, assetEntry.getCategoryIds(), assetEntry.getTagNames(), assetLinkEntryIds);
    SystemEventHierarchyEntryThreadLocal.push(KBArticle.class);
    try {
        assetEntryLocalService.deleteEntry(KBArticle.class.getName(), kbArticle.getKbArticleId());
    } finally {
        SystemEventHierarchyEntryThreadLocal.pop(KBArticle.class);
    }
    assetEntryLocalService.updateVisible(KBArticle.class.getName(), kbArticle.getResourcePrimKey(), true);
    // Social
    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
    extraDataJSONObject.put("title", kbArticle.getTitle());
    if (!kbArticle.isFirstVersion()) {
        socialActivityLocalService.addActivity(userId, kbArticle.getGroupId(), KBArticle.class.getName(), resourcePrimKey, AdminActivityKeys.UPDATE_KB_ARTICLE, extraDataJSONObject.toString(), 0);
    } else {
        socialActivityLocalService.addActivity(userId, kbArticle.getGroupId(), KBArticle.class.getName(), resourcePrimKey, AdminActivityKeys.ADD_KB_ARTICLE, extraDataJSONObject.toString(), 0);
    }
    // Indexer
    Indexer indexer = IndexerRegistryUtil.getIndexer(KBArticle.class);
    indexer.reindex(kbArticle);
    // Subscriptions
    notifySubscribers(kbArticle, serviceContext);
    return kbArticle;
}
Also used : AssetEntry(com.liferay.portlet.asset.model.AssetEntry) User(com.liferay.portal.model.User) Indexer(com.liferay.portal.kernel.search.Indexer) KBArticle(com.liferay.knowledgebase.model.KBArticle) JSONObject(com.liferay.portal.kernel.json.JSONObject) Date(java.util.Date) AssetLink(com.liferay.portlet.asset.model.AssetLink)

Aggregations

User (com.liferay.portal.model.User)97 Date (java.util.Date)22 SystemException (com.liferay.portal.kernel.exception.SystemException)17 Group (com.liferay.portal.model.Group)15 PortalException (com.liferay.portal.kernel.exception.PortalException)13 Company (com.liferay.portal.model.Company)11 ServiceContext (com.liferay.portal.service.ServiceContext)11 Indexable (com.liferay.portal.kernel.search.Indexable)10 UserGroup (com.liferay.portal.model.UserGroup)10 IOException (java.io.IOException)9 DuplicateUserEmailAddressException (com.liferay.portal.DuplicateUserEmailAddressException)8 EncryptorException (com.liferay.util.EncryptorException)7 Song (org.liferay.jukebox.model.Song)7 Contact (com.liferay.portal.model.Contact)6 PrincipalException (com.liferay.portal.security.auth.PrincipalException)6 Album (org.liferay.jukebox.model.Album)6 DuplicateUserScreenNameException (com.liferay.portal.DuplicateUserScreenNameException)5 GroupFriendlyURLException (com.liferay.portal.GroupFriendlyURLException)5 NoSuchImageException (com.liferay.portal.NoSuchImageException)5 NoSuchOrganizationException (com.liferay.portal.NoSuchOrganizationException)5