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