1 package org
.argeo
.cms
.util
;
3 import java
.io
.IOException
;
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
;
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
;
37 /** The site-related user menu */
38 public class UserMenu
extends Shell
implements CmsStyles
, CallbackHandler
{
39 private static final long serialVersionUID
= -5788157651532106301L;
41 private CmsLogin cmsLogin
;
42 // private String username = null;
43 private Text username
, password
;
45 public UserMenu(CmsLogin cmsLogin
, Control source
) {
46 super(source
.getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
47 this.cmsLogin
= cmsLogin
;
49 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
51 String username
= SecurityContextHolder
.getContext()
52 .getAuthentication().getName();
53 if (username
.equals("anonymous")) {
62 setLocation(source
.toDisplay(source
.getSize().x
- getSize().x
,
65 addShellListener(new ShellAdapter() {
66 private static final long serialVersionUID
= 5178980294808435833L;
69 public void shellDeactivated(ShellEvent e
) {
80 protected void userUi() {
81 setLayout(new GridLayout());
83 String username
= SecurityContextHolder
.getContext()
84 .getAuthentication().getName();
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>");
92 final CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
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
);
101 l
.addMouseListener(new MouseAdapter() {
102 private static final long serialVersionUID
= 6444395812777413116L;
104 public void mouseDown(MouseEvent e
) {
105 Subject subject
= new Subject();
107 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_USER
,
109 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
111 } catch (LoginException e1
) {
112 throw new ArgeoException("Cannot authenticate anonymous",
115 // SecurityContextHolder.getContext().setAuthentication(null);
116 // HttpSession httpSession = RWT.getRequest().getSession();
117 // httpSession.removeAttribute(SPRING_SECURITY_CONTEXT_KEY);
120 cmsSession
.authChange();
125 protected void anonymousUi() {
126 Integer textWidth
= 150;
127 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
128 setLayout(new GridLayout(2, false));
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
);
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
);
145 TraverseListener tl
= new TraverseListener() {
146 private static final long serialVersionUID
= -1158892811534971856L;
148 public void keyTraversed(TraverseEvent e
) {
149 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
153 username
.addTraverseListener(tl
);
154 password
.addTraverseListener(tl
);
157 protected void login() {
158 CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
161 Subject subject
= new Subject();
163 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
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
);
172 // cmsLogin.logInWithPassword(username, password);
175 // refreshUi(source.getParent());
176 cmsSession
.authChange();
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())
186 // Thread.sleep(500);
187 // } catch (InterruptedException e) {