001
014
015 package com.liferay.portal.security.auth;
016
017 import com.liferay.portal.kernel.language.LanguageUtil;
018 import com.liferay.portal.kernel.util.PropsKeys;
019 import com.liferay.portal.kernel.util.PropsUtil;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.kernel.util.StringUtil;
022 import com.liferay.portal.kernel.util.Validator;
023
024 import java.util.Locale;
025
026
029 public class DefaultScreenNameValidator implements ScreenNameValidator {
030
031 public static final String POSTFIX = "postfix";
032
033 @Override
034 public String getAUIValidatorJS() {
035 return "function(val) {var pattern = new RegExp('[^A-Za-z0-9" +
036 getSpecialChars() +
037 "]');if (val.match(pattern)) {return false;}return true;}";
038 }
039
040 @Override
041 public String getDescription(Locale locale) {
042 return LanguageUtil.format(
043 locale,
044 "the-screen-name-cannot-be-an-email-address-or-a-reserved-word",
045 new String[] {POSTFIX, getSpecialChars()}, false);
046 }
047
048 @Override
049 public boolean validate(long companyId, String screenName) {
050 if (Validator.isEmailAddress(screenName) ||
051 StringUtil.equalsIgnoreCase(screenName, POSTFIX) ||
052 hasInvalidChars(screenName)) {
053
054 return false;
055 }
056
057 return true;
058 }
059
060 protected String getSpecialChars() {
061 if (_specialChars == null) {
062 String specialChars = PropsUtil.get(
063 PropsKeys.USERS_SCREEN_NAME_SPECIAL_CHARACTERS);
064
065 _specialChars = specialChars.replaceAll(
066 StringPool.SLASH, StringPool.BLANK);
067 }
068
069 return _specialChars;
070 }
071
072 protected boolean hasInvalidChars(String screenName) {
073 return !screenName.matches("[A-Za-z0-9" + getSpecialChars() + "]+");
074 }
075
076 private String _specialChars;
077
078 }