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.security.permission;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.model.Role;
20  import com.liferay.portal.model.RoleConstants;
21  import com.liferay.portal.model.User;
22  import com.liferay.portal.service.RoleLocalServiceUtil;
23  import com.liferay.portal.service.UserLocalServiceUtil;
24  import com.liferay.portlet.admin.util.OmniadminUtil;
25  
26  import javax.portlet.PortletRequest;
27  
28  /**
29   * <a href="BasePermissionChecker.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public abstract class BasePermissionChecker implements PermissionChecker {
34  
35      public long getCompanyId() {
36          return user.getCompanyId();
37      }
38  
39      public long getOwnerRoleId () {
40          return ownerRole.getRoleId();
41      }
42  
43      public long getUserId() {
44          return user.getUserId();
45      }
46  
47      public boolean hasOwnerPermission(
48          long companyId, String name, long primKey, long ownerId,
49          String actionId) {
50  
51          return hasOwnerPermission(
52              companyId, name, String.valueOf(primKey), ownerId, actionId);
53      }
54  
55      public boolean hasPermission(
56          long groupId, String name, long primKey, String actionId) {
57  
58          return hasPermission(groupId, name, String.valueOf(primKey), actionId);
59      }
60  
61      public void init(User user, boolean checkGuest) {
62          this.user = user;
63  
64          if (user.isDefaultUser()) {
65              this.defaultUserId = user.getUserId();
66              this.signedIn = false;
67          }
68          else {
69              try {
70                  this.defaultUserId = UserLocalServiceUtil.getDefaultUserId(
71                      user.getCompanyId());
72              }
73              catch (Exception e) {
74                  _log.error(e, e);
75              }
76  
77              this.signedIn = true;
78          }
79  
80          this.checkGuest = checkGuest;
81  
82          try {
83              this.ownerRole = RoleLocalServiceUtil.getRole(
84                  user.getCompanyId(), RoleConstants.OWNER);
85          }
86          catch (Exception e) {
87              _log.error(e, e);
88          }
89      }
90  
91      public boolean isOmniadmin() {
92          if (omniadmin == null) {
93              omniadmin = Boolean.valueOf(OmniadminUtil.isOmniadmin(getUserId()));
94          }
95  
96          return omniadmin.booleanValue();
97      }
98  
99      public void recycle() {
100         user = null;
101         defaultUserId = 0;
102         signedIn = false;
103         checkGuest = false;
104         omniadmin = null;
105         resetValues();
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 = LogFactoryUtil.getLog(
133         BasePermissionChecker.class);
134 
135 }