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