]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/UserMenu.java
Use message instead of label in user menu
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / 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(new GridLayout());
79
80 String username = SecurityContextHolder.getContext()
81 .getAuthentication().getName();
82
83 Label l = new Label(this, SWT.NONE);
84 l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
85 l.setData(RWT.MARKUP_ENABLED, true);
86 l.setLayoutData(CmsUtils.fillWidth());
87 l.setText("<b>" + username + "</b>");
88
89 specificUserUi(this);
90
91 l = new Label(this, SWT.NONE);
92 l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
93 l.setText(CmsMsg.logout.lead());
94 GridData lData = CmsUtils.fillWidth();
95 lData.widthHint = 120;
96 l.setLayoutData(lData);
97
98 l.addMouseListener(new MouseAdapter() {
99 private static final long serialVersionUID = 6444395812777413116L;
100
101 public void mouseDown(MouseEvent e) {
102 logout();
103 }
104 });
105 }
106
107 protected String getUsername() {
108 String username = SecurityContextHolder.getContext()
109 .getAuthentication().getName();
110 return username;
111 }
112
113 /** To be overridden */
114 protected void specificUserUi(Composite parent) {
115
116 }
117
118 protected void anonymousUi() {
119 setLayout(CmsUtils.noSpaceGridLayout());
120
121 // We need a composite for the traversal
122 Composite c = new Composite(this, SWT.NONE);
123 c.setLayout(new GridLayout());
124 c.setLayoutData(CmsUtils.fillAll());
125
126 Integer textWidth = 150;
127 setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
128
129 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
130 username = new Text(c, SWT.BORDER);
131 username.setMessage(CmsMsg.username.lead());
132 username.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_USERNAME);
133 GridData gd = CmsUtils.fillWidth();
134 gd.widthHint = textWidth;
135 username.setLayoutData(gd);
136
137 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
138 password = new Text(c, SWT.BORDER | SWT.PASSWORD);
139 password.setMessage(CmsMsg.password.lead());
140 password.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD);
141 gd = CmsUtils.fillWidth();
142 gd.widthHint = textWidth;
143 password.setLayoutData(gd);
144
145 TraverseListener tl = new TraverseListener() {
146 private static final long serialVersionUID = -1158892811534971856L;
147
148 public void keyTraversed(TraverseEvent e) {
149 if (e.detail == SWT.TRAVERSE_RETURN)
150 login();
151 }
152 };
153 c.addTraverseListener(tl);
154 username.addTraverseListener(tl);
155 password.addTraverseListener(tl);
156 setTabList(new Control[] { c });
157 c.setTabList(new Control[] { username, password });
158 c.setFocus();
159 }
160
161 protected void login() {
162 CmsSession cmsSession = (CmsSession) getDisplay().getData(
163 CmsSession.KEY);
164 Subject subject = cmsSession.getSubject();
165 try {
166 //
167 // LOGIN
168 //
169 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS, subject)
170 .logout();
171 LoginContext loginContext = new ArgeoLoginContext(
172 KernelHeader.LOGIN_CONTEXT_USER, subject, this);
173 loginContext.login();
174 } catch (LoginException e1) {
175 try {
176 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS,
177 subject).login();
178 } catch (LoginException e) {
179 throw new CmsException("Cannot authenticate anonymous", e1);
180 }
181 throw new CmsException("Cannot authenticate", e1);
182 }
183 close();
184 dispose();
185 cmsSession.authChange();
186 }
187
188 protected void logout() {
189 final CmsSession cmsSession = (CmsSession) getDisplay().getData(
190 CmsSession.KEY);
191 Subject subject = cmsSession.getSubject();
192 try {
193 //
194 // LOGOUT
195 //
196 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_USER, subject)
197 .logout();
198 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS, subject)
199 .login();
200 } catch (LoginException e1) {
201 throw new CmsException("Cannot authenticate anonymous", e1);
202 }
203 close();
204 dispose();
205 cmsSession.authChange();
206 }
207
208 @Override
209 public void handle(Callback[] callbacks) throws IOException,
210 UnsupportedCallbackException {
211 ((NameCallback) callbacks[0]).setName(username.getText());
212 ((PasswordCallback) callbacks[1]).setPassword(password.getTextChars());
213 }
214
215 }