001    /**
002     * Copyright (c) 2000-2012 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            public String generate(long companyId, long userId, String emailAddress)
040                    throws Exception {
041    
042                    String screenName = null;
043    
044                    if (Validator.isNotNull(emailAddress)) {
045                            screenName = StringUtil.extractFirst(
046                                    emailAddress, CharPool.AT).toLowerCase();
047    
048                            for (char c : screenName.toCharArray()) {
049                                    if ((!_USERS_SCREEN_NAME_ALLOW_NUMERIC &&
050                                             Validator.isDigit(c)) ||
051                                            (!Validator.isChar(c) && (c != CharPool.DASH) &&
052                                             (c != CharPool.PERIOD))) {
053    
054                                            screenName = StringUtil.replace(
055                                                    screenName, c, CharPool.PERIOD);
056                                    }
057                            }
058    
059                            if (screenName.equals(DefaultScreenNameValidator.CYRUS) ||
060                                    screenName.equals(DefaultScreenNameValidator.POSTFIX)) {
061    
062                                    screenName += StringPool.PERIOD + userId;
063                            }
064                    }
065                    else {
066                            screenName = String.valueOf(userId);
067                    }
068    
069                    String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
070                            companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
071                            StringPool.NEW_LINE, _ADMIN_RESERVED_SCREEN_NAMES);
072    
073                    for (String reservedScreenName : reservedScreenNames) {
074                            if (screenName.equalsIgnoreCase(reservedScreenName)) {
075                                    return getUnusedScreenName(companyId, screenName);
076                            }
077                    }
078    
079                    try {
080                            UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
081                    }
082                    catch (NoSuchUserException nsue) {
083                            try {
084                                    GroupLocalServiceUtil.getFriendlyURLGroup(
085                                            companyId, StringPool.SLASH + screenName);
086                            }
087                            catch (NoSuchGroupException nsge) {
088                                    return screenName;
089                            }
090                    }
091    
092                    return getUnusedScreenName(companyId, screenName);
093            }
094    
095            protected String getUnusedScreenName(long companyId, String screenName)
096                    throws PortalException, SystemException {
097    
098                    for (int i = 1;; i++) {
099                            String tempScreenName = screenName + StringPool.PERIOD + i;
100    
101                            try {
102                                    UserLocalServiceUtil.getUserByScreenName(
103                                            companyId, tempScreenName);
104                            }
105                            catch (NoSuchUserException nsue) {
106                                    try {
107                                            GroupLocalServiceUtil.getFriendlyURLGroup(
108                                                    companyId, StringPool.SLASH + tempScreenName);
109                                    }
110                                    catch (NoSuchGroupException nsge) {
111                                            screenName = tempScreenName;
112    
113                                            break;
114                                    }
115                            }
116                    }
117    
118                    return screenName;
119            }
120    
121            private static final String[] _ADMIN_RESERVED_SCREEN_NAMES =
122                    StringUtil.splitLines(
123                            PropsUtil.get(PropsKeys.ADMIN_RESERVED_SCREEN_NAMES));
124    
125            private static final boolean _USERS_SCREEN_NAME_ALLOW_NUMERIC =
126                    GetterUtil.getBoolean(
127                            PropsUtil.get(PropsKeys.USERS_SCREEN_NAME_ALLOW_NUMERIC));
128    
129    }