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