Example usage for com.liferay.portal.kernel.service UserLocalServiceUtil getUserById

List of usage examples for com.liferay.portal.kernel.service UserLocalServiceUtil getUserById

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.service UserLocalServiceUtil getUserById.

Prototype

public static com.liferay.portal.kernel.model.User getUserById(long userId)
        throws com.liferay.portal.kernel.exception.PortalException 

Source Link

Document

Returns the user with the primary key.

Usage

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

@Override
public void updateUserOrganizations(long userId, long[] organizationIds) throws PortalException {

    _log.info("Updating user phones ... ");

    _log.debug("    ... security check ...");

    PortalPermissionUtil.check(getPermissionChecker(), AngularActionKeys.UPDATE_ORGANIZATION);

    _log.debug("    ... processing ... ");

    long creatorId = 0;

    try {//from w  ww  .j a va 2  s .c  om
        creatorId = this.getUserId();
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get current user ID", pe);
        }
    }

    User user = UserLocalServiceUtil.getUserById(userId);

    this.checkUpdateUserPermission(creatorId, userId, user.getCompanyId(), null, null, null, null, null);

    // Organization membership policy

    long[] oldOrganizationIds = user.getOrganizationIds();

    List<Long> addOrganizationIds = new ArrayList<>();
    List<Long> removeOrganizationIds = Collections.emptyList();

    if (organizationIds != null) {
        removeOrganizationIds = ListUtil.toList(oldOrganizationIds);

        organizationIds = angularOrganizationService.checkOrganizations(userId, organizationIds);

        for (long organizationId : organizationIds) {
            if (ArrayUtil.contains(oldOrganizationIds, organizationId)) {
                removeOrganizationIds.remove(organizationId);
            } else {
                addOrganizationIds.add(organizationId);
            }
        }

        OrganizationLocalServiceUtil.addUserOrganizations(user.getUserId(), organizationIds);

        UserLocalServiceUtil.updateOrganizations(user.getUserId(), organizationIds, null);

        if (!addOrganizationIds.isEmpty() || !removeOrganizationIds.isEmpty()) {

            OrganizationMembershipPolicyUtil.checkMembership(new long[] { userId },
                    ArrayUtil.toLongArray(addOrganizationIds), ArrayUtil.toLongArray(removeOrganizationIds));
        }
    }

}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

@Override
public void updateUserPhones(long userId, long[] phones) throws PortalException {

    _log.info("Updating user phones ... ");

    _log.debug("    ... security check ...");

    PortalPermissionUtil.check(getPermissionChecker(), AngularActionKeys.UPDATE_USER);

    _log.debug("    ... processing ... ");

    long creatorId = 0;

    try {//from   www.j a  va 2  s. c om
        creatorId = this.getUserId();
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get current user ID", pe);
        }
    }

    User user = UserLocalServiceUtil.getUserById(userId);

    //this.checkUpdateUserPermission(creatorId, userId, user.getCompanyId(), null, null, null, null, null);

    //this.updateUserPhones(user.getContactId(), phones);
}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

@Override
public void updateUserWebsites(long userId, List<Website> website) throws PortalException {

    _log.info("Updating user websites ... ");

    _log.debug("    ... security check ...");

    PortalPermissionUtil.check(getPermissionChecker(), AngularActionKeys.UPDATE_USER);

    _log.debug("    ... processing ... ");

    long creatorId = 0;

    try {//from www . j a va  2  s .c o  m
        creatorId = this.getUserId();
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get current user ID", pe);
        }
    }

    User user = UserLocalServiceUtil.getUserById(userId);

    this.checkUpdateUserPermission(creatorId, userId, user.getCompanyId(), null, null, null, null, null);

    this.updateUsrWebsites(user.getContactId(), website);
}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

@Override
public void updateUserEmailAddresses(long userId, List<EmailAddress> emailAddresses) throws PortalException {

    _log.info("Updating user email addresses ... ");

    _log.debug("    ... security check ...");

    PortalPermissionUtil.check(getPermissionChecker(), AngularActionKeys.UPDATE_USER);

    _log.debug("    ... processing ... ");

    long creatorId = 0;

    try {// ww w  .java2s .  c om
        creatorId = this.getUserId();
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get current user ID", pe);
        }
    }

    User user = UserLocalServiceUtil.getUserById(userId);

    this.checkUpdateUserPermission(creatorId, userId, user.getCompanyId(), null, null, null, null, null);

    this.updateUserEmailAddress(user.getContactId(), emailAddresses);
}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

