]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/CmsLogin.java
Work on rap migration.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / CmsLogin.java
1 package org.argeo.cms;
2
3 import java.util.Collections;
4 import java.util.List;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.springframework.security.authentication.AnonymousAuthenticationToken;
9 import org.springframework.security.authentication.AuthenticationManager;
10 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
11 import org.springframework.security.core.Authentication;
12 import org.springframework.security.core.authority.SimpleGrantedAuthority;
13 import org.springframework.security.core.context.SecurityContextHolder;
14 import org.springframework.security.core.userdetails.User;
15 import org.springframework.security.core.userdetails.UserDetails;
16
17 /** Gateway for user login, can also generate the related UI. */
18 public class CmsLogin {
19 private final static Log log = LogFactory.getLog(CmsLogin.class);
20 private AuthenticationManager authenticationManager;
21 private String systemKey = "argeo";
22
23 protected void logInAsAnonymous() {
24 // TODO Better deal with anonymous authentication
25 try {
26 List<SimpleGrantedAuthority> anonAuthorities = Collections
27 .singletonList(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
28 UserDetails anonUser = new User("anonymous", "", true, true, true,
29 true, anonAuthorities);
30 AnonymousAuthenticationToken anonToken = new AnonymousAuthenticationToken(
31 systemKey, anonUser, anonAuthorities);
32 Authentication authentication = authenticationManager
33 .authenticate(anonToken);
34 SecurityContextHolder.getContext()
35 .setAuthentication(authentication);
36 } catch (Exception e) {
37 throw new CmsException("Cannot authenticate", e);
38 }
39 }
40
41 protected void logInWithPassword(String username, char[] password) {
42 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
43 username, new String(password));
44 Authentication authentication = authenticationManager
45 .authenticate(token);
46 SecurityContextHolder.getContext().setAuthentication(authentication);
47 if (log.isDebugEnabled())
48 log.debug("Authenticated as " + authentication);
49 }
50
51 /*
52 * UI
53 */
54
55 // @Override
56 // public Control createUi(Composite parent, Node context)
57 // throws RepositoryException {
58 // Composite comp = new Composite(parent, SWT.NONE);
59 // comp.setLayout(new GridLayout(1, true));
60 // comp.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LOGIN);
61 // refreshUi(comp);
62 // return comp;
63 // }
64
65 // protected void refreshUi(Composite comp) {
66 // String username = SecurityContextHolder.getContext()
67 // .getAuthentication().getName();
68 // if (username.equals("anonymous"))
69 // username = null;
70 //
71 // for (Control child : comp.getChildren()) {
72 // child.dispose();
73 // }
74 //
75 // Label l = new Label(comp, SWT.NONE);
76 // l.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LOGIN);
77 // l.setData(RWT.MARKUP_ENABLED, true);
78 // l.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
79 // if (username != null) {
80 // l.setText("<b>" + username + "</b>");
81 // l.addMouseListener(new UserListener());
82 // } else {
83 // l.setText("Log in");
84 // l.addMouseListener(new LoginListener());
85 // }
86 //
87 // comp.pack();
88 // }
89
90 public void setAuthenticationManager(
91 AuthenticationManager authenticationManager) {
92 this.authenticationManager = authenticationManager;
93 }
94
95 public void setSystemKey(String systemKey) {
96 this.systemKey = systemKey;
97 }
98
99 // private class UserListener extends MouseAdapter {
100 // private static final long serialVersionUID = -3565359775509786183L;
101 // private Control source;
102 // private Shell dialog;
103 //
104 // @Override
105 // public void mouseDown(MouseEvent e) {
106 // source = ((Control) e.widget);
107 // if (dialog != null) {
108 // dialog.close();
109 // dialog.dispose();
110 // dialog = null;
111 // } else {
112 // dialog = createDialog(source);
113 // }
114 // }
115 //
116 // @SuppressWarnings("serial")
117 // protected Shell createDialog(Control source) {
118 // Shell dialog = new Shell(source.getDisplay(), SWT.NO_TRIM
119 // | SWT.BORDER | SWT.ON_TOP);
120 // dialog.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
121 // dialog.setLayout(new GridLayout(1, false));
122 //
123 // final CmsSession cmsSession = (CmsSession) source.getDisplay()
124 // .getData(CmsSession.KEY);
125 //
126 // Label l = new Label(dialog, SWT.NONE);
127 // l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
128 // l.setText("Log out");
129 // GridData lData = new GridData(SWT.FILL, SWT.FILL, true, false);
130 // lData.widthHint = 120;
131 // l.setLayoutData(lData);
132 //
133 // l.addMouseListener(new MouseAdapter() {
134 // public void mouseDown(MouseEvent e) {
135 // SecurityContextHolder.getContext().setAuthentication(null);
136 // UserListener.this.dialog.close();
137 // UserListener.this.dialog.dispose();
138 // cmsSession.authChange();
139 // }
140 // });
141 //
142 // dialog.pack();
143 // dialog.layout();
144 // dialog.setLocation(source.toDisplay(
145 // source.getSize().x - dialog.getSize().x, source.getSize().y));
146 // dialog.open();
147 // return dialog;
148 // }
149 // }
150 //
151 // private class LoginListener extends MouseAdapter {
152 // private static final long serialVersionUID = 677115566708451462L;
153 // private Control source;
154 // private Shell dialog;
155 //
156 // @Override
157 // public void mouseDown(MouseEvent e) {
158 // source = ((Control) e.widget);
159 // if (dialog != null) {
160 // dialog.close();
161 // dialog.dispose();
162 // dialog = null;
163 // } else {
164 // dialog = createDialog(source);
165 // }
166 // }
167 //
168 // @SuppressWarnings("serial")
169 // protected Shell createDialog(Control source) {
170 // Integer textWidth = 150;
171 // Shell dialog = new Shell(source.getDisplay(), SWT.NO_TRIM
172 // | SWT.BORDER | SWT.ON_TOP);
173 // dialog.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG);
174 // dialog.setLayout(new GridLayout(2, false));
175 //
176 // new Label(dialog, SWT.NONE).setText("Username");
177 // final Text username = new Text(dialog, SWT.BORDER);
178 // username.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_USERNAME);
179 // GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
180 // gd.widthHint = textWidth;
181 // username.setLayoutData(gd);
182 //
183 // new Label(dialog, SWT.NONE).setText("Password");
184 // final Text password = new Text(dialog, SWT.BORDER | SWT.PASSWORD);
185 // password.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD);
186 // gd = new GridData(SWT.FILL, SWT.FILL, true, false);
187 // gd.widthHint = textWidth;
188 // password.setLayoutData(gd);
189 //
190 // dialog.pack();
191 // dialog.layout();
192 // dialog.setLocation(source.toDisplay(
193 // source.getSize().x - dialog.getSize().x, source.getSize().y));
194 // dialog.open();
195 //
196 // // Listeners
197 // TraverseListener tl = new TraverseListener() {
198 // public void keyTraversed(TraverseEvent e) {
199 // if (e.detail == SWT.TRAVERSE_RETURN)
200 // login(username.getText(), password.getTextChars());
201 // }
202 // };
203 // username.addTraverseListener(tl);
204 // password.addTraverseListener(tl);
205 // return dialog;
206 // }
207 //
208 // protected void login(String username, char[] password) {
209 // CmsSession cmsSession = (CmsSession) source.getDisplay().getData(
210 // CmsSession.KEY);
211 // logInWithPassword(username, password);
212 // dialog.close();
213 // dialog.dispose();
214 // refreshUi(source.getParent());
215 // cmsSession.authChange();
216 // }
217 //
218 // }
219 }