]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.e4.rap/src/org/argeo/cms/e4/rap/CmsE4EntryPointFactory.java
05eb7c3d03fb64fddf4366ea186ee67ab42cf077
[lgpl/argeo-commons.git] / org.argeo.cms.e4.rap / src / org / argeo / cms / e4 / rap / CmsE4EntryPointFactory.java
1 package org.argeo.cms.e4.rap;
2
3 import java.security.PrivilegedAction;
4
5 import javax.security.auth.Subject;
6 import javax.security.auth.login.LoginContext;
7 import javax.security.auth.login.LoginException;
8
9 import org.argeo.cms.CmsException;
10 import org.argeo.cms.auth.CurrentUser;
11 import org.argeo.cms.ui.CmsImageManager;
12 import org.argeo.cms.ui.CmsView;
13 import org.argeo.cms.ui.UxContext;
14 import org.argeo.cms.util.SimpleUxContext;
15 import org.argeo.cms.widgets.auth.CmsLoginShell;
16 import org.argeo.node.NodeConstants;
17 import org.eclipse.rap.e4.E4ApplicationConfig;
18 import org.eclipse.rap.e4.E4EntryPointFactory;
19 import org.eclipse.rap.rwt.application.EntryPoint;
20 import org.eclipse.swt.widgets.Display;
21
22 public class CmsE4EntryPointFactory extends E4EntryPointFactory {
23
24 public CmsE4EntryPointFactory(E4ApplicationConfig config) {
25 super(config);
26 }
27
28 @Override
29 public EntryPoint create() {
30 // Subject subject = new Subject();
31 EntryPoint ep = createEntryPoint();
32 EntryPoint authEp = new EntryPoint() {
33
34 @Override
35 public int createUI() {
36 Subject subject = new Subject();
37 // boolean success = login(subject);
38 // if (success)
39 return Subject.doAs(subject, new PrivilegedAction<Integer>() {
40
41 @Override
42 public Integer run() {
43 // RWT.setLocale(Locale.FRENCH);
44 System.out.println("createUI");
45 return ep.createUI();
46 }
47
48 });
49 // else
50 // return 1;
51 }
52 };
53 return authEp;
54 }
55
56 protected EntryPoint createEntryPoint() {
57 return super.create();
58 }
59
60 boolean login(Subject subject) {
61 // Subject subject = Subject.getSubject(AccessController.getContext());
62 Display display = new Display();
63 CmsView cmsView = new E4CmsView(subject);
64 CmsLoginShell loginShell = new CmsLoginShell(cmsView);
65 loginShell.setSubject(subject);
66 try {
67 // try pre-auth
68 LoginContext loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, loginShell);
69 loginContext.login();
70 } catch (LoginException e) {
71 loginShell.createUi();
72 loginShell.open();
73
74 while (!loginShell.getShell().isDisposed()) {
75 if (!display.readAndDispatch())
76 display.sleep();
77 }
78 } finally {
79 display.dispose();
80 }
81 if (CurrentUser.getUsername(subject) == null)
82 return false;
83 return true;
84 }
85
86 class E4CmsView implements CmsView {
87 private LoginContext loginContext;
88 private UxContext uxContext;
89 private Subject subject;
90
91 public E4CmsView(Subject subject) {
92 this.subject = subject;
93 uxContext = new SimpleUxContext();
94 }
95
96 @Override
97 public UxContext getUxContext() {
98 return uxContext;
99 }
100
101 @Override
102 public void navigateTo(String state) {
103 // TODO Auto-generated method stub
104
105 }
106
107 @Override
108 public void authChange(LoginContext loginContext) {
109 if (loginContext == null)
110 throw new CmsException("Login context cannot be null");
111 // logout previous login context
112 // if (this.loginContext != null)
113 // try {
114 // this.loginContext.logout();
115 // } catch (LoginException e1) {
116 // System.err.println("Could not log out: " + e1);
117 // }
118 this.loginContext = loginContext;
119 }
120
121 @Override
122 public void logout() {
123 if (loginContext == null)
124 throw new CmsException("Login context should not bet null");
125 try {
126 CurrentUser.logoutCmsSession(loginContext.getSubject());
127 loginContext.logout();
128 } catch (LoginException e) {
129 throw new CmsException("Cannot log out", e);
130 }
131 }
132
133 @Override
134 public void exception(Throwable e) {
135 // TODO Auto-generated method stub
136
137 }
138
139 @Override
140 public CmsImageManager getImageManager() {
141 // TODO Auto-generated method stub
142 return null;
143 }
144
145 protected Subject getSubject() {
146 return subject;
147 }
148
149 @Override
150 public boolean isAnonymous() {
151 return CurrentUser.isAnonymous(getSubject());
152 }
153 }
154 }