1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.model.Organization;
25  import com.liferay.portal.security.auth.PrincipalException;
26  import com.liferay.portal.security.permission.ActionKeys;
27  import com.liferay.portal.service.base.OrganizationServiceBaseImpl;
28  import com.liferay.portal.service.permission.GroupPermissionUtil;
29  import com.liferay.portal.service.permission.OrganizationPermissionUtil;
30  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
31  import com.liferay.portal.service.permission.PortalPermissionUtil;
32  
33  import java.util.List;
34  
35  /**
36   * <a href="OrganizationServiceImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   * @author Jorge Ferrer
40   *
41   */
42  public class OrganizationServiceImpl extends OrganizationServiceBaseImpl {
43  
44      public void addGroupOrganizations(long groupId, long[] organizationIds)
45          throws PortalException, SystemException {
46  
47          GroupPermissionUtil.check(
48              getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
49  
50          organizationLocalService.addGroupOrganizations(
51              groupId, organizationIds);
52      }
53  
54      public void addPasswordPolicyOrganizations(
55              long passwordPolicyId, long[] organizationIds)
56          throws PortalException, SystemException {
57  
58          PasswordPolicyPermissionUtil.check(
59              getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
60  
61          organizationLocalService.addPasswordPolicyOrganizations(
62              passwordPolicyId, organizationIds);
63      }
64  
65      public Organization addOrganization(
66              long parentOrganizationId, String name, int type,
67              boolean recursable, long regionId, long countryId, int statusId,
68              String comments)
69          throws PortalException, SystemException {
70  
71          if (!OrganizationPermissionUtil.contains(
72                  getPermissionChecker(), parentOrganizationId,
73                  ActionKeys.MANAGE_SUBORGANIZATIONS) &&
74              !PortalPermissionUtil.contains(
75                  getPermissionChecker(), ActionKeys.ADD_ORGANIZATION)) {
76  
77              throw new PrincipalException(
78                  "User " + getUserId() + " does not have permissions to add " +
79                      "an organization with parent " + parentOrganizationId);
80          }
81  
82          return organizationLocalService.addOrganization(
83              getUserId(), parentOrganizationId, name, type, recursable,
84              regionId, countryId, statusId, comments);
85      }
86  
87      public void deleteOrganization(long organizationId)
88          throws PortalException, SystemException {
89  
90          OrganizationPermissionUtil.check(
91              getPermissionChecker(), organizationId, ActionKeys.DELETE);
92  
93          organizationLocalService.deleteOrganization(organizationId);
94      }
95  
96      public Organization getOrganization(long organizationId)
97          throws PortalException, SystemException {
98  
99          OrganizationPermissionUtil.check(
100             getPermissionChecker(), organizationId, ActionKeys.VIEW);
101 
102         return organizationLocalService.getOrganization(organizationId);
103     }
104 
105     public long getOrganizationId(long companyId, String name)
106         throws SystemException {
107 
108         return organizationLocalService.getOrganizationId(companyId, name);
109     }
110 
111     public List<Organization> getUserOrganizations(long userId)
112         throws SystemException {
113 
114         return organizationLocalService.getUserOrganizations(userId);
115     }
116 
117     public void setGroupOrganizations(long groupId, long[] organizationIds)
118         throws PortalException, SystemException {
119 
120         GroupPermissionUtil.check(
121             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
122 
123         organizationLocalService.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.ASSIGN_MEMBERS);
132 
133         organizationLocalService.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         organizationLocalService.unsetPasswordPolicyOrganizations(
145             passwordPolicyId, organizationIds);
146     }
147 
148     public Organization updateOrganization(
149             long organizationId, long parentOrganizationId, String name,
150             int type, boolean recursable, long regionId, long countryId,
151             int statusId, String comments)
152         throws PortalException, SystemException {
153 
154         OrganizationPermissionUtil.check(
155             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
156 
157         return organizationLocalService.updateOrganization(
158             getUser().getCompanyId(), organizationId, parentOrganizationId,
159             name, type, recursable, regionId, countryId, statusId, comments);
160     }
161 
162 }