Saturday, 26 September 2015

How to Add Liferay User inside Organization by programatically? How to assign role to user and organization by programatically

this code is working for me, i thing it will help you to user creation and create the role 

private Organization CreateRegularOrg(String name, long companyId,ActionRequest actionRequest) throws PortalException, SystemException {
     
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
Company company = CompanyLocalServiceUtil.getCompany(companyId);
ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
serviceContext.setCompanyId(companyId);
Organization org = null;
try {
List<Address> addresses = UsersAdminUtil.getAddresses(actionRequest);
List<EmailAddress> emailAddresses = UsersAdminUtil.getEmailAddresses(
actionRequest);
List<OrgLabor> orgLabors = UsersAdminUtil.getOrgLabors(actionRequest);
List<Phone> phones = UsersAdminUtil.getPhones(actionRequest);
List<Website> websites = UsersAdminUtil.getWebsites(actionRequest);

org = OrganizationServiceUtil.addOrganization(
0, tenantName, "regular-organization", 19014, 19, 12017,
StringPool.BLANK, true, addresses, emailAddresses, orgLabors, phones,
websites, serviceContext);
System.out.println("org=======11111111============"+org);

/*org = OrganizationServiceUtil.addOrganization(0, tenantName,"regular-organization", true, 19014, 19, 12017,StringPool.BLANK, true, serviceContext);*/
org.setCompanyId(companyId);
org = OrganizationLocalServiceUtil.updateOrganization(org);


//Group group = GroupLocalServiceUtil.getCompanyGroup(themeDisplay.getCompanyId());
Group group = GroupLocalServiceUtil.getOrganizationGroup(themeDisplay.getCompanyId(), org.getOrganizationId());
System.out.println("Company ID from Theme Display -> "+themeDisplay.getCompanyId());
System.out.println("Company ID from Group -> "+group.getCompanyId());
System.out.println("Org ID from Group -> "+group.getOrganizationId());
group.setCompanyId(companyId);
GroupLocalServiceUtil.updateGroup(group);

System.out.println("org===========22222222222========"+org);
         
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}

return org;
}

private User addOrgUser(Organization org, String tenantAdminName,String tenantAdminEmailId, ActionRequest actionRequest)
throws PortalException, SystemException {
Contact contact =null;
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
System.out.println("org.getCompanyId()  " +org.getCompanyId());
//Role role = RoleLocalServiceUtil.getRole(org.getCompanyId(), "Organization Administrator");
String greeting="Welcome Admin";
User user=null;
try {
user = UserLocalServiceUtil.createUser(CounterLocalServiceUtil.increment());
;
user.setFirstName(tenantAdminName);
user.setEmailAddress(tenantAdminEmailId);
user.setScreenName(tenantAdminName);
user.setCompanyId(org.getCompanyId());
user.setCreateDate(new Date());
user.setModifiedDate(new Date());
user.setGreeting(greeting);
user.setLanguageId(themeDisplay.getLanguageId());
user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
user.setPassword("test");
//insert contact for user
long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
contact = ContactLocalServiceUtil.createContact(idContact);
contact.setCompanyId(org.getCompanyId());
contact.setCreateDate(new Date());
contact.setUserName(tenantAdminName);
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.addOrganizationUser(org.getOrganizationId(),user);
User createdUser = UserLocalServiceUtil.addUser(user);

//Insert Group for a user
ClassName clsNameUser = ClassNameLocalServiceUtil.getClassName("com.liferay.portal.model.User");
long classNameId=clsNameUser.getClassNameId();
long gpId = CounterLocalServiceUtil.increment(Group.class.getName()); 
Group userGrp = GroupLocalServiceUtil.createGroup(gpId);
//User userCreated = UserLocalServiceUtil.getUserByScreenName(org.getCompanyId(), tenantAdminName);
userGrp.setClassNameId(classNameId);
userGrp.setClassPK(createdUser.getUserId());
userGrp.setCompanyId(org.getCompanyId());
userGrp.setName(String.valueOf(createdUser.getUserId()));
System.out.println("group name===========");
userGrp.setFriendlyURL("/tenantAdminName");
userGrp.setCreatorUserId(PortalUtil.getUserId(actionRequest));
userGrp.setActive(true);
Group addedGroup = GroupLocalServiceUtil.addGroup(userGrp);
//Create AssetEntry 
long assetEntryId = CounterLocalServiceUtil.increment(AssetEntry.class.getName());
AssetEntry ae = AssetEntryLocalServiceUtil.createAssetEntry(assetEntryId);
ae.setCompanyId(org.getCompanyId());
ae.setClassPK(createdUser.getUserId());
ae.setGroupId(addedGroup.getGroupId());
ae.setClassNameId(classNameId);
ae.setTitle(tenantAdminName);
ae.setUserId(createdUser.getUserId());
AssetEntryLocalServiceUtil.addAssetEntry(ae);
// role with user
long userid[] = {user.getUserId()};
long groupId = OrganizationLocalServiceUtil.getOrganization(
   org.getCompanyId(), org.getName()).getGroup().getGroupId();
System.out.println("group id--->"+groupId);
//UserLocalServiceUtil.addRoleUsers(role.getRoleId(), userid);
long roleId = RoleLocalServiceUtil.getRole(
   org.getCompanyId(),"Organization Administrator").getRoleId();
System.out.println("role id--->"+roleId);
UserGroupRoleLocalServiceUtil.addUserGroupRoles(
createdUser.getUserId(), groupId, new long[] { roleId });
long adminRoleId = RoleLocalServiceUtil.getRole(
   org.getCompanyId(),"Administrator").getRoleId();
RoleLocalServiceUtil.addUserRole(createdUser.getUserId(), adminRoleId);

long powerUserRoleId = RoleLocalServiceUtil.getRole(
   org.getCompanyId(),"Power User").getRoleId();
System.out.println("role id--->"+roleId);
RoleLocalServiceUtil.addUserRole(createdUser.getUserId(), powerUserRoleId);
/*UserGroupRoleLocalServiceUtil.addUserGroupRoles(
createdUser.getUserId(), groupId, new long[] { roleId });*/
//long roleids[]={role.getRoleId(),rolePu.getRoleId()};
//UserLocalServiceUtil.addRoleUser(roleId, userId);
//UserGroupRoleLocalServiceUtil.addUserGroupRoles(user.getUserId(), user.getGroupId(), roleids);
//Insert ResourcePermission for a User
/*long resPermId = CounterLocalServiceUtil.increment(ResourcePermission.class.getName());
ResourcePermission rpEntry = ResourcePermissionLocalServiceUtil.createResourcePermission(resPermId);
rpEntry.setCompanyId(org.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(org.getCompanyId());
layoutSetPub.setPrivateLayout(false);
layoutSetPub.setGroupId(addedGroup.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(org.getCompanyId());
layoutSetPriv.setPrivateLayout(true);
layoutSetPriv.setThemeId("classic");
layoutSetPriv.setGroupId(addedGroup.getGroupId());
try{
LayoutSetLocalServiceUtil.addLayoutSet(layoutSetPriv); 
}catch(SystemException se){ 
se.printStackTrace();
}
} catch (SystemException e) {
e.printStackTrace();
}

return user;

}



// Calling CreateRegularOrg method and addOrgUser method

Organization org = null;
org = CreateRegularOrg(tenantName, company.getCompanyId());
            System.out.println("calling org========================================"+org);
         
            addOrgUser(org, tenantAdminName,tenantAdminEmailId,actionRequest);

No comments:

Post a Comment