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.kernel.security.permission.ActionKeys;
28  import com.liferay.portal.model.Organization;
29  import com.liferay.portal.security.auth.PrincipalException;
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          checkPermission(organizationId, ActionKeys.DELETE);
94  
95          organizationLocalService.deleteOrganization(organizationId);
96      }
97  
98      public Organization getOrganization(long organizationId)
99          throws PortalException, SystemException {
100 
101         checkPermission(organizationId, ActionKeys.VIEW);
102 
103         return organizationLocalService.getOrganization(organizationId);
104     }
105 
106     public long getOrganizationId(long companyId, String name)
107         throws PortalException, SystemException {
108 
109         return organizationLocalService.getOrganizationId(companyId, name);
110     }
111 
112     public List getUserOrganizations(long userId)
113         throws PortalException, SystemException {
114 
115         return organizationLocalService.getUserOrganizations(userId);
116     }
117 
118     public void setGroupOrganizations(long groupId, long[] organizationIds)
119         throws PortalException, SystemException {
120 
121         GroupPermissionUtil.check(
122             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
123 
124         organizationLocalService.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.ASSIGN_MEMBERS);
133 
134         organizationLocalService.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         organizationLocalService.unsetPasswordPolicyOrganizations(
146             passwordPolicyId, organizationIds);
147     }
148 
149     public Organization updateOrganization(
150             long organizationId, long parentOrganizationId, String name,
151             int type, boolean recursable, long regionId, long countryId,
152             int statusId, String comments)
153         throws PortalException, SystemException {
154 
155         checkPermission(organizationId, ActionKeys.UPDATE);
156 
157         return organizationLocalService.updateOrganization(
158             getUser().getCompanyId(), organizationId, parentOrganizationId,
159             name, type, recursable, regionId, countryId, statusId, comments);
160     }
161 
162     protected void checkPermission(long organizationId, String actionId)
163         throws PortalException, SystemException {
164 
165         OrganizationPermissionUtil.check(
166             getPermissionChecker(), organizationId, actionId);
167     }
168 
169 }