留言板

Validate password from a custom portlet

Hal Seldon,修改在9 年前。

Validate password from a custom portlet

Junior Member 帖子: 34 加入日期: 13-9-20 最近的帖子
I would like to check a password inside a custom portlet.

I have seen this in UserLocalServiceImpl:

PasswordPolicy passwordPolicy = passwordPolicyLocalService.getDefaultPasswordPolicy(companyId);
PwdToolkitUtil.validate(companyId, 0, password1, password2, passwordPolicy);


I have tried to use it in a custom portlet... I could instiantate passwordPolicy, but I have not access to use PwdToolkitUtil.... the error is "The import com.liferay.portal.secutiry.pwd.PwdToolkitUtil. cannot be resolved".

Where is the problem? is there an alternative to check passwords with portal policy from a custom portlet?
thumbnail
Andew Jardine,修改在9 年前。

RE: Validate password from a custom portlet (答复)

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
That package and class "com.liferay.portal.secutiry.pwd.PwdToolkitUtil" is in the portal-impl jar which is not part of your portlets class loader so you can't access it. You have to use classes (for the portal) that can be found in the portal-service.jar which is loaded in from the global libraries.

Short version, you can't use it. You'll have to write your own class. Good news is that PasswordPolicy is in the portal-service.jar, so that one you can use.
Hal Seldon,修改在9 年前。

RE: Validate password from a custom portlet

Junior Member 帖子: 34 加入日期: 13-9-20 最近的帖子
Is there any equivalent method that allows me to use Portal password validation? I hate having duplicate code
venka reddy,修改在9 年前。

RE: Validate password from a custom portlet

Regular Member 帖子: 231 加入日期: 11-3-23 最近的帖子
Hi,

I have used the following method to check the password.

// If users entered password same as the current password.
status = PasswordTrackerLocalServiceUtil.isSameAsCurrentPassword(userId, currentPassword);
thumbnail
Jitendra Rajput,修改在9 年前。

RE: Validate password from a custom portlet

Liferay Master 帖子: 875 加入日期: 11-1-7 最近的帖子
You can also use below methods to authenticate users.

UserLocalServiceUtil.authenticateByEmailAddress()
UserLocalServiceUtil.authenticateByScreenName()
UserLocalServiceUtil.authenticateByUserId()

You can use any of the above method based on your authentication configuration. 
Method will return int value .. 

* @return  the authentication status. 

-1 This can be
	com.liferay.portal.security.auth.Authenticator#FAILURE
	indicating that the user's credentials are invalid,

1 this can be 
	com.liferay.portal.security.auth.Authenticator#SUCCESS
	indicating a successful login, 

or 0
	com.liferay.portal.security.auth.Authenticator#DNE} indicating
	that a user with that login does not exist.
Hal Seldon,修改在9 年前。

RE: Validate password from a custom portlet

Junior Member 帖子: 34 加入日期: 13-9-20 最近的帖子
Thanks Jitendra and venka... but I do not want a method to check password to login with existing user.

I want a method that checks if a new password is ok and if it will be validated by the liferay password policy.

I need to create a liferay user, but validating password before calling UserLocalServiceUtil.addUser

Thanks
thumbnail
David H Nebinger,修改在9 年前。

RE: Validate password from a custom portlet (答复)

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
These guys didn't mention that you can use reflection to access classes/methods in the portal context. You can get the portal class loader and use reflection to access an instance, invoke the method, and determine the results.

Normally it is not a good idea as these kind of things will just fail when you do an upgrade (since reflection is light coupling, a change in the actual API does not get reflected until the runtime invocation when you get method not found exceptions).
Hal Seldon,修改在9 年前。

RE: Validate password from a custom portlet

Junior Member 帖子: 34 加入日期: 13-9-20 最近的帖子
Thanks David for the info.

Can you give me an example about Portal Class Loader to achieve invoke "PwdToolkitUtil.validate(companyId, 0, password1, password2, passwordPolicy)" or another similar example?

And... what do you think is the best option?
1.- Portal Class Loader option?
2.- Duplicate PwdToolkitUtil.validate method in my custom portlet?

Regards
thumbnail
David H Nebinger,修改在9 年前。

RE: Validate password from a custom portlet

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
It's just reflection to invoke a static method in a class.

Invoking the portal's instance would be better as it will have the necessary access to determine current settings, data, etc.
Hal Seldon,修改在9 年前。

RE: Validate password from a custom portlet (答复)

Junior Member 帖子: 34 加入日期: 13-9-20 最近的帖子
Hi David

I try this

        PasswordPolicy passwordPolicy = PasswordPolicyLocalServiceUtil.getDefaultPasswordPolicy(companyId);			
	ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
	Class<!--?--> pwdToolkitUtilClass = portalClassLoader.loadClass("com.liferay.portal.security.pwd.PwdToolkitUtil");
	MethodKey methodKey = new MethodKey(pwdToolkitUtilClass,"validate",long.class,long.class,String.class, String.class, PasswordPolicy.class);
	PortalClassInvoker.invoke(true, methodKey, companyId, new Long(0), password, password,passwordPolicy);


...and works like a charm.... shared code!

Solved my problem

Thanks again
thumbnail
David H Nebinger,修改在9 年前。

RE: Validate password from a custom portlet

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Glad to help!
thumbnail
Arunjyoti Banik,修改在9 年前。

RE: Validate password from a custom portlet

Junior Member 帖子: 74 加入日期: 14-8-26 最近的帖子
Hal Seldon:
Hi David

I try this

        PasswordPolicy passwordPolicy = PasswordPolicyLocalServiceUtil.getDefaultPasswordPolicy(companyId);			
	ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
	Class<!--?--> pwdToolkitUtilClass = portalClassLoader.loadClass("com.liferay.portal.security.pwd.PwdToolkitUtil");
	MethodKey methodKey = new MethodKey(pwdToolkitUtilClass,"validate",long.class,long.class,String.class, String.class, PasswordPolicy.class);
	PortalClassInvoker.invoke(true, methodKey, companyId, new Long(0), password, password,passwordPolicy);


...and works like a charm.... shared code!

Solved my problem

Thanks again



Can you please explain the purpose of MethodKey here?
thumbnail
David H Nebinger,修改在9 年前。

RE: Validate password from a custom portlet

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
MethodKey is the container for a method definition that you are planning to invoke.
thumbnail
Arunjyoti Banik,修改在9 年前。

RE: Validate password from a custom portlet

Junior Member 帖子: 74 加入日期: 14-8-26 最近的帖子
I guess any method present in that class, no?? I just have to specify the exact method name.

Actually I was implementing this problem with PasswordTrackerLocalServiceutil as specified above, but I liked your version more, so that it wont be a problem during upgradation. But I am facing constructor undefined problem while writing this piece of code in my processAction method. where should I define this constructor?
thumbnail
Andew Jardine,修改在8 年前。

RE: Validate password from a custom portlet

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
I think you should have read the entire thread before posting that link. That link doesn't actually solve the original posters issue.