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
;
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 // Authentication authentication = SecurityContextHolder.getContext()
46 // .getAuthentication();
47 // if (authentication == null)
48 // throw new CmsException("No authentication available");
50 String username
= CurrentUserUtils
.getUsername();
51 if (username
.equalsIgnoreCase(KernelHeader
.ROLE_ANONYMOUS
)) {
60 setLocation(source
.toDisplay(source
.getSize().x
- getSize().x
,
63 addShellListener(new ShellAdapter() {
64 private static final long serialVersionUID
= 5178980294808435833L;
67 public void shellDeactivated(ShellEvent e
) {
75 protected void userUi() {
76 setLayout(CmsUtils
.noSpaceGridLayout());
77 Composite c
= new Composite(this, SWT
.NONE
);
78 c
.setLayout(new GridLayout());
79 c
.setLayoutData(CmsUtils
.fillAll());
81 // String username = SecurityContextHolder.getContext()
82 // .getAuthentication().getName();
84 // Label l = new Label(c, SWT.NONE);
85 // l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
86 // l.setData(RWT.MARKUP_ENABLED, true);
87 // l.setLayoutData(CmsUtils.fillWidth());
88 // l.setText("<b>" + username + "</b>");
92 Label l
= new Label(c
, SWT
.NONE
);
93 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
94 l
.setText(CmsMsg
.logout
.lead());
95 GridData lData
= CmsUtils
.fillWidth();
96 lData
.widthHint
= 120;
97 l
.setLayoutData(lData
);
99 l
.addMouseListener(new MouseAdapter() {
100 private static final long serialVersionUID
= 6444395812777413116L;
102 public void mouseDown(MouseEvent e
) {
108 // protected String getUsername() {
109 // // String username = SecurityContextHolder.getContext()
110 // // .getAuthentication().getName();
111 // return CurrentUserUtils.getUsername();
114 /** To be overridden */
115 protected void specificUserUi(Composite parent
) {
119 protected void anonymousUi() {
120 setLayout(CmsUtils
.noSpaceGridLayout());
122 // We need a composite for the traversal
123 Composite c
= new Composite(this, SWT
.NONE
);
124 c
.setLayout(new GridLayout());
125 c
.setLayoutData(CmsUtils
.fillAll());
127 Integer textWidth
= 120;
128 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
130 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
131 username
= new Text(c
, SWT
.BORDER
);
132 username
.setMessage(CmsMsg
.username
.lead());
133 username
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
134 GridData gd
= CmsUtils
.fillWidth();
135 gd
.widthHint
= textWidth
;
136 username
.setLayoutData(gd
);
138 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
139 password
= new Text(c
, SWT
.BORDER
| SWT
.PASSWORD
);
140 password
.setMessage(CmsMsg
.password
.lead());
141 password
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
142 gd
= CmsUtils
.fillWidth();
143 gd
.widthHint
= textWidth
;
144 password
.setLayoutData(gd
);
146 TraverseListener tl
= new TraverseListener() {
147 private static final long serialVersionUID
= -1158892811534971856L;
149 public void keyTraversed(TraverseEvent e
) {
150 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
154 c
.addTraverseListener(tl
);
155 username
.addTraverseListener(tl
);
156 password
.addTraverseListener(tl
);
157 setTabList(new Control
[] { c
});
158 c
.setTabList(new Control
[] { username
, password
});
162 protected void login() {
163 CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
165 Subject subject
= cmsSession
.getSubject();
170 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
172 LoginContext loginContext
= new ArgeoLoginContext(
173 KernelHeader
.LOGIN_CONTEXT_USER
, subject
, this);
174 loginContext
.login();
175 } catch (LoginException e1
) {
177 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
179 } catch (LoginException e
) {
180 throw new CmsException("Cannot authenticate anonymous", e1
);
182 throw new CmsException("Cannot authenticate", e1
);
186 cmsSession
.authChange();
189 protected void logout() {
190 final CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
192 Subject subject
= cmsSession
.getSubject();
197 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_USER
, subject
)
199 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
)
201 } catch (LoginException e1
) {
202 throw new CmsException("Cannot authenticate anonymous", e1
);
206 cmsSession
.navigateTo("~");
207 cmsSession
.authChange();
211 public void handle(Callback
[] callbacks
) throws IOException
,
212 UnsupportedCallbackException
{
213 ((NameCallback
) callbacks
[0]).setName(username
.getText());
214 ((PasswordCallback
) callbacks
[1]).setPassword(password
.getTextChars());