@Override
public Address updateUserAddress(long userId, long addressId, String street1, String street2, String street3,
        String city, String zip, long regionId, long countryId, long typeId, boolean shipping, boolean primary)
        throws PortalException {

    _log.info("Updating user addresses ... ");

    _log.debug("    ... security check ...");

    PortalPermissionUtil.check(getPermissionChecker(), AngularActionKeys.UPDATE_USER);

    _log.debug("    ... processing ... ");

    long creatorId = 0;

    try {//from w w  w . j  a  va2 s . c o m
        creatorId = this.getUserId();
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get current user ID", pe);
        }
    }

    _log.debug("   saving information ... ");

    User user = UserLocalServiceUtil.getUserById(userId);

    this.checkUpdateUserPermission(creatorId, userId, user.getCompanyId(), null, null, null, null, null);

    Address addressToUpdate = AddressLocalServiceUtil.getAddress(addressId);

    List<Address> addresses = new ArrayList<Address>();
    addresses.add(addressToUpdate);

    addressToUpdate.setStreet1(street1);
    addressToUpdate.setStreet2(street2);
    addressToUpdate.setStreet3(street3);
    addressToUpdate.setZip(zip);
    addressToUpdate.setCity(city);
    addressToUpdate.setRegionId(regionId);
    addressToUpdate.setCountryId(countryId);
    addressToUpdate.setTypeId(typeId);
    addressToUpdate.setMailing(shipping);
    addressToUpdate.setPrimary(primary);

    return AddressLocalServiceUtil.updateAddress(addressToUpdate);
}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

@Override
public void updateUserUserGroups(long userId, long[] userGroupIds) throws PortalException {

    _log.info("Updating user groups ... ");

    _log.debug("    ... security check ...");

    PortalPermissionUtil.check(getPermissionChecker(), AngularActionKeys.UPDATE_USER);

    _log.debug("    ... processing ... ");

    long creatorId = 0;

    try {//from w ww  . ja v  a 2 s .  co  m
        creatorId = this.getUserId();
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get current user ID", pe);
        }
    }

    User user = UserLocalServiceUtil.getUserById(userId);

    this.checkUpdateUserPermission(creatorId, userId, user.getCompanyId(), null, null, null, userGroupIds,
            null);

    // User group membership policy

    long[] oldUserGroupIds = user.getUserGroupIds();

    List<Long> addUserGroupIds = new ArrayList<>();
    List<Long> removeUserGroupIds = Collections.emptyList();

    if (userGroupIds != null) {
        removeUserGroupIds = ListUtil.toList(oldUserGroupIds);

        userGroupIds = angularUserGroupService.checkUserGroupIds(userId, userGroupIds);

        for (long userGroupId : userGroupIds) {
            if (ArrayUtil.contains(oldUserGroupIds, userGroupId)) {
                removeUserGroupIds.remove(userGroupId);
            } else {
                addUserGroupIds.add(userGroupId);
            }
        }

        UserGroupLocalServiceUtil.addUserUserGroups(user.getUserId(), userGroupIds);

        if (!addUserGroupIds.isEmpty() || !removeUserGroupIds.isEmpty()) {
            UserGroupMembershipPolicyUtil.checkMembership(new long[] { userId },
                    ArrayUtil.toLongArray(addUserGroupIds), ArrayUtil.toLongArray(removeUserGroupIds));
        }
    }
}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

@Override
public void updateUserRoles(long userId, long[] roleIds) throws PortalException {

    _log.info("Updating user roles ... ");

    _log.debug("    ... security check ...");

    PortalPermissionUtil.check(getPermissionChecker(), AngularActionKeys.UPDATE_USER);

    _log.debug("    ... processing ... ");

    long creatorId = 0;

    try {/*from  ww w  .  ja  va  2s  . co m*/
        creatorId = this.getUserId();
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get current user ID", pe);
        }
    }

    User user = UserLocalServiceUtil.getUserById(userId);

    this.checkUpdateUserPermission(creatorId, userId, user.getCompanyId(), null, null, roleIds, null, null);

    long[] oldRoleIds = user.getRoleIds();

    List<Long> addRoleIds = new ArrayList<>();
    List<Long> removeRoleIds = Collections.emptyList();

    if (roleIds != null) {
        removeRoleIds = ListUtil.toList(oldRoleIds);

        roleIds = angularRoleService.checkRoles(userId, roleIds);

        for (long roleId : roleIds) {
            if (ArrayUtil.contains(oldRoleIds, roleId)) {
                removeRoleIds.remove(roleId);
            } else {
                addRoleIds.add(roleId);
            }
        }

        RoleLocalServiceUtil.addUserRoles(user.getUserId(), ArrayUtil.toLongArray(addRoleIds));

        if (!addRoleIds.isEmpty() || !removeRoleIds.isEmpty()) {
            RoleMembershipPolicyUtil.checkRoles(new long[] { userId }, ArrayUtil.toLongArray(addRoleIds),
                    ArrayUtil.toLongArray(removeRoleIds));
        }
    }

    long[] newRoleIds = ArrayUtil.toLongArray(addRoleIds);

    if (!addRoleIds.isEmpty() || !removeRoleIds.isEmpty()) {
        RoleMembershipPolicyUtil.propagateRoles(new long[] { user.getUserId() },
                ArrayUtil.toLongArray(addRoleIds), ArrayUtil.toLongArray(removeRoleIds));
    }
}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

