Foros de discusión

Create / Add new user via API liferay

user1 liferay, modificado hace 7 años.

Create / Add new user via API liferay

Junior Member Mensajes: 28 Fecha de incorporación: 17/02/14 Mensajes recientes
Hi

I am trying to create a new user with the API, as per the instructions in the linked below https://web.liferay.com/community/forums/-/message_boards/message/14332034

But i encounter,

Caused by: com.liferay.portal.NoSuchContactException: No Contact exists with the primary key


Has anyone had the same issues before
Also, what should be the gpIdForUser in the below code if the user does not belong to any group, null?

Here's my code

public User addNewUser(ActionRequest actionRequest, NewUser newUser , long gpIdForUser) throws PortalException, SystemException {

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

long companyId = themeDisplay.getCompanyId();
long groupId = gpIdForUser;
String password1 = newUser.getPassword();
long facebookId = 0;
String openId = null;
String firstName = newUser.getFirstName();
String lastName = newUser.getLastName();
int prefixId = prefixID;
int suffixId = suffixID;
String jobTitle = newUser.getJobTitle();
String screenName = newUser.getScreenName();
if(isValidScreenName(screenName, companyId) == false )
throw new RuntimeException("Invalid Screen Name ");

String emailAddress = newUser.getEmailAddress();
Role role = RoleLocalServiceUtil.getRole(companyId, newUser.getRoles().getRoleName());

User user = null;
if (role != null) {
String greeting = "Welcome " + role.getName();
long id = CounterLocalServiceUtil.increment(); // CounterLocalServiceUtil.increment(User.class.getName());
user = UserLocalServiceUtil.createUser(id);
user.setCompanyId(companyId);
user.setPassword(password1);
user.setScreenName(screenName);
user.setGreeting(greeting);
user.setEmailAddress(emailAddress);
user.setFacebookId(facebookId);
user.setOpenId(openId);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setJobTitle(jobTitle);
user.setCreateDate(new Date());
user.setContactId(id + 1);
user.setPasswordReset(false);
user.setPasswordModifiedDate(new Date());
user.setCreateDate(new Date());
user.setModifiedDate(new Date());
user.setLanguageId(themeDisplay.getLanguageId());
user.setPasswordEncrypted(false);
user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
UserLocalServiceUtil.addUser(user);



// Associate a role with user
long userid[] = { user.getUserId() };
UserLocalServiceUtil.addRoleUsers(role.getRoleId(), userid);
long roleids[] = { role.getRoleId(), WHSUSER_ROLE, DEFAULT_USER_ROLE};
UserGroupRoleLocalServiceUtil.addUserGroupRoles(user.getUserId(),groupId, roleids);
ClassName clsNameUser = ClassNameLocalServiceUtil.getClassName("com.liferay.portal.model.User");
long classNameId = clsNameUser.getClassNameId();



// Insert Group for a user
long gpId = CounterLocalServiceUtil.increment(Group.class.getName());
Group userGrp = GroupLocalServiceUtil.createGroup(gpId);
userGrp.setClassNameId(classNameId);
userGrp.setClassPK(user.getUserId());
userGrp.setCompanyId(companyId);
//userGrp.setName("group" + String.valueOf(user.getUserId()));
//userGrp.setFriendlyURL("/group" + gpId);
userGrp.setName(String.valueOf(user.getUserId()));
userGrp.setFriendlyURL("/" + newUser.getScreenName());

userGrp.setCreatorUserId(PortalUtil.getUserId(actionRequest));
userGrp.setActive(true);
GroupLocalServiceUtil.addGroup(userGrp);



// Insert Contact for a user
long idContact = user.getUserId() + 1;// CounterLocalServiceUtil.increment(Contact.class.getName()); //user.getUserId() + 1;//
Contact contact = ContactLocalServiceUtil.createContact(idContact);
contact.setCompanyId(companyId);
contact.setCreateDate(new Date());
contact.setUserName("Test Test");
contact.setUserId(user.getUserId());
contact.setModifiedDate(new Date());
contact.setFirstName(newUser.getFirstName());
contact.setLastName(newUser.getLastName());
contact.setMiddleName(null);
contact.setPrefixId(prefixId);
contact.setBirthday(new Date());
contact.setSuffixId(suffixId);
contact.setJobTitle(jobTitle);
ContactLocalServiceUtil.addContact(contact);



// Create AssetEntry
long assetEntryId = CounterLocalServiceUtil.increment(AssetEntry.class.getName()); //user.getUserId();//
AssetEntry ae = AssetEntryLocalServiceUtil.createAssetEntry(assetEntryId);
ae.setCompanyId(companyId);
ae.setClassPK(user.getUserId());
ae.setGroupId(userGrp.getGroupId());
ae.setClassNameId(classNameId); // classnameid -- > 10005 --> user
AssetEntryLocalServiceUtil.addAssetEntry(ae);



// Insert ResourcePermission for a User
long resPermId = CounterLocalServiceUtil.increment(ResourcePermission.class.getName());
ResourcePermission rpEntry = ResourcePermissionLocalServiceUtil.createResourcePermission(resPermId);
rpEntry.setCompanyId(companyId);
rpEntry.setName("com.liferay.portal.model.User");
rpEntry.setRoleId(role.getRoleId());
rpEntry.setScope(4);
ResourcePermissionLocalServiceUtil.addResourcePermission(rpEntry);




// Insert Layoutset for public and private
long layoutSetIdPub = CounterLocalServiceUtil.increment(LayoutSet.class.getName());
LayoutSet layoutSetPub = LayoutSetLocalServiceUtil.createLayoutSet(layoutSetIdPub);
layoutSetPub.setCompanyId(companyId);
layoutSetPub.setPrivateLayout(false);
layoutSetPub.setGroupId(userGrp.getGroupId());
layoutSetPub.setThemeId("classic");
try {
LayoutSetLocalServiceUtil.addLayoutSet(layoutSetPub);
} catch (Exception se) {

}



long layoutSetIdPriv = CounterLocalServiceUtil.increment(LayoutSet.class.getName());
LayoutSet layoutSetPriv = LayoutSetLocalServiceUtil.createLayoutSet(layoutSetIdPriv);
layoutSetPriv.setCompanyId(companyId);
layoutSetPriv.setPrivateLayout(true);
layoutSetPriv.setThemeId("classic");
layoutSetPriv.setGroupId(userGrp.getGroupId());
try {
LayoutSetLocalServiceUtil.addLayoutSet(layoutSetPriv);
} catch (Exception se) {
}
} else {
System.out.println("Invalid role "+newUser.getRoles().getRoleName());
return null;
}

return user;

}
thumbnail
David H Nebinger, modificado hace 7 años.

RE: Create / Add new user via API liferay

Liferay Legend Mensajes: 14917 Fecha de incorporación: 2/09/06 Mensajes recientes
This is nuts.

Why aren't you using the already created method, addUserWithWorkflow()? You pass in the values and let Liferay manage all of the supporting objects.