1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public abstract class BasePermissionChecker implements PermissionChecker {
42  
43      public long getCompanyId() {
44          return user.getCompanyId();
45      }
46  
47      public long getOwnerRoleId () {
48          return ownerRole.getRoleId();
49      }
50  
51      public long getUserId() {
52          return user.getUserId();
53      }
54  
55      public boolean hasOwnerPermission(
56          long companyId, String name, long primKey, long ownerId,
57          String actionId) {
58  
59          return hasOwnerPermission(
60              companyId, name, String.valueOf(primKey), ownerId, actionId);
61      }
62  
63      public boolean hasPermission(
64          long groupId, String name, long primKey, String actionId) {
65  
66          return hasPermission(groupId, name, String.valueOf(primKey), actionId);
67      }
68  
69      public void init(User user, boolean checkGuest) {
70          this.user = user;
71  
72          if (user.isDefaultUser()) {
73              this.defaultUserId = user.getUserId();
74              this.signedIn = false;
75          }
76          else {
77              try {
78                  this.defaultUserId = UserLocalServiceUtil.getDefaultUserId(
79                      user.getCompanyId());
80              }
81              catch (Exception e) {
82                  _log.error(e, e);
83              }
84  
85              this.signedIn = true;
86          }
87  
88          this.checkGuest = checkGuest;
89  
90          try {
91              this.ownerRole = RoleLocalServiceUtil.getRole(
92                  user.getCompanyId(), RoleConstants.OWNER);
93          }
94          catch (Exception e) {
95              _log.error(e, e);
96          }
97      }
98  
99      public boolean isOmniadmin() {
100         if (omniadmin == null) {
101             omniadmin = Boolean.valueOf(OmniadminUtil.isOmniadmin(getUserId()));
102         }
103 
104         return omniadmin.booleanValue();
105     }
106 
107     public void recycle() {
108         user = null;
109         defaultUserId = 0;
110         signedIn = false;
111         checkGuest = false;
112         omniadmin = null;
113         resetValues();
114     }
115 
116     public void resetValues() {
117     }
118 
119     public void setCheckGuest(boolean checkGuest) {
120         this.checkGuest = checkGuest;
121     }
122 
123     public void setValues(PortletRequest portletRequest) {
124 
125         // This method is called in com.liferay.portlet.StrutsPortlet to allow
126         // developers to hook in additional parameters from the portlet request.
127         // Don't overwrite this method unless you're using Liferay in a 2 tier
128         // environment and don't expect to make remote calls. Remote calls to
129         // service beans will not have any values set from the portlet request.
130 
131     }
132 
133     protected User user;
134     protected long defaultUserId;
135     protected boolean signedIn;
136     protected boolean checkGuest;
137     protected Boolean omniadmin;
138     protected Role ownerRole;
139 
140     private static Log _log =
141         LogFactoryUtil.getLog(BasePermissionChecker.class);
142 
143 }