1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class OrganizationServiceImpl extends OrganizationServiceBaseImpl {
45  
46      public void addGroupOrganizations(long groupId, long[] organizationIds)
47          throws PortalException, SystemException {
48  
49          GroupPermissionUtil.check(
50              getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
51  
52          organizationLocalService.addGroupOrganizations(
53              groupId, organizationIds);
54      }
55  
56      public void addPasswordPolicyOrganizations(
57              long passwordPolicyId, long[] organizationIds)
58          throws PortalException, SystemException {
59  
60          PasswordPolicyPermissionUtil.check(
61              getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
62  
63          organizationLocalService.addPasswordPolicyOrganizations(
64              passwordPolicyId, organizationIds);
65      }
66  
67      public Organization addOrganization(
68              long parentOrganizationId, String name, int type,
69              boolean recursable, long regionId, long countryId, int statusId,
70              String comments)
71          throws PortalException, SystemException {
72  
73          if (!OrganizationPermissionUtil.contains(
74                  getPermissionChecker(), parentOrganizationId,
75                  ActionKeys.MANAGE_SUBORGANIZATIONS) &&
76              !PortalPermissionUtil.contains(
77                  getPermissionChecker(), ActionKeys.ADD_ORGANIZATION)) {
78  
79              throw new PrincipalException(
80                  "User " + getUserId() + " does not have permissions to add " +
81                      "an organization with parent " + parentOrganizationId);
82          }
83  
84          return organizationLocalService.addOrganization(
85              getUserId(), parentOrganizationId, name, type, recursable,
86              regionId, countryId, statusId, comments);
87      }
88  
89      public void deleteOrganization(long organizationId)
90          throws PortalException, SystemException {
91  
92          OrganizationPermissionUtil.check(
93              getPermissionChecker(), organizationId, ActionKeys.DELETE);
94  
95          organizationLocalService.deleteOrganization(organizationId);
96      }
97  
98      public Organization getOrganization(long organizationId)
99          throws PortalException, SystemException {
100 
101         OrganizationPermissionUtil.check(
102             getPermissionChecker(), organizationId, ActionKeys.VIEW);
103 
104         return organizationLocalService.getOrganization(organizationId);
105     }
106 
107     public long getOrganizationId(long companyId, String name)
108         throws SystemException {
109 
110         return organizationLocalService.getOrganizationId(companyId, name);
111     }
112 
113     public List<Organization> getUserOrganizations(long userId)
114         throws PortalException, SystemException {
115 
116         return organizationLocalService.getUserOrganizations(userId);
117     }
118 
119     public List<Organization> getUserOrganizations(
120             long userId, boolean inheritUserGroups)
121         throws PortalException, SystemException {
122 
123         return organizationLocalService.getUserOrganizations(
124             userId, inheritUserGroups);
125     }
126 
127     public void setGroupOrganizations(long groupId, long[] organizationIds)
128         throws PortalException, SystemException {
129 
130         GroupPermissionUtil.check(
131             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
132 
133         organizationLocalService.setGroupOrganizations(
134             groupId, organizationIds);
135     }
136 
137     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
138         throws PortalException, SystemException {
139 
140         GroupPermissionUtil.check(
141             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
142 
143         organizationLocalService.unsetGroupOrganizations(
144             groupId, organizationIds);
145     }
146 
147     public void unsetPasswordPolicyOrganizations(
148             long passwordPolicyId, long[] organizationIds)
149         throws PortalException, SystemException {
150 
151         PasswordPolicyPermissionUtil.check(
152             getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
153 
154         organizationLocalService.unsetPasswordPolicyOrganizations(
155             passwordPolicyId, organizationIds);
156     }
157 
158     public Organization updateOrganization(
159             long organizationId, long parentOrganizationId, String name,
160             int type, boolean recursable, long regionId, long countryId,
161             int statusId, String comments)
162         throws PortalException, SystemException {
163 
164         OrganizationPermissionUtil.check(
165             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
166 
167         return organizationLocalService.updateOrganization(
168             getUser().getCompanyId(), organizationId, parentOrganizationId,
169             name, type, recursable, regionId, countryId, statusId, comments);
170     }
171 
172 }