Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package org.litesoft.security;

import org.litesoft.bo.views.*;
import org.litesoft.bo.views.communication.*;
import org.litesoft.bo.views.server.*;
import org.litesoft.commonfoundation.base.*;
import org.litesoft.commonfoundation.exceptions.*;
import org.litesoft.core.*;
import org.litesoft.orsup.*;
import org.litesoft.orsup.base.*;
import org.litesoft.orsup.transact.*;
import org.litesoft.sql.*;

import java.util.*;

@SuppressWarnings({"UnusedDeclaration"})
public abstract class SecurityUserViewPoServerVoDataProvider<VO extends SecurityUserView<VO>, PO extends SecurityUser<PO>,
        RRPO extends SecurityRestrictedResource<RRPO>>
        extends PoServerVoDataProvider<VO> {
    private String mDefaultUserLoginEmail = ConstrainTo.significantOrNull( System.getProperty( "User" ) );

    protected String getDefaultUserLoginEmail() {
        String zInitialUserEmail = mDefaultUserLoginEmail;
        mDefaultUserLoginEmail = null;
        return ((zInitialUserEmail != null) && (null == cursoryEmailCheck( zInitialUserEmail, SecurityUserViewNames.aLogonEmail ))) ? zInitialUserEmail : null;
    }

    private final String[] mDemoUserEmails;
    private final Set<String> mSetOfDemoUserEmails;

    protected SecurityUserViewPoServerVoDataProvider( VoMetaData<VO> pVOMetaData, MetaDataStore pMetaDataStore, String... pDemoUserEmails ) {
        super( pVOMetaData, pMetaDataStore );
        mSetOfDemoUserEmails = new HashSet<String>( Arrays.asList( mDemoUserEmails = pDemoUserEmails ) );
    }

    @Override
    protected void LLinitialize() {
        super.LLinitialize();

        register( new SecurityUserViewServerFunctionLogin<VO>() );
        register( new SecurityUserViewServerFunctionLogout<VO>() );
        register( new SecurityUserViewServerFunctionSwitchRestrictedResources<VO>() );
        register( new SecurityUserViewServerFunctionChangePassword<VO>() );
        register( new SecurityUserViewServerFunctionValidatePassword<VO>() );
        register( new SecurityUserViewServerFunctionValidateLogonEmailAvailable<VO>() );
        register( new SecurityUserViewServerFunctionNextDemoLoginUser<VO>() );
        register( new SecurityUserViewServerFunctionFetchCurrentLoginUser<VO>() );
    }

    public SCresult changePassword( SecurityUserView pUserView ) {
        String zLogonEmail = pUserView.getLogonEmail();
        String zCurrentPassword = pUserView.getCurrentPassword();
        String zNewPassword = ConstrainTo.significantOrNull( pUserView.getNewPassword() );
        String zConfirmPassword = ConstrainTo.significantOrNull( pUserView.getConfirmPassword() );

        String zError = cursoryPasswordCheck( zCurrentPassword, SecurityUserViewNames.aCurrentPassword );
        if ( null == zError ) {
            if ( null == (zError = cursoryPasswordCheck( zNewPassword, SecurityUserViewNames.aNewPassword )) ) {
                if ( null == (zError = cursoryPasswordCheck( zConfirmPassword, SecurityUserViewNames.aConfirmPassword )) ) {
                    if ( !zNewPassword.equals( zConfirmPassword ) ) {
                        zError = "New & Confirm Passwords do not match";
                    } else {
                        while ( zError == null ) {
                            SecurityUser<PO> zUser = getUserByEmailFromCurrentDS( zLogonEmail );
                            if ( (zUser == null) || !zUser.validatePassword( zCurrentPassword ) ) {
                                zError = "Invalid Credentials";
                            } else if ( null == (zError = zUser.acceptablePassword( zNewPassword )) ) {
                                Transaction zTransaction = DataStoreLocator.get().getUnfilteredFinder().createTransaction();
                                zUser = zUser.copyInto( zTransaction );
                                zUser.setPassword( zNewPassword );
                                try {
                                    zTransaction.commit();
                                    return VOSC.result( createView( zUser, false ) );
                                }
                                catch ( ConcurrentPOModificationException e ) {
                                    // Loop...
                                }
                                catch ( RuntimeException e ) {
                                    LOGGER.warn.log( e );
                                    zError = e.getMessage();
                                }
                            }
                        }
                    }
                }
            }
        }
        return VOSC.result( zError );
    }

    public String validatePassword( SecurityUserView pUserView ) {
        String zCurrentPassword = pUserView.getCurrentPassword();
        String zError = cursoryPasswordCheck( zCurrentPassword, SecurityUserViewNames.aCurrentPassword );
        if ( null == zError ) {
            zError = "Invalid";
            SecurityUser<PO> zUser = getUserByEmailFromCurrentDS( pUserView.getLogonEmail() );
            if ( (zUser != null) && zUser.validatePassword( zCurrentPassword ) ) {
                return null;
            }
        }
        return zError;
    }

    public SecurityUserView validateLogonEmailAvailable( String pLogonEmail ) {
        String zError = cursoryEmailCheck( pLogonEmail, SecurityUserViewNames.aLogonEmail );
        if ( zError != null ) {
            throw new DisplayableRuntimeException( zError );
        }
        SecurityUser<PO> zUser = getUserByEmailFromCurrentDS( pLogonEmail );
        return createView( zUser, false );
    }

    public SecurityUserView loginUser( String pLogonEmail, String pPassword ) {
        String zError = cursoryEmailCheck( pLogonEmail, SecurityUserViewNames.aLogonEmail );
        if ( null == zError ) {
            if ( null == (zError = cursoryPasswordCheck( pPassword, SecurityUserViewNames.aCurrentPassword )) ) {
                zError = "Invalid Credentials";
                SecurityUser<PO> zUser = getUserByEmailFromMasterDS( pLogonEmail );
                if ( zUser != null ) {
                    if ( zUser.validatePassword( pPassword ) ) {
                        return updateLoggedInUserFromMasterDS( zUser );
                    }
                }
            }
        }
        throw new DisplayableRuntimeException( zError );
    }

    public SecurityUserView loginDefaultUser( String pLogonEmail ) {
        try {
            SecurityUser<PO> zUser = getUserByEmailFromMasterDS( pLogonEmail );
            if ( zUser != null ) {
                return updateLoggedInUserFromMasterDS( zUser );
            }
        }
        catch ( RuntimeException e ) {
            LOGGER.error.log( e );
        }
        return null;
    }

    abstract public SecurityUserView switchRestrictedResources( SecurityRestrictedResourceView pRestrictedResourceView );

    private SecurityUserView updateLoggedInUserFromMasterDS( SecurityUser<PO> pUser ) {
        if ( !pUser.isDemo() ) {
            initializeRealDB();
        } else {
            initializeDemoDB();
            Confirm.isNotNull( "Demo User Refreshed", pUser = getUserByEmailFromCurrentDS( pUser.getLogonEmail() ) );
        }
        return finiUserLogin( pUser );
    }

    public SecurityUserView nextDemoUserView() {
        initializeDemoDB();
        int zNextIndex = findCurrentDemoIndex( currentLoggedInUserView() ) + 1;
        for ( int i = 2 + mDemoUserEmails.length; --i > 0; zNextIndex++ ) {
            if ( mDemoUserEmails.length <= zNextIndex ) {
                zNextIndex = 0;
            }
            SecurityUser<PO> zUser = getUserByEmailFromCurrentDS( mDemoUserEmails[zNextIndex] );
            if ( zUser != null ) {
                zUser.setDemo();
                return finiUserLogin( zUser );
            }
        }
        throw new IllegalStateException( "No Demo Users!" );
    }

    private void initializeRealDB() {
        ServerContext.get().getServerStore().set( DataStore.class, ServerStores.getMaster().get( DataStore.class ) ); // Master DS
    }

    private void initializeDemoDB() {
        ServerContext zContext = ServerContext.get();
        DemoDataStoreManager.Struct zDemoDS = DemoDataStoreManager.getDemoDS( zContext.getContextID() ); // Demo DS
        zContext.getServerStore().set( DataStore.class, zDemoDS.getDataStore() );
        if ( zDemoDS.isEmptyDemo() ) {
            populateDemoDS();
        }
    }

    abstract public SecurityUserView<VO> currentLoggedInUserView();

    abstract public void logoutUser();

    abstract protected SecurityUserView finiUserLogin( SecurityUser<PO> pUser );

    private SecurityUser<PO> getUserByEmailFromMasterDS( String pLogonEmail ) {
        return getUserByEmail( ServerStores.getMaster().get( DataStore.class ).getUnfilteredFinder(), pLogonEmail );
    }

    private SecurityUser<PO> getUserByEmailFromCurrentDS( String pLogonEmail ) {
        return getUserByEmail( DataStoreLocator.get().getUnfilteredFinder(), pLogonEmail );
    }

    private SecurityUser<PO> getUserByEmail( Finder pFinder, String pLogonEmail ) {
        SecurityUser<PO> zUser = findUserByEmail( pFinder, pLogonEmail );
        if ( isUserDemo( zUser ) ) {
            zUser.setDemo();
        }
        return zUser;
    }

    private boolean isUserDemo( SecurityUser<PO> pUser ) {
        return ((pUser != null) && mSetOfDemoUserEmails.contains( ConstrainTo.notNull( pUser.getLogonEmail() ).toLowerCase() ));
    }

    private int findCurrentDemoIndex( SecurityUserView pUser ) {
        if ( pUser == null ) {
            return 0;
        }
        int i = mDemoUserEmails.length;
        while ( --i >= 0 ) {
            if ( mDemoUserEmails[i].equalsIgnoreCase( pUser.getLogonEmail() ) ) {
                return i;
            }
        }
        return i;
    }

    private void populateDemoDS() {
        DataLoader.INSTANCE.loadDemoData();
    }

    abstract protected SecurityUser<PO> findUserByEmail( Finder pFinder, String pLogonEmail );
}

Commits for litesoft/trunk/Java/GWT/Server/src/org/litesoft/security/SecurityUserViewPoServerVoDataProvider.java

Diff revisions: vs.
Revision Author Commited Message
953 Diff Diff GeorgeS picture GeorgeS Fri 20 Jun, 2014 23:12:43 +0000

Drop Nas-Video.
Normalize the SecurityPOs.

950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
486 Diff Diff GeorgeS picture GeorgeS Tue 06 Sep, 2011 03:00:59 +0000
483 GeorgeS picture GeorgeS Sun 04 Sep, 2011 19:58:09 +0000