1 package org
.argeo
.cms
.util
;
3 import static org
.argeo
.cms
.KernelHeader
.ACCESS_CONTROL_CONTEXT
;
4 import static org
.argeo
.cms
.KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
;
5 import static org
.argeo
.cms
.KernelHeader
.LOGIN_CONTEXT_USER
;
7 import java
.io
.IOException
;
8 import java
.security
.AccessController
;
9 import java
.security
.PrivilegedAction
;
11 import javax
.security
.auth
.Subject
;
12 import javax
.security
.auth
.callback
.Callback
;
13 import javax
.security
.auth
.callback
.CallbackHandler
;
14 import javax
.security
.auth
.callback
.NameCallback
;
15 import javax
.security
.auth
.callback
.PasswordCallback
;
16 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
17 import javax
.security
.auth
.login
.LoginContext
;
18 import javax
.security
.auth
.login
.LoginException
;
19 import javax
.servlet
.http
.HttpServletRequest
;
20 import javax
.servlet
.http
.HttpSession
;
22 import org
.argeo
.cms
.CmsException
;
23 import org
.argeo
.cms
.CmsMsg
;
24 import org
.argeo
.cms
.CmsView
;
25 import org
.argeo
.cms
.CmsStyles
;
26 import org
.argeo
.cms
.KernelHeader
;
27 import org
.argeo
.cms
.auth
.ArgeoLoginContext
;
28 import org
.eclipse
.rap
.rwt
.RWT
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.events
.MouseAdapter
;
31 import org
.eclipse
.swt
.events
.MouseEvent
;
32 import org
.eclipse
.swt
.events
.ShellAdapter
;
33 import org
.eclipse
.swt
.events
.ShellEvent
;
34 import org
.eclipse
.swt
.events
.TraverseEvent
;
35 import org
.eclipse
.swt
.events
.TraverseListener
;
36 import org
.eclipse
.swt
.layout
.GridData
;
37 import org
.eclipse
.swt
.layout
.GridLayout
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Control
;
40 import org
.eclipse
.swt
.widgets
.Label
;
41 import org
.eclipse
.swt
.widgets
.Shell
;
42 import org
.eclipse
.swt
.widgets
.Text
;
44 /** The site-related user menu */
45 public class UserMenu
extends Shell
implements CmsStyles
, CallbackHandler
{
46 private static final long serialVersionUID
= -5788157651532106301L;
47 private Text username
, password
;
49 public UserMenu(Control source
) {
50 super(source
.getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
51 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
53 // Authentication authentication = SecurityContextHolder.getContext()
54 // .getAuthentication();
55 // if (authentication == null)
56 // throw new CmsException("No authentication available");
58 String username
= CurrentUserUtils
.getUsername();
59 if (username
.equalsIgnoreCase(KernelHeader
.ROLE_ANONYMOUS
)) {
68 setLocation(source
.toDisplay(source
.getSize().x
- getSize().x
,
71 addShellListener(new ShellAdapter() {
72 private static final long serialVersionUID
= 5178980294808435833L;
75 public void shellDeactivated(ShellEvent e
) {
83 protected void userUi() {
84 setLayout(CmsUtils
.noSpaceGridLayout());
85 Composite c
= new Composite(this, SWT
.NONE
);
86 c
.setLayout(new GridLayout());
87 c
.setLayoutData(CmsUtils
.fillAll());
89 // String username = SecurityContextHolder.getContext()
90 // .getAuthentication().getName();
92 // Label l = new Label(c, SWT.NONE);
93 // l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
94 // l.setData(RWT.MARKUP_ENABLED, true);
95 // l.setLayoutData(CmsUtils.fillWidth());
96 // l.setText("<b>" + username + "</b>");
100 Label l
= new Label(c
, SWT
.NONE
);
101 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
102 l
.setText(CmsMsg
.logout
.lead());
103 GridData lData
= CmsUtils
.fillWidth();
104 lData
.widthHint
= 120;
105 l
.setLayoutData(lData
);
107 l
.addMouseListener(new MouseAdapter() {
108 private static final long serialVersionUID
= 6444395812777413116L;
110 public void mouseDown(MouseEvent e
) {
116 // protected String getUsername() {
117 // // String username = SecurityContextHolder.getContext()
118 // // .getAuthentication().getName();
119 // return CurrentUserUtils.getUsername();
122 /** To be overridden */
123 protected void specificUserUi(Composite parent
) {
127 protected void anonymousUi() {
128 setLayout(CmsUtils
.noSpaceGridLayout());
130 // We need a composite for the traversal
131 Composite c
= new Composite(this, SWT
.NONE
);
132 c
.setLayout(new GridLayout());
133 c
.setLayoutData(CmsUtils
.fillAll());
135 Integer textWidth
= 120;
136 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
138 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
139 username
= new Text(c
, SWT
.BORDER
);
140 username
.setMessage(CmsMsg
.username
.lead());
141 username
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
142 GridData gd
= CmsUtils
.fillWidth();
143 gd
.widthHint
= textWidth
;
144 username
.setLayoutData(gd
);
146 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
147 password
= new Text(c
, SWT
.BORDER
| SWT
.PASSWORD
);
148 password
.setMessage(CmsMsg
.password
.lead());
149 password
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
150 gd
= CmsUtils
.fillWidth();
151 gd
.widthHint
= textWidth
;
152 password
.setLayoutData(gd
);
154 TraverseListener tl
= new TraverseListener() {
155 private static final long serialVersionUID
= -1158892811534971856L;
157 public void keyTraversed(TraverseEvent e
) {
158 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
162 c
.addTraverseListener(tl
);
163 username
.addTraverseListener(tl
);
164 password
.addTraverseListener(tl
);
165 setTabList(new Control
[] { c
});
166 c
.setTabList(new Control
[] { username
, password
});
170 protected void login() {
171 CmsView cmsSession
= (CmsView
) getDisplay().getData(
173 Subject subject
= cmsSession
.getSubject();
178 new ArgeoLoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).logout();
179 LoginContext loginContext
= new ArgeoLoginContext(
180 LOGIN_CONTEXT_USER
, subject
, this);
181 loginContext
.login();
183 // save context in session
184 final HttpSession httpSession
= RWT
.getRequest().getSession();
185 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
189 httpSession
.setAttribute(ACCESS_CONTROL_CONTEXT
,
190 AccessController
.getContext());
194 } catch (LoginException e1
) {
196 new ArgeoLoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).login();
197 } catch (LoginException e
) {
198 throw new CmsException("Cannot authenticate anonymous", e1
);
200 throw new CmsException("Cannot authenticate", e1
);
204 cmsSession
.authChange();
207 protected void logout() {
208 final CmsView cmsSession
= (CmsView
) getDisplay().getData(
210 Subject subject
= cmsSession
.getSubject();
215 new ArgeoLoginContext(LOGIN_CONTEXT_USER
, subject
).logout();
216 new ArgeoLoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).login();
218 HttpServletRequest httpRequest
= RWT
.getRequest();
219 HttpSession httpSession
= httpRequest
.getSession();
220 httpSession
.setAttribute(ACCESS_CONTROL_CONTEXT
, null);
221 } catch (LoginException e1
) {
222 throw new CmsException("Cannot authenticate anonymous", e1
);
226 cmsSession
.navigateTo("~");
227 cmsSession
.authChange();
231 public void handle(Callback
[] callbacks
) throws IOException
,
232 UnsupportedCallbackException
{
233 ((NameCallback
) callbacks
[0]).setName(username
.getText());
234 ((PasswordCallback
) callbacks
[1]).setPassword(password
.getTextChars());