1 package org
.argeo
.cms
.widgets
.auth
;
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
;
9 import java
.io
.IOException
;
10 import java
.util
.List
;
11 import java
.util
.Locale
;
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
;
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
;
50 public class CmsLogin
implements CmsStyles
, CallbackHandler
{
51 private Text usernameT
, passwordT
;
52 private Composite credentialsBlock
;
54 private final Locale defaultLocale
;
55 private LocaleChoice localeChoice
= null;
57 private final CmsView cmsView
;
59 public CmsLogin(CmsView cmsView
) {
60 this.cmsView
= cmsView
;
61 defaultLocale
= getKernelHeader().getDefaultLocale();
62 List
<Locale
> locales
= getKernelHeader().getLocales();
64 localeChoice
= new LocaleChoice(locales
, defaultLocale
);
67 protected boolean isAnonymous() {
68 return CurrentUser
.isAnonymous(cmsView
.getSubject());
71 public void createContents(Composite parent
) {
72 defaultCreateContents(parent
);
75 public final void defaultCreateContents(Composite parent
) {
76 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
77 Composite credentialsBlock
= createCredentialsBlock(parent
);
78 if (parent
instanceof Shell
) {
79 credentialsBlock
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
,
84 public final Composite
createCredentialsBlock(Composite parent
) {
86 return anonymousUi(parent
);
88 return userUi(parent
);
92 protected Composite
getCredentialsBlock() {
93 return credentialsBlock
;
96 protected Composite
userUi(Composite parent
) {
97 credentialsBlock
= new Composite(parent
, SWT
.NONE
);
98 credentialsBlock
.setLayout(new GridLayout());
99 credentialsBlock
.setLayoutData(CmsUtils
.fillAll());
101 specificUserUi(credentialsBlock
);
103 Label l
= new Label(credentialsBlock
, SWT
.NONE
);
104 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
105 l
.setText(CmsMsg
.logout
.lead());
106 GridData lData
= CmsUtils
.fillWidth();
107 lData
.widthHint
= 120;
108 l
.setLayoutData(lData
);
110 l
.addMouseListener(new MouseAdapter() {
111 private static final long serialVersionUID
= 6444395812777413116L;
113 public void mouseDown(MouseEvent e
) {
117 return credentialsBlock
;
120 /** To be overridden */
121 protected void specificUserUi(Composite parent
) {
125 protected Composite
anonymousUi(Composite parent
) {
126 // We need a composite for the traversal
127 credentialsBlock
= new Composite(parent
, SWT
.NONE
);
128 credentialsBlock
.setLayout(new GridLayout());
129 credentialsBlock
.setLayoutData(CmsUtils
.fillAll());
131 Integer textWidth
= 120;
132 parent
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
134 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
135 usernameT
= new Text(credentialsBlock
, SWT
.BORDER
);
136 usernameT
.setMessage(username
.lead(defaultLocale
));
137 usernameT
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
138 GridData gd
= CmsUtils
.fillWidth();
139 gd
.widthHint
= textWidth
;
140 usernameT
.setLayoutData(gd
);
142 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
143 passwordT
= new Text(credentialsBlock
, SWT
.BORDER
| SWT
.PASSWORD
);
144 passwordT
.setMessage(password
.lead(defaultLocale
));
145 passwordT
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
146 gd
= CmsUtils
.fillWidth();
147 gd
.widthHint
= textWidth
;
148 passwordT
.setLayoutData(gd
);
150 TraverseListener tl
= new TraverseListener() {
151 private static final long serialVersionUID
= -1158892811534971856L;
153 public void keyTraversed(TraverseEvent e
) {
154 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
158 credentialsBlock
.addTraverseListener(tl
);
159 usernameT
.addTraverseListener(tl
);
160 passwordT
.addTraverseListener(tl
);
161 parent
.setTabList(new Control
[] { credentialsBlock
});
162 credentialsBlock
.setTabList(new Control
[] { usernameT
, passwordT
});
163 credentialsBlock
.setFocus();
165 if (localeChoice
!= null)
166 createLocalesBlock(credentialsBlock
);
167 return credentialsBlock
;
170 protected Composite
createLocalesBlock(final Composite parent
) {
171 Composite c
= new Composite(parent
, SWT
.NONE
);
172 c
.setLayout(CmsUtils
.noSpaceGridLayout());
173 c
.setLayoutData(CmsUtils
.fillAll());
175 SelectionListener selectionListener
= new SelectionAdapter() {
176 private static final long serialVersionUID
= 4891637813567806762L;
178 public void widgetSelected(SelectionEvent event
) {
179 localeChoice
.setSelectedIndex((Integer
) event
.widget
.getData());
180 Locale selectedLocale
= localeChoice
.getSelectedLocale();
181 usernameT
.setMessage(username
.lead(selectedLocale
));
182 passwordT
.setMessage(password
.lead(selectedLocale
));
186 List
<Locale
> locales
= localeChoice
.getLocales();
187 for (Integer i
= 0; i
< locales
.size(); i
++) {
188 Locale locale
= locales
.get(i
);
189 Button button
= new Button(c
, SWT
.RADIO
);
191 button
.setText(Msg
.lead(locale
.getDisplayName(locale
), locale
)
192 + " (" + locale
+ ")");
193 // button.addListener(SWT.Selection, listener);
194 button
.addSelectionListener(selectionListener
);
195 if (i
== localeChoice
.getDefaultIndex())
196 button
.setSelection(true);
201 protected void login() {
202 Subject subject
= cmsView
.getSubject();
203 LoginContext loginContext
;
208 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).logout();
209 loginContext
= new LoginContext(LOGIN_CONTEXT_USER
, subject
, this);
210 loginContext
.login();
211 } catch (LoginException e1
) {
212 throw new CmsException("Cannot authenticate", e1
);
214 cmsView
.authChange(loginContext
);
217 protected void logout() {
219 cmsView
.navigateTo("~");
223 public void handle(Callback
[] callbacks
) throws IOException
,
224 UnsupportedCallbackException
{
225 for (Callback callback
: callbacks
) {
226 if (callback
instanceof NameCallback
)
227 ((NameCallback
) callback
).setName(usernameT
.getText());
228 else if (callback
instanceof PasswordCallback
)
229 ((PasswordCallback
) callback
).setPassword(passwordT
231 else if (callback
instanceof HttpRequestCallback
)
232 ((HttpRequestCallback
) callback
).setRequest(RWT
.getRequest());
233 else if (callback
instanceof LanguageCallback
234 && localeChoice
!= null)
235 ((LanguageCallback
) callback
).setLocale(localeChoice
236 .getSelectedLocale());