001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PrefsPropsUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Alexander Chow
032     * @author Juan Fern??ndez
033     */
034    public class DefaultScreenNameGenerator implements ScreenNameGenerator {
035    
036            @Override
037            public String generate(long companyId, long userId, String emailAddress)
038                    throws Exception {
039    
040                    String screenName = null;
041    
042                    if (Validator.isNotNull(emailAddress)) {
043                            screenName = StringUtil.extractFirst(emailAddress, CharPool.AT);
044    
045                            screenName = StringUtil.toLowerCase(screenName);
046    
047                            for (char c : screenName.toCharArray()) {
048                                    if (!Validator.isChar(c) && !Validator.isDigit(c) &&
049                                            (c != CharPool.DASH) && (c != CharPool.PERIOD)) {
050    
051                                            screenName = StringUtil.replace(
052                                                    screenName, c, CharPool.PERIOD);
053                                    }
054                            }
055    
056                            if (screenName.equals(DefaultScreenNameValidator.CYRUS) ||
057                                    screenName.equals(DefaultScreenNameValidator.POSTFIX)) {
058    
059                                    screenName += StringPool.PERIOD + userId;
060                            }
061                    }
062                    else {
063                            screenName = String.valueOf(userId);
064                    }
065    
066                    if (!_USERS_SCREEN_NAME_ALLOW_NUMERIC &&
067                            Validator.isNumber(screenName)) {
068    
069                            screenName = _NON_NUMERICAL_PREFIX + screenName;
070                    }
071    
072                    String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
073                            companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
074                            StringPool.NEW_LINE, _ADMIN_RESERVED_SCREEN_NAMES);
075    
076                    for (String reservedScreenName : reservedScreenNames) {
077                            if (StringUtil.equalsIgnoreCase(screenName, reservedScreenName)) {
078                                    return getUnusedScreenName(companyId, screenName);
079                            }
080                    }
081    
082                    if (UserLocalServiceUtil.fetchUserByScreenName(
083                                    companyId, screenName) != null) {
084    
085                            return getUnusedScreenName(companyId, screenName);
086                    }
087    
088                    if (GroupLocalServiceUtil.fetchFriendlyURLGroup(
089                                    companyId, StringPool.SLASH + screenName) == null) {
090    
091                            return screenName;
092                    }
093    
094                    return getUnusedScreenName(companyId, screenName);
095            }
096    
097            protected String getUnusedScreenName(long companyId, String screenName)
098                    throws SystemException {
099    
100                    for (int i = 1;; i++) {
101                            String tempScreenName = screenName + StringPool.PERIOD + i;
102    
103                            if (UserLocalServiceUtil.fetchUserByScreenName(
104                                            companyId, tempScreenName) != null) {
105    
106                                    continue;
107                            }
108    
109                            if (GroupLocalServiceUtil.fetchFriendlyURLGroup(
110                                            companyId, StringPool.SLASH + tempScreenName) == null) {
111    
112                                    return tempScreenName;
113                            }
114                    }
115            }
116    
117            private static final String[] _ADMIN_RESERVED_SCREEN_NAMES =
118                    StringUtil.splitLines(
119                            PropsUtil.get(PropsKeys.ADMIN_RESERVED_SCREEN_NAMES));
120    
121            private static final String _NON_NUMERICAL_PREFIX = "user.";
122    
123            private static final boolean _USERS_SCREEN_NAME_ALLOW_NUMERIC =
124                    GetterUtil.getBoolean(
125                            PropsUtil.get(PropsKeys.USERS_SCREEN_NAME_ALLOW_NUMERIC));
126    
127    }