|
@@ -1,178 +1,21 @@ |
1 |
1 |
|
package org.litesoft.datt.server; |
2 |
2 |
|
|
3 |
3 |
|
import org.litesoft.bo.views.*; |
4 |
|
- |
import org.litesoft.changemanagement.*; |
5 |
4 |
|
import org.litesoft.core.*; |
6 |
5 |
|
import org.litesoft.datt.client.boviews.*; |
7 |
6 |
|
import org.litesoft.datt.server.boviews.podataproviders.*; |
8 |
7 |
|
import org.litesoft.datt.server.pos.*; |
9 |
|
- |
import org.litesoft.util.*; |
|
8 |
+ |
import org.litesoft.security.*; |
10 |
9 |
|
|
11 |
|
- |
public class CurrentUserAccessor implements ServerStateChangedListener |
|
10 |
+ |
public class CurrentUserAccessor extends AbstractCurrentSecurityUserAccessor<User, UserView, RestrictedResource> |
12 |
11 |
|
{ |
13 |
|
- |
public static synchronized CurrentUserAccessor get() |
14 |
|
- |
{ |
15 |
|
- |
ServerStore zStore = ServerContext.get().getServerStore(); |
16 |
|
- |
CurrentUserAccessor zAccessor = zStore.get( CurrentUserAccessor.class ); |
17 |
|
- |
if ( zAccessor == null ) |
18 |
|
- |
{ |
19 |
|
- |
zStore.set( CurrentUserAccessor.class, zAccessor = new CurrentUserAccessor() ); |
20 |
|
- |
} |
21 |
|
- |
return zAccessor; |
22 |
|
- |
} |
23 |
|
- |
|
24 |
|
- |
private boolean mViewStale; |
25 |
|
- |
private ChangeListenerManager mRegisteredWithCLM; |
26 |
|
- |
private User mPO; |
27 |
|
- |
|
28 |
|
- |
public User getRequiredUser() |
29 |
|
- |
{ |
30 |
|
- |
User zUser = getOptionalUser( null ); |
31 |
|
- |
if ( zUser == null ) |
32 |
|
- |
{ |
33 |
|
- |
throw new SessionExpiredException( "No Current User" ); |
34 |
|
- |
} |
35 |
|
- |
return zUser; |
36 |
|
- |
} |
37 |
|
- |
|
38 |
|
- |
public User getOptionalUser( String pDefaultUserLoginEmail ) |
39 |
|
- |
{ |
40 |
|
- |
Pair zPair = getPair( pDefaultUserLoginEmail ); |
41 |
|
- |
System.out.println(zPair); |
42 |
|
- |
if ( zPair.getVO() == null ) |
43 |
|
- |
{ |
44 |
|
- |
return null; |
45 |
|
- |
} |
46 |
|
- |
if ( (zPair.getPO() == null) || !zPair.inSync() ) |
47 |
|
- |
{ |
48 |
|
- |
zPair = refresh( zPair ); |
49 |
|
- |
} |
50 |
|
- |
return zPair.getPO(); |
51 |
|
- |
} |
52 |
|
- |
|
53 |
|
- |
public UserView getUserView( String pDefaultUserLoginEmail ) |
54 |
|
- |
{ |
55 |
|
- |
Pair zPair = getPair( pDefaultUserLoginEmail ); |
56 |
|
- |
if ( zPair.isStale() ) |
57 |
|
- |
{ |
58 |
|
- |
zPair = refresh( zPair ); |
59 |
|
- |
} |
60 |
|
- |
return zPair.getVO(); |
61 |
|
- |
} |
62 |
|
- |
|
63 |
|
- |
public void logout() |
64 |
|
- |
{ |
65 |
|
- |
update( null, null ); |
66 |
|
- |
} |
67 |
|
- |
|
68 |
|
- |
public UserView updateUser( User pPO ) |
69 |
|
- |
{ |
70 |
|
- |
UserView zView = getDataProvider().createView( pPO, false ); |
71 |
|
- |
update( zView, pPO ); |
72 |
|
- |
return zView; |
73 |
|
- |
} |
74 |
|
- |
|
75 |
|
- |
private Pair refresh( Pair pPair ) |
|
12 |
+ |
private CurrentUserAccessor() |
76 |
13 |
|
{ |
77 |
|
- |
UserViewPoServerVoDataProvider zDP = getDataProvider(); |
78 |
|
- |
User zPO = (User) zDP.getPO( pPair.getVO().getID() ); |
79 |
|
- |
return update( zDP.createView( zPO, false ), zPO ); |
|
14 |
+ |
super( User.class, UserView.class, (UserViewPoServerVoDataProvider) ServerContext.getMasterServerStore().get( ServerVoDataProviderMap.class ).get( UserView.class ) ); |
80 |
15 |
|
} |
81 |
16 |
|
|
82 |
|
- |
private Pair update( UserView pView, User pPO ) |
83 |
|
- |
{ |
84 |
|
- |
Pair rv; |
85 |
|
- |
User zOld, zNew; |
86 |
|
- |
synchronized ( this ) |
87 |
|
- |
{ |
88 |
|
- |
rv = new Pair( mViewStale = false, pView, pPO ); |
89 |
|
- |
ServerContext.get().getServerSession().setAttribute( UserView.class, pView ); |
90 |
|
- |
zOld = mPO; |
91 |
|
- |
zNew = mPO = pPO; |
92 |
|
- |
} |
93 |
|
- |
if ( !Utils.areNonArraysEqual( zOld, zNew ) ) |
94 |
|
- |
{ |
95 |
|
- |
if ( zOld != null ) |
96 |
|
- |
{ |
97 |
|
- |
mRegisteredWithCLM.removeListener( this ); |
98 |
|
- |
mRegisteredWithCLM = null; |
99 |
|
- |
} |
100 |
|
- |
if ( zNew != null ) |
101 |
|
- |
{ |
102 |
|
- |
(mRegisteredWithCLM = ChangeListenerManager.get()).addListener( this, User.class, zNew.getPersistentObjectUniqueKey() ); |
103 |
|
- |
} |
104 |
|
- |
} |
105 |
|
- |
return rv; |
106 |
|
- |
} |
107 |
|
- |
|
108 |
|
- |
private Pair getPair( String pDefaultUserLoginEmail ) |
109 |
|
- |
{ |
110 |
|
- |
synchronized ( this ) |
111 |
|
- |
{ |
112 |
|
- |
UserView zView = ServerContext.get().getServerSession().getAttribute( UserView.class ); |
113 |
|
- |
if ( (zView != null) || (pDefaultUserLoginEmail == null) ) |
114 |
|
- |
{ |
115 |
|
- |
return new Pair( mViewStale, zView, mPO ); |
116 |
|
- |
} |
117 |
|
- |
} |
118 |
|
- |
getDataProvider().loginDefaultUser( pDefaultUserLoginEmail ); |
119 |
|
- |
synchronized ( this ) |
120 |
|
- |
{ |
121 |
|
- |
UserView zView = ServerContext.get().getServerSession().getAttribute( UserView.class ); |
122 |
|
- |
return new Pair( mViewStale, zView, mPO ); |
123 |
|
- |
} |
124 |
|
- |
} |
125 |
|
- |
|
126 |
|
- |
@Override |
127 |
|
- |
public synchronized void serverStateChanged( ServerStateChangeSet pServerStateChangeSet ) |
128 |
|
- |
{ |
129 |
|
- |
mViewStale = true; |
130 |
|
- |
} |
131 |
|
- |
|
132 |
|
- |
private UserViewPoServerVoDataProvider getDataProvider() |
133 |
|
- |
{ |
134 |
|
- |
return (UserViewPoServerVoDataProvider) ServerContext.getMasterServerStore().get( ServerVoDataProviderMap.class ).get( UserView.class ); |
135 |
|
- |
} |
136 |
|
- |
|
137 |
|
- |
private static class Pair |
|
17 |
+ |
public static synchronized CurrentUserAccessor get() |
138 |
18 |
|
{ |
139 |
|
- |
private boolean mStale; |
140 |
|
- |
private UserView mVO; |
141 |
|
- |
private User mPO; |
142 |
|
- |
|
143 |
|
- |
private Pair( boolean pStale, UserView pVO, User pPO ) |
144 |
|
- |
{ |
145 |
|
- |
mStale = (pVO != null) && pStale; |
146 |
|
- |
mVO = pVO; |
147 |
|
- |
mPO = pPO; |
148 |
|
- |
} |
149 |
|
- |
|
150 |
|
- |
public boolean isStale() |
151 |
|
- |
{ |
152 |
|
- |
return mStale; |
153 |
|
- |
} |
154 |
|
- |
|
155 |
|
- |
public UserView getVO() |
156 |
|
- |
{ |
157 |
|
- |
return mVO; |
158 |
|
- |
} |
159 |
|
- |
|
160 |
|
- |
public User getPO() |
161 |
|
- |
{ |
162 |
|
- |
return mPO; |
163 |
|
- |
} |
164 |
|
- |
|
165 |
|
- |
public boolean inSync() // Only called when Both are !null |
166 |
|
- |
{ |
167 |
|
- |
return !mStale && // |
168 |
|
- |
Utils.areNonArraysEqual( mPO.getID(), mVO.getID() ) && // |
169 |
|
- |
Utils.areNonArraysEqual( mPO.getRecordVersion(), mVO.getRecordVersion() ); |
170 |
|
- |
} |
171 |
|
- |
|
172 |
|
- |
@Override |
173 |
|
- |
public String toString() |
174 |
|
- |
{ |
175 |
|
- |
return "DATT: " + mStale + " PO: " + mPO + " VO: " + mVO; |
176 |
|
- |
} |
|
19 |
+ |
return getCurrentInstance( CurrentUserAccessor.class ); |
177 |
20 |
|
} |
178 |
21 |
|
} |