Subversion Repository Public Repository

litesoft

Diff Revisions 952 vs 953 for /trunk/GWT_Sandbox/InitFrom/src/org/litesoft/initfrom/server/boviews/podataproviders/UserViewPoServerVoDataProvider.java

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