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
.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
;
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
;
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
);
47 Authentication authentication
= SecurityContextHolder
.getContext()
49 if (authentication
== null)
50 throw new CmsException("No authentication available");
52 String username
= authentication
.getName();
53 if (username
.equals(KernelHeader
.USERNAME_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
) {
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());
83 // String username = SecurityContextHolder.getContext()
84 // .getAuthentication().getName();
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>");
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
);
101 l
.addMouseListener(new MouseAdapter() {
102 private static final long serialVersionUID
= 6444395812777413116L;
104 public void mouseDown(MouseEvent e
) {
110 protected String
getUsername() {
111 String username
= SecurityContextHolder
.getContext()
112 .getAuthentication().getName();
116 /** To be overridden */
117 protected void specificUserUi(Composite parent
) {
121 protected void anonymousUi() {
122 setLayout(CmsUtils
.noSpaceGridLayout());
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());
129 Integer textWidth
= 120;
130 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
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
);
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
);
148 TraverseListener tl
= new TraverseListener() {
149 private static final long serialVersionUID
= -1158892811534971856L;
151 public void keyTraversed(TraverseEvent e
) {
152 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
156 c
.addTraverseListener(tl
);
157 username
.addTraverseListener(tl
);
158 password
.addTraverseListener(tl
);
159 setTabList(new Control
[] { c
});
160 c
.setTabList(new Control
[] { username
, password
});
164 protected void login() {
165 CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
167 Subject subject
= cmsSession
.getSubject();
172 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
174 LoginContext loginContext
= new ArgeoLoginContext(
175 KernelHeader
.LOGIN_CONTEXT_USER
, subject
, this);
176 loginContext
.login();
177 } catch (LoginException e1
) {
179 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
181 } catch (LoginException e
) {
182 throw new CmsException("Cannot authenticate anonymous", e1
);
184 throw new CmsException("Cannot authenticate", e1
);
188 cmsSession
.authChange();
191 protected void logout() {
192 final CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
194 Subject subject
= cmsSession
.getSubject();
199 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_USER
, subject
)
201 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
203 } catch (LoginException e1
) {
204 throw new CmsException("Cannot authenticate anonymous", e1
);
208 cmsSession
.authChange();
212 public void handle(Callback
[] callbacks
) throws IOException
,
213 UnsupportedCallbackException
{
214 ((NameCallback
) callbacks
[0]).setName(username
.getText());
215 ((PasswordCallback
) callbacks
[1]).setPassword(password
.getTextChars());