]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/UserMenu.java
[maven-release-plugin] prepare for next development iteration
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / UserMenu.java
1 package org.argeo.cms.util;
2
3 import java.io.IOException;
4
5 import javax.security.auth.Subject;
6 import javax.security.auth.callback.Callback;
7 import javax.security.auth.callback.CallbackHandler;
8 import javax.security.auth.callback.NameCallback;
9 import javax.security.auth.callback.PasswordCallback;
10 import javax.security.auth.callback.UnsupportedCallbackException;
11 import javax.security.auth.login.LoginContext;
12 import javax.security.auth.login.LoginException;
13
14 import org.argeo.cms.CmsException;
15 import org.argeo.cms.CmsMsg;
16 import org.argeo.cms.CmsSession;
17 import org.argeo.cms.CmsStyles;
18 import org.argeo.cms.KernelHeader;
19 import org.argeo.cms.auth.ArgeoLoginContext;
20 import org.eclipse.rap.rwt.RWT;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.MouseAdapter;
23 import org.eclipse.swt.events.MouseEvent;
24 import org.eclipse.swt.events.ShellAdapter;
25 import org.eclipse.swt.events.ShellEvent;
26 import org.eclipse.swt.events.TraverseEvent;
27 import org.eclipse.swt.events.TraverseListener;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.swt.widgets.Text;
35 import org.springframework.security.core.Authentication;
36 import org.springframework.security.core.context.SecurityContextHolder;
37
38 /** The site-related user menu */
39 public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
40 private static final long serialVersionUID = -5788157651532106301L;
41 private Text username, password;
42
43 public UserMenu(Control source) {
44 super(source.getDisplay(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
45 setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
46
47 Authentication authentication = SecurityContextHolder.getContext()
48 .getAuthentication();
49 if (authentication == null)
50 throw new CmsException("No authentication available");
51
52 String username = authentication.getName();
53 if (username.equals(KernelHeader.USERNAME_ANONYMOUS)) {
54 username = null;
55 anonymousUi();
56 } else {
57 userUi();
58 }
59
60 pack();
61 layout();
62 setLocation(source.toDisplay(source.getSize().x - getSize().x,
63 source.getSize().y));
64
65 addShellListener(new ShellAdapter() {
66 private static final long serialVersionUID = 5178980294808435833L;
67
68 @Override
69 public void shellDeactivated(ShellEvent e) {
70 close();
71 dispose();
72 }
73 });
74 open();
75 }
76
77 protected void userUi() {
78 setLayout(new GridLayout());
79
80 String username = SecurityContextHolder.getContext()
81 .getAuthentication().getName();
82
83 Label l = new Label(this, SWT.NONE);
84 l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
85 l.setData(RWT.MARKUP_ENABLED, true);
86 l.setLayoutData(CmsUtils.fillWidth());
87 l.setText("<b>" + username + "</b>");
88
89 specificUserUi(this);
90
91 l = new Label(this, SWT.NONE);
92 l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
93 l.setText(CmsMsg.logout.lead());
94 GridData lData = CmsUtils.fillWidth();
95 lData.widthHint = 120;
96 l.setLayoutData(lData);
97
98 l.addMouseListener(new MouseAdapter() {
99 private static final long serialVersionUID = 6444395812777413116L;
100
101 public void mouseDown(MouseEvent e) {
102 logout();
103 }
104 });
105 }
106
107 protected String getUsername() {
108 String username = SecurityContextHolder.getContext()
109 .getAuthentication().getName();
110 return username;
111 }
112
113 /** To be overridden */
114 protected void specificUserUi(Composite parent) {
115
116 }
117
118 protected void anonymousUi() {
119 Integer textWidth = 150;
120 setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
121 setLayout(new GridLayout(2, false));
122
123 new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
124 username = new Text(this, SWT.BORDER);
125 username.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_USERNAME);
126 GridData gd = CmsUtils.fillWidth();
127 gd.widthHint = textWidth;
128 username.setLayoutData(gd);
129
130 new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
131 password = new Text(this, SWT.BORDER | SWT.PASSWORD);
132 password.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD);
133 gd = CmsUtils.fillWidth();
134 gd.widthHint = textWidth;
135 password.setLayoutData(gd);
136
137 TraverseListener tl = new TraverseListener() {
138 private static final long serialVersionUID = -1158892811534971856L;
139
140 public void keyTraversed(TraverseEvent e) {
141 if (e.detail == SWT.TRAVERSE_RETURN)
142 login();
143 }
144 };
145 username.addTraverseListener(tl);
146 password.addTraverseListener(tl);
147 }
148
149 protected void login() {
150 CmsSession cmsSession = (CmsSession) getDisplay().getData(
151 CmsSession.KEY);
152 Subject subject = cmsSession.getSubject();
153 try {
154 //
155 // LOGIN
156 //
157 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS, subject)
158 .logout();
159 LoginContext loginContext = new ArgeoLoginContext(
160 KernelHeader.LOGIN_CONTEXT_USER, subject, this);
161 loginContext.login();
162 } catch (LoginException e1) {
163 try {
164 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS,
165 subject).login();
166 } catch (LoginException e) {
167 throw new CmsException("Cannot authenticate anonymous", e1);
168 }
169 throw new CmsException("Cannot authenticate", e1);
170 }
171 close();
172 dispose();
173 cmsSession.authChange();
174 }
175
176 protected void logout() {
177 final CmsSession cmsSession = (CmsSession) getDisplay().getData(
178 CmsSession.KEY);
179 Subject subject = cmsSession.getSubject();
180 try {
181 //
182 // LOGOUT
183 //
184 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_USER, subject)
185 .logout();
186 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS, subject)
187 .login();
188 } catch (LoginException e1) {
189 throw new CmsException("Cannot authenticate anonymous", e1);
190 }
191 close();
192 dispose();
193 cmsSession.authChange();
194 }
195
196 @Override
197 public void handle(Callback[] callbacks) throws IOException,
198 UnsupportedCallbackException {
199 ((NameCallback) callbacks[0]).setName(username.getText());
200 ((PasswordCallback) callbacks[1]).setPassword(password.getTextChars());
201 }
202
203 }