01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-render-impl/impl/src/java/org/sakaiproject/portal/render/portlet/servlet/SakaiServletUtil.java $
03: * $Id: SakaiServletUtil.java 29143 2007-04-19 01:10:38Z ajpoland@iupui.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.portal.render.portlet.servlet;
21:
22: import org.sakaiproject.authz.api.Role;
23: import org.sakaiproject.authz.cover.SecurityService;
24: import org.sakaiproject.exception.IdUnusedException;
25: import org.sakaiproject.portal.render.portlet.services.state.PortletState;
26: import org.sakaiproject.site.api.Site;
27: import org.sakaiproject.site.api.ToolConfiguration;
28: import org.sakaiproject.site.cover.SiteService;
29: import org.sakaiproject.tool.api.Session;
30: import org.sakaiproject.tool.cover.SessionManager;
31:
32: // This utility class is so that the different servlet wrappers can share code
33:
34: /**
35: * @author ddwolf
36: * @author ieb
37: * @since Sakai 2.4
38: * @version $Rev: 29143 $
39: */
40: public class SakaiServletUtil {
41:
42: public static boolean isUserInRole(String string, PortletState state) {
43: if (string == null)
44: return false;
45: if (string.equalsIgnoreCase("admin")
46: && SecurityService.isSuperUser())
47: return true;
48: // Gridsphere convention
49: if (string.equalsIgnoreCase("super")
50: && SecurityService.isSuperUser())
51: return true;
52:
53: String placementId = state.getId();
54: // System.out.println("state.getId()="+placementId);
55:
56: // find the tool from some site
57: ToolConfiguration siteTool = SiteService.findTool(placementId);
58: // System.out.println("siteTool="+siteTool);
59: if (siteTool == null)
60: return false;
61:
62: String siteId = siteTool.getSiteId();
63: // System.out.println("siteId="+siteId);
64:
65: String siteReference = SiteService.siteReference(siteId);
66: // System.out.println("Reference="+siteReference);
67:
68: if (SecurityService.unlock(string, siteReference))
69: return true;
70:
71: Session session = SessionManager.getCurrentSession();
72: // System.out.println("Session = " + session);
73:
74: if (session == null)
75: return false;
76:
77: String userId = session.getUserId();
78: // System.out.println("userId = "+userId);
79:
80: // Fall through to roles
81: try {
82: Site site = SiteService.getSite(siteId);
83: // System.out.println("Site = "+site);
84: Role role = site.getUserRole(userId);
85: // System.out.println("Role = "+role);
86: if (role == null)
87: return false;
88: // System.out.println("Role = "+role.getId());
89: return string.equalsIgnoreCase(role.getId());
90: } catch (IdUnusedException e) {
91: return false;
92: }
93: }
94: }
|