1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portal.service.permission;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.model.User;
20  import com.liferay.portal.security.auth.PrincipalException;
21  import com.liferay.portal.security.permission.ActionKeys;
22  import com.liferay.portal.security.permission.PermissionChecker;
23  import com.liferay.portal.service.UserLocalServiceUtil;
24  import com.liferay.portal.util.PropsValues;
25  
26  /**
27   * <a href="UserPermissionImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Charles May
30   * @author Jorge Ferrer
31   */
32  public class UserPermissionImpl implements UserPermission {
33  
34      /**
35       * @deprecated
36       */
37      public void check(
38              PermissionChecker permissionChecker, long userId,
39              long organizationId, long locationId, String actionId)
40          throws PrincipalException {
41  
42          check(
43              permissionChecker, userId, new long[] {organizationId, locationId},
44              actionId);
45      }
46  
47      public void check(
48              PermissionChecker permissionChecker, long userId,
49              long[] organizationIds, String actionId)
50          throws PrincipalException {
51  
52          if (!contains(
53                  permissionChecker, userId, organizationIds, actionId)) {
54  
55              throw new PrincipalException();
56          }
57      }
58  
59      public void check(
60              PermissionChecker permissionChecker, long userId, String actionId)
61          throws PrincipalException {
62  
63          if (!contains(permissionChecker, userId, actionId)) {
64              throw new PrincipalException();
65          }
66      }
67  
68      /**
69       * @deprecated
70       */
71      public boolean contains(
72          PermissionChecker permissionChecker, long userId, long organizationId,
73          long locationId, String actionId) {
74  
75          return contains(
76              permissionChecker, userId, new long[] {organizationId, locationId},
77              actionId);
78      }
79  
80      public boolean contains(
81          PermissionChecker permissionChecker, long userId,
82          long[] organizationIds, String actionId) {
83  
84          if (((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5 ||
85                PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) &&
86               (permissionChecker.hasOwnerPermission(
87                  permissionChecker.getCompanyId(), User.class.getName(), userId,
88                  userId, actionId))) ||
89              (permissionChecker.getUserId() == userId)) {
90  
91              return true;
92          }
93          else if (permissionChecker.hasPermission(
94                      0, User.class.getName(), userId, actionId)) {
95  
96              return true;
97          }
98          else {
99              try {
100                 if (organizationIds == null) {
101                     User user = UserLocalServiceUtil.getUserById(userId);
102 
103                     organizationIds = user.getOrganizationIds();
104                 }
105 
106                 for (int i = 0; i < organizationIds.length; i++) {
107                     long organizationId = organizationIds[i];
108 
109                     if (OrganizationPermissionUtil.contains(
110                             permissionChecker, organizationId,
111                             ActionKeys.MANAGE_USERS)) {
112 
113                         return true;
114                     }
115                 }
116             }
117             catch (Exception e) {
118                 _log.error(e, e);
119             }
120         }
121 
122         return false;
123     }
124 
125     public boolean contains(
126         PermissionChecker permissionChecker, long userId, String actionId) {
127 
128         return contains(permissionChecker, userId, null, actionId);
129     }
130 
131     private static Log _log = LogFactoryUtil.getLog(UserPermissionImpl.class);
132 
133 }