]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/UserMenu.java
Clean up code
[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.ArgeoException;
15 import org.argeo.cms.CmsLogin;
16 import org.argeo.cms.CmsMsg;
17 import org.argeo.cms.CmsSession;
18 import org.argeo.cms.CmsStyles;
19 import org.argeo.cms.KernelHeader;
20 import org.argeo.cms.auth.ArgeoLoginContext;
21 import org.eclipse.rap.rwt.RWT;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.MouseAdapter;
24 import org.eclipse.swt.events.MouseEvent;
25 import org.eclipse.swt.events.ShellAdapter;
26 import org.eclipse.swt.events.ShellEvent;
27 import org.eclipse.swt.events.TraverseEvent;
28 import org.eclipse.swt.events.TraverseListener;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
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.context.SecurityContextHolder;
36
37 /** The site-related user menu */
38 public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
39 private static final long serialVersionUID = -5788157651532106301L;
40
41 private CmsLogin cmsLogin;
42 // private String username = null;
43 private Text username, password;
44
45 public UserMenu(CmsLogin cmsLogin, Control source) {
46 super(source.getDisplay(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
47 this.cmsLogin = cmsLogin;
48
49 setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
50
51 String username = SecurityContextHolder.getContext()
52 .getAuthentication().getName();
53 if (username.equals("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 });
75
76 open();
77
78 }
79
80 protected void userUi() {
81 setLayout(new GridLayout());
82
83 String username = SecurityContextHolder.getContext()
84 .getAuthentication().getName();
85
86 Label l = new Label(this, 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 final CmsSession cmsSession = (CmsSession) getDisplay().getData(
93 CmsSession.KEY);
94 l = new Label(this, 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 Subject subject = new Subject();
106 try {
107 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_USER,
108 subject).logout();
109 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS,
110 subject).login();
111 } catch (LoginException e1) {
112 throw new ArgeoException("Cannot authenticate anonymous",
113 e1);
114 }
115 // SecurityContextHolder.getContext().setAuthentication(null);
116 // HttpSession httpSession = RWT.getRequest().getSession();
117 // httpSession.removeAttribute(SPRING_SECURITY_CONTEXT_KEY);
118 close();
119 dispose();
120 cmsSession.authChange();
121 }
122 });
123 }
124
125 protected void anonymousUi() {
126 Integer textWidth = 150;
127 setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
128 setLayout(new GridLayout(2, false));
129
130 new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
131 username = new Text(this, SWT.BORDER);
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(this, SWT.BORDER | SWT.PASSWORD);
139 password.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD);
140 gd = CmsUtils.fillWidth();
141 gd.widthHint = textWidth;
142 password.setLayoutData(gd);
143
144 // Listeners
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 username.addTraverseListener(tl);
154 password.addTraverseListener(tl);
155 }
156
157 protected void login() {
158 CmsSession cmsSession = (CmsSession) getDisplay().getData(
159 CmsSession.KEY);
160
161 Subject subject = new Subject();
162 try {
163 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS, subject)
164 .logout();
165 LoginContext loginContext = new ArgeoLoginContext(
166 KernelHeader.LOGIN_CONTEXT_USER, subject, this);
167 loginContext.login();
168 } catch (LoginException e1) {
169 throw new ArgeoException("Cannot authenticate anonymous", e1);
170 }
171
172 // cmsLogin.logInWithPassword(username, password);
173 close();
174 dispose();
175 // refreshUi(source.getParent());
176 cmsSession.authChange();
177 }
178
179 @Override
180 public void handle(Callback[] callbacks) throws IOException,
181 UnsupportedCallbackException {
182 ((NameCallback) callbacks[0]).setName(username.getText());
183 ((PasswordCallback) callbacks[1]).setPassword(password.getTextChars());
184 // while (!isDisposed())
185 // try {
186 // Thread.sleep(500);
187 // } catch (InterruptedException e) {
188 // // silent
189 // }
190 }
191
192 }