1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.Organization;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portal.service.base.OrganizationServiceBaseImpl;
31  import com.liferay.portal.service.permission.GroupPermissionUtil;
32  import com.liferay.portal.service.permission.OrganizationPermissionUtil;
33  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
34  import com.liferay.portal.service.permission.PortalPermissionUtil;
35  
36  import java.util.List;
37  
38  /**
39   * <a href="OrganizationServiceImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Jorge Ferrer
43   *
44   */
45  public class OrganizationServiceImpl extends OrganizationServiceBaseImpl {
46  
47      public void addGroupOrganizations(long groupId, long[] organizationIds)
48          throws PortalException, SystemException {
49  
50          GroupPermissionUtil.check(
51              getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
52  
53          organizationLocalService.addGroupOrganizations(
54              groupId, organizationIds);
55      }
56  
57      public void addPasswordPolicyOrganizations(
58              long passwordPolicyId, long[] organizationIds)
59          throws PortalException, SystemException {
60  
61          PasswordPolicyPermissionUtil.check(
62              getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
63  
64          organizationLocalService.addPasswordPolicyOrganizations(
65              passwordPolicyId, organizationIds);
66      }
67  
68      public Organization addOrganization(
69              long parentOrganizationId, String name, int type,
70              boolean recursable, long regionId, long countryId, int statusId,
71              String comments)
72          throws PortalException, SystemException {
73  
74          if (!OrganizationPermissionUtil.contains(
75                  getPermissionChecker(), parentOrganizationId,
76                  ActionKeys.MANAGE_SUBORGANIZATIONS) &&
77              !PortalPermissionUtil.contains(
78                  getPermissionChecker(), ActionKeys.ADD_ORGANIZATION)) {
79  
80              throw new PrincipalException(
81                  "User " + getUserId() + " does not have permissions to add " +
82                      "an organization with parent " + parentOrganizationId);
83          }
84  
85          return organizationLocalService.addOrganization(
86              getUserId(), parentOrganizationId, name, type, recursable,
87              regionId, countryId, statusId, comments);
88      }
89  
90      public void deleteOrganization(long organizationId)
91          throws PortalException, SystemException {
92  
93          OrganizationPermissionUtil.check(
94              getPermissionChecker(), organizationId, ActionKeys.DELETE);
95  
96          organizationLocalService.deleteOrganization(organizationId);
97      }
98  
99      public Organization getOrganization(long organizationId)
100         throws PortalException, SystemException {
101 
102         OrganizationPermissionUtil.check(
103             getPermissionChecker(), organizationId, ActionKeys.VIEW);
104 
105         return organizationLocalService.getOrganization(organizationId);
106     }
107 
108     public long getOrganizationId(long companyId, String name)
109         throws SystemException {
110 
111         return organizationLocalService.getOrganizationId(companyId, name);
112     }
113 
114     public List<Organization> getUserOrganizations(long userId)
115         throws SystemException {
116 
117         return organizationLocalService.getUserOrganizations(userId);
118     }
119 
120     public void setGroupOrganizations(long groupId, long[] organizationIds)
121         throws PortalException, SystemException {
122 
123         GroupPermissionUtil.check(
124             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
125 
126         organizationLocalService.setGroupOrganizations(
127             groupId, organizationIds);
128     }
129 
130     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
131         throws PortalException, SystemException {
132 
133         GroupPermissionUtil.check(
134             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
135 
136         organizationLocalService.unsetGroupOrganizations(
137             groupId, organizationIds);
138     }
139 
140     public void unsetPasswordPolicyOrganizations(
141             long passwordPolicyId, long[] organizationIds)
142         throws PortalException, SystemException {
143 
144         PasswordPolicyPermissionUtil.check(
145             getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
146 
147         organizationLocalService.unsetPasswordPolicyOrganizations(
148             passwordPolicyId, organizationIds);
149     }
150 
151     public Organization updateOrganization(
152             long organizationId, long parentOrganizationId, String name,
153             int type, boolean recursable, long regionId, long countryId,
154             int statusId, String comments)
155         throws PortalException, SystemException {
156 
157         OrganizationPermissionUtil.check(
158             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
159 
160         return organizationLocalService.updateOrganization(
161             getUser().getCompanyId(), organizationId, parentOrganizationId,
162             name, type, recursable, regionId, countryId, statusId, comments);
163     }
164 
165 }