Example usage for com.liferay.portal.kernel.service UserLocalService addUser

List of usage examples for com.liferay.portal.kernel.service UserLocalService addUser

Introduction

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

Prototype

public User addUser(long creatorUserId, long companyId, boolean autoPassword, String password1,
        String password2, boolean autoScreenName, String screenName, String emailAddress, long facebookId,
        String openId, Locale locale, String firstName, String middleName, String lastName, long prefixId,
        long suffixId, boolean male, int birthdayMonth, int birthdayDay, int birthdayYear, String jobTitle,
        long[] groupIds, long[] organizationIds, long[] roleIds, long[] userGroupIds, boolean sendEmail,
        ServiceContext serviceContext) throws PortalException;

Source Link

Document

Adds a user.

Usage

From source file:com.liferay.faces.demos.bean.TestSetupBackingBean.java

License:Open Source License

public void addUser(long creatorUserId, long groupId, long companyId, String firstName, String lastName)
        throws PortalException {

    boolean autoPassword = false;
    String password1 = "test";
    String password2 = password1;
    boolean autoScreenName = false;
    String screenName = firstName.toLowerCase() + "." + lastName.toLowerCase();
    String emailAddress = screenName + "@" + "liferay.com";
    long facebookId = 0L;
    String openId = "";
    Locale locale = Locale.ENGLISH;
    String middleName = "";
    int prefixId = 0;
    int suffixId = 0;
    boolean male = true;
    int birthdayMonth = 1;
    int birthdayDay = 1;
    int birthdayYear = 1970;
    String jobTitle = "";
    long[] groupIds = new long[] { groupId };
    long[] organizationIds = new long[] {};
    long[] roleIds = new long[] {};
    long[] userGroupIds = new long[] {};
    boolean sendEmail = false;
    ServiceContext serviceContext = new ServiceContext();
    UserLocalService userLocalService;

    if (userLocalServiceTracker.isEmpty()) {
        FacesContextHelperUtil.addGlobalErrorMessage("is-temporarily-unavailable", "User service");
    } else {/*w  ww.j  ava  2 s .c o  m*/
        userLocalService = userLocalServiceTracker.getService();

        boolean addUser = false;

        try {
            User user = userLocalService.getUserByScreenName(companyId, screenName);

            if ("john.adams".equals(screenName)) {
                userLocalService.deleteUser(user);
                addUser = true;
            }
        } catch (NoSuchUserException e) {
            addUser = true;
        }

        if (addUser) {
            userLocalService.addUser(creatorUserId, companyId, autoPassword, password1, password2,
                    autoScreenName, screenName, emailAddress, facebookId, openId, locale, firstName, middleName,
                    lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
                    groupIds, organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
            logger.debug("Added user=[{0}]", screenName);
        }
    }
}