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.kernel.util.ListUtil;
28  import com.liferay.portal.model.Address;
29  import com.liferay.portal.model.EmailAddress;
30  import com.liferay.portal.model.OrgLabor;
31  import com.liferay.portal.model.Organization;
32  import com.liferay.portal.model.OrganizationConstants;
33  import com.liferay.portal.model.Phone;
34  import com.liferay.portal.model.Website;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.security.permission.ActionKeys;
37  import com.liferay.portal.security.permission.PermissionChecker;
38  import com.liferay.portal.service.ServiceContext;
39  import com.liferay.portal.service.base.OrganizationServiceBaseImpl;
40  import com.liferay.portal.service.permission.GroupPermissionUtil;
41  import com.liferay.portal.service.permission.OrganizationPermissionUtil;
42  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
43  import com.liferay.portal.service.permission.PortalPermissionUtil;
44  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
45  
46  import java.util.Iterator;
47  import java.util.LinkedHashMap;
48  import java.util.List;
49  
50  /**
51   * <a href="OrganizationServiceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   * @author Jorge Ferrer
55   * @author Julio Camarero
56   */
57  public class OrganizationServiceImpl extends OrganizationServiceBaseImpl {
58  
59      public void addGroupOrganizations(long groupId, long[] organizationIds)
60          throws PortalException, SystemException {
61  
62          GroupPermissionUtil.check(
63              getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
64  
65          organizationLocalService.addGroupOrganizations(
66              groupId, organizationIds);
67      }
68  
69      public void addPasswordPolicyOrganizations(
70              long passwordPolicyId, long[] organizationIds)
71          throws PortalException, SystemException {
72  
73          PasswordPolicyPermissionUtil.check(
74              getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
75  
76          organizationLocalService.addPasswordPolicyOrganizations(
77              passwordPolicyId, organizationIds);
78      }
79  
80      public Organization addOrganization(
81              long parentOrganizationId, String name, String type,
82              boolean recursable, long regionId, long countryId, int statusId,
83              String comments, ServiceContext serviceContext)
84          throws PortalException, SystemException {
85  
86          if (!OrganizationPermissionUtil.contains(
87                  getPermissionChecker(), parentOrganizationId,
88                  ActionKeys.MANAGE_SUBORGANIZATIONS) &&
89              !PortalPermissionUtil.contains(
90                  getPermissionChecker(), ActionKeys.ADD_ORGANIZATION)) {
91  
92              throw new PrincipalException(
93                  "User " + getUserId() + " does not have permissions to add " +
94                      "an organization with parent " + parentOrganizationId);
95          }
96  
97          return organizationLocalService.addOrganization(
98              getUserId(), parentOrganizationId, name, type, recursable,
99              regionId, countryId, statusId, comments, serviceContext);
100     }
101 
102     public Organization addOrganization(
103             long parentOrganizationId, String name, String type,
104             boolean recursable, long regionId, long countryId, int statusId,
105             String comments, List<Address> addresses,
106             List<EmailAddress> emailAddresses, List<OrgLabor> orgLabors,
107             List<Phone> phones, List<Website> websites,
108             ServiceContext serviceContext)
109         throws PortalException, SystemException {
110 
111         Organization organization = addOrganization(
112             parentOrganizationId, name, type, recursable, regionId, countryId,
113             statusId, comments, serviceContext);
114 
115         EnterpriseAdminUtil.updateAddresses(
116             Organization.class.getName(), organization.getOrganizationId(),
117             addresses);
118 
119         EnterpriseAdminUtil.updateEmailAddresses(
120             Organization.class.getName(), organization.getOrganizationId(),
121             emailAddresses);
122 
123         EnterpriseAdminUtil.updateOrgLabors(organization.getOrganizationId(),
124             orgLabors);
125 
126         EnterpriseAdminUtil.updatePhones(
127             Organization.class.getName(), organization.getOrganizationId(),
128             phones);
129 
130         EnterpriseAdminUtil.updateWebsites(
131             Organization.class.getName(), organization.getOrganizationId(),
132             websites);
133 
134         return organization;
135     }
136 
137     public void deleteLogo(long organizationId)
138         throws PortalException, SystemException {
139 
140         OrganizationPermissionUtil.check(
141             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
142 
143         organizationLocalService.deleteLogo(organizationId);
144     }
145 
146     public void deleteOrganization(long organizationId)
147         throws PortalException, SystemException {
148 
149         OrganizationPermissionUtil.check(
150             getPermissionChecker(), organizationId, ActionKeys.DELETE);
151 
152         organizationLocalService.deleteOrganization(organizationId);
153     }
154 
155     public List<Organization> getManageableOrganizations(
156             String actionId, int max)
157         throws PortalException, SystemException {
158 
159         PermissionChecker permissionChecker = getPermissionChecker();
160 
161         if (permissionChecker.isCompanyAdmin()) {
162             return organizationLocalService.search(
163                 permissionChecker.getCompanyId(),
164                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null,
165                 null, null, null, 0, max);
166         }
167 
168         LinkedHashMap<String, Object> params =
169             new LinkedHashMap<String, Object>();
170 
171         List<Organization> userOrganizations =
172             organizationLocalService.getUserOrganizations(
173                 permissionChecker.getUserId());
174 
175         Long[][] leftAndRightOrganizationIds =
176             EnterpriseAdminUtil.getLeftAndRightOrganizationIds(
177                 userOrganizations);
178 
179         params.put("organizationsTree", leftAndRightOrganizationIds);
180 
181         List<Organization> manageableOrganizations =
182             organizationLocalService.search(
183                 permissionChecker.getCompanyId(),
184                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null,
185                 null, null, params, 0, max);
186 
187         manageableOrganizations = ListUtil.copy(manageableOrganizations);
188 
189         Iterator<Organization> itr = manageableOrganizations.iterator();
190 
191         while (itr.hasNext()) {
192             Organization organization = itr.next();
193 
194             if (!OrganizationPermissionUtil.contains(
195                     permissionChecker, organization, actionId)) {
196 
197                 itr.remove();
198             }
199         }
200 
201         return manageableOrganizations;
202     }
203 
204     public Organization getOrganization(long organizationId)
205         throws PortalException, SystemException {
206 
207         OrganizationPermissionUtil.check(
208             getPermissionChecker(), organizationId, ActionKeys.VIEW);
209 
210         return organizationLocalService.getOrganization(organizationId);
211     }
212 
213     public long getOrganizationId(long companyId, String name)
214         throws SystemException {
215 
216         return organizationLocalService.getOrganizationId(companyId, name);
217     }
218 
219     public List<Organization> getUserOrganizations(long userId)
220         throws PortalException, SystemException {
221 
222         return organizationLocalService.getUserOrganizations(userId);
223     }
224 
225     public List<Organization> getUserOrganizations(
226             long userId, boolean inheritUserGroups)
227         throws PortalException, SystemException {
228 
229         return organizationLocalService.getUserOrganizations(
230             userId, inheritUserGroups);
231     }
232 
233     public void setGroupOrganizations(long groupId, long[] organizationIds)
234         throws PortalException, SystemException {
235 
236         GroupPermissionUtil.check(
237             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
238 
239         organizationLocalService.setGroupOrganizations(
240             groupId, organizationIds);
241     }
242 
243     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
244         throws PortalException, SystemException {
245 
246         GroupPermissionUtil.check(
247             getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
248 
249         organizationLocalService.unsetGroupOrganizations(
250             groupId, organizationIds);
251     }
252 
253     public void unsetPasswordPolicyOrganizations(
254             long passwordPolicyId, long[] organizationIds)
255         throws PortalException, SystemException {
256 
257         PasswordPolicyPermissionUtil.check(
258             getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
259 
260         organizationLocalService.unsetPasswordPolicyOrganizations(
261             passwordPolicyId, organizationIds);
262     }
263 
264     public Organization updateOrganization(
265             long organizationId, long parentOrganizationId, String name,
266             String type, boolean recursable, long regionId, long countryId,
267             int statusId, String comments, ServiceContext serviceContext)
268         throws PortalException, SystemException {
269 
270         OrganizationPermissionUtil.check(
271             getPermissionChecker(), organizationId, ActionKeys.UPDATE);
272 
273         return organizationLocalService.updateOrganization(
274             getUser().getCompanyId(), organizationId, parentOrganizationId,
275             name, type, recursable, regionId, countryId, statusId, comments,
276             serviceContext);
277     }
278 
279     public Organization updateOrganization(
280             long organizationId, long parentOrganizationId, String name,
281             String type, boolean recursable, long regionId, long countryId,
282             int statusId, String comments, List<Address> addresses,
283             List<EmailAddress> emailAddresses, List<OrgLabor> orgLabors,
284             List<Phone> phones, List<Website> websites,
285             ServiceContext serviceContext)
286         throws PortalException, SystemException {
287 
288         Organization organization = updateOrganization(
289             organizationId, parentOrganizationId, name, type, recursable,
290             regionId, countryId, statusId, comments, serviceContext);
291 
292         EnterpriseAdminUtil.updateAddresses(
293             Organization.class.getName(), organizationId, addresses);
294 
295         EnterpriseAdminUtil.updateEmailAddresses(
296             Organization.class.getName(), organizationId, emailAddresses);
297 
298         EnterpriseAdminUtil.updateOrgLabors(organizationId, orgLabors);
299 
300         EnterpriseAdminUtil.updatePhones(
301             Organization.class.getName(), organizationId, phones);
302 
303         EnterpriseAdminUtil.updateWebsites(
304             Organization.class.getName(), organizationId, websites);
305 
306         return organization;
307     }
308 
309 }