You are here

create Luminis user programatically

Submitted by alin.tomoiaga@t... on Tue, 07/07/2015 - 16:38

Forums:

How can we create a Luminis user programatically?

I am using all the Ellucian provided functions that I can find and they do not create a user anywhere (User_, LP_USER tables or the ldap server )and they do not produce any errors or other messages in the logs; what I am missing?

DelegatingPersonService ps = new DelegatingPersonService();

Person p = null;
try {

p = ps.buildValidPerson();
p.getListOfRoles();
p.setCollaborationProvisioned(false);
p.setCredential("dsasdsadasdasd");
p.setDateOfBirth(new Date());
p.setDisplayName("user display name");
p.setEmailAddress("userj@ttu.edu");
p.setEnabled(true);
p.setExtPersonId(""+866);
p.setFirstName("J");
p.setHomeInstitution(null);
p.setImsSrcId("SunGard SCT Banner");
p.setLastLoginDate(new Date());
p.setLastName("user");
p.setLastPasswordChangeDate(new Date());

List roleList = ps.fetchAllRoles();

//role.
p.setListOfRoles(roleList);
//p.setListOfMajors();
p.setLoginId(loginId);
p.setPersonId("R00893677");

p.setPseudonym("jhook");
p.setSessionTimeout(30);
p.setTagLine("tag line");
p.setUdcIdentifier("134567");

String portalUserid = ps.createPortalUser(p);
p.setPortalUserId(portalUserid);
ps.createPerson(p);

} catch (PersonException | RoleException e) {
// TODO Auto-generated catch block
System.out.println("exception creating person"+e);
e.printStackTrace();
}
return p;

Luminis Version:

On the other hand, using the Liferay api, creates a user in the User_ table but not in the LP_PERSON table (or the ldap):

long companyId = 10154;
long groupId= 10354;
String password1= "pass1234";//organization.getName().toLowerCase();
String domainName = null;
long facebookId=0;
String openId=null;
//String firstName=organization.getName();
String lastName= "Hooker";
int prefixId=123;
int suffixId=234;
String jobTitle="job title";
//domainName = organization.getName();
//domainName= domainName.trim();
//if(domainName.contains(" ")) {
//domainName = domainName.replaceAll("\\s+", "-");
//}
//String emailAddress="administrator@"+domainName+".com";
String emailAddress="juser@ttu.edu";
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 J";
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="juser"+id;
user.setScreenName(screenName);
user.setEmailAddress(emailAddress);
user.setFacebookId(facebookId);
user.setOpenId(openId);
user.setGreeting(greeting);
//user.setFirstName(firstName);
user.setFirstName("J");
user.setLastName("userr");
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("en_US");
//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(id);
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.getContac

Does anyone have any suggestions about this? Have you encountered a similar problem?

Jason, thank you for your answer.

We do not have the event system installed and I believe will opt out of it, so I am looking for any other way.

On the other hand, I was able to create a person in a portlet or a hook; something like the code below works fine:

PersonService ps = new DelegatingPersonService();

List roleList = ps.fetchAllRoles();
List personRoleList = new ArrayList();
for ( com.sghe.luminis.person.entity.Role role : roleList )
{
// Set just the student role
if("STUDENT".equals(role.getRoleDesc()))
{
personRoleList.add(role);
}
}
p = ps.buildValidPerson();

//Required fields to create a user
p.setLoginId(loginId);
p.setCredential("dsasdsadasdasd");
p.setDisplayName( loginId+"display name");
p.setEmailAddress(loginId+"@ttu.edu");
p.setFirstName("J");
p.setLastName(loginId);
p.setListOfRoles(personRoleList);// ^^^ see above ^^^
p.setSessionTimeout(30);

ps.createPerson(p);

The problem is that I am working in a filter, and I get the error:

org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext

I am not sure what this Authentication object is...

I'd be interested in hearing others' opinion on this as well... we currently use LMG/Glassfish/LMB to provision users in Luminis 5 but are looking at setting up a more "proper" identity store using LDAP or AD. One question I'm working on is whether to have separate "Users" and "Portal" OUs and use the event system to provision into Luminis and the Portal OU, while our custom scripts feed the Users OU, or to simply write our own provisioning to both OUs.

The other (seemingly more simple) option is to use the event system to provision Luminis accounts and the User OU and add additional attributes to what Ellucian creates. Anyone have any input on the approach they use?

Sacilot,
We had similar concerns and we eventually opted for automated user creation for users that have been CAS authenticated. We have made a modification to the luminis spring configuration file so it uses the class below:

package edu.ttu.luminis;

import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.sghe.luminis.person.entity.Person;
import com.sghe.luminis.person.services.PersonService;
import com.sghe.luminis.security.SecurityManager;
import com.sghe.luminis.security.spring.authen.UserDetailsService;

public class TtuUserDetailsService extends UserDetailsService {

public void setTtuM(SecurityManager ttuM) {
this.ttuM = ttuM;
}

public void setTtuPs(PersonService ttuPs) {
this.ttuPs = ttuPs;
}

private PersonService ttuPs;
private SecurityManager ttuM;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException{
try {
System.out.println("TTuUserDetailsService: begin ");
TtuCreateNewPerson runGetPerson = new TtuCreateNewPerson(username);
Person person = (Person)this.ttuM.doAsService(username, (PrivilegedExceptionAction)runGetPerson);
}
catch (Exception e) {
System.out.println("UserDetailsService: error finding " + username + " : " + e.getMessage());
throw new UsernameNotFoundException("Error getting/creating requested person " + username, (Throwable)e);
}
return super.loadUserByUsername(username);
}

class TtuCreateNewPerson implements PrivilegedExceptionAction {
private String userName;
private Person returnPerson;

public TtuCreateNewPerson(String userName) {

this.returnPerson = null;
this.userName = userName;
}

@Override
public Person run() {
try {

this.returnPerson = ttuPs.findPersonByLoginId(this.userName);

if (this.returnPerson == null) {

addLumPerson(this.userName);
}

}
catch (Exception e) {

}
return this.returnPerson;
}

private Person addLumPerson(String loginId){

Person p = null;
try {

List roleList = ttuPs.fetchAllRoles();

List personRoleList = new ArrayList();

for ( com.sghe.luminis.person.entity.Role role : roleList )
{
// Set just the student role
if("STUDENT".equals(role.getRoleDesc()))
{
personRoleList.add(role);
}
}

p = ttuPs.buildValidPerson();

//Required fields to create a user
p.setLoginId(loginId);
p.setCredential("dsasdsadasdasd");
p.setDisplayName( loginId+"display name");
p.setEmailAddress(loginId+"@ttu.edu");
p.setFirstName("J");
p.setLastName(loginId);
p.setListOfRoles(personRoleList);// ^^^ see above ^^^
p.setSessionTimeout(30);

ttuPs.createPerson(p); // Creating a Luminis person also creates the Liferay user. No need to create portal user.

} catch (Exception e) {

throw new RuntimeException(e);
}
return p;
}

}
}