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 CYRUS = "cyrus";
032    
033            public static final String POSTFIX = "postfix";
034    
035            @Override
036            public String getAUIValidatorJS() {
037                    return "function(val) {var pattern = new RegExp('[^A-Za-z0-9" +
038                            getSpecialChars() +
039                                    "]');if (val.match(pattern)) {return false;}return true;}";
040            }
041    
042            @Override
043            public String getDescription(Locale locale) {
044                    return LanguageUtil.format(
045                            locale,
046                            "the-screen-name-cannot-be-an-email-address-or-a-reserved-word",
047                            new String[] {CYRUS + ", " + POSTFIX, getSpecialChars()}, false);
048            }
049    
050            @Override
051            public boolean validate(long companyId, String screenName) {
052                    if (Validator.isEmailAddress(screenName) ||
053                            StringUtil.equalsIgnoreCase(screenName, CYRUS) ||
054                            StringUtil.equalsIgnoreCase(screenName, POSTFIX) ||
055                            hasInvalidChars(screenName)) {
056    
057                            return false;
058                    }
059    
060                    return true;
061            }
062    
063            protected String getSpecialChars() {
064                    if (_specialChars == null) {
065                            String specialChars = PropsUtil.get(
066                                    PropsKeys.USERS_SCREEN_NAME_SPECIAL_CHARACTERS);
067    
068                            _specialChars = specialChars.replaceAll(
069                                    StringPool.SLASH, StringPool.BLANK);
070                    }
071    
072                    return _specialChars;
073            }
074    
075            protected boolean hasInvalidChars(String screenName) {
076                    return !screenName.matches("[A-Za-z0-9" + getSpecialChars() + "]+");
077            }
078    
079            private String _specialChars;
080    
081    }