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