]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLogin.java
Reset to argeo.cnd, in order to avoid surprises at this stage
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / widgets / auth / CmsLogin.java
1 package org.argeo.cms.widgets.auth;
2
3 import static org.argeo.cms.CmsMsg.password;
4 import static org.argeo.cms.CmsMsg.username;
5 import static org.argeo.cms.auth.AuthConstants.LOGIN_CONTEXT_ANONYMOUS;
6 import static org.argeo.cms.auth.AuthConstants.LOGIN_CONTEXT_USER;
7 import static org.argeo.cms.internal.kernel.Activator.getNodeState;
8
9 import java.io.IOException;
10 import java.util.List;
11 import java.util.Locale;
12
13 import javax.security.auth.Subject;
14 import javax.security.auth.callback.Callback;
15 import javax.security.auth.callback.CallbackHandler;
16 import javax.security.auth.callback.LanguageCallback;
17 import javax.security.auth.callback.NameCallback;
18 import javax.security.auth.callback.PasswordCallback;
19 import javax.security.auth.callback.UnsupportedCallbackException;
20 import javax.security.auth.login.FailedLoginException;
21 import javax.security.auth.login.LoginContext;
22 import javax.security.auth.login.LoginException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.cms.CmsMsg;
27 import org.argeo.cms.CmsStyles;
28 import org.argeo.cms.CmsView;
29 import org.argeo.cms.auth.CurrentUser;
30 import org.argeo.cms.auth.HttpRequestCallback;
31 import org.argeo.cms.i18n.LocaleUtils;
32 import org.argeo.cms.internal.auth.LocaleChoice;
33 import org.argeo.cms.util.CmsUtils;
34 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
35 import org.eclipse.rap.rwt.RWT;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.MouseAdapter;
38 import org.eclipse.swt.events.MouseEvent;
39 import org.eclipse.swt.events.SelectionAdapter;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.events.SelectionListener;
42 import org.eclipse.swt.events.TraverseEvent;
43 import org.eclipse.swt.events.TraverseListener;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Button;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Control;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Text;
52
53 public class CmsLogin implements CmsStyles, CallbackHandler {
54 private final static Log log = LogFactory.getLog(CmsLogin.class);
55
56 private Composite parent;
57 private Text usernameT, passwordT;
58 private Composite credentialsBlock;
59 private final SelectionListener loginSelectionListener;
60
61 private final Locale defaultLocale;
62 private LocaleChoice localeChoice = null;
63
64 private final CmsView cmsView;
65
66 public CmsLogin(CmsView cmsView) {
67 this.cmsView = cmsView;
68 defaultLocale = getNodeState().getDefaultLocale();
69 List<Locale> locales = getNodeState().getLocales();
70 if (locales != null)
71 localeChoice = new LocaleChoice(locales, defaultLocale);
72 loginSelectionListener = new SelectionListener() {
73 private static final long serialVersionUID = -8832133363830973578L;
74
75 @Override
76 public void widgetSelected(SelectionEvent e) {
77 login();
78 }
79
80 @Override
81 public void widgetDefaultSelected(SelectionEvent e) {
82 }
83 };
84 }
85
86 protected boolean isAnonymous() {
87 return CurrentUser.isAnonymous(cmsView.getSubject());
88 }
89
90 public final void createUi(Composite parent) {
91 this.parent = parent;
92 createContents(parent);
93 }
94
95 protected void createContents(Composite parent) {
96 defaultCreateContents(parent);
97 }
98
99 public final void defaultCreateContents(Composite parent) {
100 parent.setLayout(CmsUtils.noSpaceGridLayout());
101 Composite credentialsBlock = createCredentialsBlock(parent);
102 if (parent instanceof Shell) {
103 credentialsBlock.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
104 true, true));
105 }
106 }
107
108 public final Composite createCredentialsBlock(Composite parent) {
109 if (isAnonymous()) {
110 return anonymousUi(parent);
111 } else {
112 return userUi(parent);
113 }
114 }
115
116 protected Composite getCredentialsBlock() {
117 return credentialsBlock;
118 }
119
120 protected Composite userUi(Composite parent) {
121 Locale locale = localeChoice == null ? this.defaultLocale
122 : localeChoice.getSelectedLocale();
123 credentialsBlock = new Composite(parent, SWT.NONE);
124 credentialsBlock.setLayout(new GridLayout());
125 credentialsBlock.setLayoutData(CmsUtils.fillAll());
126
127 specificUserUi(credentialsBlock);
128
129 Label l = new Label(credentialsBlock, SWT.NONE);
130 l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
131 l.setText(CmsMsg.logout.lead(locale));
132 GridData lData = CmsUtils.fillWidth();
133 lData.widthHint = 120;
134 l.setLayoutData(lData);
135
136 l.addMouseListener(new MouseAdapter() {
137 private static final long serialVersionUID = 6444395812777413116L;
138
139 public void mouseDown(MouseEvent e) {
140 logout();
141 }
142 });
143 return credentialsBlock;
144 }
145
146 /** To be overridden */
147 protected void specificUserUi(Composite parent) {
148
149 }
150
151 protected Composite anonymousUi(Composite parent) {
152 Locale locale = localeChoice == null ? this.defaultLocale
153 : localeChoice.getSelectedLocale();
154 // We need a composite for the traversal
155 credentialsBlock = new Composite(parent, SWT.NONE);
156 credentialsBlock.setLayout(new GridLayout());
157 credentialsBlock.setLayoutData(CmsUtils.fillAll());
158
159 Integer textWidth = 120;
160 parent.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
161
162 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
163 usernameT = new Text(credentialsBlock, SWT.BORDER);
164 usernameT.setMessage(username.lead(locale));
165 usernameT.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_USERNAME);
166 GridData gd = CmsUtils.fillWidth();
167 gd.widthHint = textWidth;
168 usernameT.setLayoutData(gd);
169
170 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
171 passwordT = new Text(credentialsBlock, SWT.BORDER | SWT.PASSWORD);
172 passwordT.setMessage(password.lead(locale));
173 passwordT.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD);
174 gd = CmsUtils.fillWidth();
175 gd.widthHint = textWidth;
176 passwordT.setLayoutData(gd);
177
178 TraverseListener tl = new TraverseListener() {
179 private static final long serialVersionUID = -1158892811534971856L;
180
181 public void keyTraversed(TraverseEvent e) {
182 if (e.detail == SWT.TRAVERSE_RETURN)
183 login();
184 }
185 };
186 credentialsBlock.addTraverseListener(tl);
187 usernameT.addTraverseListener(tl);
188 passwordT.addTraverseListener(tl);
189 parent.setTabList(new Control[] { credentialsBlock });
190 credentialsBlock.setTabList(new Control[] { usernameT, passwordT });
191 // credentialsBlock.setFocus();
192
193 extendsCredentialsBlock(credentialsBlock, locale,
194 loginSelectionListener);
195 if (localeChoice != null)
196 createLocalesBlock(credentialsBlock);
197 return credentialsBlock;
198 }
199
200 /**
201 * To be overridden in order to provide custome login button and other
202 * links.
203 */
204 protected void extendsCredentialsBlock(Composite credentialsBlock,
205 Locale selectedLocale, SelectionListener loginSelectionListener) {
206
207 }
208
209 protected void updateLocale(Locale selectedLocale) {
210 // save already entered values
211 String usernameStr = usernameT.getText();
212 char[] pwd = passwordT.getTextChars();
213
214 for (Control child : parent.getChildren())
215 child.dispose();
216 createContents(parent);
217 if (parent.getParent() != null)
218 parent.getParent().layout();
219 else
220 parent.layout();
221 usernameT.setText(usernameStr);
222 passwordT.setTextChars(pwd);
223 }
224
225 protected Composite createLocalesBlock(final Composite parent) {
226 Composite c = new Composite(parent, SWT.NONE);
227 c.setLayout(CmsUtils.noSpaceGridLayout());
228 c.setLayoutData(CmsUtils.fillAll());
229
230 SelectionListener selectionListener = new SelectionAdapter() {
231 private static final long serialVersionUID = 4891637813567806762L;
232
233 public void widgetSelected(SelectionEvent event) {
234 Button button = (Button) event.widget;
235 if (button.getSelection()) {
236 localeChoice.setSelectedIndex((Integer) event.widget
237 .getData());
238 updateLocale(localeChoice.getSelectedLocale());
239 }
240 };
241 };
242
243 List<Locale> locales = localeChoice.getLocales();
244 for (Integer i = 0; i < locales.size(); i++) {
245 Locale locale = locales.get(i);
246 Button button = new Button(c, SWT.RADIO);
247 button.setData(i);
248 button.setText(LocaleUtils.lead(locale.getDisplayName(locale),
249 locale) + " (" + locale + ")");
250 // button.addListener(SWT.Selection, listener);
251 button.addSelectionListener(selectionListener);
252 if (i == localeChoice.getSelectedIndex())
253 button.setSelection(true);
254 }
255 return c;
256 }
257
258 protected boolean login() {
259 Subject subject = cmsView.getSubject();
260 LoginContext loginContext;
261 try {
262 //
263 // LOGIN
264 //
265 new LoginContext(LOGIN_CONTEXT_ANONYMOUS, subject).logout();
266 loginContext = new LoginContext(LOGIN_CONTEXT_USER, subject, this);
267 loginContext.login();
268 } catch (FailedLoginException e) {
269 log.warn(e.getMessage());
270 try {
271 Thread.sleep(3000);
272 } catch (InterruptedException e2) {
273 // silent
274 }
275 // ErrorFeedback.show("Login failed", e);
276 return false;
277 } catch (LoginException e) {
278 log.error("Cannot login", e);
279 return false;
280 }
281 cmsView.authChange(loginContext);
282 return true;
283 }
284
285 protected void logout() {
286 cmsView.logout();
287 cmsView.navigateTo("~");
288 }
289
290 @Override
291 public void handle(Callback[] callbacks) throws IOException,
292 UnsupportedCallbackException {
293 for (Callback callback : callbacks) {
294 if (callback instanceof NameCallback)
295 ((NameCallback) callback).setName(usernameT.getText());
296 else if (callback instanceof PasswordCallback)
297 ((PasswordCallback) callback).setPassword(passwordT
298 .getTextChars());
299 else if (callback instanceof HttpRequestCallback)
300 ((HttpRequestCallback) callback).setRequest(RWT.getRequest());
301 else if (callback instanceof LanguageCallback
302 && localeChoice != null)
303 ((LanguageCallback) callback).setLocale(localeChoice
304 .getSelectedLocale());
305 }
306 }
307
308 }