001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
027     * @author Brian Wing Shun Chan
028     */
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    }