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