1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.ListUtil;
25  import com.liferay.portal.model.Address;
26  import com.liferay.portal.model.EmailAddress;
27  import com.liferay.portal.model.OrgLabor;
28  import com.liferay.portal.model.Organization;
29  import com.liferay.portal.model.OrganizationConstants;
30  import com.liferay.portal.model.Phone;
31  import com.liferay.portal.model.Website;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.security.permission.ActionKeys;
34  import com.liferay.portal.security.permission.PermissionChecker;
35  import com.liferay.portal.service.ServiceContext;
36  import com.liferay.portal.service.base.OrganizationServiceBaseImpl;
37  import com.liferay.portal.service.permission.GroupPermissionUtil;
38  import com.liferay.portal.service.permission.OrganizationPermissionUtil;
39  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
40  import com.liferay.portal.service.permission.PortalPermissionUtil;
41  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
42  
43  import java.util.Iterator;
44  import java.util.LinkedHashMap;
45  import java.util.List;
46  
47  /**
48   * <a href="OrganizationServiceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Jorge Ferrer
52   * @author Julio Camarero
53   *
54   */
55  public class OrganizationServiceImpl extends OrganizationServiceBaseImpl {
56  
57      public void addGroupOrganizations(long groupId, long[] organizationIds)
58          throws PortalException, SystemException {
59  
60          GroupPermissionUtil.check(
61              getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
62  
63          organizationLocalService.addGroupOrganizations(
64              groupId, organizationIds);
65      }
66  
67      public void addPasswordPolicyOrganizations(
68              long passwordPolicyId, long[] organizationIds)
69          throws PortalException, SystemException {
70  
71          PasswordPolicyPermissionUtil.check(
72              getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
73  
74          organizationLocalService.addPasswordPolicyOrganizations(
75              passwordPolicyId, organizationIds);
76      }
77  
78      public Organization addOrganization(
79              long parentOrganizationId, String name, String type,
80              boolean recursable, long regionId, long countryId, int statusId,
81              String comments, ServiceContext serviceContext)
82          throws PortalException, SystemException {
83  
84          if (!OrganizationPermissionUtil.contains(
85                  getPermissionChecker(), parentOrganizationId,
86                  ActionKeys.MANAGE_SUBORGANIZATIONS) &&
87              !PortalPermissionUtil.contains(
88                  getPermissionChecker(), ActionKeys.ADD_ORGANIZATION)) {
89  
90              throw new PrincipalException(
91                  "User " + getUserId() + " does not have permissions to add " +
92                      "an organization with parent " + parentOrganizationId);
93          }
94  
95          return organizationLocalService.addOrganization(
96              getUserId(), parentOrganizationId, name, type, recursable,
97              regionId, countryId, statusId, comments, serviceContext);
98      }
99  
100     public Organization addOrganization(
101             long parentOrganizationId, String name, String type,
102             boolean recursable, long regionId, long countryId, int statusId,
103             String comments, List<Address> addresses,
104             List<EmailAddress> emailAddresses, List<OrgLabor> orgLabors,
105             List<Phone> phones, List<Website> websites,
106             ServiceContext serviceContext)
107         throws PortalException, SystemException {
108 
109         Organization organization = addOrganization(
110             parentOrganizationId, name, type, recursable, regionId, countryId,
111             statusId, comments, serviceContext);
112 
113         EnterpriseAdminUtil.updateAddresses(
114             Organization.class.getName(), organization.getOrganizationId(),
115             addresses);
116 
117         EnterpriseAdminUtil.updateEmailAddresses(
118             Organization.class.getName(), organization.getOrganizationId(),
119             emailAddresses);
120 
121         EnterpriseAdminUtil.updateOrgLabors(organization.getOrganizationId(),
122             orgLabors);
123 
124         EnterpriseAdminUtil.updatePhones(
125             Organization.class.getName(), organization.getOrganizationId(),
126             phones);
127 
128         EnterpriseAdminUtil.updateWebsites(
129             Organization.class.getName(), organization.getOrganizationId(),
130             websites);
131 
132         return organization;
133     }
134 
135     public void deleteLogo(long organizationId)
136         throws PortalException, SystemException {
137 
138         OrganizationPermissionUtil.check(
139             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
140 
141         organizationLocalService.deleteLogo(organizationId);
142     }
143 
144     public void deleteOrganization(long organizationId)
145         throws PortalException, SystemException {
146 
147         OrganizationPermissionUtil.check(
148             getPermissionChecker(), organizationId, ActionKeys.DELETE);
149 
150         organizationLocalService.deleteOrganization(organizationId);
151     }
152 
153     public List<Organization> getManageableOrganizations(
154             String actionId, int max)
155         throws PortalException, SystemException {
156 
157         PermissionChecker permissionChecker = getPermissionChecker();
158 
159         if (permissionChecker.isCompanyAdmin()) {
160             return organizationLocalService.search(
161                 permissionChecker.getCompanyId(),
162                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null,
163                 null, null, null, 0, max);
164         }
165 
166         LinkedHashMap<String, Object> params =
167             new LinkedHashMap<String, Object>();
168 
169         List<Organization> userOrganizations =
170             organizationLocalService.getUserOrganizations(
171                 permissionChecker.getUserId());
172 
173         Long[][] leftAndRightOrganizationIds =
174             EnterpriseAdminUtil.getLeftAndRightOrganizationIds(
175                 userOrganizations);
176 
177         params.put("organizationsTree", leftAndRightOrganizationIds);
178 
179         List<Organization> manageableOrganizations =
180             organizationLocalService.search(
181                 permissionChecker.getCompanyId(),
182                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null,
183                 null, null, params, 0, max);
184 
185         manageableOrganizations = ListUtil.copy(manageableOrganizations);
186 
187         Iterator<Organization> itr = manageableOrganizations.iterator();
188 
189         while (itr.hasNext()) {
190             Organization organization = itr.next();
191 
192             if (!OrganizationPermissionUtil.contains(
193                     permissionChecker, organization, actionId)) {
194 
195                 itr.remove();
196             }
197         }
198 
199         return manageableOrganizations;
200     }
201 
202     public Organization getOrganization(long organizationId)
203         throws PortalException, SystemException {
204 
205         OrganizationPermissionUtil.check(
206             getPermissionChecker(), organizationId, ActionKeys.VIEW);
207 
208         return organizationLocalService.getOrganization(organizationId);
209     }
210 
211     public long getOrganizationId(long companyId, String name)
212         throws SystemException {
213 
214         return organizationLocalService.getOrganizationId(companyId, name);
215     }
216 
217     public List<Organization> getUserOrganizations(long userId)
218         throws SystemException {
219 
220         return organizationLocalService.getUserOrganizations(userId);
221     }
222 
223     public void setGroupOrganizations(long groupId, long[] organizationIds)
224         throws PortalException, SystemException {
225 
226         GroupPermissionUtil.check(
227             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
228 
229         organizationLocalService.setGroupOrganizations(
230             groupId, organizationIds);
231     }
232 
233     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
234         throws PortalException, SystemException {
235 
236         GroupPermissionUtil.check(
237             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
238 
239         organizationLocalService.unsetGroupOrganizations(
240             groupId, organizationIds);
241     }
242 
243     public void unsetPasswordPolicyOrganizations(
244             long passwordPolicyId, long[] organizationIds)
245         throws PortalException, SystemException {
246 
247         PasswordPolicyPermissionUtil.check(
248             getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
249 
250         organizationLocalService.unsetPasswordPolicyOrganizations(
251             passwordPolicyId, organizationIds);
252     }
253 
254     public Organization updateOrganization(
255             long organizationId, long parentOrganizationId, String name,
256             String type, boolean recursable, long regionId, long countryId,
257             int statusId, String comments, ServiceContext serviceContext)
258         throws PortalException, SystemException {
259 
260         OrganizationPermissionUtil.check(
261             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
262 
263         return organizationLocalService.updateOrganization(
264             getUser().getCompanyId(), organizationId, parentOrganizationId,
265             name, type, recursable, regionId, countryId, statusId, comments,
266             serviceContext);
267     }
268 
269     public Organization updateOrganization(
270             long organizationId, long parentOrganizationId, String name,
271             String type, boolean recursable, long regionId, long countryId,
272             int statusId, String comments, List<Address> addresses,
273             List<EmailAddress> emailAddresses, List<OrgLabor> orgLabors,
274             List<Phone> phones, List<Website> websites,
275             ServiceContext serviceContext)
276         throws PortalException, SystemException {
277 
278         Organization organization = updateOrganization(
279             organizationId, parentOrganizationId, name, type, recursable,
280             regionId, countryId, statusId, comments, serviceContext);
281 
282         EnterpriseAdminUtil.updateAddresses(
283             Organization.class.getName(), organizationId, addresses);
284 
285         EnterpriseAdminUtil.updateEmailAddresses(
286             Organization.class.getName(), organizationId, emailAddresses);
287 
288         EnterpriseAdminUtil.updateOrgLabors(organizationId, orgLabors);
289 
290         EnterpriseAdminUtil.updatePhones(
291             Organization.class.getName(), organizationId, phones);
292 
293         EnterpriseAdminUtil.updateWebsites(
294             Organization.class.getName(), organizationId, websites);
295 
296         return organization;
297     }
298 
299 }