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