RE: User create from backend java code

thumbnail
Yan Paing, modified 6 Years ago. Regular Member Posts: 130 Join Date: 3/11/10 Recent Posts
Dear All,I created user from backend by using UserLocalServiceUtil as per following code. I tried with both  user.setPasswordEncrypted(true) and user.setPasswordEncrypted(false). However in database the password record is saved without encrypted. And also created user can't login. Please suggest. If possible please share some reference link or sample code for liferay 7 user creation with role and permission. Thank youlong userId = CounterLocalServiceUtil.increment(User.class.getName());
        _log.info("CounterLocalServiceUtil Done !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");
        User user = UserLocalServiceUtil.createUser(userId);
        user.setCompanyId(companyId);
        user.setCreateDate(new Date());
        user.setEmailAddress(emailAddress);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        user.setModifiedDate(new Date());
        user.setLanguageId(themeDisplay.getLanguageId());
        user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
        user.setPassword(password);
        user.setPasswordEncrypted(true);
        user.setReminderQueryAnswer("test");
        user.setReminderQueryQuestion("Test?");
        user.setScreenName(screenName);
        user.setUuid(serviceContext.getUuid());
        long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
        Contact contact = ContactLocalServiceUtil.createContact(idContact);
           contact.setCompanyId(themeDisplay.getCompanyId());
           contact.setCreateDate(new Date());
           contact.setUserName(screenName);
           contact.setUserId(user.getUserId());
           contact.setModifiedDate(new Date());
           contact.setFirstName("contact-"+contact.getContactId());
           contact.setLastName("contact-"+contact.getContactId());
           contact.setMiddleName("contact-"+contact.getContactId());
           contact.setPrefixId(0);
           contact.setSuffixId(0);
           contact.setJobTitle(StringPool.BLANK); 
           contact.setBirthday(new Date());
           contact=ContactLocalServiceUtil.addContact(contact); 
        user.setContactId(contact.getContactId());
        UserLocalServiceUtil.addUser(user);
thumbnail
David H Nebinger, modified 6 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
You should not be doing any of this nonsense.

Instead, you should just be calling userLocalService.addUserWithWorkflow(), populating all of the parameters.

And we don't use the static *LocalServiceUtil classes any more. Those are just for legacy code only. If you are using them at all, it is an indication that you are doing something wrong.
thumbnail
Yan Paing, modified 6 Years ago. Regular Member Posts: 130 Join Date: 3/11/10 Recent Posts
Hi Daviad,
Thank you for your response.  Is there any reference code or sample to download for liferay 7 version user creation from backend? I am taking sample from 
https://liferay.dev/forums/-/message_boards/message/14332034 , http://tanajilondhe.blogspot.com/2013/01/liferay-add-user-programmatically.htmlhttp://tanajilondhe.blogspot.com/2013/01/liferay-add-user-programmatically.html. But all are very old.   
thumbnail
Christoph Rabel, modified 6 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
The last one of your links looks pretty ok.
But the best place for examples is often the Liferay sourcecode itself.  e.g. here:
https://github.com/liferay/liferay-portal/blob/7.2.x/modules/apps/login/login-web/src/main/java/com/liferay/login/web/internal/portlet/action/CreateAccountMVCActionCommand.java
(I searched in the github repository for addUserWithWorkflow and that was one of the hits using that method)
We usually create the user with addUserWithWorkflow and then we set any extra fields (that the method doesn't have) afterwards. The method does all the necessary things like encrypting the password.
thumbnail
Yan Paing, modified 6 Years ago. Regular Member Posts: 130 Join Date: 3/11/10 Recent Posts
Thank you for reference Christoph.