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