1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.security.auth;
16  
17  import com.liferay.portal.NoSuchGroupException;
18  import com.liferay.portal.NoSuchUserException;
19  import com.liferay.portal.PortalException;
20  import com.liferay.portal.SystemException;
21  import com.liferay.portal.kernel.util.PrefsPropsUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.PropsUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  
30  /**
31   * <a href="DefaultScreenNameGenerator.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Alexander Chow
35   * @author Juan Fernández
36   */
37  public class DefaultScreenNameGenerator implements ScreenNameGenerator {
38  
39      public String generate(long companyId, long userId, String emailAddress)
40          throws Exception {
41  
42          String screenName = null;
43  
44          if (Validator.isNotNull(emailAddress)) {
45              screenName = StringUtil.extractFirst(
46                  emailAddress, StringPool.AT).toLowerCase();
47  
48              screenName = StringUtil.replace(
49                  screenName,
50                  new String[] {StringPool.SLASH, StringPool.UNDERLINE},
51                  new String[] {StringPool.PERIOD, StringPool.PERIOD});
52  
53              if (screenName.equals(DefaultScreenNameValidator.CYRUS) ||
54                  screenName.equals(DefaultScreenNameValidator.POSTFIX)) {
55  
56                  screenName += StringPool.PERIOD + userId;
57              }
58          }
59          else {
60              screenName = String.valueOf(userId);
61          }
62  
63          String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
64              companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
65              StringPool.NEW_LINE, _ADMIN_RESERVED_SCREEN_NAMES);
66  
67          for (String reservedScreenName : reservedScreenNames) {
68              if (screenName.equalsIgnoreCase(reservedScreenName)) {
69                  return getUnusedScreenName(companyId, screenName);
70              }
71          }
72  
73          try {
74              UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
75          }
76          catch (NoSuchUserException nsue) {
77              try {
78                  GroupLocalServiceUtil.getFriendlyURLGroup(
79                      companyId, StringPool.SLASH + screenName);
80              }
81              catch (NoSuchGroupException nsge) {
82                  return screenName;
83              }
84          }
85  
86          return getUnusedScreenName(companyId, screenName);
87      }
88  
89      protected String getUnusedScreenName(long companyId, String screenName)
90          throws PortalException, SystemException {
91  
92          for (int i = 1;; i++) {
93              String tempScreenName = screenName + StringPool.PERIOD + i;
94  
95              try {
96                  UserLocalServiceUtil.getUserByScreenName(
97                      companyId, tempScreenName);
98              }
99              catch (NoSuchUserException nsue) {
100                 try {
101                     GroupLocalServiceUtil.getFriendlyURLGroup(
102                         companyId, StringPool.SLASH + tempScreenName);
103                 }
104                 catch (NoSuchGroupException nsge) {
105                     screenName = tempScreenName;
106 
107                     break;
108                 }
109             }
110         }
111 
112         return screenName;
113     }
114 
115     private static final String[] _ADMIN_RESERVED_SCREEN_NAMES =
116         StringUtil.split(
117             PropsUtil.get(PropsKeys.ADMIN_RESERVED_SCREEN_NAMES),
118             StringPool.NEW_LINE);
119 
120 }