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 resetValues() {
106     }
107 
108     public void setCheckGuest(boolean checkGuest) {
109         this.checkGuest = checkGuest;
110     }
111 
112     public void setValues(PortletRequest portletRequest) {
113 
114         // This method is called in com.liferay.portlet.StrutsPortlet to allow
115         // developers to hook in additional parameters from the portlet request.
116         // Don't overwrite this method unless you're using Liferay in a 2 tier
117         // environment and don't expect to make remote calls. Remote calls to
118         // service beans will not have any values set from the portlet request.
119 
120     }
121 
122     protected User user;
123     protected long defaultUserId;
124     protected boolean signedIn;
125     protected boolean checkGuest;
126     protected Boolean omniadmin;
127     protected Role ownerRole;
128 
129     private static Log _log =
130         LogFactoryUtil.getLog(BasePermissionChecker.class);
131 
132 }