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