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
.graphics
.Point
;
37 import org
.eclipse
.swt
.graphics
.Rectangle
;
38 import org
.eclipse
.swt
.layout
.GridData
;
39 import org
.eclipse
.swt
.layout
.GridLayout
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Control
;
42 import org
.eclipse
.swt
.widgets
.Display
;
43 import org
.eclipse
.swt
.widgets
.Label
;
44 import org
.eclipse
.swt
.widgets
.Shell
;
45 import org
.eclipse
.swt
.widgets
.Text
;
47 /** The site-related user menu */
48 public class UserMenu
implements CmsStyles
, CallbackHandler
{
49 private final CmsView cmsView
;
50 private final Shell shell
;
52 private Text username
, password
;
54 public UserMenu(Control source
, boolean autoclose
) {
55 shell
= new Shell(source
.getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
57 shell
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
58 // cmsView = (CmsView) shell.getDisplay().getData(CmsView.KEY);
59 cmsView
= CmsUtils
.getCmsView();
61 if (cmsView
!= null) {
62 String username
= CurrentUser
.getUsername(CmsUtils
.getCmsView()
65 || username
.equalsIgnoreCase(AuthConstants
.ROLE_ANONYMOUS
)) {
77 if (autoclose
)// popup
78 shell
.setLocation(source
.toDisplay(
79 source
.getSize().x
- shell
.getSize().x
, source
.getSize().y
));
82 Rectangle shellBounds
= Display
.getCurrent().getBounds();// RAP
83 Point dialogSize
= shell
.getSize();
84 int x
= shellBounds
.x
+ (shellBounds
.width
- dialogSize
.x
) / 2;
85 int y
= shellBounds
.y
+ (shellBounds
.height
- dialogSize
.y
) / 2;
86 shell
.setLocation(x
, y
);
90 shell
.addShellListener(new ShellAdapter() {
91 private static final long serialVersionUID
= 5178980294808435833L;
94 public void shellDeactivated(ShellEvent e
) {
102 protected void closeShell() {
107 protected void userUi(Composite parent
) {
108 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
109 Composite c
= new Composite(parent
, SWT
.NONE
);
110 c
.setLayout(new GridLayout());
111 c
.setLayoutData(CmsUtils
.fillAll());
115 Label l
= new Label(c
, SWT
.NONE
);
116 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
117 l
.setText(CmsMsg
.logout
.lead());
118 GridData lData
= CmsUtils
.fillWidth();
119 lData
.widthHint
= 120;
120 l
.setLayoutData(lData
);
122 l
.addMouseListener(new MouseAdapter() {
123 private static final long serialVersionUID
= 6444395812777413116L;
125 public void mouseDown(MouseEvent e
) {
131 /** To be overridden */
132 protected void specificUserUi(Composite parent
) {
136 protected void anonymousUi(Composite parent
) {
137 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
139 // We need a composite for the traversal
140 Composite c
= new Composite(parent
, SWT
.NONE
);
141 c
.setLayout(new GridLayout());
142 c
.setLayoutData(CmsUtils
.fillAll());
144 Integer textWidth
= 120;
145 parent
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
147 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
148 username
= new Text(c
, SWT
.BORDER
);
149 username
.setMessage(CmsMsg
.username
.lead());
150 username
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
151 GridData gd
= CmsUtils
.fillWidth();
152 gd
.widthHint
= textWidth
;
153 username
.setLayoutData(gd
);
155 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
156 password
= new Text(c
, SWT
.BORDER
| SWT
.PASSWORD
);
157 password
.setMessage(CmsMsg
.password
.lead());
158 password
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
159 gd
= CmsUtils
.fillWidth();
160 gd
.widthHint
= textWidth
;
161 password
.setLayoutData(gd
);
163 TraverseListener tl
= new TraverseListener() {
164 private static final long serialVersionUID
= -1158892811534971856L;
166 public void keyTraversed(TraverseEvent e
) {
167 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
171 c
.addTraverseListener(tl
);
172 username
.addTraverseListener(tl
);
173 password
.addTraverseListener(tl
);
174 parent
.setTabList(new Control
[] { c
});
175 c
.setTabList(new Control
[] { username
, password
});
179 protected void login() {
180 Subject subject
= cmsView
.getSubject();
185 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).logout();
186 LoginContext loginContext
= new LoginContext(LOGIN_CONTEXT_USER
,
188 loginContext
.login();
190 // save context in session
191 final HttpSession httpSession
= RWT
.getRequest().getSession();
192 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
196 httpSession
.setAttribute(ACCESS_CONTROL_CONTEXT
,
197 AccessController
.getContext());
201 } catch (LoginException e1
) {
203 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).login();
204 } catch (LoginException e
) {
205 throw new CmsException("Cannot authenticate anonymous", e1
);
207 throw new CmsException("Cannot authenticate", e1
);
210 cmsView
.authChange();
213 protected void logout() {
214 Subject subject
= cmsView
.getSubject();
219 new LoginContext(LOGIN_CONTEXT_USER
, subject
).logout();
220 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).login();
222 HttpServletRequest httpRequest
= RWT
.getRequest();
223 HttpSession httpSession
= httpRequest
.getSession();
224 httpSession
.setAttribute(ACCESS_CONTROL_CONTEXT
, null);
225 } catch (LoginException e1
) {
226 throw new CmsException("Cannot authenticate anonymous", e1
);
229 cmsView
.navigateTo("~");
230 cmsView
.authChange();
234 public void handle(Callback
[] callbacks
) throws IOException
,
235 UnsupportedCallbackException
{
236 ((NameCallback
) callbacks
[0]).setName(username
.getText());
237 ((PasswordCallback
) callbacks
[1]).setPassword(password
.getTextChars());
240 public Shell
getShell() {