1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.security; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.Map; |
21 | |
import java.util.Map.Entry; |
22 | |
|
23 | |
import org.kuali.student.common.rice.authorization.PermissionType; |
24 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
25 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
26 | |
import org.kuali.student.common.ui.client.service.SecurityRpcService; |
27 | |
import org.kuali.student.common.ui.client.service.SecurityRpcServiceAsync; |
28 | |
|
29 | |
import com.google.gwt.core.client.GWT; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public class SecurityContext { |
38 | |
|
39 | 0 | protected SecurityRpcServiceAsync securityRpcService = GWT.create(SecurityRpcService.class); |
40 | |
|
41 | 0 | private String principalName = ""; |
42 | |
|
43 | 0 | private Map <String,Boolean> permissionCache = new HashMap<String, Boolean>(); |
44 | |
|
45 | |
public void initializeSecurityContext(final Callback<Boolean> contextIntializedCallback){ |
46 | 0 | securityRpcService.getPrincipalUsername(new KSAsyncCallback<String>(){ |
47 | |
public void handleFailure(Throwable caught) { |
48 | 0 | permissionCache.clear(); |
49 | 0 | throw new RuntimeException("Fatal - Unable to initialze security context"); |
50 | |
} |
51 | |
|
52 | |
@Override |
53 | |
public void onSuccess(String userId) { |
54 | 0 | permissionCache.clear(); |
55 | 0 | setPrincipalName(userId); |
56 | 0 | contextIntializedCallback.exec(true); |
57 | 0 | } |
58 | |
}); |
59 | 0 | } |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public void setPrincipalName(String principalName) { |
67 | 0 | this.principalName = principalName; |
68 | 0 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public String getUserId() { |
74 | 0 | return principalName; |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public boolean checkPermissionCache(String permission){ |
83 | 0 | return permissionCache.containsKey(permission); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public void checkScreenPermission(final String screenComponent, final Callback<Boolean> checkPermissionCallback){ |
94 | 0 | if (permissionCache.containsKey(screenComponent)){ |
95 | 0 | checkPermissionCallback.exec(permissionCache.get(screenComponent)); |
96 | |
} else { |
97 | 0 | securityRpcService.hasScreenPermission(screenComponent, new KSAsyncCallback<Boolean>() { |
98 | |
@Override |
99 | |
public void onSuccess(Boolean result) { |
100 | 0 | permissionCache.put(screenComponent, result); |
101 | 0 | checkPermissionCallback.exec(result); |
102 | 0 | } |
103 | |
}); |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public void checkPermission(final String permissionName, final Callback<Boolean> checkPermissionCallback){ |
115 | 0 | if (permissionCache.containsKey(permissionName)){ |
116 | 0 | checkPermissionCallback.exec(permissionCache.get(permissionName)); |
117 | |
} else { |
118 | 0 | securityRpcService.hasPermissionByPermissionName(permissionName, new KSAsyncCallback<Boolean>() { |
119 | |
@Override |
120 | |
public void onSuccess(Boolean result) { |
121 | 0 | permissionCache.put(permissionName, result); |
122 | 0 | checkPermissionCallback.exec(result); |
123 | 0 | } |
124 | |
}); |
125 | |
} |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public void checkPermission(final String[] permissionNames, final Callback<Boolean> checkPermissionCallback){ |
135 | 0 | ArrayList<String> permissionsToGet = new ArrayList<String>(); |
136 | |
|
137 | |
|
138 | |
|
139 | 0 | for (String permissionName:permissionNames){ |
140 | 0 | if (permissionCache.containsKey(permissionName)){ |
141 | 0 | if (permissionCache.get(permissionName)){ |
142 | 0 | checkPermissionCallback.exec(permissionCache.get(true)); |
143 | 0 | break; |
144 | |
} |
145 | |
} else { |
146 | 0 | permissionsToGet.add(permissionName); |
147 | |
} |
148 | |
} |
149 | |
|
150 | |
|
151 | 0 | if (!permissionsToGet.isEmpty()){ |
152 | 0 | securityRpcService.getPermissions(permissionsToGet, new KSAsyncCallback<HashMap<String,Boolean>>() { |
153 | |
@Override |
154 | |
public void onSuccess(HashMap<String, Boolean> result) { |
155 | 0 | boolean hasAccess = false; |
156 | |
|
157 | 0 | for (Entry<String,Boolean> entry:result.entrySet()){ |
158 | 0 | permissionCache.put(entry.getKey(), entry.getValue()); |
159 | 0 | if (entry.getValue()){ |
160 | 0 | hasAccess = true; |
161 | |
} |
162 | |
} |
163 | |
|
164 | 0 | checkPermissionCallback.exec(hasAccess); |
165 | 0 | } |
166 | |
}); |
167 | |
} |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public void checkPermissionNoCache(final String permissionName, final Callback<Boolean> checkPermissionCallback){ |
177 | 0 | securityRpcService.hasScreenPermission(permissionName, new KSAsyncCallback<Boolean>() { |
178 | |
@Override |
179 | |
public void onSuccess(Boolean result) { |
180 | 0 | checkPermissionCallback.exec(result); |
181 | 0 | } |
182 | |
}); |
183 | |
|
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public void loadPermissionsByPermissionType(PermissionType permissionType){ |
190 | 0 | securityRpcService.getPermissionsByType(permissionType, new KSAsyncCallback<ArrayList<String>>(){ |
191 | |
|
192 | |
@Override |
193 | |
public void onSuccess(ArrayList<String> result) { |
194 | 0 | for (String permissionName:result){ |
195 | 0 | permissionCache.put(permissionName, true); |
196 | |
} |
197 | 0 | } |
198 | |
}); |
199 | 0 | } |
200 | |
} |