1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.security.permission;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.model.Role;
25  import com.liferay.portal.model.RoleConstants;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.service.RoleLocalServiceUtil;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  import com.liferay.portlet.admin.util.OmniadminUtil;
30  
31  import javax.portlet.PortletRequest;
32  
33  /**
34   * <a href="BasePermissionChecker.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public abstract class BasePermissionChecker implements PermissionChecker {
40  
41      public long getCompanyId() {
42          return user.getCompanyId();
43      }
44  
45      public long getOwnerRoleId () {
46          return ownerRole.getRoleId();
47      }
48  
49      public long getUserId() {
50          return user.getUserId();
51      }
52  
53      public boolean hasOwnerPermission(
54          long companyId, String name, long primKey, long ownerId,
55          String actionId) {
56  
57          return hasOwnerPermission(
58              companyId, name, String.valueOf(primKey), ownerId, actionId);
59      }
60  
61      public boolean hasPermission(
62          long groupId, String name, long primKey, String actionId) {
63  
64          return hasPermission(groupId, name, String.valueOf(primKey), actionId);
65      }
66  
67      public void init(User user, boolean checkGuest) {
68          this.user = user;
69  
70          if (user.isDefaultUser()) {
71              this.defaultUserId = user.getUserId();
72              this.signedIn = false;
73          }
74          else {
75              try {
76                  this.defaultUserId = UserLocalServiceUtil.getDefaultUserId(
77                      user.getCompanyId());
78              }
79              catch (Exception e) {
80                  _log.error(e, e);
81              }
82  
83              this.signedIn = true;
84          }
85  
86          this.checkGuest = checkGuest;
87  
88          try {
89              this.ownerRole = RoleLocalServiceUtil.getRole(
90                  user.getCompanyId(), RoleConstants.OWNER);
91          }
92          catch (Exception e) {
93              _log.error(e, e);
94          }
95      }
96  
97      public boolean isOmniadmin() {
98          if (omniadmin == null) {
99              omniadmin = Boolean.valueOf(OmniadminUtil.isOmniadmin(getUserId()));
100         }
101 
102         return omniadmin.booleanValue();
103     }
104 
105     public void recycle() {
106         user = null;
107         defaultUserId = 0;
108         signedIn = false;
109         checkGuest = false;
110         omniadmin = null;
111         resetValues();
112     }
113 
114     public void resetValues() {
115     }
116 
117     public void setCheckGuest(boolean checkGuest) {
118         this.checkGuest = checkGuest;
119     }
120 
121     public void setValues(PortletRequest portletRequest) {
122 
123         // This method is called in com.liferay.portlet.StrutsPortlet to allow
124         // developers to hook in additional parameters from the portlet request.
125         // Don't overwrite this method unless you're using Liferay in a 2 tier
126         // environment and don't expect to make remote calls. Remote calls to
127         // service beans will not have any values set from the portlet request.
128 
129     }
130 
131     protected User user;
132     protected long defaultUserId;
133     protected boolean signedIn;
134     protected boolean checkGuest;
135     protected Boolean omniadmin;
136     protected Role ownerRole;
137 
138     private static Log _log =
139          LogFactoryUtil.getLog(BasePermissionChecker.class);
140 
141 }