110
110
111
111
public final class HugeGraphAuthProxy implements HugeGraph {
112
112
113
+ private static final Logger LOG = Log .logger (HugeGraphAuthProxy .class );
114
+ private static final ThreadLocal <Context > CONTEXTS = new InheritableThreadLocal <>();
115
+
113
116
static {
114
117
HugeGraph .registerTraversalStrategies (HugeGraphAuthProxy .class );
115
118
}
116
119
117
- private static final Logger LOG = Log .logger (HugeGraphAuthProxy .class );
118
120
private final Cache <Id , UserWithRole > usersRoleCache ;
119
121
private final Cache <Id , RateLimiter > auditLimiters ;
120
122
private final double auditLogMaxRate ;
121
-
122
123
private final HugeGraph hugegraph ;
123
124
private final TaskSchedulerProxy taskScheduler ;
124
125
private final AuthManagerProxy authManager ;
@@ -141,6 +142,40 @@ public HugeGraphAuthProxy(HugeGraph hugegraph) {
141
142
LOG .info ("Audit log rate limit is {}/s" , this .auditLogMaxRate );
142
143
}
143
144
145
+ static Context setContext (Context context ) {
146
+ Context old = CONTEXTS .get ();
147
+ CONTEXTS .set (context );
148
+ return old ;
149
+ }
150
+
151
+ static void resetContext () {
152
+ CONTEXTS .remove ();
153
+ }
154
+
155
+ private static Context getContext () {
156
+ // Return task context first
157
+ String taskContext = TaskManager .getContext ();
158
+ User user = User .fromJson (taskContext );
159
+ if (user != null ) {
160
+ return new Context (user );
161
+ }
162
+
163
+ return CONTEXTS .get ();
164
+ }
165
+
166
+ private static String getContextString () {
167
+ Context context = getContext ();
168
+ if (context == null ) {
169
+ return null ;
170
+ }
171
+ return context .user ().toJson ();
172
+ }
173
+
174
+ static void logUser (User user , String path ) {
175
+ LOG .info ("User '{}' login from client [{}] with path '{}'" ,
176
+ user .username (), user .client (), path );
177
+ }
178
+
144
179
@ Override
145
180
public HugeGraph hugegraph () {
146
181
this .verifyAdminPermission ();
@@ -1016,6 +1051,61 @@ else if (ro.type().isGrantOrUser()) {
1016
1051
return result ;
1017
1052
}
1018
1053
1054
+ static class Context {
1055
+
1056
+ private static final Context ADMIN = new Context (User .ADMIN );
1057
+
1058
+ private final User user ;
1059
+
1060
+ public Context (User user ) {
1061
+ E .checkNotNull (user , "user" );
1062
+ this .user = user ;
1063
+ }
1064
+
1065
+ public static Context admin () {
1066
+ return ADMIN ;
1067
+ }
1068
+
1069
+ public User user () {
1070
+ return this .user ;
1071
+ }
1072
+ }
1073
+
1074
+ static class ContextTask implements Runnable {
1075
+
1076
+ private final Runnable runner ;
1077
+ private final Context context ;
1078
+
1079
+ public ContextTask (Runnable runner ) {
1080
+ this .context = getContext ();
1081
+ this .runner = runner ;
1082
+ }
1083
+
1084
+ @ Override
1085
+ public void run () {
1086
+ setContext (this .context );
1087
+ try {
1088
+ this .runner .run ();
1089
+ } finally {
1090
+ resetContext ();
1091
+ }
1092
+ }
1093
+ }
1094
+
1095
+ public static class ContextThreadPoolExecutor extends ThreadPoolExecutor {
1096
+
1097
+ public ContextThreadPoolExecutor (int corePoolSize , int maxPoolSize ,
1098
+ ThreadFactory threadFactory ) {
1099
+ super (corePoolSize , maxPoolSize , 0L , TimeUnit .MILLISECONDS ,
1100
+ new LinkedBlockingQueue <>(), threadFactory );
1101
+ }
1102
+
1103
+ @ Override
1104
+ public void execute (Runnable command ) {
1105
+ super .execute (new ContextTask (command ));
1106
+ }
1107
+ }
1108
+
1019
1109
class TaskSchedulerProxy implements TaskScheduler {
1020
1110
1021
1111
private final TaskScheduler taskScheduler ;
@@ -1622,8 +1712,14 @@ public void enabledWhiteIpList(boolean status) {
1622
1712
1623
1713
@ Override
1624
1714
public String loginUser (String username , String password ) {
1715
+ return this .loginUser (username , password , -1L );
1716
+ }
1717
+
1718
+ // TODO: the expire haven't been implemented yet
1719
+ @ Override
1720
+ public String loginUser (String username , String password , long expire ) {
1625
1721
try {
1626
- return this .authManager .loginUser (username , password );
1722
+ return this .authManager .loginUser (username , password , expire );
1627
1723
} catch (AuthenticationException e ) {
1628
1724
throw new NotAuthorizedException (e .getMessage (), e );
1629
1725
}
@@ -1841,95 +1937,4 @@ public String toString() {
1841
1937
return this .origin .toString ();
1842
1938
}
1843
1939
}
1844
-
1845
- private static final ThreadLocal <Context > CONTEXTS = new InheritableThreadLocal <>();
1846
-
1847
- protected static Context setContext (Context context ) {
1848
- Context old = CONTEXTS .get ();
1849
- CONTEXTS .set (context );
1850
- return old ;
1851
- }
1852
-
1853
- protected static void resetContext () {
1854
- CONTEXTS .remove ();
1855
- }
1856
-
1857
- protected static Context getContext () {
1858
- // Return task context first
1859
- String taskContext = TaskManager .getContext ();
1860
- User user = User .fromJson (taskContext );
1861
- if (user != null ) {
1862
- return new Context (user );
1863
- }
1864
-
1865
- return CONTEXTS .get ();
1866
- }
1867
-
1868
- protected static String getContextString () {
1869
- Context context = getContext ();
1870
- if (context == null ) {
1871
- return null ;
1872
- }
1873
- return context .user ().toJson ();
1874
- }
1875
-
1876
- protected static void logUser (User user , String path ) {
1877
- LOG .info ("User '{}' login from client [{}] with path '{}'" ,
1878
- user .username (), user .client (), path );
1879
- }
1880
-
1881
- static class Context {
1882
-
1883
- private static final Context ADMIN = new Context (User .ADMIN );
1884
-
1885
- private final User user ;
1886
-
1887
- public Context (User user ) {
1888
- E .checkNotNull (user , "user" );
1889
- this .user = user ;
1890
- }
1891
-
1892
- public User user () {
1893
- return this .user ;
1894
- }
1895
-
1896
- public static Context admin () {
1897
- return ADMIN ;
1898
- }
1899
- }
1900
-
1901
- static class ContextTask implements Runnable {
1902
-
1903
- private final Runnable runner ;
1904
- private final Context context ;
1905
-
1906
- public ContextTask (Runnable runner ) {
1907
- this .context = getContext ();
1908
- this .runner = runner ;
1909
- }
1910
-
1911
- @ Override
1912
- public void run () {
1913
- setContext (this .context );
1914
- try {
1915
- this .runner .run ();
1916
- } finally {
1917
- resetContext ();
1918
- }
1919
- }
1920
- }
1921
-
1922
- public static class ContextThreadPoolExecutor extends ThreadPoolExecutor {
1923
-
1924
- public ContextThreadPoolExecutor (int corePoolSize , int maxPoolSize ,
1925
- ThreadFactory threadFactory ) {
1926
- super (corePoolSize , maxPoolSize , 0L , TimeUnit .MILLISECONDS ,
1927
- new LinkedBlockingQueue <>(), threadFactory );
1928
- }
1929
-
1930
- @ Override
1931
- public void execute (Runnable command ) {
1932
- super .execute (new ContextTask (command ));
1933
- }
1934
- }
1935
1940
}
0 commit comments