]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/UserMenu.java
Remove unused directory
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / UserMenu.java
1 package org.argeo.cms.util;
2
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;
6
7 import java.io.IOException;
8 import java.security.AccessController;
9 import java.security.PrivilegedAction;
10
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;
21
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;
43
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;
48
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);
52
53 String username = CurrentUser.getUsername(CmsUtils.getCmsView().getSubject());
54 if (username.equalsIgnoreCase(AuthConstants.ROLE_ANONYMOUS)) {
55 username = null;
56 anonymousUi();
57 } else {
58 userUi();
59 }
60
61 pack();
62 layout();
63 setLocation(source.toDisplay(source.getSize().x - getSize().x,
64 source.getSize().y));
65
66 addShellListener(new ShellAdapter() {
67 private static final long serialVersionUID = 5178980294808435833L;
68
69 @Override
70 public void shellDeactivated(ShellEvent e) {
71 close();
72 dispose();
73 }
74 });
75 open();
76 }
77
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());
83
84 specificUserUi(c);
85
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);
92
93 l.addMouseListener(new MouseAdapter() {
94 private static final long serialVersionUID = 6444395812777413116L;
95
96 public void mouseDown(MouseEvent e) {
97 logout();
98 }
99 });
100 }
101
102 /** To be overridden */
103 protected void specificUserUi(Composite parent) {
104
105 }
106
107 protected void anonymousUi() {
108 setLayout(CmsUtils.noSpaceGridLayout());
109
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());
114
115 Integer textWidth = 120;
116 setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
117
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);
125
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);
133
134 TraverseListener tl = new TraverseListener() {
135 private static final long serialVersionUID = -1158892811534971856L;
136
137 public void keyTraversed(TraverseEvent e) {
138 if (e.detail == SWT.TRAVERSE_RETURN)
139 login();
140 }
141 };
142 c.addTraverseListener(tl);
143 username.addTraverseListener(tl);
144 password.addTraverseListener(tl);
145 setTabList(new Control[] { c });
146 c.setTabList(new Control[] { username, password });
147 c.setFocus();
148 }
149
150 protected void login() {
151 CmsView cmsSession = (CmsView) getDisplay().getData(CmsView.KEY);
152 Subject subject = cmsSession.getSubject();
153 try {
154 //
155 // LOGIN
156 //
157 new LoginContext(LOGIN_CONTEXT_ANONYMOUS, subject).logout();
158 LoginContext loginContext = new LoginContext(LOGIN_CONTEXT_USER,
159 subject, this);
160 loginContext.login();
161
162 // save context in session
163 final HttpSession httpSession = RWT.getRequest().getSession();
164 Subject.doAs(subject, new PrivilegedAction<Void>() {
165
166 @Override
167 public Void run() {
168 httpSession.setAttribute(ACCESS_CONTROL_CONTEXT,
169 AccessController.getContext());
170 return null;
171 }
172 });
173 } catch (LoginException e1) {
174 try {
175 new LoginContext(LOGIN_CONTEXT_ANONYMOUS, subject).login();
176 } catch (LoginException e) {
177 throw new CmsException("Cannot authenticate anonymous", e1);
178 }
179 throw new CmsException("Cannot authenticate", e1);
180 }
181 close();
182 dispose();
183 cmsSession.authChange();
184 }
185
186 protected void logout() {
187 final CmsView cmsSession = (CmsView) getDisplay().getData(CmsView.KEY);
188 Subject subject = cmsSession.getSubject();
189 try {
190 //
191 // LOGOUT
192 //
193 new LoginContext(LOGIN_CONTEXT_USER, subject).logout();
194 new LoginContext(LOGIN_CONTEXT_ANONYMOUS, subject).login();
195
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);
201 }
202 close();
203 dispose();
204 cmsSession.navigateTo("~");
205 cmsSession.authChange();
206 }
207
208 @Override
209 public void handle(Callback[] callbacks) throws IOException,
210 UnsupportedCallbackException {
211 ((NameCallback) callbacks[0]).setName(username.getText());
212 ((PasswordCallback) callbacks[1]).setPassword(password.getTextChars());
213 }
214
215 }