掲示板
How to Programmatically Add Liferay Users in Java
12年前 に Darren Smith によって更新されました。
How to Programmatically Add Liferay Users in Java
New Member 投稿: 14 参加年月日: 11/07/16 最新の投稿
Hi all,
does anyone have any sample Java code (or links to) to programmatically add users to Liferay 6? Does anyone know what database table users are saved to? I can't seem to find this information, APIs etc on the Liferay docs.
Thanks
Darren
does anyone have any sample Java code (or links to) to programmatically add users to Liferay 6? Does anyone know what database table users are saved to? I can't seem to find this information, APIs etc on the Liferay docs.
Thanks
Darren
12年前 に David H Nebinger によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
User creation results in multiple tables getting updated, the first of which is the User_ table, but there are many others. I would not attempt manual user creation by updating the tables directly.
Your best bet is probably to look at the various import process code (i.e. the LDAP user import, etc.). This path will probably identify any services or methods that you would want to leverage when creating users programmatically.
Your best bet is probably to look at the various import process code (i.e. the LDAP user import, etc.). This path will probably identify any services or methods that you would want to leverage when creating users programmatically.
12年前 に Pranay R Patadiya によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Regular Member 投稿: 177 参加年月日: 10/02/23 最新の投稿
Hi,
You should check portal source code for better reference.
You should check addUser method of UserLocalServiceImpl class of portal source code.
And about database entry, when you add user at that time not a single DB table being updated.
There are many tables are affected. The above mentioned class will let you know more about that.
Let me know if you have any query.
Thanks,
Pranay
You should check portal source code for better reference.
You should check addUser method of UserLocalServiceImpl class of portal source code.
And about database entry, when you add user at that time not a single DB table being updated.
There are many tables are affected. The above mentioned class will let you know more about that.
Let me know if you have any query.
Thanks,
Pranay
12年前 に Sagar A Vyas によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Liferay Master 投稿: 679 参加年月日: 09/04/17 最新の投稿
Hi,
Agree with Pranay,
Main table is user_ but though there are lot more.
Better to manage user operation from UserLocalServiceUtil instead of do it from DB.
you can user addUser() method ,you have to provide lots of argument for execute this method.
Thanks,
Sagar Vyas
Agree with Pranay,
Main table is user_ but though there are lot more.
Better to manage user operation from UserLocalServiceUtil instead of do it from DB.
you can user addUser() method ,you have to provide lots of argument for execute this method.
Thanks,
Sagar Vyas
12年前 に Darren Smith によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
New Member 投稿: 14 参加年月日: 11/07/16 最新の投稿
Thanks for your help guys, I can add users now .
Darren
Darren
12年前 に Arvind Mishra によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Regular Member 投稿: 226 参加年月日: 08/02/13 最新の投稿
If you want to add users pro grammatically using web service. You can look in to following post.
http://bit.ly/rq3WBx
http://bit.ly/rq3WBx
12年前 に RathnaDevi Chellaiah によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Junior Member 投稿: 29 参加年月日: 12/03/27 最新の投稿
hi,
can u send the project which is runnable? i am facing issue with user creation
can u send the project which is runnable? i am facing issue with user creation
11年前 に Rob Hall によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Junior Member 投稿: 47 参加年月日: 11/11/30 最新の投稿
I've figured out the programmatic user creation, what I'm struggling with is how to programatically log them into Liferay after adding them. I have a application-specific user registration portlet, the new user fills out some information, which I then use to create the Liferay user, populate the tables, etc. What I want to do next is automatically log them into Liferay and redirect to a landing page, without them having to enter their userid and password.
11年前 に Milen Dyankov によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Regular Member 投稿: 171 参加年月日: 09/09/23 最新の投稿Rob Hall:
What I want to do next is automatically log them into Liferay
To do this you need to create custom auto login hook.
Examine the source code of the existing hooks to see how it is done.
11年前 に Dhandapani S によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Regular Member 投稿: 176 参加年月日: 09/03/24 最新の投稿RathnaDevi Chellaiah:
hi,
can u send the project which is runnable? i am facing issue with user creation
Hi Rathnadevi,
You can add Liferay Users in Java using UserLocalServiceUtil class... For that u need to copy portal-impl.jar in your portlet lib folder and build path.
Regards
Dhandapani S
11年前 に Milen Dyankov によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Regular Member 投稿: 171 参加年月日: 09/09/23 最新の投稿Dhandapani S:
For that u need to copy portal-impl.jar in your portlet lib folder
That is a very bad advice!!! I would like to quote
Ray Augé:
NEVER put portal-impl into anywhere other than where it came from
Read this thread for details!
11年前 に Rajesh Chaurasia によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Regular Member 投稿: 183 参加年月日: 11/08/18 最新の投稿
In liferay , around some designated entities model listeners are attached which means that if you create and persist an instance of entity ,simultaneous inserts are made for model listeners.For eg if you create an organization in liferay , you need to insert entries for Asset entry,group,layout set , layout and resource permission taking care of ids .
Similar is the case with User creation.In liferay u can create user frorm UI but this does not stop u from creating users programmatically.
private User insertOrgAdmin(ActionRequest actionRequest,Organization organization,ServiceContext serviceContext,long gpIdForUser) throws PortalException, SystemException {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
long companyId = themeDisplay.getCompanyId();
long groupId= gpIdForUser;
String password1= "orgadmin";//organization.getName().toLowerCase();
String domainName = null;
long facebookId=0;
String openId=null;
//String firstName=organization.getName();
String lastName=organization.getName();
int prefixId=123;
int suffixId=234;
String jobTitle="Organization Administrator";
domainName = organization.getName();
domainName= domainName.trim();
if(domainName.contains(" ")) {
domainName = domainName.replaceAll("\\s+", "-");
}
//String emailAddress="administrator@"+domainName+".com";
String emailAddress="administrator@panchayatportal.gov.in";
Role role = RoleLocalServiceUtil.getRole(companyId, "Organization Administrator");
User user = null;
if(role!= null){
long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
//String greeting="Welcome "+role.getName();
String greeting="Welcome Admin";
long id = CounterLocalServiceUtil.increment(User.class.getName());
user = UserLocalServiceUtil.createUser(id);
user.setCompanyId(companyId);
user.setPassword(password1);
//String screenName=organization.getName().toLowerCase()+id;
String screenName="admin"+id;
user.setScreenName(screenName);
user.setEmailAddress(emailAddress);
user.setFacebookId(facebookId);
user.setOpenId(openId);
user.setGreeting(greeting);
//user.setFirstName(firstName);
user.setFirstName("Site Admin");
//user.setLastName(lastName);
user.setJobTitle(jobTitle);
user.setCreateDate(new Date ());
user.setContactId(idContact);
user.setPasswordReset(true);
user.setPasswordEncrypted(false);
user.setPasswordModifiedDate(new Date());
user.setCreateDate(new Date());
user.setModifiedDate(new Date());
user.setLanguageId(themeDisplay.getLanguageId());
user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
UserLocalServiceUtil.addUser(user);
//Associate a role with user
long userid[] = {user.getUserId()};
//UserLocalServiceUtil.addRoleUsers(role.getRoleId(), userid);
Role rolePu = RoleLocalServiceUtil.getRole(companyId, "Power User");
//UserLocalServiceUtil.addRoleUsers(rolePu.getRoleId(), userid);
long roleids[]={role.getRoleId(),rolePu.getRoleId()};
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(userid[0]);
userGrp.setCompanyId(companyId);
userGrp.setName("group"+String.valueOf(userid[0]));
userGrp.setFriendlyURL("/group"+gpId);
userGrp.setCreatorUserId(PortalUtil.getUserId(actionRequest));
userGrp.setActive(true);
GroupLocalServiceUtil.addGroup(userGrp);
//Insert Contact for a user
//long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
Contact contact = ContactLocalServiceUtil.createContact(idContact);
contact.setCompanyId(companyId);
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(prefixId);
contact.setSuffixId(suffixId);
contact.setJobTitle(jobTitle+contact.getContactId());
contact.setBirthday(new Date());
ContactLocalServiceUtil.addContact(contact);
//Create AssetEntry
long assetEntryId = CounterLocalServiceUtil.increment(AssetEntry.class.getName());
AssetEntry ae = AssetEntryLocalServiceUtil.createAssetEntry(assetEntryId);
ae.setCompanyId(companyId);
ae.setClassPK(user.getUserId());
ae.setGroupId(userGrp.getGroupId());
ae.setClassNameId(classNameId);
AssetEntryLocalServiceUtil.addAssetEntry(ae);
//Insert ResourcePermission for a User
long resPermId = CounterLocalServiceUtil.increment(ResourcePermission.class.getName());
ResourcePermission rpEntry = ResourcePermissionLocalServiceUtil.createResourcePermission(resPermId);
rpEntry.setCompanyId(organization.getCompanyId());
rpEntry.setName("com.liferay.portal.model.User");
rpEntry.setRoleId(role.getRoleId());
rpEntry.setScope(4);
rpEntry.setActionIds(31);
// rpEntry.setPrimaryKey(userid[0]);
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(SystemException 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(SystemException se){
}
}else{
return null;
}
return user;
}
Similar is the case with User creation.In liferay u can create user frorm UI but this does not stop u from creating users programmatically.
private User insertOrgAdmin(ActionRequest actionRequest,Organization organization,ServiceContext serviceContext,long gpIdForUser) throws PortalException, SystemException {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
long companyId = themeDisplay.getCompanyId();
long groupId= gpIdForUser;
String password1= "orgadmin";//organization.getName().toLowerCase();
String domainName = null;
long facebookId=0;
String openId=null;
//String firstName=organization.getName();
String lastName=organization.getName();
int prefixId=123;
int suffixId=234;
String jobTitle="Organization Administrator";
domainName = organization.getName();
domainName= domainName.trim();
if(domainName.contains(" ")) {
domainName = domainName.replaceAll("\\s+", "-");
}
//String emailAddress="administrator@"+domainName+".com";
String emailAddress="administrator@panchayatportal.gov.in";
Role role = RoleLocalServiceUtil.getRole(companyId, "Organization Administrator");
User user = null;
if(role!= null){
long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
//String greeting="Welcome "+role.getName();
String greeting="Welcome Admin";
long id = CounterLocalServiceUtil.increment(User.class.getName());
user = UserLocalServiceUtil.createUser(id);
user.setCompanyId(companyId);
user.setPassword(password1);
//String screenName=organization.getName().toLowerCase()+id;
String screenName="admin"+id;
user.setScreenName(screenName);
user.setEmailAddress(emailAddress);
user.setFacebookId(facebookId);
user.setOpenId(openId);
user.setGreeting(greeting);
//user.setFirstName(firstName);
user.setFirstName("Site Admin");
//user.setLastName(lastName);
user.setJobTitle(jobTitle);
user.setCreateDate(new Date ());
user.setContactId(idContact);
user.setPasswordReset(true);
user.setPasswordEncrypted(false);
user.setPasswordModifiedDate(new Date());
user.setCreateDate(new Date());
user.setModifiedDate(new Date());
user.setLanguageId(themeDisplay.getLanguageId());
user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
UserLocalServiceUtil.addUser(user);
//Associate a role with user
long userid[] = {user.getUserId()};
//UserLocalServiceUtil.addRoleUsers(role.getRoleId(), userid);
Role rolePu = RoleLocalServiceUtil.getRole(companyId, "Power User");
//UserLocalServiceUtil.addRoleUsers(rolePu.getRoleId(), userid);
long roleids[]={role.getRoleId(),rolePu.getRoleId()};
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(userid[0]);
userGrp.setCompanyId(companyId);
userGrp.setName("group"+String.valueOf(userid[0]));
userGrp.setFriendlyURL("/group"+gpId);
userGrp.setCreatorUserId(PortalUtil.getUserId(actionRequest));
userGrp.setActive(true);
GroupLocalServiceUtil.addGroup(userGrp);
//Insert Contact for a user
//long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
Contact contact = ContactLocalServiceUtil.createContact(idContact);
contact.setCompanyId(companyId);
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(prefixId);
contact.setSuffixId(suffixId);
contact.setJobTitle(jobTitle+contact.getContactId());
contact.setBirthday(new Date());
ContactLocalServiceUtil.addContact(contact);
//Create AssetEntry
long assetEntryId = CounterLocalServiceUtil.increment(AssetEntry.class.getName());
AssetEntry ae = AssetEntryLocalServiceUtil.createAssetEntry(assetEntryId);
ae.setCompanyId(companyId);
ae.setClassPK(user.getUserId());
ae.setGroupId(userGrp.getGroupId());
ae.setClassNameId(classNameId);
AssetEntryLocalServiceUtil.addAssetEntry(ae);
//Insert ResourcePermission for a User
long resPermId = CounterLocalServiceUtil.increment(ResourcePermission.class.getName());
ResourcePermission rpEntry = ResourcePermissionLocalServiceUtil.createResourcePermission(resPermId);
rpEntry.setCompanyId(organization.getCompanyId());
rpEntry.setName("com.liferay.portal.model.User");
rpEntry.setRoleId(role.getRoleId());
rpEntry.setScope(4);
rpEntry.setActionIds(31);
// rpEntry.setPrimaryKey(userid[0]);
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(SystemException 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(SystemException se){
}
}else{
return null;
}
return user;
}
10年前 に Orin Fink によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Junior Member 投稿: 65 参加年月日: 10/03/25 最新の投稿
Rajesh. I can't believe that nobody has said "Thank you" for that awesome post. I'm so grateful that you have shared all the work it took to build that snippet. Thanks.
10年前 に Kevin Emamirad によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
New Member 投稿: 12 参加年月日: 13/09/04 最新の投稿
Thanks Rajesh Chaurasia! Your code snippet saved me a good amount of time ;)
9年前 に driss driss によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
New Member 投稿: 3 参加年月日: 14/03/18 最新の投稿
hi
i m new in liferay comuniy,
could you please tell me how to user this code; where should i put it ?...
thanks for this wonderful job.
i m new in liferay comuniy,
could you please tell me how to user this code; where should i put it ?...
thanks for this wonderful job.
9年前 に Eswaran R によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Junior Member 投稿: 82 参加年月日: 14/12/29 最新の投稿
hi Rajesh Chaurasia,,
Thanks....
how to assign site a add liferay users porgrammatically ....
i create add user ..
Date date=new Date();
ServiceContext serviceContext = new ServiceContext();
serviceContext.setUuid(UUID.randomUUID().toString());
serviceContext.setCreateDate(date);
serviceContext.setModifiedDate(date);
long companyId=PortalUtil.getDefaultCompanyId();
long creatorUserId=0;
boolean autoPassword=false;
boolean autoScreenName=false;
boolean male=true;
boolean sendEmail = false;
int prefixId=1;
int suffixId=1;
int birthdayMonth=1;
int birthdayDay=1;
int birthdayYear=1970;
String screenName="sceene";
String jobTitle="";
long[] groupIds = null;
long[] organizationIds = null;
long[] userGroupIds = null;
String password1="test";
String password2="test";
String firstName="firstname";
String emailAddress="usertest@liferay.com";
long facebookId=0;
String openId="";
Locale locale=LocaleUtil.getDefault();
Role rolePu = RoleLocalServiceUtil.getRole(companyId, RoleConstants.POWER_USER);
long[] roleIds= {rolePu.getRoleId()};
User users43=UserLocalServiceUtil.addUserWithWorkflow(creatorUserId, companyId, autoPassword, password1,
password2, autoScreenName, screenName, emailAddress, facebookId, openId, locale, firstName,
"", "", prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
System.out.println(users43);
how to this user assign site.....
kindly help me...
thanks...
Regards..
Eswaran.R
Thanks....
how to assign site a add liferay users porgrammatically ....
i create add user ..
Date date=new Date();
ServiceContext serviceContext = new ServiceContext();
serviceContext.setUuid(UUID.randomUUID().toString());
serviceContext.setCreateDate(date);
serviceContext.setModifiedDate(date);
long companyId=PortalUtil.getDefaultCompanyId();
long creatorUserId=0;
boolean autoPassword=false;
boolean autoScreenName=false;
boolean male=true;
boolean sendEmail = false;
int prefixId=1;
int suffixId=1;
int birthdayMonth=1;
int birthdayDay=1;
int birthdayYear=1970;
String screenName="sceene";
String jobTitle="";
long[] groupIds = null;
long[] organizationIds = null;
long[] userGroupIds = null;
String password1="test";
String password2="test";
String firstName="firstname";
String emailAddress="usertest@liferay.com";
long facebookId=0;
String openId="";
Locale locale=LocaleUtil.getDefault();
Role rolePu = RoleLocalServiceUtil.getRole(companyId, RoleConstants.POWER_USER);
long[] roleIds= {rolePu.getRoleId()};
User users43=UserLocalServiceUtil.addUserWithWorkflow(creatorUserId, companyId, autoPassword, password1,
password2, autoScreenName, screenName, emailAddress, facebookId, openId, locale, firstName,
"", "", prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
System.out.println(users43);
how to this user assign site.....
kindly help me...
thanks...
Regards..
Eswaran.R
9年前 に Eswaran R によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Junior Member 投稿: 82 参加年月日: 14/12/29 最新の投稿
Hi friends ..........
how to this user assign site.....
Answer below..
Date date=new Date();
ServiceContext serviceContext = new ServiceContext();
serviceContext.setUuid(UUID.randomUUID().toString());
serviceContext.setCreateDate(date);
serviceContext.setModifiedDate(date);
long companyId=PortalUtil.getDefaultCompanyId();
long creatorUserId=0;
boolean autoPassword=false;
boolean autoScreenName=false;
boolean male=true;
boolean sendEmail = false;
int prefixId=1;
int suffixId=1;
int birthdayMonth=1;
int birthdayDay=1;
int birthdayYear=1970;
String screenName="sceene";
String jobTitle="";
long[] groupIds = null;
long[] organizationIds = {15970};
long[] userGroupIds = null;
String password1="test";
String password2="test";
String firstName="firstname";
String emailAddress="usertest@liferay.com";
long facebookId=0;
String openId="";
Locale locale=LocaleUtil.getDefault();
Role rolePu = RoleLocalServiceUtil.getRole(companyId, RoleConstants.POWER_USER);
long[] roleIds= {rolePu.getRoleId()};
User users43=UserLocalServiceUtil.addUserWithWorkflow(creatorUserId, companyId, autoPassword, password1,
password2, autoScreenName, screenName, emailAddress, facebookId, openId, locale, firstName,
"", "", prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
Regards..
Eswaran.R
how to this user assign site.....
Answer below..
Date date=new Date();
ServiceContext serviceContext = new ServiceContext();
serviceContext.setUuid(UUID.randomUUID().toString());
serviceContext.setCreateDate(date);
serviceContext.setModifiedDate(date);
long companyId=PortalUtil.getDefaultCompanyId();
long creatorUserId=0;
boolean autoPassword=false;
boolean autoScreenName=false;
boolean male=true;
boolean sendEmail = false;
int prefixId=1;
int suffixId=1;
int birthdayMonth=1;
int birthdayDay=1;
int birthdayYear=1970;
String screenName="sceene";
String jobTitle="";
long[] groupIds = null;
long[] organizationIds = {15970};
long[] userGroupIds = null;
String password1="test";
String password2="test";
String firstName="firstname";
String emailAddress="usertest@liferay.com";
long facebookId=0;
String openId="";
Locale locale=LocaleUtil.getDefault();
Role rolePu = RoleLocalServiceUtil.getRole(companyId, RoleConstants.POWER_USER);
long[] roleIds= {rolePu.getRoleId()};
User users43=UserLocalServiceUtil.addUserWithWorkflow(creatorUserId, companyId, autoPassword, password1,
password2, autoScreenName, screenName, emailAddress, facebookId, openId, locale, firstName,
"", "", prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
Regards..
Eswaran.R
9年前 に Raja Datla によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
New Member 投稿: 3 参加年月日: 10/02/22 最新の投稿Rajesh Chaurasia:
In liferay , around some designated entities model listeners are attached which means that if you create and persist an instance of entity ,simultaneous inserts are made for model listeners.For eg if you create an organization in liferay , you need to insert entries for Asset entry,group,layout set , layout and resource permission taking care of ids .
Similar is the case with User creation.In liferay u can create user frorm UI but this does not stop u from creating users programmatically.
private User insertOrgAdmin(ActionRequest actionRequest,Organization organization,ServiceContext serviceContext,long gpIdForUser) throws PortalException, SystemException {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
long companyId = themeDisplay.getCompanyId();
long groupId= gpIdForUser;
String password1= "orgadmin";//organization.getName().toLowerCase();
String domainName = null;
long facebookId=0;
String openId=null;
//String firstName=organization.getName();
String lastName=organization.getName();
int prefixId=123;
int suffixId=234;
String jobTitle="Organization Administrator";
domainName = organization.getName();
domainName= domainName.trim();
if(domainName.contains(" ")) {
domainName = domainName.replaceAll("\\s+", "-");
}
//String emailAddress="administrator@"+domainName+".com";
String emailAddress="administrator@panchayatportal.gov.in";
Role role = RoleLocalServiceUtil.getRole(companyId, "Organization Administrator");
User user = null;
if(role!= null){
long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
//String greeting="Welcome "+role.getName();
String greeting="Welcome Admin";
long id = CounterLocalServiceUtil.increment(User.class.getName());
user = UserLocalServiceUtil.createUser(id);
user.setCompanyId(companyId);
user.setPassword(password1);
//String screenName=organization.getName().toLowerCase()+id;
String screenName="admin"+id;
user.setScreenName(screenName);
user.setEmailAddress(emailAddress);
user.setFacebookId(facebookId);
user.setOpenId(openId);
user.setGreeting(greeting);
//user.setFirstName(firstName);
user.setFirstName("Site Admin");
//user.setLastName(lastName);
user.setJobTitle(jobTitle);
user.setCreateDate(new Date ());
user.setContactId(idContact);
user.setPasswordReset(true);
user.setPasswordEncrypted(false);
user.setPasswordModifiedDate(new Date());
user.setCreateDate(new Date());
user.setModifiedDate(new Date());
user.setLanguageId(themeDisplay.getLanguageId());
user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
UserLocalServiceUtil.addUser(user);
//Associate a role with user
long userid[] = {user.getUserId()};
//UserLocalServiceUtil.addRoleUsers(role.getRoleId(), userid);
Role rolePu = RoleLocalServiceUtil.getRole(companyId, "Power User");
//UserLocalServiceUtil.addRoleUsers(rolePu.getRoleId(), userid);
long roleids[]={role.getRoleId(),rolePu.getRoleId()};
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(userid[0]);
userGrp.setCompanyId(companyId);
userGrp.setName("group"+String.valueOf(userid[0]));
userGrp.setFriendlyURL("/group"+gpId);
userGrp.setCreatorUserId(PortalUtil.getUserId(actionRequest));
userGrp.setActive(true);
GroupLocalServiceUtil.addGroup(userGrp);
//Insert Contact for a user
//long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
Contact contact = ContactLocalServiceUtil.createContact(idContact);
contact.setCompanyId(companyId);
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(prefixId);
contact.setSuffixId(suffixId);
contact.setJobTitle(jobTitle+contact.getContactId());
contact.setBirthday(new Date());
ContactLocalServiceUtil.addContact(contact);
//Create AssetEntry
long assetEntryId = CounterLocalServiceUtil.increment(AssetEntry.class.getName());
AssetEntry ae = AssetEntryLocalServiceUtil.createAssetEntry(assetEntryId);
ae.setCompanyId(companyId);
ae.setClassPK(user.getUserId());
ae.setGroupId(userGrp.getGroupId());
ae.setClassNameId(classNameId);
AssetEntryLocalServiceUtil.addAssetEntry(ae);
//Insert ResourcePermission for a User
long resPermId = CounterLocalServiceUtil.increment(ResourcePermission.class.getName());
ResourcePermission rpEntry = ResourcePermissionLocalServiceUtil.createResourcePermission(resPermId);
rpEntry.setCompanyId(organization.getCompanyId());
rpEntry.setName("com.liferay.portal.model.User");
rpEntry.setRoleId(role.getRoleId());
rpEntry.setScope(4);
rpEntry.setActionIds(31);
// rpEntry.setPrimaryKey(userid[0]);
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(SystemException 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(SystemException se){
}
}else{
return null;
}
return user;
}
Great work Rajesh!
8年前 に anas esseidi によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
New Member 投稿: 3 参加年月日: 15/08/06 最新の投稿
Thanks for that post
4年前 に Enrique Valdes Lacasa によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Junior Member 投稿: 92 参加年月日: 14/07/29 最新の投稿
Instead of worrying about initializing objects like ResourcePermission or Contact, I instead recommend using the Liferay API's most convenient method which will take care of those additional tasks for us.
For example, one can just call the UserLocalServiceUtil.addUser method with the desired parameters, and that will be enough for users to be added. For Liferay 7.2 - and I assume things hadn't changed much from Liferay 6.x in respect to this - adding a user with the bare minimum is as simple as calling a single method :
You'll be able to see them in Users & Organizations afterwards. This is what I do whenever I need a new user programmatically.
For example, one can just call the UserLocalServiceUtil.addUser method with the desired parameters, and that will be enough for users to be added. For Liferay 7.2 - and I assume things hadn't changed much from Liferay 6.x in respect to this - adding a user with the bare minimum is as simple as calling a single method :
User user = UserLocalServiceUtil.addUser(
creatorUserId, //creatorUserId
companyId, //companyId
true, //autoPassword
null, //password1
null, //password2
false, //autoScreenName
screenName, //screenName
emailAddress, //emailAddress
0, //facebookId
null, //openId
defaultLocale, //locale
firstName, //firstName
midInit, //middleName
lastName, //lastName
0, //prefixId
0, //suffixId
false, //male
0, //birthdayMonth
1, //birthdayDay
1970, //birthdayYear
null, //jobTitle
null, //groupIds
null, //organizationIds
null, //roleIds
null, //userGroupIds
false, //sendEmail
null); //serviceContext
You'll be able to see them in Users & Organizations afterwards. This is what I do whenever I need a new user programmatically.
4年前 に Aamjad Pinjari によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
New Member 投稿: 9 参加年月日: 20/03/24 最新の投稿I am trying to create new user using below code, my user is successfully crated in database but i am getting error while login please help..........!NOTE:- i attached error image below & liferay version 7 long facebookId=0;
String openId=null;
long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
String screenname = ParamUtil.getString(request, "screenname");
String email = ParamUtil.getString(request, "email");
String firstname = ParamUtil.getString(request, "firstname");
String lastname = ParamUtil.getString(request, "lastname");
String password1 = ParamUtil.getString(request, "password");
//String password2 = ParamUtil.getString(request, "password");
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY); long companyId = themeDisplay.getCompanyId();
long id = CounterLocalServiceUtil.increment(User.class.getName());
User user = UserLocalServiceUtil.createUser(id);
user.setCompanyId(companyId);
user.setScreenName(screenname);
user.setEmailAddress(email);
user.setFacebookId(facebookId);
user.setOpenId(openId);
user.setGreeting("Hi "+ screenname);
//user.setFirstName(firstName);
user.setFirstName(firstname);
user.setPassword(password1);
user.setLastName(lastname);
user.setJobTitle("Job Title");
user.setCreateDate(new Date ());
user.setContactId(idContact);
user.setPasswordReset(true);
user.setPasswordEncrypted(false);
user.setPasswordModifiedDate(new Date());
user.setCreateDate(new Date());
user.setModifiedDate(new Date());
user.setLanguageId(themeDisplay.getLanguageId());
user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
UserLocalServiceUtil.addUser(user);
添付ファイル:
4年前 に Mohammed Yasin によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Liferay Master 投稿: 591 参加年月日: 14/08/08 最新の投稿
Hi ,Try using below api
UserLocalServiceUtil.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)
4年前 に Aamjad Pinjari によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
New Member 投稿: 9 参加年月日: 20/03/24 最新の投稿
I am using "com.liferay.portal.kernel.service.UserLocalServiceUtil"class and this is not accepting this method
4年前 に Aamjad Pinjari によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
New Member 投稿: 9 参加年月日: 20/03/24 最新の投稿
UserLocalServiceUtil.addUser(id, companyId, false,password1 , password1, false, screenname, email, facebookId, null, LocaleUtil.getDefault(), firstname, "middlename", lastname, prefixId,suffixId, true, 13, 06, 1997, "Job Title", null, null, null,null, false, null);
i am getting error while using this method i dont know what i am doing wring
i am getting error while using this method i dont know what i am doing wring
4年前 に Mohammed Yasin によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
Liferay Master 投稿: 591 参加年月日: 14/08/08 最新の投稿Aamjad Pinjari:
UserLocalServiceUtil.addUser(id, companyId, false,password1 , password1, false, screenname, email, facebookId, null, LocaleUtil.getDefault(), firstname, "middlename", lastname, prefixId,suffixId, true, 13, 06, 1997, "Job Title", null, null, null,null, false, null);
i am getting error while using this method i dont know what i am doing wring
What error your getting using when above method ? can you pls share error log ?
4年前 に Aamjad Pinjari によって更新されました。
RE: How to Programmatically Add Liferay Users in Java
New Member 投稿: 9 参加年月日: 20/03/24 最新の投稿
<p>I am getting an error at the front end "<strong><strong> </strong></strong>Your request failed to complete."</p>