]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLogin.java
Remove unused login entry point
[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.getKernelHeader;
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.LoginContext;
21 import javax.security.auth.login.LoginException;
22
23 import org.argeo.cms.CmsException;
24 import org.argeo.cms.CmsMsg;
25 import org.argeo.cms.CmsStyles;
26 import org.argeo.cms.CmsView;
27 import org.argeo.cms.auth.CurrentUser;
28 import org.argeo.cms.auth.HttpRequestCallback;
29 import org.argeo.cms.i18n.Msg;
30 import org.argeo.cms.util.CmsUtils;
31 import org.argeo.util.LocaleChoice;
32 import org.eclipse.rap.rwt.RWT;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.MouseAdapter;
35 import org.eclipse.swt.events.MouseEvent;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.events.SelectionListener;
39 import org.eclipse.swt.events.TraverseEvent;
40 import org.eclipse.swt.events.TraverseListener;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Label;
47 import org.eclipse.swt.widgets.Shell;
48 import org.eclipse.swt.widgets.Text;
49
50 public class CmsLogin implements CmsStyles, CallbackHandler {
51 private Composite parent;
52 private Text usernameT, passwordT;
53 private Composite credentialsBlock;
54 private final SelectionListener loginSelectionListener;
55
56 private final Locale defaultLocale;
57 private LocaleChoice localeChoice = null;
58
59 private final CmsView cmsView;
60
61 public CmsLogin(CmsView cmsView) {
62 this.cmsView = cmsView;
63 defaultLocale = getKernelHeader().getDefaultLocale();
64 List<Locale> locales = getKernelHeader().getLocales();
65 if (locales != null)
66 localeChoice = new LocaleChoice(locales, defaultLocale);
67 loginSelectionListener = new SelectionListener() {
68 private static final long serialVersionUID = -8832133363830973578L;
69
70 @Override
71 public void widgetSelected(SelectionEvent e) {
72 login();
73 }
74
75 @Override
76 public void widgetDefaultSelected(SelectionEvent e) {
77 }
78 };
79 }
80
81 protected boolean isAnonymous() {
82 return CurrentUser.isAnonymous(cmsView.getSubject());
83 }
84
85 public final void createUi(Composite parent) {
86 this.parent = parent;
87 createContents(parent);
88 }
89
90 protected void createContents(Composite parent) {
91 defaultCreateContents(parent);
92 }
93
94 public final void defaultCreateContents(Composite parent) {
95 parent.setLayout(CmsUtils.noSpaceGridLayout());
96 Composite credentialsBlock = createCredentialsBlock(parent);
97 if (parent instanceof Shell) {
98 credentialsBlock.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
99 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 credentialsBlock = new Composite(parent, SWT.NONE);
117 credentialsBlock.setLayout(new GridLayout());
118 credentialsBlock.setLayoutData(CmsUtils.fillAll());
119
120 specificUserUi(credentialsBlock);
121
122 Label l = new Label(credentialsBlock, SWT.NONE);
123 l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
124 l.setText(CmsMsg.logout.lead());
125 GridData lData = CmsUtils.fillWidth();
126 lData.widthHint = 120;
127 l.setLayoutData(lData);
128
129 l.addMouseListener(new MouseAdapter() {
130 private static final long serialVersionUID = 6444395812777413116L;
131
132 public void mouseDown(MouseEvent e) {
133 logout();
134 }
135 });
136 return credentialsBlock;
137 }
138
139 /** To be overridden */
140 protected void specificUserUi(Composite parent) {
141
142 }
143
144 protected Composite anonymousUi(Composite parent) {
145 Locale locale = localeChoice == null ? this.defaultLocale
146 : 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 parent.setData(RWT.CUSTOM_VARIANT, 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 usernameT.setData(RWT.CUSTOM_VARIANT, 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 passwordT.setData(RWT.CUSTOM_VARIANT, 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,
187 loginSelectionListener);
188 if (localeChoice != null)
189 createLocalesBlock(credentialsBlock);
190 return credentialsBlock;
191 }
192
193 /**
194 * To be overridden in order to provide custome login button and other
195 * links.
196 */
197 protected void extendsCredentialsBlock(Composite credentialsBlock,
198 Locale selectedLocale, SelectionListener loginSelectionListener) {
199
200 }
201
202 protected void updateLocale(Locale selectedLocale) {
203 // usernameT.setMessage(username.lead(selectedLocale));
204 // passwordT.setMessage(password.lead(selectedLocale));
205 for (Control child : parent.getChildren())
206 child.dispose();
207 createContents(parent);
208 if (parent.getParent() != null)
209 parent.getParent().layout();
210 else
211 parent.layout();
212 }
213
214 protected Composite createLocalesBlock(final Composite parent) {
215 Composite c = new Composite(parent, SWT.NONE);
216 c.setLayout(CmsUtils.noSpaceGridLayout());
217 c.setLayoutData(CmsUtils.fillAll());
218
219 SelectionListener selectionListener = new SelectionAdapter() {
220 private static final long serialVersionUID = 4891637813567806762L;
221
222 public void widgetSelected(SelectionEvent event) {
223 Button button = (Button) event.widget;
224 if (button.getSelection()) {
225 localeChoice.setSelectedIndex((Integer) event.widget
226 .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(Msg.lead(locale.getDisplayName(locale), locale)
238 + " (" + locale + ")");
239 // button.addListener(SWT.Selection, listener);
240 button.addSelectionListener(selectionListener);
241 if (i == localeChoice.getSelectedIndex())
242 button.setSelection(true);
243 }
244 return c;
245 }
246
247 protected void login() {
248 Subject subject = cmsView.getSubject();
249 LoginContext loginContext;
250 try {
251 //
252 // LOGIN
253 //
254 new LoginContext(LOGIN_CONTEXT_ANONYMOUS, subject).logout();
255 loginContext = new LoginContext(LOGIN_CONTEXT_USER, subject, this);
256 loginContext.login();
257 } catch (LoginException e1) {
258 throw new CmsException("Cannot authenticate", e1);
259 }
260 cmsView.authChange(loginContext);
261 }
262
263 protected void logout() {
264 cmsView.logout();
265 cmsView.navigateTo("~");
266 }
267
268 @Override
269 public void handle(Callback[] callbacks) throws IOException,
270 UnsupportedCallbackException {
271 for (Callback callback : callbacks) {
272 if (callback instanceof NameCallback)
273 ((NameCallback) callback).setName(usernameT.getText());
274 else if (callback instanceof PasswordCallback)
275 ((PasswordCallback) callback).setPassword(passwordT
276 .getTextChars());
277 else if (callback instanceof HttpRequestCallback)
278 ((HttpRequestCallback) callback).setRequest(RWT.getRequest());
279 else if (callback instanceof LanguageCallback
280 && localeChoice != null)
281 ((LanguageCallback) callback).setLocale(localeChoice
282 .getSelectedLocale());
283 }
284 }
285
286 }