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