Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
dominick campbell
Screen Name Validation
February 6, 2013 9:19 AM
Answer

dominick campbell

Rank: Junior Member

Posts: 57

Join Date: August 15, 2012

Recent Posts

Hey everybody

I was wondering if anyone knew how to validate a screen name programmatically in liferay

Right now I am using the UserLocalServiceUtil.addUser function, but when the screen name is too short it throws the following exception:

com.liferay.portal.GroupFriendlyURLException
at com.liferay.portal.service.impl.UserLocalServiceImpl.validateScreenName(UserLocalServiceImpl.java:5770)

which doesnt really tell me anything

What i would like to do is validate the screen name before I get to the addUser function

Thanks
Pranay R Patadiya
RE: Screen Name Validation
February 6, 2013 10:54 AM
Answer

Pranay R Patadiya

Rank: Regular Member

Posts: 181

Join Date: February 22, 2010

Recent Posts

Hi Campbell,

Yes, Liferay internally validating the ScreenName. To avoid that message you need to validate the ScreenName before submit or adding user.

If you want you can have a look at :

 1protected void validateScreenName(
 2            long companyId, long userId, String screenName)
 3        throws PortalException, SystemException {
 4
 5        if (Validator.isNull(screenName)) {
 6            throw new UserScreenNameException();
 7        }
 8
 9        ScreenNameValidator screenNameValidator =
10            ScreenNameValidatorFactory.getInstance();
11
12        if (!screenNameValidator.validate(companyId, screenName)) {
13            throw new UserScreenNameException();
14        }
15
16        if (Validator.isNumber(screenName) &&
17            !screenName.equals(String.valueOf(userId))) {
18
19            if (!PropsValues.USERS_SCREEN_NAME_ALLOW_NUMERIC) {
20                throw new UserScreenNameException();
21            }
22
23            Group group = groupPersistence.fetchByPrimaryKey(
24                GetterUtil.getLong(screenName));
25
26            if (group != null) {
27                throw new UserScreenNameException();
28            }
29        }
30
31        for (char c : screenName.toCharArray()) {
32            if ((!Validator.isChar(c)) && (!Validator.isDigit(c)) &&
33                (c != CharPool.DASH) && (c != CharPool.PERIOD) &&
34                (c != CharPool.UNDERLINE)) {
35
36                throw new UserScreenNameException();
37            }
38        }
39
40        String[] anonymousNames = PrincipalBean.ANONYMOUS_NAMES;
41
42        for (int i = 0; i < anonymousNames.length; i++) {
43            if (screenName.equalsIgnoreCase(anonymousNames[i])) {
44                throw new UserScreenNameException();
45            }
46        }
47
48        User user = userPersistence.fetchByC_SN(companyId, screenName);
49
50        if (user != null) {
51            throw new DuplicateUserScreenNameException();
52        }
53
54        String friendlyURL = StringPool.SLASH + screenName;
55
56        Group group = groupPersistence.fetchByC_F(companyId, friendlyURL);
57
58        if (group != null) {
59            throw new DuplicateUserScreenNameException();
60        }
61
62        int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);
63
64        if (exceptionType != -1) {
65            throw new UserScreenNameException(
66                new GroupFriendlyURLException(exceptionType));
67        }
68
69        String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
70            companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
71            StringPool.NEW_LINE, PropsValues.ADMIN_RESERVED_SCREEN_NAMES);
72
73        for (int i = 0; i < reservedScreenNames.length; i++) {
74            if (screenName.equalsIgnoreCase(reservedScreenNames[i])) {
75                throw new ReservedUserScreenNameException();
76            }
77        }
78    }


Above method is validating the screen name.

Also, if you want you can change the screenanme validator as per below from property file:
1#
2    # Input a class name that implements
3    # com.liferay.portal.security.auth.ScreenNameValidator. This class will be
4    # called to validate user screen names.
5    #
6    users.screen.name.validator=com.liferay.portal.security.auth.DefaultScreenNameValidator
7    #users.screen.name.validator=com.liferay.portal.security.auth.LiberalScreenNameValidator



Let me know if you any other information on this.

Regards,
Pranay
Hitoshi Ozawa
RE: Screen Name Validation
February 6, 2013 2:39 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7990

Join Date: March 23, 2010

Recent Posts

Well, you can change screen name validation by setting a property in portal-ext.properties.

#
# Input a class name that implements
# com.liferay.portal.security.auth.ScreenNameValidator. This class will be
# called to validate user screen names.
#
users.screen.name.validator=com.liferay.portal.security.auth.DefaultScreenNameValidator
#users.screen.name.validator=com.liferay.portal.security.auth.LiberalScreenNameValidator
dominick campbell
RE: Screen Name Validation
February 7, 2013 6:01 AM
Answer

dominick campbell

Rank: Junior Member

Posts: 57

Join Date: August 15, 2012

Recent Posts

Ok so I did a bit of playing around with the ScreenNameValidator in portal-impl.jar

I added in the following jars to the dependancy list:
portal-impl.jar,easyconf.jar,commons-lang.jar,commons-configuration.jar,commons-beanutils.jar,commons-digester.jar,xstream.jar,xpp3.jar,commons-collections.jar

and made the following call before the add user call:
ScreenNameValidator screenNameValidator = ScreenNameValidatorFactory.getInstance();
System.err.println("xxx : " + screenNameValidator.validate(ConstantsUtil.COMPANY_ID, newUser.getUser().getScreenName()));

however, now that I make this call, the short screen name that was failing is now working

This function doesn't check if the screen name is valid, it automatically validates it which isn't what I was looking for

is there a way to test if a screen name is valid?

Thanks