1   /**
2    * Copyright (c) 2000-2007 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.kernel.security.permission.ActionKeys;
28  import com.liferay.portal.model.Organization;
29  import com.liferay.portal.service.OrganizationLocalServiceUtil;
30  import com.liferay.portal.service.OrganizationService;
31  import com.liferay.portal.service.permission.GroupPermissionUtil;
32  import com.liferay.portal.service.permission.LocationPermissionUtil;
33  import com.liferay.portal.service.permission.OrganizationPermissionUtil;
34  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
35  import com.liferay.portal.service.permission.PortalPermissionUtil;
36  import com.liferay.portal.service.persistence.OrganizationUtil;
37  
38  import java.util.List;
39  
40  /**
41   * <a href="OrganizationServiceImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class OrganizationServiceImpl extends PrincipalBean
47      implements OrganizationService {
48  
49      public void addGroupOrganizations(long groupId, long[] organizationIds)
50          throws PortalException, SystemException {
51  
52          GroupPermissionUtil.check(
53              getPermissionChecker(), groupId, ActionKeys.UPDATE);
54  
55          OrganizationLocalServiceUtil.addGroupOrganizations(
56              groupId, organizationIds);
57      }
58  
59      public void addPasswordPolicyOrganizations(
60              long passwordPolicyId, long[] organizationIds)
61          throws PortalException, SystemException {
62  
63          PasswordPolicyPermissionUtil.check(
64              getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
65  
66          OrganizationLocalServiceUtil.addPasswordPolicyOrganizations(
67              passwordPolicyId, organizationIds);
68      }
69  
70      public Organization addOrganization(
71              long parentOrganizationId, String name, boolean location,
72              boolean recursable, long regionId, long countryId, int statusId)
73          throws PortalException, SystemException {
74  
75          if (location) {
76              OrganizationPermissionUtil.check(
77                  getPermissionChecker(), parentOrganizationId,
78                  ActionKeys.ADD_LOCATION);
79          }
80          else {
81              PortalPermissionUtil.check(
82                  getPermissionChecker(), ActionKeys.ADD_ORGANIZATION);
83          }
84  
85          return OrganizationLocalServiceUtil.addOrganization(
86              getUserId(), parentOrganizationId, name, location, recursable,
87              regionId, countryId, statusId);
88      }
89  
90      public void deleteOrganization(long organizationId)
91          throws PortalException, SystemException {
92  
93          checkPermission(organizationId, ActionKeys.DELETE);
94  
95          OrganizationLocalServiceUtil.deleteOrganization(organizationId);
96      }
97  
98      public Organization getOrganization(long organizationId)
99          throws PortalException, SystemException {
100 
101         checkPermission(organizationId, ActionKeys.VIEW);
102 
103         return OrganizationLocalServiceUtil.getOrganization(organizationId);
104     }
105 
106     public long getOrganizationId(long companyId, String name)
107         throws PortalException, SystemException {
108 
109         return OrganizationLocalServiceUtil.getOrganizationId(companyId, name);
110     }
111 
112     public List getUserOrganizations(long userId)
113         throws PortalException, SystemException {
114 
115         return OrganizationLocalServiceUtil.getUserOrganizations(userId);
116     }
117 
118     public void setGroupOrganizations(long groupId, long[] organizationIds)
119         throws PortalException, SystemException {
120 
121         GroupPermissionUtil.check(
122             getPermissionChecker(), groupId, ActionKeys.UPDATE);
123 
124         OrganizationLocalServiceUtil.setGroupOrganizations(
125             groupId, organizationIds);
126     }
127 
128     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
129         throws PortalException, SystemException {
130 
131         GroupPermissionUtil.check(
132             getPermissionChecker(), groupId, ActionKeys.UPDATE);
133 
134         OrganizationLocalServiceUtil.unsetGroupOrganizations(
135             groupId, organizationIds);
136     }
137 
138     public void unsetPasswordPolicyOrganizations(
139             long passwordPolicyId, long[] organizationIds)
140         throws PortalException, SystemException {
141 
142         PasswordPolicyPermissionUtil.check(
143             getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
144 
145         OrganizationLocalServiceUtil.unsetPasswordPolicyOrganizations(
146             passwordPolicyId, organizationIds);
147     }
148 
149     public Organization updateOrganization(
150             long organizationId, long parentOrganizationId, String name,
151             boolean location, boolean recursable, long regionId, long countryId,
152             int statusId)
153         throws PortalException, SystemException {
154 
155         checkPermission(organizationId, ActionKeys.UPDATE);
156 
157         return OrganizationLocalServiceUtil.updateOrganization(
158             getUser().getCompanyId(), organizationId, parentOrganizationId,
159             name, location, recursable, regionId, countryId, statusId);
160     }
161 
162     public Organization updateOrganization(long organizationId, String comments)
163         throws PortalException, SystemException {
164 
165         checkPermission(organizationId, ActionKeys.UPDATE);
166 
167         return OrganizationLocalServiceUtil.updateOrganization(
168             organizationId, comments);
169     }
170 
171     protected void checkPermission(long organizationId, String actionId)
172         throws PortalException, SystemException {
173 
174         Organization organization =
175             OrganizationUtil.findByPrimaryKey(organizationId);
176 
177         if (!organization.isLocation()) {
178             OrganizationPermissionUtil.check(
179                 getPermissionChecker(), organizationId, actionId);
180         }
181         else {
182             LocationPermissionUtil.check(
183                 getPermissionChecker(), organizationId, actionId);
184         }
185     }
186 
187 }