]> git.argeo.org Git - lgpl/argeo-commons.git/blob - UserMenu.java
3fef6792b610a4c48a0f66fd518ba2c73fbd6454
[lgpl/argeo-commons.git] / UserMenu.java
1 package org.argeo.cms.util;
2
3 import static org.argeo.cms.KernelHeader.ACCESS_CONTROL_CONTEXT;
4 import static org.argeo.cms.KernelHeader.LOGIN_CONTEXT_ANONYMOUS;
5 import static org.argeo.cms.KernelHeader.LOGIN_CONTEXT_USER;
6
7 import java.io.IOException;
8 import java.security.AccessController;
9 import java.security.PrivilegedAction;
10
11 import javax.security.auth.Subject;
12 import javax.security.auth.callback.Callback;
13 import javax.security.auth.callback.CallbackHandler;
14 import javax.security.auth.callback.NameCallback;
15 import javax.security.auth.callback.PasswordCallback;
16 import javax.security.auth.callback.UnsupportedCallbackException;
17 import javax.security.auth.login.LoginContext;
18 import javax.security.auth.login.LoginException;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpSession;
21
22 import org.argeo.cms.CmsException;
23 import org.argeo.cms.CmsMsg;
24 import org.argeo.cms.CmsSession;
25 import org.argeo.cms.CmsStyles;
26 import org.argeo.cms.KernelHeader;
27 import org.argeo.cms.auth.ArgeoLoginContext;
28 import org.eclipse.rap.rwt.RWT;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.MouseAdapter;
31 import org.eclipse.swt.events.MouseEvent;
32 import org.eclipse.swt.events.ShellAdapter;
33 import org.eclipse.swt.events.ShellEvent;
34 import org.eclipse.swt.events.TraverseEvent;
35 import org.eclipse.swt.events.TraverseListener;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.swt.widgets.Text;
43
44 /** The site-related user menu */
45 public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
46 private static final long serialVersionUID = -5788157651532106301L;
47 private Text username, password;
48
49 public UserMenu(Control source) {
50 super(source.getDisplay(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
51 setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
52
53 // Authentication authentication = SecurityContextHolder.getContext()
54 // .getAuthentication();
55 // if (authentication == null)
56 // throw new CmsException("No authentication available");
57
58 String username = CurrentUserUtils.getUsername();
59 if (username.equalsIgnoreCase(KernelHeader.ROLE_ANONYMOUS)) {
60 username = null;
61 anonymousUi();
62 } else {
63 userUi();
64 }
65
66 pack();
67 layout();
68 setLocation(source.toDisplay(source.getSize().x - getSize().x,
69 source.getSize().y));
70
71 addShellListener(new ShellAdapter() {
72 private static final long serialVersionUID = 5178980294808435833L;
73
74 @Override
75 public void shellDeactivated(ShellEvent e) {
76 close();
77 dispose();
78 }
79 });
80 open();
81 }
82
83 protected void userUi() {
84 setLayout(CmsUtils.noSpaceGridLayout());
85 Composite c = new Composite(this, SWT.NONE);
86 c.setLayout(new GridLayout());
87 c.setLayoutData(CmsUtils.fillAll());
88
89 // String username = SecurityContextHolder.getContext()
90 // .getAuthentication().getName();
91 //
92 // Label l = new Label(c, SWT.NONE);
93 // l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
94 // l.setData(RWT.MARKUP_ENABLED, true);
95 // l.setLayoutData(CmsUtils.fillWidth());
96 // l.setText("<b>" + username + "</b>");
97
98 specificUserUi(c);
99
100 Label l = new Label(c, SWT.NONE);
101 l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
102 l.setText(CmsMsg.logout.lead());
103 GridData lData = CmsUtils.fillWidth();
104 lData.widthHint = 120;
105 l.setLayoutData(lData);
106
107 l.addMouseListener(new MouseAdapter() {
108 private static final long serialVersionUID = 6444395812777413116L;
109
110 public void mouseDown(MouseEvent e) {
111 logout();
112 }
113 });
114 }
115
116 // protected String getUsername() {
117 // // String username = SecurityContextHolder.getContext()
118 // // .getAuthentication().getName();
119 // return CurrentUserUtils.getUsername();
120 // }
121
122 /** To be overridden */
123 protected void specificUserUi(Composite parent) {
124
125 }
126
127 protected void anonymousUi() {
128 setLayout(CmsUtils.noSpaceGridLayout());
129
130 // We need a composite for the traversal
131 Composite c = new Composite(this, SWT.NONE);
132 c.setLayout(new GridLayout());
133 c.setLayoutData(CmsUtils.fillAll());
134
135 Integer textWidth = 120;
136 setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
137
138 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
139 username = new Text(c, SWT.BORDER);
140 username.setMessage(CmsMsg.username.lead());
141 username.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_USERNAME);
142 GridData gd = CmsUtils.fillWidth();
143 gd.widthHint = textWidth;
144 username.setLayoutData(gd);
145
146 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
147 password = new Text(c, SWT.BORDER | SWT.PASSWORD);
148 password.setMessage(CmsMsg.password.lead());
149 password.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD);
150 gd = CmsUtils.fillWidth();
151 gd.widthHint = textWidth;
152 password.setLayoutData(gd);
153
154 TraverseListener tl = new TraverseListener() {
155 private static final long serialVersionUID = -1158892811534971856L;
156
157 public void keyTraversed(TraverseEvent e) {
158 if (e.detail == SWT.TRAVERSE_RETURN)
159 login();
160 }
161 };
162 c.addTraverseListener(tl);
163 username.addTraverseListener(tl);
164 password.addTraverseListener(tl);
165 setTabList(new Control[] { c });
166 c.setTabList(new Control[] { username, password });
167 c.setFocus();
168 }
169
170 protected void login() {
171 CmsSession cmsSession = (CmsSession) getDisplay().getData(
172 CmsSession.KEY);
173 Subject subject = cmsSession.getSubject();
174 try {
175 //
176 // LOGIN
177 //
178 new ArgeoLoginContext(LOGIN_CONTEXT_ANONYMOUS, subject).logout();
179 LoginContext loginContext = new ArgeoLoginContext(
180 LOGIN_CONTEXT_USER, subject, this);
181 loginContext.login();
182
183 // save context in session
184 final HttpSession httpSession = RWT.getRequest().getSession();
185 Subject.doAs(subject, new PrivilegedAction<Void>() {
186
187 @Override
188 public Void run() {
189 httpSession.setAttribute(ACCESS_CONTROL_CONTEXT,
190 AccessController.getContext());
191 return null;
192 }
193 });
194 } catch (LoginException e1) {
195 try {
196 new ArgeoLoginContext(LOGIN_CONTEXT_ANONYMOUS, subject).login();
197 } catch (LoginException e) {
198 throw new CmsException("Cannot authenticate anonymous", e1);
199 }
200 throw new CmsException("Cannot authenticate", e1);
201 }
202 close();
203 dispose();
204 cmsSession.authChange();
205 }
206
207 protected void logout() {
208 final CmsSession cmsSession = (CmsSession) getDisplay().getData(
209 CmsSession.KEY);
210 Subject subject = cmsSession.getSubject();
211 try {
212 //
213 // LOGOUT
214 //
215 new ArgeoLoginContext(LOGIN_CONTEXT_USER, subject).logout();
216 new ArgeoLoginContext(LOGIN_CONTEXT_ANONYMOUS, subject).login();
217
218 HttpServletRequest httpRequest = RWT.getRequest();
219 HttpSession httpSession = httpRequest.getSession();
220 httpSession.setAttribute(ACCESS_CONTROL_CONTEXT, null);
221 } catch (LoginException e1) {
222 throw new CmsException("Cannot authenticate anonymous", e1);
223 }
224 close();
225 dispose();
226 cmsSession.navigateTo("~");
227 cmsSession.authChange();
228 }
229
230 @Override
231 public void handle(Callback[] callbacks) throws IOException,
232 UnsupportedCallbackException {
233 ((NameCallback) callbacks[0]).setName(username.getText());
234 ((PasswordCallback) callbacks[1]).setPassword(password.getTextChars());
235 }
236
237 }