0

I am trying to consume Liferay's User entity to add users by writing code. The password is not encrypting, so log in is failing. The code is pasting below.

    int countOfUsr = UserLocalServiceUtil.getUsersCount();
    User user = UserLocalServiceUtil.createUser(countOfUsr + 1);
    Date date = new Date();
    user.setCompanyId(countOfUsr + 1);
    user.setCreateDate(date);
    user.setModifiedDate(date);
    user.setDefaultUser(false);
    user.setContactId(countOfUsr + 1);
    user.setPasswordEncrypted(true);
    user.setAgreedToTermsOfUse(true);
    user.setPassword("123");
    user.setPasswordReset(false);
    user.setPasswordModifiedDate(date);
    user.setReminderQueryQuestion("what-is-your-father's-middle-name");
    user.setReminderQueryAnswer("daddad");
    user.setGraceLoginCount(0);
    user.setScreenName("shibu");
    user.setFirstName("SHIBU");
    user.setEmailAddress("shibu@liferay.com");
    user.setFacebookId(0);
    user.setOpenId("");
    user.setPortraitId(0);
    user.setLanguageId("en_US");
    user.setTimeZoneId("GMT");
    UserLocalServiceUtil.addUser(user);
  1. How to modify the code to save the user properly?
  2. What to do for pssword encryption?
  3. How to give value for user.setDigest(arg)?
Dani
  • 3,744
  • 4
  • 27
  • 35
Boat
  • 515
  • 2
  • 8
  • 28

2 Answers2

1

Specify the encryption algorithm to encrypt passwords in portal-ext.properties file.

For eg.,

passwords.encryption.algorithm=SHA //Check out different algorithms in portal.properties

Vikas V
  • 3,176
  • 2
  • 37
  • 60
0

I think you should use UserLocalServiceUtil.addUser(whole bunch of arguments) instead of UserLocalServiceUtil.addUser(User). It will do what you want : create your user and encrypt the password.

The method signature is :

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, int prefixId, int 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, SystemException

Beware : if your password do not validate the password policie, it will throw a UserPasswordException

Dani
  • 3,744
  • 4
  • 27
  • 35
TahitianGabriel
  • 712
  • 6
  • 18