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(new GridLayout());
80 String username
= SecurityContextHolder
.getContext()
81 .getAuthentication().getName();
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>");
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
);
98 l
.addMouseListener(new MouseAdapter() {
99 private static final long serialVersionUID
= 6444395812777413116L;
101 public void mouseDown(MouseEvent e
) {
107 protected String
getUsername() {
108 String username
= SecurityContextHolder
.getContext()
109 .getAuthentication().getName();
113 /** To be overridden */
114 protected void specificUserUi(Composite parent
) {
118 protected void anonymousUi() {
119 setLayout(CmsUtils
.noSpaceGridLayout());
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());
126 Integer textWidth
= 150;
127 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
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
);
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
);
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 c
.addTraverseListener(tl
);
154 username
.addTraverseListener(tl
);
155 password
.addTraverseListener(tl
);
156 setTabList(new Control
[] { c
});
157 c
.setTabList(new Control
[] { username
, password
});
161 protected void login() {
162 CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
164 Subject subject
= cmsSession
.getSubject();
169 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
171 LoginContext loginContext
= new ArgeoLoginContext(
172 KernelHeader
.LOGIN_CONTEXT_USER
, subject
, this);
173 loginContext
.login();
174 } catch (LoginException e1
) {
176 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
178 } catch (LoginException e
) {
179 throw new CmsException("Cannot authenticate anonymous", e1
);
181 throw new CmsException("Cannot authenticate", e1
);
185 cmsSession
.authChange();
188 protected void logout() {
189 final CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
191 Subject subject
= cmsSession
.getSubject();
196 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_USER
, subject
)
198 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
200 } catch (LoginException e1
) {
201 throw new CmsException("Cannot authenticate anonymous", e1
);
205 cmsSession
.authChange();
209 public void handle(Callback
[] callbacks
) throws IOException
,
210 UnsupportedCallbackException
{
211 ((NameCallback
) callbacks
[0]).setName(username
.getText());
212 ((PasswordCallback
) callbacks
[1]).setPassword(password
.getTextChars());