1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.googleapps;
16  
17  import com.liferay.portal.kernel.googleapps.GNickname;
18  import com.liferay.portal.kernel.googleapps.GNicknameManager;
19  import com.liferay.portal.kernel.googleapps.GUser;
20  import com.liferay.portal.kernel.googleapps.GUserManager;
21  import com.liferay.portal.kernel.googleapps.GoogleAppsException;
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.StringBundler;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.service.UserLocalServiceUtil;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  /**
38   * <a href="GUserManagerImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class GUserManagerImpl extends GBaseManagerImpl implements GUserManager {
43  
44      public GUserManagerImpl(GoogleApps googleApps) {
45          super(googleApps);
46  
47          GAuthenticator gAuthenticator = googleApps.getGAuthenticator();
48  
49          StringBundler sb = new StringBundler(4);
50  
51          sb.append(APPS_URL);
52          sb.append(StringPool.SLASH);
53          sb.append(gAuthenticator.getDomain());
54          sb.append("/user/2.0");
55  
56          userURL = sb.toString();
57      }
58  
59      public void addGUser(
60              long userId, String password, String firstName, String lastName)
61          throws GoogleAppsException {
62  
63          Document document = SAXReaderUtil.createDocument();
64  
65          Element atomEntryElement = addAtomEntry(document);
66  
67          addAtomCategory(atomEntryElement, "user");
68  
69          Element appsLoginElement = atomEntryElement.addElement(
70              "apps:login");
71  
72          appsLoginElement.addAttribute("password", password);
73          appsLoginElement.addAttribute("userName", String.valueOf(userId));
74  
75          Element appsNameElement = atomEntryElement.addElement("apps:name");
76  
77          appsNameElement.addAttribute("familyName", lastName);
78          appsNameElement.addAttribute("givenName", firstName);
79  
80          submitAdd(userURL, document);
81      }
82  
83      public void deleteGUser(long userId) throws GoogleAppsException {
84          submitDelete(getUserURL(userId));
85      }
86  
87      public GUser getGUser(long userId) throws GoogleAppsException {
88          Document document = getDocument(getUserURL(userId));
89  
90          if (hasError(document)) {
91              if (_log.isInfoEnabled()) {
92                  _log.info(getErrorMessage(document));
93              }
94  
95              return null;
96          }
97  
98          Element atomEntryElement = document.getRootElement();
99  
100         return getGUser(atomEntryElement);
101     }
102 
103     public GUser getGUser(String emailAddress) throws GoogleAppsException {
104         int pos = emailAddress.indexOf(StringPool.AT);
105 
106         if (pos == -1) {
107             return null;
108         }
109 
110         String nickname = emailAddress.substring(0, pos);
111 
112         if (Validator.isNumber(nickname)) {
113             long userId = GetterUtil.getLong(nickname);
114 
115             return getGUser(userId);
116         }
117         else {
118             try {
119                 User user = UserLocalServiceUtil.getUserByEmailAddress(
120                     getCompanyId(), emailAddress);
121 
122                 return getGUser(user.getUserId());
123             }
124             catch (Exception e) {
125             }
126 
127             GNicknameManager gNicknameManager =
128                 googleApps.getGNicknameManager();
129 
130             GNickname gNickname = gNicknameManager.getGNickname(nickname);
131 
132             if (gNickname != null) {
133                 return getGUser(gNickname.getUserId());
134             }
135 
136             return null;
137         }
138     }
139 
140     public List<GUser> getGUsers() throws GoogleAppsException {
141         List<GUser> gUsers = new ArrayList<GUser>();
142 
143         getGUsers(gUsers, userURL);
144 
145         return gUsers;
146     }
147 
148     public void updateActive(long userId, boolean active)
149         throws GoogleAppsException {
150 
151         Document document = getDocument(getUserURL(userId));
152 
153         if (hasError(document)) {
154             if (_log.isInfoEnabled()) {
155                 _log.info(getErrorMessage(document));
156             }
157 
158             return;
159         }
160 
161         Element atomEntryElement = document.getRootElement();
162 
163         Element appsLoginElement = atomEntryElement.element(
164             getAppsQName("login"));
165 
166         appsLoginElement.addAttribute("suspended", String.valueOf(!active));
167 
168         submitUpdate(getUserURL(userId), document);
169     }
170 
171     public void updatePassword(long userId, String password)
172         throws GoogleAppsException {
173 
174         Document document = getDocument(getUserURL(userId));
175 
176         if (hasError(document)) {
177             if (_log.isInfoEnabled()) {
178                 _log.info(getErrorMessage(document));
179             }
180 
181             return;
182         }
183 
184         Element atomEntryElement = document.getRootElement();
185 
186         Element appsLoginElement = atomEntryElement.element(
187             getAppsQName("login"));
188 
189         appsLoginElement.addAttribute("password", password);
190 
191         submitUpdate(getUserURL(userId), document);
192     }
193 
194     protected GUser getGUser(Element atomEntryElement) {
195         GUser gUser = new GUser();
196 
197         Element appsLoginElement = atomEntryElement.element(
198             getAppsQName("login"));
199         Element appsNameElement = atomEntryElement.element(
200             getAppsQName("name"));
201 
202         boolean active = !GetterUtil.getBoolean(
203             appsLoginElement.attributeValue("suspended"));
204 
205         gUser.setActive(active);
206 
207         boolean administrator = GetterUtil.getBoolean(
208             appsLoginElement.attributeValue("admin"));
209 
210         gUser.setAdministrator(administrator);
211 
212         boolean agreedToTermsOfUse = GetterUtil.getBoolean(
213             appsLoginElement.attributeValue("agreedToTerms"));
214 
215         gUser.setAgreedToTermsOfUse(agreedToTermsOfUse);
216 
217         String firstName = appsNameElement.attributeValue("givenName");
218 
219         gUser.setFirstName(firstName);
220 
221         String lastName = appsNameElement.attributeValue("familyName");
222 
223         gUser.setLastName(lastName);
224 
225         long userId = GetterUtil.getLong(
226             appsLoginElement.attributeValue("userName"));
227 
228         gUser.setUserId(userId);
229 
230         return gUser;
231     }
232 
233     protected void getGUsers(final List<GUser> gUsers, String url)
234         throws GoogleAppsException {
235 
236         Document document = getDocument(url);
237 
238         Element atomFeedElement = document.getRootElement();
239 
240         List<Element> atomEntryElements = atomFeedElement.elements(
241             getAtomQName("entry"));
242 
243         for (Element atomEntryElement : atomEntryElements) {
244             GUser gUser = getGUser(atomEntryElement);
245 
246             gUsers.add(gUser);
247         }
248 
249         new GetNextItems(url, atomFeedElement) {
250 
251             public void getNextItems(String nextURL)
252                 throws GoogleAppsException {
253 
254                 getGUsers(gUsers, nextURL);
255             }
256 
257         };
258     }
259 
260     protected String getUserURL(long userId) {
261         return userURL.concat(StringPool.SLASH).concat(String.valueOf(userId));
262     }
263 
264     protected String userURL;
265 
266     private static Log _log = LogFactoryUtil.getLog(GUserManagerImpl.class);
267 
268 }