001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.security.sso;
018:
019: import java.io.IOException;
020: import java.sql.Types;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.PortletConfig;
027: import javax.portlet.PortletException;
028: import javax.portlet.PortletMode;
029: import javax.portlet.RenderRequest;
030: import javax.portlet.RenderResponse;
031: import javax.security.auth.Subject;
032:
033: import org.apache.jetspeed.CommonPortletServices;
034: import org.apache.jetspeed.portlets.security.SecurityUtil;
035: import org.apache.jetspeed.security.GroupManager;
036: import org.apache.jetspeed.security.SecurityException;
037: import org.apache.jetspeed.security.User;
038: import org.apache.jetspeed.security.UserManager;
039: import org.apache.jetspeed.sso.SSOException;
040: import org.apache.jetspeed.sso.SSOProvider;
041: import org.apache.jetspeed.sso.SSOSite;
042: import org.apache.portals.gems.browser.BrowserIterator;
043: import org.apache.portals.gems.browser.DatabaseBrowserIterator;
044: import org.apache.portals.gems.browser.BrowserPortlet;
045: import org.apache.portals.gems.util.StatusMessage;
046: import org.apache.portals.messaging.PortletMessaging;
047: import org.apache.velocity.context.Context;
048:
049: /**
050: * SSODetails
051: *
052: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
053: * @version $Id: SSODetails.java 348264 2005-11-22 22:06:45Z taylor $
054: */
055: public class SSODetails extends BrowserPortlet {
056: private SSOProvider sso;
057: private UserManager userManager;
058: private GroupManager groupManager;
059:
060: public void init(PortletConfig config) throws PortletException {
061: super .init(config);
062: sso = (SSOProvider) getPortletContext().getAttribute(
063: CommonPortletServices.CPS_SSO_COMPONENT);
064: if (null == sso) {
065: throw new PortletException(
066: "Failed to find the SSO Provider on portlet initialization");
067: }
068: userManager = (UserManager) getPortletContext().getAttribute(
069: CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
070: if (null == userManager) {
071: throw new PortletException(
072: "Failed to find the User Manager on portlet initialization");
073: }
074: groupManager = (GroupManager) getPortletContext().getAttribute(
075: CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
076: if (null == groupManager) {
077: throw new PortletException(
078: "Failed to find the Group Manager on portlet initialization");
079: }
080: }
081:
082: public void getRows(RenderRequest request, String sql,
083: int windowSize) throws Exception {
084: List resultSetTitleList = new ArrayList();
085: List resultSetTypeList = new ArrayList();
086: try {
087: SSOSite site = null;
088: List list = null;
089: resultSetTypeList.add(String.valueOf(Types.VARCHAR));
090: resultSetTitleList.add("Principal");
091: resultSetTypeList.add(String.valueOf(Types.VARCHAR));
092: resultSetTitleList.add("Remote");
093:
094: String selectedSite = (String) PortletMessaging.receive(
095: request, "site", "selectedUrl");
096: if (selectedSite != null) {
097: site = sso.getSite(selectedSite);
098: list = sso.getPrincipalsForSite(site);
099: } else {
100: list = new ArrayList();
101: }
102: BrowserIterator iterator = new DatabaseBrowserIterator(
103: list, resultSetTitleList, resultSetTypeList,
104: windowSize);
105: setBrowserIterator(request, iterator);
106: } catch (Exception e) {
107: //log.error("Exception in CMSBrowserAction.getRows: ", e);
108: e.printStackTrace();
109: throw e;
110: }
111: }
112:
113: public void doView(RenderRequest request, RenderResponse response)
114: throws PortletException, IOException {
115: String change = (String) PortletMessaging.consume(request,
116: "site", "change");
117: if (change != null) {
118: this .clearBrowserIterator(request);
119: }
120: Context context = this .getContext(request);
121: String selectedSite = (String) PortletMessaging.receive(
122: request, "site", "selectedUrl");
123: if (selectedSite != null) {
124: context.put("currentSite", selectedSite);
125: }
126:
127: // get relative link, TODO: encapsulate Jetspeed links access into component
128: String userChooser = SecurityUtil.getAbsoluteUrl(request,
129: "/Administrative/choosers/users.psml");
130: String groupChooser = SecurityUtil.getAbsoluteUrl(request,
131: "/Administrative/choosers/groups.psml");
132:
133: context.put("userChooser", userChooser);
134: context.put("groupChooser", groupChooser);
135:
136: StatusMessage msg = (StatusMessage) PortletMessaging.consume(
137: request, "SSODetails", "status");
138: if (msg != null) {
139: this .getContext(request).put("statusMsg", msg);
140: }
141:
142: super .doView(request, response);
143: }
144:
145: public void processAction(ActionRequest request,
146: ActionResponse response) throws PortletException,
147: IOException {
148: if (request.getPortletMode() == PortletMode.VIEW) {
149: String refresh = request.getParameter("sso.refresh");
150: String add = request.getParameter("sso.add");
151: String delete = request.getParameter("ssoDelete");
152:
153: if (refresh != null) {
154: this .clearBrowserIterator(request);
155: } else if (delete != null && !(isEmpty(delete))) {
156: try {
157: String siteName = (String) PortletMessaging
158: .receive(request, "site", "selectedUrl");
159: SSOSite site = sso.getSite(siteName);
160: User user = null;
161: try {
162: user = userManager.getUser(delete);
163: } catch (SecurityException se) {
164: // User doesn't exist -- maybe a group
165: user = null;
166: }
167:
168: if (site != null) {
169: /*
170: * If the user is null try to remove a group
171: */
172: if (user != null) {
173: // Remove USER
174: Subject subject = user.getSubject();
175: sso.removeCredentialsForSite(subject, site
176: .getSiteURL());
177: this .clearBrowserIterator(request);
178: } else {
179: // Try group removal
180: String fullPath = "/group/" + delete;
181: sso.removeCredentialsForSite(fullPath, site
182: .getSiteURL());
183: this .clearBrowserIterator(request);
184: }
185: }
186: } catch (SSOException e) {
187: publishStatusMessage(request, "SSODetails",
188: "status", e, "Could not remove credentials");
189: }
190: } else if (add != null) {
191: // Roger: here is the principal type
192: String principalType = request
193: .getParameter("principal.type"); //group user
194: String portalPrincipal = request
195: .getParameter("portal.principal");
196: String remotePrincipal = request
197: .getParameter("remote.principal");
198: String remoteCredential = request
199: .getParameter("remote.credential");
200:
201: // The principal type can benull if the user just typed the name instead of
202: // using the choosers.
203:
204: if (principalType == null
205: || principalType.length() == 0)
206: principalType = "user";
207:
208: if (!(isEmpty(remotePrincipal)
209: || isEmpty(remotePrincipal) || isEmpty(remoteCredential))) {
210: try {
211: String siteName = (String) PortletMessaging
212: .receive(request, "site", "selectedUrl");
213: SSOSite site = sso.getSite(siteName);
214: Subject subject = null;
215: String groupFullPath = null;
216:
217: if (principalType.compareTo("user") == 0) {
218: User user = userManager
219: .getUser(portalPrincipal);
220: subject = user.getSubject();
221: } else {
222: // Create fullPath
223: groupFullPath = "/group/" + portalPrincipal;
224: }
225:
226: if (site != null
227: && (subject != null || groupFullPath != null)) {
228: if (subject != null)
229: sso.addCredentialsForSite(subject,
230: remotePrincipal, site
231: .getSiteURL(),
232: remoteCredential);
233: else
234: sso.addCredentialsForSite(
235: groupFullPath, remotePrincipal,
236: site.getSiteURL(),
237: remoteCredential);
238:
239: this .clearBrowserIterator(request);
240: }
241: } catch (SSOException e) {
242: publishStatusMessage(request, "SSODetails",
243: "status", e,
244: "Could not add credentials");
245: } catch (SecurityException se) {
246: publishStatusMessage(request, "SSODetails",
247: "status", se,
248: "Could not add credentials");
249: }
250: }
251: }
252: }
253: super .processAction(request, response);
254:
255: }
256:
257: private boolean isEmpty(String s) {
258: if (s == null)
259: return true;
260:
261: if (s.trim().equals(""))
262: return true;
263:
264: return false;
265: }
266:
267: }
|