Subversion Repository Public Repository

litesoft

Diff Revisions 495 vs 496 for /trunk/Java/DATT/src/org/litesoft/datt/server/boviews/podataproviders/UserViewPoServerVoDataProvider.java

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