1   /**
2    * Copyright (c) 2000-2009 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.security.permission;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.model.Role;
28  import com.liferay.portal.model.RoleConstants;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.RoleLocalServiceUtil;
31  import com.liferay.portal.service.UserLocalServiceUtil;
32  import com.liferay.portlet.admin.util.OmniadminUtil;
33  
34  import javax.portlet.PortletRequest;
35  
36  /**
37   * <a href="BasePermissionChecker.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public abstract class BasePermissionChecker implements PermissionChecker {
43  
44      public long getCompanyId() {
45          return user.getCompanyId();
46      }
47  
48      public long getOwnerRoleId () {
49          return ownerRole.getRoleId();
50      }
51  
52      public long getUserId() {
53          return user.getUserId();
54      }
55  
56      public boolean hasOwnerPermission(
57          long companyId, String name, long primKey, long ownerId,
58          String actionId) {
59  
60          return hasOwnerPermission(
61              companyId, name, String.valueOf(primKey), ownerId, actionId);
62      }
63  
64      public boolean hasPermission(
65          long groupId, String name, long primKey, String actionId) {
66  
67          return hasPermission(groupId, name, String.valueOf(primKey), actionId);
68      }
69  
70      public void init(User user, boolean checkGuest) {
71          this.user = user;
72  
73          if (user.isDefaultUser()) {
74              this.defaultUserId = user.getUserId();
75              this.signedIn = false;
76          }
77          else {
78              try {
79                  this.defaultUserId = UserLocalServiceUtil.getDefaultUserId(
80                      user.getCompanyId());
81              }
82              catch (Exception e) {
83                  _log.error(e, e);
84              }
85  
86              this.signedIn = true;
87          }
88  
89          this.checkGuest = checkGuest;
90  
91          try {
92              this.ownerRole = RoleLocalServiceUtil.getRole(
93                  user.getCompanyId(), RoleConstants.OWNER);
94          }
95          catch (Exception e) {
96              _log.error(e, e);
97          }
98      }
99  
100     public boolean isOmniadmin() {
101         if (omniadmin == null) {
102             omniadmin = Boolean.valueOf(OmniadminUtil.isOmniadmin(getUserId()));
103         }
104 
105         return omniadmin.booleanValue();
106     }
107 
108     public void resetValues() {
109     }
110 
111     public void setCheckGuest(boolean checkGuest) {
112         this.checkGuest = checkGuest;
113     }
114 
115     public void setValues(PortletRequest portletRequest) {
116 
117         // This method is called in com.liferay.portlet.StrutsPortlet to allow
118         // developers to hook in additional parameters from the portlet request.
119         // Don't overwrite this method unless you're using Liferay in a 2 tier
120         // environment and don't expect to make remote calls. Remote calls to
121         // service beans will not have any values set from the portlet request.
122 
123     }
124 
125     protected User user;
126     protected long defaultUserId;
127     protected boolean signedIn;
128     protected boolean checkGuest;
129     protected Boolean omniadmin;
130     protected Role ownerRole;
131 
132     private static Log _log =
133          LogFactoryUtil.getLog(BasePermissionChecker.class);
134 
135 }