1 package org
.argeo
.cms
.util
;
3 import static org
.argeo
.cms
.auth
.AuthConstants
.ACCESS_CONTROL_CONTEXT
;
4 import static org
.argeo
.cms
.auth
.AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
;
5 import static org
.argeo
.cms
.auth
.AuthConstants
.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
.CmsStyles
;
25 import org
.argeo
.cms
.CmsView
;
26 import org
.argeo
.cms
.auth
.AuthConstants
;
27 import org
.argeo
.cms
.auth
.CurrentUser
;
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 String username
= CurrentUser
.getUsername(CmsUtils
.getCmsView().getSubject());
54 if (username
.equalsIgnoreCase(AuthConstants
.ROLE_ANONYMOUS
)) {
63 setLocation(source
.toDisplay(source
.getSize().x
- getSize().x
,
66 addShellListener(new ShellAdapter() {
67 private static final long serialVersionUID
= 5178980294808435833L;
70 public void shellDeactivated(ShellEvent e
) {
78 protected void userUi() {
79 setLayout(CmsUtils
.noSpaceGridLayout());
80 Composite c
= new Composite(this, SWT
.NONE
);
81 c
.setLayout(new GridLayout());
82 c
.setLayoutData(CmsUtils
.fillAll());
86 Label l
= new Label(c
, 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 /** To be overridden */
103 protected void specificUserUi(Composite parent
) {
107 protected void anonymousUi() {
108 setLayout(CmsUtils
.noSpaceGridLayout());
110 // We need a composite for the traversal
111 Composite c
= new Composite(this, SWT
.NONE
);
112 c
.setLayout(new GridLayout());
113 c
.setLayoutData(CmsUtils
.fillAll());
115 Integer textWidth
= 120;
116 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
118 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
119 username
= new Text(c
, SWT
.BORDER
);
120 username
.setMessage(CmsMsg
.username
.lead());
121 username
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
122 GridData gd
= CmsUtils
.fillWidth();
123 gd
.widthHint
= textWidth
;
124 username
.setLayoutData(gd
);
126 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
127 password
= new Text(c
, SWT
.BORDER
| SWT
.PASSWORD
);
128 password
.setMessage(CmsMsg
.password
.lead());
129 password
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
130 gd
= CmsUtils
.fillWidth();
131 gd
.widthHint
= textWidth
;
132 password
.setLayoutData(gd
);
134 TraverseListener tl
= new TraverseListener() {
135 private static final long serialVersionUID
= -1158892811534971856L;
137 public void keyTraversed(TraverseEvent e
) {
138 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
142 c
.addTraverseListener(tl
);
143 username
.addTraverseListener(tl
);
144 password
.addTraverseListener(tl
);
145 setTabList(new Control
[] { c
});
146 c
.setTabList(new Control
[] { username
, password
});
150 protected void login() {
151 CmsView cmsSession
= (CmsView
) getDisplay().getData(CmsView
.KEY
);
152 Subject subject
= cmsSession
.getSubject();
157 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).logout();
158 LoginContext loginContext
= new LoginContext(LOGIN_CONTEXT_USER
,
160 loginContext
.login();
162 // save context in session
163 final HttpSession httpSession
= RWT
.getRequest().getSession();
164 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
168 httpSession
.setAttribute(ACCESS_CONTROL_CONTEXT
,
169 AccessController
.getContext());
173 } catch (LoginException e1
) {
175 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).login();
176 } catch (LoginException e
) {
177 throw new CmsException("Cannot authenticate anonymous", e1
);
179 throw new CmsException("Cannot authenticate", e1
);
183 cmsSession
.authChange();
186 protected void logout() {
187 final CmsView cmsSession
= (CmsView
) getDisplay().getData(CmsView
.KEY
);
188 Subject subject
= cmsSession
.getSubject();
193 new LoginContext(LOGIN_CONTEXT_USER
, subject
).logout();
194 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).login();
196 HttpServletRequest httpRequest
= RWT
.getRequest();
197 HttpSession httpSession
= httpRequest
.getSession();
198 httpSession
.setAttribute(ACCESS_CONTROL_CONTEXT
, null);
199 } catch (LoginException e1
) {
200 throw new CmsException("Cannot authenticate anonymous", e1
);
204 cmsSession
.navigateTo("~");
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());