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