1   /**
2    * Copyright (c) 2000-2009 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 PortalException, SystemException {
116 
117         return organizationLocalService.getUserOrganizations(userId);
118     }
119 
120     public List<Organization> getUserOrganizations(
121             long userId, boolean inheritUserGroups)
122         throws PortalException, SystemException {
123 
124         return organizationLocalService.getUserOrganizations(
125             userId, inheritUserGroups);
126     }
127 
128     public void setGroupOrganizations(long groupId, long[] organizationIds)
129         throws PortalException, SystemException {
130 
131         GroupPermissionUtil.check(
132             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
133 
134         organizationLocalService.setGroupOrganizations(
135             groupId, organizationIds);
136     }
137 
138     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
139         throws PortalException, SystemException {
140 
141         GroupPermissionUtil.check(
142             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
143 
144         organizationLocalService.unsetGroupOrganizations(
145             groupId, organizationIds);
146     }
147 
148     public void unsetPasswordPolicyOrganizations(
149             long passwordPolicyId, long[] organizationIds)
150         throws PortalException, SystemException {
151 
152         PasswordPolicyPermissionUtil.check(
153             getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
154 
155         organizationLocalService.unsetPasswordPolicyOrganizations(
156             passwordPolicyId, organizationIds);
157     }
158 
159     public Organization updateOrganization(
160             long organizationId, long parentOrganizationId, String name,
161             int type, boolean recursable, long regionId, long countryId,
162             int statusId, String comments)
163         throws PortalException, SystemException {
164 
165         OrganizationPermissionUtil.check(
166             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
167 
168         return organizationLocalService.updateOrganization(
169             getUser().getCompanyId(), organizationId, parentOrganizationId,
170             name, type, recursable, regionId, countryId, statusId, comments);
171     }
172 
173 }