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