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
.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
.context
.SecurityContextHolder
;
37 /** The site-related user menu */
38 public class UserMenu
extends Shell
implements CmsStyles
, CallbackHandler
{
39 private static final long serialVersionUID
= -5788157651532106301L;
40 private Text username
, password
;
42 public UserMenu(Control source
) {
43 super(source
.getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
44 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
46 String username
= SecurityContextHolder
.getContext()
47 .getAuthentication().getName();
48 if (username
.equals("anonymous")) {
57 setLocation(source
.toDisplay(source
.getSize().x
- getSize().x
,
60 addShellListener(new ShellAdapter() {
61 private static final long serialVersionUID
= 5178980294808435833L;
64 public void shellDeactivated(ShellEvent e
) {
72 protected void userUi() {
73 setLayout(new GridLayout());
75 String username
= SecurityContextHolder
.getContext()
76 .getAuthentication().getName();
78 Label l
= new Label(this, SWT
.NONE
);
79 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
80 l
.setData(RWT
.MARKUP_ENABLED
, true);
81 l
.setLayoutData(CmsUtils
.fillWidth());
82 l
.setText("<b>" + username
+ "</b>");
86 l
= new Label(this, SWT
.NONE
);
87 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
88 l
.setText(CmsMsg
.logout
.lead());
89 GridData lData
= CmsUtils
.fillWidth();
90 lData
.widthHint
= 120;
91 l
.setLayoutData(lData
);
93 l
.addMouseListener(new MouseAdapter() {
94 private static final long serialVersionUID
= 6444395812777413116L;
96 public void mouseDown(MouseEvent e
) {
102 protected String
getUsername() {
103 String username
= SecurityContextHolder
.getContext()
104 .getAuthentication().getName();
108 /** To be overridden */
109 protected void specificUserUi(Composite parent
) {
113 protected void anonymousUi() {
114 Integer textWidth
= 150;
115 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
116 setLayout(new GridLayout(2, false));
118 new Label(this, SWT
.NONE
).setText(CmsMsg
.username
.lead());
119 username
= new Text(this, SWT
.BORDER
);
120 username
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
121 GridData gd
= CmsUtils
.fillWidth();
122 gd
.widthHint
= textWidth
;
123 username
.setLayoutData(gd
);
125 new Label(this, SWT
.NONE
).setText(CmsMsg
.password
.lead());
126 password
= new Text(this, SWT
.BORDER
| SWT
.PASSWORD
);
127 password
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
128 gd
= CmsUtils
.fillWidth();
129 gd
.widthHint
= textWidth
;
130 password
.setLayoutData(gd
);
132 TraverseListener tl
= new TraverseListener() {
133 private static final long serialVersionUID
= -1158892811534971856L;
135 public void keyTraversed(TraverseEvent e
) {
136 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
140 username
.addTraverseListener(tl
);
141 password
.addTraverseListener(tl
);
144 protected void login() {
145 CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
147 Subject subject
= cmsSession
.getSubject();
152 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
154 LoginContext loginContext
= new ArgeoLoginContext(
155 KernelHeader
.LOGIN_CONTEXT_USER
, subject
, this);
156 loginContext
.login();
157 } catch (LoginException e1
) {
158 throw new ArgeoException("Cannot authenticate anonymous", e1
);
162 cmsSession
.authChange();
165 protected void logout() {
166 final CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
168 Subject subject
= cmsSession
.getSubject();
173 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_USER
, subject
)
175 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
177 } catch (LoginException e1
) {
178 throw new ArgeoException("Cannot authenticate anonymous", e1
);
182 cmsSession
.authChange();
186 public void handle(Callback
[] callbacks
) throws IOException
,
187 UnsupportedCallbackException
{
188 ((NameCallback
) callbacks
[0]).setName(username
.getText());
189 ((PasswordCallback
) callbacks
[1]).setPassword(password
.getTextChars());