@Override
public User updateUser(long userId, String oldPassword, String newPassword1, String newPassword2,
        boolean passwordReset, String reminderQueryQuestion, String reminderQueryAnswer, String screenName,
        String emailAddress, long facebookId, String openId, boolean portrait, byte[] portraitBytes,
        String languageId, String timeZoneId, String greeting, String comments, String firstName,
        String middleName, String lastName, long prefixId, long suffixId, boolean male, int birthdayMonth,
        int birthdayDay, int birthdayYear, String smsSn, String facebookSn, String jabberSn, String skypeSn,
        String twitterSn, String jobTitle, long[] groupIds, long[] organizationIds, long[] roleIds,
        List<UserGroupRole> userGroupRoles, long[] userGroupIds, List<Address> addresses,
        List<EmailAddress> emailAddresses, List<Phone> phones, List<Website> websites,
        List<AnnouncementsDelivery> announcementsDelivers, ServiceContext serviceContext)
        throws PortalException {

    _log.info("Updating user ... ");

    _log.debug("    ... security check ...");

    PortalPermissionUtil.check(getPermissionChecker(), AngularActionKeys.UPDATE_USER);

    _log.debug("    ... processing ... ");

    long creatorId = 0;

    try {/*from   w  ww .j  a v  a2s .  com*/
        creatorId = this.getUserId();
    } catch (PrincipalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to get current user ID", pe);
        }
    }

    if (StringUtils.isEmpty(emailAddress)) {
        throw new PortalException("A email address is needed for new users!");
    }

    // Null safe actions
    if (groupIds == null) {
        groupIds = new long[0];
    }

    if (organizationIds == null) {
        organizationIds = new long[0];
    }

    if (roleIds == null) {
        roleIds = new long[0];
    }

    if (userGroupRoles == null) {
        userGroupRoles = new ArrayList<UserGroupRole>();
    }

    if (userGroupIds == null) {
        userGroupIds = new long[0];
    }

    if (addresses == null) {
        addresses = new ArrayList<Address>();
    }

    if (emailAddresses == null) {
        emailAddresses = new ArrayList<EmailAddress>();
    }

    if (phones == null) {
        phones = new ArrayList<Phone>();
    }

    if (websites == null) {
        websites = new ArrayList<Website>();
    }

    if (announcementsDelivers == null) {
        announcementsDelivers = new ArrayList<AnnouncementsDelivery>();
    }

    User userToUpdate = UserLocalServiceUtil.getUserById(userId);

    if (StringUtils.isEmpty(screenName)) {
        screenName = userToUpdate.getScreenName();
    }

    this.checkUpdateUserPermission(creatorId, userId, userToUpdate.getCompanyId(), groupIds, organizationIds,
            roleIds, userGroupIds, serviceContext);

    // Update contact parts
    this.updateUserAddresses(userToUpdate.getContactId(), addresses);
    this.updateUserEmailAddress(userToUpdate.getContactId(), emailAddresses);
    this.updateUserPhones(userToUpdate.getContactId(), phones);
    this.updateUsrWebsites(userToUpdate.getContactId(), websites);
    this.updateAnnouncementsDeliveries(userToUpdate.getContactId(), announcementsDelivers);

    if (creatorId == userId) {
        emailAddress = StringUtil.toLowerCase(emailAddress.trim());

        if (!StringUtil.equalsIgnoreCase(emailAddress, userToUpdate.getEmailAddress())) {

            validateEmail(emailAddress, userToUpdate);
        }
    }

    validateUpdatePermission(userToUpdate, screenName, emailAddress, firstName, middleName, lastName, prefixId,
            suffixId, birthdayMonth, birthdayDay, birthdayYear, male, jobTitle);

    // Group membership policy

    long[] oldGroupIds = userToUpdate.getGroupIds();

    List<Long> addGroupIds = new ArrayList<>();
    List<Long> removeGroupIds = Collections.emptyList();

    if (groupIds != null) {
        removeGroupIds = ListUtil.toList(oldGroupIds);

        groupIds = angularGroupService.checkGroups(userId, groupIds);

        for (long groupId : groupIds) {
            if (ArrayUtil.contains(oldGroupIds, groupId)) {
                removeGroupIds.remove(groupId);
            } else {
                addGroupIds.add(groupId);
            }
        }

        if (!addGroupIds.isEmpty() || !removeGroupIds.isEmpty()) {
            SiteMembershipPolicyUtil.checkMembership(new long[] { userId }, ArrayUtil.toLongArray(addGroupIds),
                    ArrayUtil.toLongArray(removeGroupIds));
        }
    }

    // Organization membership policy

    long[] oldOrganizationIds = userToUpdate.getOrganizationIds();

    List<Long> addOrganizationIds = new ArrayList<>();
    List<Long> removeOrganizationIds = Collections.emptyList();

    if (organizationIds != null) {
        removeOrganizationIds = ListUtil.toList(oldOrganizationIds);

        organizationIds = angularOrganizationService.checkOrganizations(userId, organizationIds);

        for (long organizationId : organizationIds) {
            if (ArrayUtil.contains(oldOrganizationIds, organizationId)) {
                removeOrganizationIds.remove(organizationId);
            } else {
                addOrganizationIds.add(organizationId);
            }
        }

        if (!addOrganizationIds.isEmpty() || !removeOrganizationIds.isEmpty()) {

            OrganizationMembershipPolicyUtil.checkMembership(new long[] { userId },
                    ArrayUtil.toLongArray(addOrganizationIds), ArrayUtil.toLongArray(removeOrganizationIds));
        }
    }

    // Role membership policy

    long[] oldRoleIds = userToUpdate.getRoleIds();

    List<Long> addRoleIds = new ArrayList<>();
    List<Long> removeRoleIds = Collections.emptyList();

    if (roleIds != null) {
        removeRoleIds = ListUtil.toList(oldRoleIds);

        roleIds = angularRoleService.checkRoles(userId, roleIds);

        for (long roleId : roleIds) {
            if (ArrayUtil.contains(oldRoleIds, roleId)) {
                removeRoleIds.remove(roleId);
            } else {
                addRoleIds.add(roleId);
            }
        }

        if (!addRoleIds.isEmpty() || !removeRoleIds.isEmpty()) {
            RoleMembershipPolicyUtil.checkRoles(new long[] { userId }, ArrayUtil.toLongArray(addRoleIds),
                    ArrayUtil.toLongArray(removeRoleIds));
        }
    }

    List<UserGroupRole> oldOrganizationUserGroupRoles = new ArrayList<>();
    List<UserGroupRole> oldSiteUserGroupRoles = new ArrayList<>();

    List<UserGroupRole> oldUserGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(userId);

    for (UserGroupRole oldUserGroupRole : oldUserGroupRoles) {
        Role role = oldUserGroupRole.getRole();

        if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
            oldOrganizationUserGroupRoles.add(oldUserGroupRole);
        } else if (role.getType() == RoleConstants.TYPE_SITE) {
            oldSiteUserGroupRoles.add(oldUserGroupRole);
        }
    }

    List<UserGroupRole> addOrganizationUserGroupRoles = new ArrayList<>();
    List<UserGroupRole> removeOrganizationUserGroupRoles = Collections.emptyList();
    List<UserGroupRole> addSiteUserGroupRoles = new ArrayList<>();
    List<UserGroupRole> removeSiteUserGroupRoles = Collections.emptyList();

    if (userGroupRoles != null) {
        userGroupRoles = angularRoleService.checkUserGroupRoles(userId, userGroupRoles);

        removeOrganizationUserGroupRoles = ListUtil.copy(oldOrganizationUserGroupRoles);
        removeSiteUserGroupRoles = ListUtil.copy(oldSiteUserGroupRoles);

        for (UserGroupRole userGroupRole : userGroupRoles) {
            Role role = userGroupRole.getRole();

            if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
                if (oldOrganizationUserGroupRoles.contains(userGroupRole)) {
                    removeOrganizationUserGroupRoles.remove(userGroupRole);
                } else {
                    addOrganizationUserGroupRoles.add(userGroupRole);
                }
            } else if (role.getType() == RoleConstants.TYPE_SITE) {
                if (oldSiteUserGroupRoles.contains(userGroupRole)) {
                    removeSiteUserGroupRoles.remove(userGroupRole);
                } else {
                    addSiteUserGroupRoles.add(userGroupRole);
                }
            }
        }

        if (!addOrganizationUserGroupRoles.isEmpty() || !removeOrganizationUserGroupRoles.isEmpty()) {

            OrganizationMembershipPolicyUtil.checkRoles(addOrganizationUserGroupRoles,
                    removeOrganizationUserGroupRoles);
        }

        if (!addSiteUserGroupRoles.isEmpty() || !removeSiteUserGroupRoles.isEmpty()) {

            SiteMembershipPolicyUtil.checkRoles(addSiteUserGroupRoles, removeSiteUserGroupRoles);
        }
    }

    // User group membership policy

    long[] oldUserGroupIds = userToUpdate.getUserGroupIds();

    List<Long> addUserGroupIds = new ArrayList<>();
    List<Long> removeUserGroupIds = Collections.emptyList();

    if (userGroupIds != null) {
        removeUserGroupIds = ListUtil.toList(oldUserGroupIds);

        userGroupIds = angularUserGroupService.checkUserGroupIds(userId, userGroupIds);

        for (long userGroupId : userGroupIds) {
            if (ArrayUtil.contains(oldUserGroupIds, userGroupId)) {
                removeUserGroupIds.remove(userGroupId);
            } else {
                addUserGroupIds.add(userGroupId);
            }
        }

        if (!addUserGroupIds.isEmpty() || !removeUserGroupIds.isEmpty()) {
            UserGroupMembershipPolicyUtil.checkMembership(new long[] { userId },
                    ArrayUtil.toLongArray(addUserGroupIds), ArrayUtil.toLongArray(removeUserGroupIds));
        }
    }

    userToUpdate = userLocalService.updateUser(userId, oldPassword, newPassword1, newPassword2, passwordReset,
            reminderQueryQuestion, reminderQueryAnswer, screenName, emailAddress, facebookId, openId, portrait,
            portraitBytes, languageId, timeZoneId, greeting, comments, firstName, middleName, lastName,
            prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, smsSn, facebookSn, jabberSn,
            skypeSn, twitterSn, jobTitle, groupIds, organizationIds, roleIds, userGroupRoles, userGroupIds,
            serviceContext);

    if (!addGroupIds.isEmpty() || !removeGroupIds.isEmpty()) {
        SiteMembershipPolicyUtil.propagateMembership(new long[] { userToUpdate.getUserId() },
                ArrayUtil.toLongArray(addGroupIds), ArrayUtil.toLongArray(removeGroupIds));
    }

    if (!addOrganizationIds.isEmpty() || !removeOrganizationIds.isEmpty()) {
        OrganizationMembershipPolicyUtil.propagateMembership(new long[] { userToUpdate.getUserId() },
                ArrayUtil.toLongArray(addOrganizationIds), ArrayUtil.toLongArray(removeOrganizationIds));
    }

    if (!addRoleIds.isEmpty() || !removeRoleIds.isEmpty()) {
        RoleMembershipPolicyUtil.propagateRoles(new long[] { userToUpdate.getUserId() },
                ArrayUtil.toLongArray(addRoleIds), ArrayUtil.toLongArray(removeRoleIds));
    }

    if (!addSiteUserGroupRoles.isEmpty() || !removeSiteUserGroupRoles.isEmpty()) {

        SiteMembershipPolicyUtil.propagateRoles(addSiteUserGroupRoles, removeSiteUserGroupRoles);
    }

    if (!addOrganizationUserGroupRoles.isEmpty() || !removeOrganizationUserGroupRoles.isEmpty()) {

        OrganizationMembershipPolicyUtil.propagateRoles(addOrganizationUserGroupRoles,
                removeOrganizationUserGroupRoles);
    }

    if (!addUserGroupIds.isEmpty() || !removeUserGroupIds.isEmpty()) {
        UserGroupMembershipPolicyUtil.propagateMembership(new long[] { userToUpdate.getUserId() },
                ArrayUtil.toLongArray(addUserGroupIds), ArrayUtil.toLongArray(removeUserGroupIds));
    }

    return userToUpdate;
}

From source file:eu.gerhards.liferay.services.angular.service.impl.AngularUserServiceImpl.java

License:Open Source License

protected void reindex(long userId) throws SearchException, PortalException {
    User user = UserLocalServiceUtil.getUserById(userId);
    reindex(user);
}

From source file:ru.dokstrudio.med.srv.model.impl.ApplicationUsageModelImpl.java

License:Open Source License

@Override
public String getApplicationUserUuid() {
    try {/*ww  w  .j a  v a 2s  .  c  o m*/
        User user = UserLocalServiceUtil.getUserById(getApplicationUserId());

        return user.getUuid();
    } catch (PortalException pe) {
        return StringPool.BLANK;
    }
}