Subversion Repository Public Repository

litesoft

Diff Revisions 939 vs 948 for /trunk/Java/GWT/Server/src/org/litesoft/security/SecurityUserViewPoServerVoDataProvider.java

Diff revisions: vs.
  @@ -1,27 +1,26 @@
1 1 package org.litesoft.security;
2 2
3 - import org.litesoft.commonfoundation.exceptions.*;
4 - import org.litesoft.commonfoundation.typeutils.*;
5 -
6 - import java.util.*;
7 -
8 3 import org.litesoft.bo.views.*;
9 4 import org.litesoft.bo.views.communication.*;
10 5 import org.litesoft.bo.views.server.*;
11 - import org.litesoft.core.*;
6 + import org.litesoft.commonfoundation.exceptions.*;
12 7 import org.litesoft.commonfoundation.typeutils.Objects;
8 + import org.litesoft.commonfoundation.typeutils.*;
9 + import org.litesoft.core.*;
13 10 import org.litesoft.orsup.*;
14 11 import org.litesoft.orsup.base.*;
15 12 import org.litesoft.orsup.transact.*;
16 13 import org.litesoft.sql.*;
17 14
15 + import java.util.*;
16 +
18 17 @SuppressWarnings({"UnusedDeclaration"})
19 - public abstract class SecurityUserViewPoServerVoDataProvider<VO extends SecurityUserView<VO>, PO extends SecurityUser<PO>, RRPO extends SecurityRestrictedResource<RRPO>> extends PoServerVoDataProvider<VO>
20 - {
18 + public abstract class SecurityUserViewPoServerVoDataProvider<VO extends SecurityUserView<VO>, PO extends SecurityUser<PO>,
19 + RRPO extends SecurityRestrictedResource<RRPO>>
20 + extends PoServerVoDataProvider<VO> {
21 21 private String mDefaultUserLoginEmail = Strings.noEmpty( System.getProperty( "User" ) );
22 22
23 - protected String getDefaultUserLoginEmail()
24 - {
23 + protected String getDefaultUserLoginEmail() {
25 24 String zInitialUserEmail = mDefaultUserLoginEmail;
26 25 mDefaultUserLoginEmail = null;
27 26 return ((zInitialUserEmail != null) && (null == cursoryEmailCheck( zInitialUserEmail, SecurityUserViewNames.aLogonEmail ))) ? zInitialUserEmail : null;
  @@ -30,15 +29,13 @@
30 29 private final String[] mDemoUserEmails;
31 30 private final Set<String> mSetOfDemoUserEmails;
32 31
33 - protected SecurityUserViewPoServerVoDataProvider( VoMetaData<VO> pVOMetaData, MetaDataStore pMetaDataStore, String... pDemoUserEmails )
34 - {
32 + protected SecurityUserViewPoServerVoDataProvider( VoMetaData<VO> pVOMetaData, MetaDataStore pMetaDataStore, String... pDemoUserEmails ) {
35 33 super( pVOMetaData, pMetaDataStore );
36 34 mSetOfDemoUserEmails = new HashSet<String>( Arrays.asList( mDemoUserEmails = pDemoUserEmails ) );
37 35 }
38 36
39 37 @Override
40 - protected void LLinitialize()
41 - {
38 + protected void LLinitialize() {
42 39 super.LLinitialize();
43 40
44 41 register( new SecurityUserViewServerFunctionLogin<VO>() );
  @@ -51,49 +48,35 @@
51 48 register( new SecurityUserViewServerFunctionFetchCurrentLoginUser<VO>() );
52 49 }
53 50
54 - public SCresult changePassword( SecurityUserView pUserView )
55 - {
51 + public SCresult changePassword( SecurityUserView pUserView ) {
56 52 String zLogonEmail = pUserView.getLogonEmail();
57 53 String zCurrentPassword = pUserView.getCurrentPassword();
58 54 String zNewPassword = Strings.noEmpty( pUserView.getNewPassword() );
59 55 String zConfirmPassword = Strings.noEmpty( pUserView.getConfirmPassword() );
60 56
61 57 String zError = cursoryPasswordCheck( zCurrentPassword, SecurityUserViewNames.aCurrentPassword );
62 - if ( null == zError )
63 - {
64 - if ( null == (zError = cursoryPasswordCheck( zNewPassword, SecurityUserViewNames.aNewPassword )) )
65 - {
66 - if ( null == (zError = cursoryPasswordCheck( zConfirmPassword, SecurityUserViewNames.aConfirmPassword )) )
67 - {
68 - if ( !zNewPassword.equals( zConfirmPassword ) )
69 - {
58 + if ( null == zError ) {
59 + if ( null == (zError = cursoryPasswordCheck( zNewPassword, SecurityUserViewNames.aNewPassword )) ) {
60 + if ( null == (zError = cursoryPasswordCheck( zConfirmPassword, SecurityUserViewNames.aConfirmPassword )) ) {
61 + if ( !zNewPassword.equals( zConfirmPassword ) ) {
70 62 zError = "New & Confirm Passwords do not match";
71 - }
72 - else
73 - {
74 - while ( zError == null )
75 - {
63 + } else {
64 + while ( zError == null ) {
76 65 SecurityUser<PO> zUser = getUserByEmailFromCurrentDS( zLogonEmail );
77 - if ( (zUser == null) || !zUser.validatePassword( zCurrentPassword ) )
78 - {
66 + if ( (zUser == null) || !zUser.validatePassword( zCurrentPassword ) ) {
79 67 zError = "Invalid Credentials";
80 - }
81 - else if ( null == (zError = zUser.acceptablePassword( zNewPassword )) )
82 - {
68 + } else if ( null == (zError = zUser.acceptablePassword( zNewPassword )) ) {
83 69 Transaction zTransaction = DataStoreLocator.get().getUnfilteredFinder().createTransaction();
84 70 zUser = zUser.copyInto( zTransaction );
85 71 zUser.setPassword( zNewPassword );
86 - try
87 - {
72 + try {
88 73 zTransaction.commit();
89 74 return VOSC.result( createView( zUser, false ) );
90 75 }
91 - catch ( ConcurrentPOModificationException e )
92 - {
76 + catch ( ConcurrentPOModificationException e ) {
93 77 // Loop...
94 78 }
95 - catch ( RuntimeException e )
96 - {
79 + catch ( RuntimeException e ) {
97 80 LOGGER.warn.log( e );
98 81 zError = e.getMessage();
99 82 }
  @@ -106,46 +89,36 @@
106 89 return VOSC.result( zError );
107 90 }
108 91
109 - public String validatePassword( SecurityUserView pUserView )
110 - {
92 + public String validatePassword( SecurityUserView pUserView ) {
111 93 String zCurrentPassword = pUserView.getCurrentPassword();
112 94 String zError = cursoryPasswordCheck( zCurrentPassword, SecurityUserViewNames.aCurrentPassword );
113 - if ( null == zError )
114 - {
95 + if ( null == zError ) {
115 96 zError = "Invalid";
116 97 SecurityUser<PO> zUser = getUserByEmailFromCurrentDS( pUserView.getLogonEmail() );
117 - if ( (zUser != null) && zUser.validatePassword( zCurrentPassword ) )
118 - {
98 + if ( (zUser != null) && zUser.validatePassword( zCurrentPassword ) ) {
119 99 return null;
120 100 }
121 101 }
122 102 return zError;
123 103 }
124 104
125 - public SecurityUserView validateLogonEmailAvailable( String pLogonEmail )
126 - {
105 + public SecurityUserView validateLogonEmailAvailable( String pLogonEmail ) {
127 106 String zError = cursoryEmailCheck( pLogonEmail, SecurityUserViewNames.aLogonEmail );
128 - if ( zError != null )
129 - {
107 + if ( zError != null ) {
130 108 throw new DisplayableRuntimeException( zError );
131 109 }
132 110 SecurityUser<PO> zUser = getUserByEmailFromCurrentDS( pLogonEmail );
133 111 return createView( zUser, false );
134 112 }
135 113
136 - public SecurityUserView loginUser( String pLogonEmail, String pPassword )
137 - {
114 + public SecurityUserView loginUser( String pLogonEmail, String pPassword ) {
138 115 String zError = cursoryEmailCheck( pLogonEmail, SecurityUserViewNames.aLogonEmail );
139 - if ( null == zError )
140 - {
141 - if ( null == (zError = cursoryPasswordCheck( pPassword, SecurityUserViewNames.aCurrentPassword )) )
142 - {
116 + if ( null == zError ) {
117 + if ( null == (zError = cursoryPasswordCheck( pPassword, SecurityUserViewNames.aCurrentPassword )) ) {
143 118 zError = "Invalid Credentials";
144 119 SecurityUser<PO> zUser = getUserByEmailFromMasterDS( pLogonEmail );
145 - if ( zUser != null )
146 - {
147 - if ( zUser.validatePassword( pPassword ) )
148 - {
120 + if ( zUser != null ) {
121 + if ( zUser.validatePassword( pPassword ) ) {
149 122 return updateLoggedInUserFromMasterDS( zUser );
150 123 }
151 124 }
  @@ -154,18 +127,14 @@
154 127 throw new DisplayableRuntimeException( zError );
155 128 }
156 129
157 - public SecurityUserView loginDefaultUser( String pLogonEmail )
158 - {
159 - try
160 - {
130 + public SecurityUserView loginDefaultUser( String pLogonEmail ) {
131 + try {
161 132 SecurityUser<PO> zUser = getUserByEmailFromMasterDS( pLogonEmail );
162 - if ( zUser != null )
163 - {
133 + if ( zUser != null ) {
164 134 return updateLoggedInUserFromMasterDS( zUser );
165 135 }
166 136 }
167 - catch ( RuntimeException e )
168 - {
137 + catch ( RuntimeException e ) {
169 138 LOGGER.error.log( e );
170 139 }
171 140 return null;
  @@ -173,33 +142,25 @@
173 142
174 143 abstract public SecurityUserView switchRestrictedResources( SecurityRestrictedResourceView pRestrictedResourceView );
175 144
176 - private SecurityUserView updateLoggedInUserFromMasterDS( SecurityUser<PO> pUser )
177 - {
178 - if ( !pUser.isDemo() )
179 - {
145 + private SecurityUserView updateLoggedInUserFromMasterDS( SecurityUser<PO> pUser ) {
146 + if ( !pUser.isDemo() ) {
180 147 initializeRealDB();
181 - }
182 - else
183 - {
148 + } else {
184 149 initializeDemoDB();
185 150 Objects.assertNotNull( "Demo User Refreshed", pUser = getUserByEmailFromCurrentDS( pUser.getLogonEmail() ) );
186 151 }
187 152 return finiUserLogin( pUser );
188 153 }
189 154
190 - public SecurityUserView nextDemoUserView()
191 - {
155 + public SecurityUserView nextDemoUserView() {
192 156 initializeDemoDB();
193 157 int zNextIndex = findCurrentDemoIndex( currentLoggedInUserView() ) + 1;
194 - for ( int i = 2 + mDemoUserEmails.length; --i > 0; zNextIndex++ )
195 - {
196 - if ( mDemoUserEmails.length <= zNextIndex )
197 - {
158 + for ( int i = 2 + mDemoUserEmails.length; --i > 0; zNextIndex++ ) {
159 + if ( mDemoUserEmails.length <= zNextIndex ) {
198 160 zNextIndex = 0;
199 161 }
200 162 SecurityUser<PO> zUser = getUserByEmailFromCurrentDS( mDemoUserEmails[zNextIndex] );
201 - if ( zUser != null )
202 - {
163 + if ( zUser != null ) {
203 164 zUser.setDemo();
204 165 return finiUserLogin( zUser );
205 166 }
  @@ -207,18 +168,15 @@
207 168 throw new IllegalStateException( "No Demo Users!" );
208 169 }
209 170
210 - private void initializeRealDB()
211 - {
171 + private void initializeRealDB() {
212 172 ServerContext.get().getServerStore().set( DataStore.class, ServerContext.getMasterServerStore().get( DataStore.class ) ); // Master DS
213 173 }
214 174
215 - private void initializeDemoDB()
216 - {
175 + private void initializeDemoDB() {
217 176 ServerContext zContext = ServerContext.get();
218 177 DemoDataStoreManager.Struct zDemoDS = DemoDataStoreManager.getDemoDS( zContext.getContextID() ); // Demo DS
219 178 zContext.getServerStore().set( DataStore.class, zDemoDS.getDataStore() );
220 - if ( zDemoDS.isEmptyDemo() )
221 - {
179 + if ( zDemoDS.isEmptyDemo() ) {
222 180 populateDemoDS();
223 181 }
224 182 }
  @@ -229,50 +187,40 @@
229 187
230 188 abstract protected SecurityUserView finiUserLogin( SecurityUser<PO> pUser );
231 189
232 - private SecurityUser<PO> getUserByEmailFromMasterDS( String pLogonEmail )
233 - {
190 + private SecurityUser<PO> getUserByEmailFromMasterDS( String pLogonEmail ) {
234 191 return getUserByEmail( ServerContext.getMasterServerStore().get( DataStore.class ).getUnfilteredFinder(), pLogonEmail );
235 192 }
236 193
237 - private SecurityUser<PO> getUserByEmailFromCurrentDS( String pLogonEmail )
238 - {
194 + private SecurityUser<PO> getUserByEmailFromCurrentDS( String pLogonEmail ) {
239 195 return getUserByEmail( DataStoreLocator.get().getUnfilteredFinder(), pLogonEmail );
240 196 }
241 197
242 - private SecurityUser<PO> getUserByEmail( Finder pFinder, String pLogonEmail )
243 - {
198 + private SecurityUser<PO> getUserByEmail( Finder pFinder, String pLogonEmail ) {
244 199 SecurityUser<PO> zUser = findUserByEmail( pFinder, pLogonEmail );
245 - if ( isUserDemo( zUser ) )
246 - {
200 + if ( isUserDemo( zUser ) ) {
247 201 zUser.setDemo();
248 202 }
249 203 return zUser;
250 204 }
251 205
252 - private boolean isUserDemo( SecurityUser<PO> pUser )
253 - {
206 + private boolean isUserDemo( SecurityUser<PO> pUser ) {
254 207 return ((pUser != null) && mSetOfDemoUserEmails.contains( Strings.deNull( pUser.getLogonEmail() ).toLowerCase() ));
255 208 }
256 209
257 - private int findCurrentDemoIndex( SecurityUserView pUser )
258 - {
259 - if ( pUser == null )
260 - {
210 + private int findCurrentDemoIndex( SecurityUserView pUser ) {
211 + if ( pUser == null ) {
261 212 return 0;
262 213 }
263 214 int i = mDemoUserEmails.length;
264 - while ( --i >= 0 )
265 - {
266 - if ( mDemoUserEmails[i].equalsIgnoreCase( pUser.getLogonEmail() ) )
267 - {
215 + while ( --i >= 0 ) {
216 + if ( mDemoUserEmails[i].equalsIgnoreCase( pUser.getLogonEmail() ) ) {
268 217 return i;
269 218 }
270 219 }
271 220 return i;
272 221 }
273 222
274 - private void populateDemoDS()
275 - {
223 + private void populateDemoDS() {
276 224 DataLoader.INSTANCE.loadDemoData();
277 225 }
278 226