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
.Control
;
31 import org
.eclipse
.swt
.widgets
.Label
;
32 import org
.eclipse
.swt
.widgets
.Shell
;
33 import org
.eclipse
.swt
.widgets
.Text
;
34 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
36 /** The site-related user menu */
37 public class UserMenu
extends Shell
implements CmsStyles
, CallbackHandler
{
38 private static final long serialVersionUID
= -5788157651532106301L;
39 private Text username
, password
;
41 public UserMenu(Control source
) {
42 super(source
.getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
43 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
45 String username
= SecurityContextHolder
.getContext()
46 .getAuthentication().getName();
47 if (username
.equals("anonymous")) {
56 setLocation(source
.toDisplay(source
.getSize().x
- getSize().x
,
59 addShellListener(new ShellAdapter() {
60 private static final long serialVersionUID
= 5178980294808435833L;
63 public void shellDeactivated(ShellEvent e
) {
71 protected void userUi() {
72 setLayout(new GridLayout());
74 String username
= SecurityContextHolder
.getContext()
75 .getAuthentication().getName();
77 Label l
= new Label(this, SWT
.NONE
);
78 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
79 l
.setData(RWT
.MARKUP_ENABLED
, true);
80 l
.setLayoutData(CmsUtils
.fillWidth());
81 l
.setText("<b>" + username
+ "</b>");
83 l
= new Label(this, SWT
.NONE
);
84 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
85 l
.setText(CmsMsg
.logout
.lead());
86 GridData lData
= CmsUtils
.fillWidth();
87 lData
.widthHint
= 120;
88 l
.setLayoutData(lData
);
90 l
.addMouseListener(new MouseAdapter() {
91 private static final long serialVersionUID
= 6444395812777413116L;
93 public void mouseDown(MouseEvent e
) {
99 protected void anonymousUi() {
100 Integer textWidth
= 150;
101 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
102 setLayout(new GridLayout(2, false));
104 new Label(this, SWT
.NONE
).setText(CmsMsg
.username
.lead());
105 username
= new Text(this, SWT
.BORDER
);
106 username
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
107 GridData gd
= CmsUtils
.fillWidth();
108 gd
.widthHint
= textWidth
;
109 username
.setLayoutData(gd
);
111 new Label(this, SWT
.NONE
).setText(CmsMsg
.password
.lead());
112 password
= new Text(this, SWT
.BORDER
| SWT
.PASSWORD
);
113 password
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
114 gd
= CmsUtils
.fillWidth();
115 gd
.widthHint
= textWidth
;
116 password
.setLayoutData(gd
);
118 TraverseListener tl
= new TraverseListener() {
119 private static final long serialVersionUID
= -1158892811534971856L;
121 public void keyTraversed(TraverseEvent e
) {
122 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
126 username
.addTraverseListener(tl
);
127 password
.addTraverseListener(tl
);
130 protected void login() {
131 CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
133 Subject subject
= new Subject();
138 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
140 LoginContext loginContext
= new ArgeoLoginContext(
141 KernelHeader
.LOGIN_CONTEXT_USER
, subject
, this);
142 loginContext
.login();
143 } catch (LoginException e1
) {
144 throw new ArgeoException("Cannot authenticate anonymous", e1
);
148 cmsSession
.authChange();
151 protected void logout() {
152 final CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
154 Subject subject
= new Subject();
159 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_USER
, subject
)
161 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
163 } catch (LoginException e1
) {
164 throw new ArgeoException("Cannot authenticate anonymous", e1
);
168 cmsSession
.authChange();
172 public void handle(Callback
[] callbacks
) throws IOException
,
173 UnsupportedCallbackException
{
174 ((NameCallback
) callbacks
[0]).setName(username
.getText());
175 ((PasswordCallback
) callbacks
[1]).setPassword(password
.getTextChars());