have a problem with adding new user when authenticate with liferay 4.0

17536, modified 19 Years ago. New Member Posts: 16 Join Date: 11/2/05 Recent Posts
Im using liferay 4.0. I made a class to control authenticate by UserId is MyLogin.class. <br />The purpose of this class is authenticate the userid. If this userId existed in the portal, the authentication will return success. If this userId is new, i use the funtion UserLocalServiceUtil.addUser(paramater1,paramater1,...paramatern) to add new user to liferay portal and then the liferay system will auto redirect to a page with portal&#39;s rules an request user accept to become o new user of the system. But this funtion throws an exceptions. I performed the authenticate throungt this way with liferay 3.6.1 and it was success. <br /><br />The content of MyLogin.java as followinig (This class is modifed from LDAPAuth.java, only modify the function authenticateByUserId()):<br />import com.liferay.counter.service.spring.CounterServiceUtil;<br />import com.liferay.mail.service.spring.MailServiceUtil;<br />import com.liferay.portal.NoSuchGroupException;<br />import com.liferay.portal.NoSuchRoleException;<br />import com.liferay.portal.NoSuchUserException;<br />import com.liferay.portal.PortalException;<br />import com.liferay.portal.SystemException;<br />import com.liferay.portal.language.LanguageException;<br />import com.liferay.portal.language.LanguageUtil;<br />import com.liferay.portal.model.Contact;<br />import com.liferay.portal.model.Group;<br />import com.liferay.portal.model.Role;<br />import com.liferay.portal.model.User;<br />import com.liferay.portal.pwd.PwdToolkitUtil;<br />import com.liferay.portal.security.auth.AuthException;<br />import com.liferay.portal.security.auth.Authenticator;<br />import com.liferay.portal.service.persistence.ContactUtil;<br />import com.liferay.portal.service.persistence.GroupFinder;<br />import com.liferay.portal.service.persistence.RoleFinder;<br />import com.liferay.portal.service.persistence.UserUtil;<br />import com.liferay.portal.service.spring.GroupLocalServiceUtil;<br />import com.liferay.portal.service.spring.ResourceLocalServiceUtil;<br />import com.liferay.portal.service.spring.UserLocalServiceUtil;<br />import com.liferay.portal.service.spring.UserServiceUtil;<br />import com.liferay.portal.util.PortalUtil;<br />import com.liferay.portal.util.PropsUtil;<br />import com.liferay.portlet.admin.util.AdminUtil;<br />import com.liferay.util.GetterUtil;<br />import com.liferay.util.StringPool;<br />import com.liferay.util.Time;<br />import com.liferay.util.Validator;<br /><br />import java.io.FileOutputStream;<br />import java.io.IOException;<br />import java.io.PrintStream;<br />import java.util.ArrayList;<br />import java.util.Date;<br />import java.util.List;<br />import java.util.Locale;<br />public class MyLogin implements Authenticator {<br /><br /> public int authenticateByEmailAddress(<br /> String companyId, String emailAddress, String password)<br /> throws AuthException {<br /><br /> try {<br /><br /> // Check the LDAP server to ensure the company id, email address,<br /> // and password is valid<br /><br /> boolean success = true;<br /><br /> if (success) {<br /><br /> // Make sure the user has a portal account<br /><br /> User user = null;<br /><br /> try {<br /> user = UserLocalServiceUtil.getUserByEmailAddress(<br /> companyId, emailAddress);<br /> }<br /> catch (NoSuchUserException nsue) {<br /><br /> // Grab attributes from LDAP<br /><br /> boolean autoUserId = true;<br /> String userId = &quot;&quot;;<br /> boolean autoPassword = false;<br /> String password1 = password;<br /> String password2 = password;<br /> boolean passwordReset = false;<br /> String firstName = &quot;&quot;;<br /> String middleName = &quot;&quot;;<br /> String lastName = &quot;&quot;;<br /> String nickName = &quot;&quot;;<br /> boolean male = true;<br /> Date birthday = new Date();<br /> Locale locale = Locale.US; <br /> /*<br /> user = UserLocalServiceUtil.addUser(<br /> companyId, autoUserId, userId, autoPassword, password1,<br /> password2, passwordReset, firstName, middleName,<br /> lastName, nickName, male, birthday, emailAddress,<br /> locale);<br /> */<br /> }<br /><br /> return SUCCESS;<br /> }<br /> else {<br /> return FAILURE;<br /> }<br /> }<br /> catch (Exception e) {<br /> throw new AuthException(e);<br /> }<br /> }<br /> <br /> public int authenticateByUserId(<br /> String companyId, String userId, String password)<br /> throws AuthException {<br /><br /> try {<br /><br /> // Check the LDAP server to ensure the company id, user id, and<br /> // password is valid<br /><br /> boolean success = true;<br /><br /> if (success) {<br /><br /> // Make sure the user has a portal account<br /><br /> User user = null;<br /> try { <br /> user = UserLocalServiceUtil.getUserById(companyId, userId);<br /> }<br /> catch (NoSuchUserException nsue) {<br /><br /> // Grab attributes from LDAP<br /> boolean autoUserId = false;<br /> boolean autoPassword = false;<br /> String password1 = password;<br /> String password2 = password;<br /> boolean passwordReset = false;<br /> Locale locale = Locale.US; <br /> String nickName = &quot;tonha&quot;;//StringPool.BLANK;<br /> String prefixId = &quot;Mr.&quot;;<br /> String suffixId = &quot;II&quot;;//StringPool.BLANK; <br /> String firstName = &quot;nha&quot;;<br /> String middleName = &quot;van&quot;;<br /> String lastName = &quot;to&quot;;<br /> String emailAddress = &quot;tnha@lacviet.com.vn&quot;;<br /> boolean male = true; <br /> Date birthday = null;<br /> int birthdayMonth=7;<br /> int birthdayDay=10;<br /> int birthdayYear=1981; <br /> <br /> String jobTitle = StringPool.BLANK;<br /> String organizationId = &quot;Liferay Asia&quot;;//null;<br /> String locationId = &quot;Liferay Shanghai&quot;;//null;<br /> <br /> try {<br /> <br /> /*<br /> user = UserServiceUtil.addUser(<br /> companyId, autoUserId, userId, autoPassword,<br /> password1, password2, passwordReset, emailAddress,<br /> locale, firstName, middleName, lastName,<br /> nickName, prefixId, suffixId, male, birthdayMonth, birthdayDay,<br /> birthdayYear, jobTitle, organizationId, locationId);<br /> <br /> <br /> user = UserServiceUtil.addUser(<br /> companyId, autoUserId, userId, autoPassword,<br /> password1, password2, passwordReset, emailAddress,<br /> locale, firstName, middleName, lastName,<br /> nickName, prefixId, suffixId, male, birthdayMonth, birthdayDay,<br /> birthdayYear, jobTitle, organizationId, locationId);<br /> */<br /> /*<br /> user = UserLocalServiceUtil.addUser(<br /> userId, companyId, autoUserId, userId, autoPassword,<br /> password1, password2, passwordReset, emailAddress, locale,<br /> firstName, middleName, lastName, nickName, prefixId, suffixId, male,<br /> birthdayMonth, birthdayDay, birthdayYear, jobTitle, organizationId,<br /> locationId);<br /> */ } catch (PortalException e1) {<br /> e1.printStackTrace();<br /> } catch (SystemException e1) {<br /> e1.printStackTrace();<br /> } <br /> }<br /> <br /> return SUCCESS;<br /> }<br /> else {<br /> return FAILURE;<br /> }<br /> }<br /> catch (Exception e) {<br /> throw new AuthException(e);<br /> }<br /> }<br /><br />Please tell me another way to deal with the problem above. Thanks alot.