1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.security.sso;
18
19 import java.io.IOException;
20 import java.sql.Types;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import javax.portlet.ActionRequest;
25 import javax.portlet.ActionResponse;
26 import javax.portlet.PortletConfig;
27 import javax.portlet.PortletException;
28 import javax.portlet.PortletMode;
29 import javax.portlet.RenderRequest;
30 import javax.portlet.RenderResponse;
31 import javax.security.auth.Subject;
32
33 import org.apache.jetspeed.CommonPortletServices;
34 import org.apache.jetspeed.portlets.security.SecurityUtil;
35 import org.apache.jetspeed.security.GroupManager;
36 import org.apache.jetspeed.security.SecurityException;
37 import org.apache.jetspeed.security.User;
38 import org.apache.jetspeed.security.UserManager;
39 import org.apache.jetspeed.sso.SSOException;
40 import org.apache.jetspeed.sso.SSOProvider;
41 import org.apache.jetspeed.sso.SSOSite;
42 import org.apache.portals.gems.browser.BrowserIterator;
43 import org.apache.portals.gems.browser.DatabaseBrowserIterator;
44 import org.apache.portals.gems.browser.BrowserPortlet;
45 import org.apache.portals.gems.util.StatusMessage;
46 import org.apache.portals.messaging.PortletMessaging;
47 import org.apache.velocity.context.Context;
48
49 /***
50 * SSODetails
51 *
52 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
53 * @version $Id: SSODetails.java 348264 2005-11-22 22:06:45Z taylor $
54 */
55 public class SSODetails extends BrowserPortlet
56 {
57 private SSOProvider sso;
58 private UserManager userManager;
59 private GroupManager groupManager;
60
61 public void init(PortletConfig config)
62 throws PortletException
63 {
64 super.init(config);
65 sso = (SSOProvider)getPortletContext().getAttribute(CommonPortletServices.CPS_SSO_COMPONENT);
66 if (null == sso)
67 {
68 throw new PortletException("Failed to find the SSO Provider on portlet initialization");
69 }
70 userManager = (UserManager) getPortletContext().getAttribute(CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
71 if (null == userManager)
72 {
73 throw new PortletException("Failed to find the User Manager on portlet initialization");
74 }
75 groupManager = (GroupManager) getPortletContext().getAttribute(CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
76 if (null == groupManager)
77 {
78 throw new PortletException("Failed to find the Group Manager on portlet initialization");
79 }
80 }
81
82
83 public void getRows(RenderRequest request, String sql, int windowSize)
84 throws Exception
85 {
86 List resultSetTitleList = new ArrayList();
87 List resultSetTypeList = new ArrayList();
88 try
89 {
90 SSOSite site = null;
91 List list = null;
92 resultSetTypeList.add(String.valueOf(Types.VARCHAR));
93 resultSetTitleList.add("Principal");
94 resultSetTypeList.add(String.valueOf(Types.VARCHAR));
95 resultSetTitleList.add("Remote");
96
97 String selectedSite = (String)PortletMessaging.receive(request, "site", "selectedUrl");
98 if (selectedSite != null)
99 {
100 site = sso.getSite(selectedSite);
101 list = sso.getPrincipalsForSite(site);
102 }
103 else
104 {
105 list = new ArrayList();
106 }
107 BrowserIterator iterator = new DatabaseBrowserIterator(
108 list, resultSetTitleList, resultSetTypeList,
109 windowSize);
110 setBrowserIterator(request, iterator);
111 }
112 catch (Exception e)
113 {
114
115 e.printStackTrace();
116 throw e;
117 }
118 }
119
120 public void doView(RenderRequest request, RenderResponse response)
121 throws PortletException, IOException
122 {
123 String change = (String)PortletMessaging.consume(request, "site", "change");
124 if (change != null)
125 {
126 this.clearBrowserIterator(request);
127 }
128 Context context = this.getContext(request);
129 String selectedSite = (String)PortletMessaging.receive(request, "site", "selectedUrl");
130 if (selectedSite != null)
131 {
132 context.put("currentSite", selectedSite);
133 }
134
135
136 String userChooser = SecurityUtil.getAbsoluteUrl(request, "/Administrative/choosers/users.psml");
137 String groupChooser = SecurityUtil.getAbsoluteUrl(request, "/Administrative/choosers/groups.psml");
138
139 context.put("userChooser", userChooser);
140 context.put("groupChooser", groupChooser);
141
142 StatusMessage msg = (StatusMessage)PortletMessaging.consume(request, "SSODetails", "status");
143 if (msg != null)
144 {
145 this.getContext(request).put("statusMsg", msg);
146 }
147
148 super.doView(request, response);
149 }
150
151 public void processAction(ActionRequest request, ActionResponse response)
152 throws PortletException, IOException
153 {
154 if (request.getPortletMode() == PortletMode.VIEW)
155 {
156 String refresh = request.getParameter("sso.refresh");
157 String add = request.getParameter("sso.add");
158 String delete = request.getParameter("ssoDelete");
159
160 if (refresh != null)
161 {
162 this.clearBrowserIterator(request);
163 }
164 else if (delete != null && !(isEmpty(delete)))
165 {
166 try
167 {
168 String siteName = (String)PortletMessaging.receive(request, "site", "selectedUrl");
169 SSOSite site = sso.getSite(siteName);
170 User user = null;
171 try
172 {
173 user = userManager.getUser(delete);
174 }
175 catch(SecurityException se)
176 {
177
178 user =null;
179 }
180
181 if ( site != null )
182 {
183
184
185
186 if ( user != null)
187 {
188
189 Subject subject = user.getSubject();
190 sso.removeCredentialsForSite(subject, site.getSiteURL());
191 this.clearBrowserIterator(request);
192 }
193 else
194 {
195
196 String fullPath = "/group/" + delete;
197 sso.removeCredentialsForSite(fullPath, site.getSiteURL());
198 this.clearBrowserIterator(request);
199 }
200 }
201 }
202 catch (SSOException e)
203 {
204 publishStatusMessage(request, "SSODetails", "status", e, "Could not remove credentials");
205 }
206 }
207 else if (add != null)
208 {
209
210 String principalType = request.getParameter("principal.type");
211 String portalPrincipal = request.getParameter("portal.principal");
212 String remotePrincipal = request.getParameter("remote.principal");
213 String remoteCredential = request.getParameter("remote.credential");
214
215
216
217
218 if (principalType == null || principalType.length() == 0 )
219 principalType = "user";
220
221 if (!(isEmpty(remotePrincipal) || isEmpty(remotePrincipal) || isEmpty(remoteCredential)))
222 {
223 try
224 {
225 String siteName = (String)PortletMessaging.receive(request, "site", "selectedUrl");
226 SSOSite site = sso.getSite(siteName);
227 Subject subject = null;
228 String groupFullPath = null;
229
230 if (principalType.compareTo("user") == 0)
231 {
232 User user = userManager.getUser(portalPrincipal);
233 subject = user.getSubject();
234 }
235 else
236 {
237
238 groupFullPath = "/group/" + portalPrincipal;
239 }
240
241 if (site != null && (subject != null || groupFullPath != null) )
242 {
243 if (subject != null )
244 sso.addCredentialsForSite(subject, remotePrincipal, site.getSiteURL(), remoteCredential);
245 else
246 sso.addCredentialsForSite(groupFullPath, remotePrincipal, site.getSiteURL(), remoteCredential);
247
248 this.clearBrowserIterator(request);
249 }
250 }
251 catch (SSOException e)
252 {
253 publishStatusMessage(request, "SSODetails", "status", e, "Could not add credentials");
254 }
255 catch (SecurityException se)
256 {
257 publishStatusMessage(request, "SSODetails", "status", se, "Could not add credentials");
258 }
259 }
260 }
261 }
262 super.processAction(request, response);
263
264 }
265
266 private boolean isEmpty(String s)
267 {
268 if (s == null) return true;
269
270 if (s.trim().equals("")) return true;
271
272 return false;
273 }
274
275 }