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