|
@@ -1,355 +1,20 @@ |
1 |
1 |
|
package org.litesoft.prioritizer.server.boviews.podataproviders; |
2 |
2 |
|
|
3 |
|
- |
import java.util.*; |
4 |
|
- |
|
5 |
|
- |
import org.litesoft.bo.views.*; |
6 |
|
- |
import org.litesoft.bo.views.communication.*; |
7 |
|
- |
import org.litesoft.bo.views.server.*; |
8 |
|
- |
import org.litesoft.core.*; |
9 |
|
- |
import org.litesoft.exceptions.*; |
10 |
|
- |
import org.litesoft.orsup.*; |
11 |
3 |
|
import org.litesoft.orsup.base.*; |
12 |
|
- |
import org.litesoft.orsup.selection.*; |
13 |
|
- |
import org.litesoft.orsup.transact.*; |
14 |
4 |
|
import org.litesoft.prioritizer.client.boviews.*; |
15 |
|
- |
import org.litesoft.prioritizer.server.*; |
16 |
|
- |
import org.litesoft.prioritizer.server.pos.*; |
17 |
|
- |
import org.litesoft.sql.*; |
18 |
|
- |
import org.litesoft.util.*; |
19 |
5 |
|
|
20 |
|
- |
public class UserViewPoServerVoDataProvider extends PoServerVoDataProvider<UserView> implements UserViewNames |
|
6 |
+ |
public class UserViewPoServerVoDataProvider extends SecurityUserViewPoServerVoDataProvider<UserView> |
21 |
7 |
|
{ |
22 |
|
- |
private String mDefaultUserLoginEmail = Utils.noEmpty( System.getProperty( "User" ) ); |
23 |
|
- |
|
24 |
|
- |
private String getDefaultUserLoginEmail() |
25 |
|
- |
{ |
26 |
|
- |
String zInitialUserEmail = mDefaultUserLoginEmail; |
27 |
|
- |
mDefaultUserLoginEmail = null; |
28 |
|
- |
return ((zInitialUserEmail != null) && (null == cursoryEmailCheck( zInitialUserEmail, aLogonEmail ))) ? zInitialUserEmail : null; |
29 |
|
- |
} |
30 |
|
- |
|
31 |
8 |
|
public UserViewPoServerVoDataProvider( MetaDataStore pMetaDataStore ) |
32 |
9 |
|
{ |
33 |
|
- |
super( UserViewMetaData.getInstance(), pMetaDataStore ); |
34 |
|
- |
} |
35 |
|
- |
|
36 |
|
- |
@Override |
37 |
|
- |
protected void LLinitialize() |
38 |
|
- |
{ |
39 |
|
- |
super.LLinitialize(); |
40 |
|
- |
|
41 |
|
- |
register( new UserViewServerFunctionLogin() ); |
42 |
|
- |
register( new UserViewServerFunctionLogout() ); |
43 |
|
- |
register( new UserViewServerFunctionSwitchRestrictedResources() ); |
44 |
|
- |
register( new UserViewServerFunctionChangePassword() ); |
45 |
|
- |
register( new UserViewServerFunctionValidatePassword() ); |
46 |
|
- |
register( new UserViewServerFunctionValidateLogonEmailAvailable() ); |
47 |
|
- |
register( new UserViewServerFunctionNextDemoLoginUser() ); |
48 |
|
- |
register( new UserViewServerFunctionFetchCurrentLoginUser() ); |
49 |
|
- |
} |
50 |
|
- |
|
51 |
|
- |
public SCresult changePassword( UserView pUserView ) |
52 |
|
- |
{ |
53 |
|
- |
String zLogonEmail = pUserView.getLogonEmail(); |
54 |
|
- |
String zCurrentPassword = pUserView.getCurrentPassword(); |
55 |
|
- |
String zNewPassword = Utils.noEmpty( pUserView.getNewPassword() ); |
56 |
|
- |
String zConfirmPassword = Utils.noEmpty( pUserView.getConfirmPassword() ); |
57 |
|
- |
|
58 |
|
- |
String zError = cursoryPasswordCheck( zCurrentPassword, aCurrentPassword ); |
59 |
|
- |
if ( null == zError ) |
60 |
|
- |
{ |
61 |
|
- |
if ( null == (zError = cursoryPasswordCheck( zNewPassword, aNewPassword )) ) |
62 |
|
- |
{ |
63 |
|
- |
if ( null == (zError = cursoryPasswordCheck( zConfirmPassword, aConfirmPassword )) ) |
64 |
|
- |
{ |
65 |
|
- |
if ( !zNewPassword.equals( zConfirmPassword ) ) |
66 |
|
- |
{ |
67 |
|
- |
zError = "New & Confirm Passwords do not match"; |
68 |
|
- |
} |
69 |
|
- |
else |
70 |
|
- |
{ |
71 |
|
- |
while ( zError == null ) |
72 |
|
- |
{ |
73 |
|
- |
User zUser = getUserByEmailFromCurrentDS( zLogonEmail ); |
74 |
|
- |
if ( (zUser == null) || !zUser.validatePassword( zCurrentPassword ) ) |
75 |
|
- |
{ |
76 |
|
- |
zError = "Invalid Credentials"; |
77 |
|
- |
} |
78 |
|
- |
else if ( null == (zError = zUser.acceptablePassword( zNewPassword )) ) |
79 |
|
- |
{ |
80 |
|
- |
Transaction zTransaction = DataStoreLocator.get().getUnfilteredFinder().createTransaction(); |
81 |
|
- |
zUser = zUser.copyInto( zTransaction ); |
82 |
|
- |
zUser.setPassword( zNewPassword ); |
83 |
|
- |
try |
84 |
|
- |
{ |
85 |
|
- |
zTransaction.commit(); |
86 |
|
- |
return VOSC.result( createView( zUser, false ) ); |
87 |
|
- |
} |
88 |
|
- |
catch ( ConcurrentPOModificationException e ) |
89 |
|
- |
{ |
90 |
|
- |
// Loop... |
91 |
|
- |
} |
92 |
|
- |
catch ( RuntimeException e ) |
93 |
|
- |
{ |
94 |
|
- |
LOGGER.warn.log( e ); |
95 |
|
- |
zError = e.getMessage(); |
96 |
|
- |
} |
97 |
|
- |
} |
98 |
|
- |
} |
99 |
|
- |
} |
100 |
|
- |
} |
101 |
|
- |
} |
102 |
|
- |
} |
103 |
|
- |
return VOSC.result( zError ); |
104 |
|
- |
} |
105 |
|
- |
|
106 |
|
- |
public String validatePassword( UserView pUserView ) |
107 |
|
- |
{ |
108 |
|
- |
String zCurrentPassword = pUserView.getCurrentPassword(); |
109 |
|
- |
String zError = cursoryPasswordCheck( zCurrentPassword, aCurrentPassword ); |
110 |
|
- |
if ( null == zError ) |
111 |
|
- |
{ |
112 |
|
- |
zError = "Invalid"; |
113 |
|
- |
User zUser = getUserByEmailFromCurrentDS( pUserView.getLogonEmail() ); |
114 |
|
- |
if ( (zUser != null) && zUser.validatePassword( zCurrentPassword ) ) |
115 |
|
- |
{ |
116 |
|
- |
return null; |
117 |
|
- |
} |
118 |
|
- |
} |
119 |
|
- |
return zError; |
120 |
|
- |
} |
121 |
|
- |
|
122 |
|
- |
public UserView validateLogonEmailAvailable( String pLogonEmail ) |
123 |
|
- |
{ |
124 |
|
- |
String zError = cursoryEmailCheck( pLogonEmail, aLogonEmail ); |
125 |
|
- |
if ( zError != null ) |
126 |
|
- |
{ |
127 |
|
- |
throw new DisplayableRuntimeException( zError ); |
128 |
|
- |
} |
129 |
|
- |
User zUser = getUserByEmailFromCurrentDS( pLogonEmail ); |
130 |
|
- |
return createView( zUser, false ); |
131 |
|
- |
} |
132 |
|
- |
|
133 |
|
- |
public UserView loginUser( String pLogonEmail, String pPassword ) |
134 |
|
- |
{ |
135 |
|
- |
String zError = cursoryEmailCheck( pLogonEmail, aLogonEmail ); |
136 |
|
- |
if ( null == zError ) |
137 |
|
- |
{ |
138 |
|
- |
if ( null == (zError = cursoryPasswordCheck( pPassword, aCurrentPassword )) ) |
139 |
|
- |
{ |
140 |
|
- |
zError = "Invalid Credentials"; |
141 |
|
- |
User zUser = getUserByEmailFromMasterDS( pLogonEmail ); |
142 |
|
- |
if ( zUser != null ) |
143 |
|
- |
{ |
144 |
|
- |
if ( zUser.validatePassword( pPassword ) ) |
145 |
|
- |
{ |
146 |
|
- |
return updateLoggedInUserFromMasterDS( zUser ); |
147 |
|
- |
} |
148 |
|
- |
} |
149 |
|
- |
} |
150 |
|
- |
} |
151 |
|
- |
throw new DisplayableRuntimeException( zError ); |
152 |
|
- |
} |
153 |
|
- |
|
154 |
|
- |
public UserView loginDefaultUser( String pLogonEmail ) |
155 |
|
- |
{ |
156 |
|
- |
try |
157 |
|
- |
{ |
158 |
|
- |
User zUser = getUserByEmailFromMasterDS( pLogonEmail ); |
159 |
|
- |
if ( zUser != null ) |
160 |
|
- |
{ |
161 |
|
- |
return updateLoggedInUserFromMasterDS( zUser ); |
162 |
|
- |
} |
163 |
|
- |
} |
164 |
|
- |
catch ( RuntimeException e ) |
165 |
|
- |
{ |
166 |
|
- |
LOGGER.error.log( e ); |
167 |
|
- |
} |
168 |
|
- |
return null; |
169 |
|
- |
} |
170 |
|
- |
|
171 |
|
- |
public UserView switchRestrictedResources( RestrictedResourceView pRestrictedResourceView ) |
172 |
|
- |
{ |
173 |
|
- |
User zUser = CurrentUserAccessor.get().getRequiredUser(); |
174 |
|
- |
RestrictedResource zRestrictedResource = (RestrictedResource) getNotUsDataProvider( RestrictedResourceView.class ).getExistingPO( DataStoreLocator.get().getUnfilteredFinder(), pRestrictedResourceView ); |
175 |
|
- |
if ( zRestrictedResource == null ) |
176 |
|
- |
{ |
177 |
|
- |
throw new DisplayableRuntimeException( "RestrictedResourceNotFound", pRestrictedResourceView.toString() ); |
178 |
|
- |
} |
179 |
|
- |
UserRestrictedResourcePair zPair = zUser.loginToRestrictedResourceIfAcceptable( zRestrictedResource ); |
180 |
|
- |
return finiUserLogin( zPair.getUser(), zPair.getRestrictedResource() ); |
181 |
|
- |
} |
182 |
|
- |
|
183 |
|
- |
private UserView updateLoggedInUserFromMasterDS( User pUser ) |
184 |
|
- |
{ |
185 |
|
- |
|
186 |
|
- |
if ( !pUser.isDemo() ) |
187 |
|
- |
{ |
188 |
|
- |
initializeRealDB(); |
189 |
|
- |
} |
190 |
|
- |
else |
191 |
|
- |
{ |
192 |
|
- |
initializeDemoDB(); |
193 |
|
- |
Utils.assertNotNull( "Demo User Refreshed", pUser = getUserByEmailFromCurrentDS( pUser.getLogonEmail() ) ); |
194 |
|
- |
} |
195 |
|
- |
return finiUserLogin( pUser ); |
196 |
|
- |
} |
197 |
|
- |
|
198 |
|
- |
public UserView nextDemoUserView() |
199 |
|
- |
{ |
200 |
|
- |
initializeDemoDB(); |
201 |
|
- |
int zNextIndex = findCurrentDemoIndex( currentLoggedInUserView() ) + 1; |
202 |
|
- |
for ( int i = 2 + DEMO_USERS_EMAIL_LIST.length; --i > 0; zNextIndex++ ) |
203 |
|
- |
{ |
204 |
|
- |
if ( DEMO_USERS_EMAIL_LIST.length <= zNextIndex ) |
205 |
|
- |
{ |
206 |
|
- |
zNextIndex = 0; |
207 |
|
- |
} |
208 |
|
- |
User zUser = getUserByEmailFromCurrentDS( DEMO_USERS_EMAIL_LIST[zNextIndex] ); |
209 |
|
- |
if ( zUser != null ) |
210 |
|
- |
{ |
211 |
|
- |
zUser.setDemo(); |
212 |
|
- |
return finiUserLogin( zUser ); |
213 |
|
- |
} |
214 |
|
- |
} |
215 |
|
- |
throw new IllegalStateException( "No Demo Users!" ); |
216 |
|
- |
} |
217 |
|
- |
|
218 |
|
- |
private void initializeRealDB() |
219 |
|
- |
{ |
220 |
|
- |
ServerContext.get().getServerStore().set( DataStore.class, ServerContext.getMasterServerStore().get( DataStore.class ) ); // Master DS |
221 |
|
- |
} |
222 |
|
- |
|
223 |
|
- |
private void initializeDemoDB() |
224 |
|
- |
{ |
225 |
|
- |
ServerContext zContext = ServerContext.get(); |
226 |
|
- |
DemoDataStoreManager.Struct zDemoDS = DemoDataStoreManager.getDemoDS( zContext.getContextID() ); // Demo DS |
227 |
|
- |
zContext.getServerStore().set( DataStore.class, zDemoDS.getDataStore() ); |
228 |
|
- |
if ( zDemoDS.isEmptyDemo() ) |
229 |
|
- |
{ |
230 |
|
- |
populateDemoDS(); |
231 |
|
- |
} |
232 |
|
- |
} |
233 |
|
- |
|
234 |
|
- |
public UserView currentLoggedInUserView() |
235 |
|
- |
{ |
236 |
|
- |
return CurrentUserAccessor.get().getUserView( getDefaultUserLoginEmail() ); |
237 |
|
- |
} |
238 |
|
- |
|
239 |
|
- |
public void logoutUser() |
240 |
|
- |
{ |
241 |
|
- |
CurrentUserAccessor.get().logout(); |
242 |
|
- |
CurrentRestrictedResourceAccessor.get().logout(); |
243 |
|
- |
} |
244 |
|
- |
|
245 |
|
- |
private UserView finiUserLogin( User pUser ) |
246 |
|
- |
{ |
247 |
|
- |
return finiUserLogin( pUser, CurrentRestrictedResourceAccessor.get().getOptionalRestrictedResource() ); |
248 |
|
- |
} |
249 |
|
- |
|
250 |
|
- |
private UserView finiUserLogin( User pUser, RestrictedResource pRestrictedResource ) |
251 |
|
- |
{ |
252 |
|
- |
UserRestrictedResourcePair zUserRestrictedResourcePair = pUser.getLoginRestrictedResource( pRestrictedResource ); |
253 |
|
- |
User zUser = zUserRestrictedResourcePair.getUser(); |
254 |
|
- |
RestrictedResource zRestrictedResource = zUserRestrictedResourcePair.getRestrictedResource(); |
255 |
|
- |
CurrentRestrictedResourceAccessor zRestrictedResourceAccessor = CurrentRestrictedResourceAccessor.get(); |
256 |
|
- |
zRestrictedResourceAccessor.updateRestrictedResource( zRestrictedResource ); |
257 |
|
- |
if ( !zRestrictedResource.equals( zUser.getLastRestrictedResource() ) ) |
258 |
|
- |
{ |
259 |
|
- |
Transaction zTransaction = zUser.getFinder().createTransaction(); |
260 |
|
- |
zUser = zUser.copyInto( zTransaction ); |
261 |
|
- |
zUser.setLastRestrictedResource( zRestrictedResource ); |
262 |
|
- |
zTransaction.commit(); |
263 |
|
- |
} |
264 |
|
- |
zRestrictedResourceAccessor.updateRestrictedResource( zRestrictedResource ); |
265 |
|
- |
UserView zUserView = CurrentUserAccessor.get().updateUser( zUser ); |
266 |
|
- |
zUserView.setCurrentRestrictedResource( zRestrictedResourceAccessor.getRestrictedResourceView() ); |
267 |
|
- |
if ( !zUser.canAccessAllRestrictedResources() ) |
268 |
|
- |
{ |
269 |
|
- |
zUserView.setAlternateRestrictedResourceOptions( convertToRestrictedResourceOptions( zUser.getAlternateRestrictedResourceOptions( zRestrictedResource ) ) ); |
270 |
|
- |
} |
271 |
|
- |
return zUserView; |
272 |
|
- |
} |
273 |
|
- |
|
274 |
|
- |
private User getUserByEmailFromMasterDS( String pLogonEmail ) |
275 |
|
- |
{ |
276 |
|
- |
return getUserByEmail( ServerContext.getMasterServerStore().get( DataStore.class ).getUnfilteredFinder(), pLogonEmail ); |
277 |
|
- |
} |
278 |
|
- |
|
279 |
|
- |
private User getUserByEmailFromCurrentDS( String pLogonEmail ) |
280 |
|
- |
{ |
281 |
|
- |
return getUserByEmail( DataStoreLocator.get().getUnfilteredFinder(), pLogonEmail ); |
282 |
|
- |
} |
283 |
|
- |
|
284 |
|
- |
private User getUserByEmail( Finder pFinder, String pLogonEmail ) |
285 |
|
- |
{ |
286 |
|
- |
User zUser = pFinder.findOne( User.class, WCF.isEqual( User.CD_LogonEmail, pLogonEmail ) ); |
287 |
|
- |
if ( isUserDemo( zUser ) ) |
288 |
|
- |
{ |
289 |
|
- |
zUser.setDemo(); |
290 |
|
- |
} |
291 |
|
- |
return zUser; |
292 |
|
- |
} |
293 |
|
- |
|
294 |
|
- |
private AlternateRestrictedResourceOption[] convertToRestrictedResourceOptions( RestrictedResource[] pRestrictedResources ) |
295 |
|
- |
{ |
296 |
|
- |
AlternateRestrictedResourceOption[] rv = new AlternateRestrictedResourceOption[pRestrictedResources.length]; |
297 |
|
- |
PoServerVoDataProvider<RestrictedResourceView> zRestrictedResourceViewDP = getNotUsDataProvider( RestrictedResourceView.class ); |
298 |
|
- |
for ( int i = 0; i < pRestrictedResources.length; i++ ) |
299 |
|
- |
{ |
300 |
|
- |
rv[i] = zRestrictedResourceViewDP.createView( pRestrictedResources[i], false ); |
301 |
|
- |
} |
302 |
|
- |
Arrays.sort( rv ); |
303 |
|
- |
return rv; |
304 |
|
- |
} |
305 |
|
- |
|
306 |
|
- |
@Override |
307 |
|
- |
protected WhereClause augment_getMatchingVOs( WhereClause pWhereClause ) |
308 |
|
- |
{ |
309 |
|
- |
User zUser = CurrentUserAccessor.get().getRequiredUser(); |
310 |
|
- |
if ( !zUser.canAccessAllRestrictedResources() ) // If no user it will blow up with a null pointer exception which is appropriate. |
311 |
|
- |
{ |
312 |
|
- |
pWhereClause = WCF.and( pWhereClause, User.getLimitToCurrentRestrictedResourceAndVisible() ); // Add filter to where clause to only select based on RestrictedResource. |
313 |
|
- |
} |
314 |
|
- |
return super.augment_getMatchingVOs( pWhereClause ); |
315 |
|
- |
} |
316 |
|
- |
|
317 |
|
- |
private boolean isUserDemo( User pUser ) |
318 |
|
- |
{ |
319 |
|
- |
return ((pUser != null) && DEMO_USERS_EMAIL_SET.contains( Utils.deNull( pUser.getLogonEmail() ).toLowerCase() )); |
320 |
|
- |
} |
321 |
|
- |
|
322 |
|
- |
private int findCurrentDemoIndex( UserView pUser ) |
323 |
|
- |
{ |
324 |
|
- |
if ( pUser == null ) |
325 |
|
- |
{ |
326 |
|
- |
return 0; |
327 |
|
- |
} |
328 |
|
- |
int i = DEMO_USERS_EMAIL_LIST.length; |
329 |
|
- |
while ( --i >= 0 ) |
330 |
|
- |
{ |
331 |
|
- |
if ( DEMO_USERS_EMAIL_LIST[i].equalsIgnoreCase( pUser.getLogonEmail() ) ) |
332 |
|
- |
{ |
333 |
|
- |
return i; |
334 |
|
- |
} |
335 |
|
- |
} |
336 |
|
- |
return i; |
337 |
|
- |
} |
338 |
|
- |
|
339 |
|
- |
private static final String[] DEMO_USERS_EMAIL_LIST = new String[] // |
340 |
|
- |
{ // |
341 |
|
- |
"dm@richasp.com", // ........... Dee Man |
342 |
|
- |
"hdeskman@richasp.com", // ..... Helpy Deskman |
343 |
|
- |
"slick@richasp.com", // ........ Slick Salesman |
344 |
|
- |
// |
345 |
|
- |
"akern@testsite.com", // ....... Anthony Kern |
346 |
|
- |
"dsteele@testsite.com", // ..... Donald Steele |
347 |
|
- |
"saly@testsite.com", // ........ Sally Operator |
348 |
|
- |
}; |
349 |
|
- |
private static final Set<String> DEMO_USERS_EMAIL_SET = new HashSet<String>( Arrays.asList( DEMO_USERS_EMAIL_LIST ) ); |
350 |
|
- |
|
351 |
|
- |
private void populateDemoDS() |
352 |
|
- |
{ |
353 |
|
- |
DataLoader.INSTANCE.loadDemoData(); |
|
10 |
+ |
super( UserViewMetaData.getInstance(), pMetaDataStore, // |
|
11 |
+ |
"dm@richasp.com", // ........... Dee Man |
|
12 |
+ |
"hdeskman@richasp.com", // ..... Helpy Deskman |
|
13 |
+ |
"slick@richasp.com", // ........ Slick Salesman |
|
14 |
+ |
// |
|
15 |
+ |
"akern@testsite.com", // ....... Anthony Kern |
|
16 |
+ |
"dsteele@testsite.com", // ..... Donald Steele |
|
17 |
+ |
"saly@testsite.com" // ........ Sally Operator |
|
18 |
+ |
); |
354 |
19 |
|
} |
355 |
20 |
|
} |