@@ -46,8 +46,7 @@ use crate::sessions::SessionType;
46
46
pub struct Session {
47
47
pub ( in crate :: sessions) id : String ,
48
48
pub ( in crate :: sessions) typ : RwLock < SessionType > ,
49
- pub ( in crate :: sessions) session_ctx : Arc < SessionContext > ,
50
- pub ( in crate :: sessions) privilege_mgr : SessionPrivilegeManagerImpl ,
49
+ pub ( in crate :: sessions) session_ctx : Box < SessionContext > ,
51
50
status : Arc < RwLock < SessionStatus > > ,
52
51
pub ( in crate :: sessions) mysql_connection_id : Option < u32 > ,
53
52
format_settings : FormatSettings ,
@@ -57,17 +56,15 @@ impl Session {
57
56
pub fn try_create (
58
57
id : String ,
59
58
typ : SessionType ,
60
- session_ctx : Arc < SessionContext > ,
59
+ session_ctx : Box < SessionContext > ,
61
60
mysql_connection_id : Option < u32 > ,
62
61
) -> Result < Arc < Session > > {
63
62
let status = Arc :: new ( Default :: default ( ) ) ;
64
- let privilege_mgr = SessionPrivilegeManagerImpl :: new ( session_ctx. clone ( ) ) ;
65
63
Ok ( Arc :: new ( Session {
66
64
id,
67
65
typ : RwLock :: new ( typ) ,
68
66
status,
69
67
session_ctx,
70
- privilege_mgr,
71
68
mysql_connection_id,
72
69
format_settings : FormatSettings :: default ( ) ,
73
70
} ) )
@@ -114,7 +111,8 @@ impl Session {
114
111
}
115
112
116
113
pub fn quit ( self : & Arc < Self > ) {
117
- let session_ctx = self . session_ctx . clone ( ) ;
114
+ let session_ctx = self . session_ctx . as_ref ( ) ;
115
+
118
116
if session_ctx. get_current_query_id ( ) . is_some ( ) {
119
117
if let Some ( shutdown_fun) = session_ctx. take_io_shutdown_tx ( ) {
120
118
shutdown_fun ( ) ;
@@ -138,9 +136,7 @@ impl Session {
138
136
}
139
137
140
138
pub fn force_kill_query ( self : & Arc < Self > , cause : ErrorCode ) {
141
- let session_ctx = self . session_ctx . clone ( ) ;
142
-
143
- if let Some ( context_shared) = session_ctx. get_query_context_shared ( ) {
139
+ if let Some ( context_shared) = self . session_ctx . get_query_context_shared ( ) {
144
140
context_shared. kill ( cause) ;
145
141
}
146
142
}
@@ -205,7 +201,11 @@ impl Session {
205
201
}
206
202
207
203
pub fn get_current_user ( self : & Arc < Self > ) -> Result < UserInfo > {
208
- self . privilege_mgr . get_current_user ( )
204
+ self . privilege_mgr ( ) . get_current_user ( )
205
+ }
206
+
207
+ pub fn privilege_mgr ( & self ) -> SessionPrivilegeManagerImpl < ' _ > {
208
+ SessionPrivilegeManagerImpl :: new ( self . session_ctx . as_ref ( ) )
209
209
}
210
210
211
211
// set_authed_user() is called after authentication is passed in various protocol handlers, like
@@ -218,21 +218,23 @@ impl Session {
218
218
user : UserInfo ,
219
219
restricted_role : Option < String > ,
220
220
) -> Result < ( ) > {
221
- self . privilege_mgr
221
+ self . privilege_mgr ( )
222
222
. set_authed_user ( user, restricted_role)
223
223
. await
224
224
}
225
225
226
226
#[ async_backtrace:: framed]
227
227
pub async fn validate_available_role ( self : & Arc < Self > , role_name : & str ) -> Result < RoleInfo > {
228
- self . privilege_mgr . validate_available_role ( role_name) . await
228
+ self . privilege_mgr ( )
229
+ . validate_available_role ( role_name)
230
+ . await
229
231
}
230
232
231
233
// Only the available role can be set as current role. The current role can be set by the SET
232
234
// ROLE statement, or by the `session.role` field in the HTTP query request body.
233
235
#[ async_backtrace:: framed]
234
236
pub async fn set_current_role_checked ( self : & Arc < Self > , role_name : & str ) -> Result < ( ) > {
235
- self . privilege_mgr
237
+ self . privilege_mgr ( )
236
238
. set_current_role ( Some ( role_name. to_string ( ) ) )
237
239
. await
238
240
}
@@ -242,33 +244,33 @@ impl Session {
242
244
self : & Arc < Self > ,
243
245
role_names : Option < Vec < String > > ,
244
246
) -> Result < ( ) > {
245
- self . privilege_mgr . set_secondary_roles ( role_names) . await
247
+ self . privilege_mgr ( ) . set_secondary_roles ( role_names) . await
246
248
}
247
249
248
250
pub fn get_current_role ( self : & Arc < Self > ) -> Option < RoleInfo > {
249
- self . privilege_mgr . get_current_role ( )
251
+ self . privilege_mgr ( ) . get_current_role ( )
250
252
}
251
253
252
254
pub fn get_secondary_roles ( self : & Arc < Self > ) -> Option < Vec < String > > {
253
- self . privilege_mgr . get_secondary_roles ( )
255
+ self . privilege_mgr ( ) . get_secondary_roles ( )
254
256
}
255
257
256
258
#[ async_backtrace:: framed]
257
259
pub async fn unset_current_role ( self : & Arc < Self > ) -> Result < ( ) > {
258
- self . privilege_mgr . set_current_role ( None ) . await
260
+ self . privilege_mgr ( ) . set_current_role ( None ) . await
259
261
}
260
262
261
263
// Returns all the roles the current session has. If the user have been granted restricted_role,
262
264
// the other roles will be ignored.
263
265
// On executing SET ROLE, the role have to be one of the available roles.
264
266
#[ async_backtrace:: framed]
265
267
pub async fn get_all_available_roles ( self : & Arc < Self > ) -> Result < Vec < RoleInfo > > {
266
- self . privilege_mgr . get_all_available_roles ( ) . await
268
+ self . privilege_mgr ( ) . get_all_available_roles ( ) . await
267
269
}
268
270
269
271
#[ async_backtrace:: framed]
270
272
pub async fn get_all_effective_roles ( self : & Arc < Self > ) -> Result < Vec < RoleInfo > > {
271
- self . privilege_mgr . get_all_effective_roles ( ) . await
273
+ self . privilege_mgr ( ) . get_all_effective_roles ( ) . await
272
274
}
273
275
274
276
#[ async_backtrace:: framed]
@@ -280,7 +282,7 @@ impl Session {
280
282
if matches ! ( self . get_type( ) , SessionType :: Local ) {
281
283
return Ok ( ( ) ) ;
282
284
}
283
- self . privilege_mgr
285
+ self . privilege_mgr ( )
284
286
. validate_privilege ( object, privilege)
285
287
. await
286
288
}
@@ -290,12 +292,12 @@ impl Session {
290
292
if matches ! ( self . get_type( ) , SessionType :: Local ) {
291
293
return Ok ( true ) ;
292
294
}
293
- self . privilege_mgr . has_ownership ( object) . await
295
+ self . privilege_mgr ( ) . has_ownership ( object) . await
294
296
}
295
297
296
298
#[ async_backtrace:: framed]
297
299
pub async fn get_visibility_checker ( & self ) -> Result < GrantObjectVisibilityChecker > {
298
- self . privilege_mgr . get_visibility_checker ( ) . await
300
+ self . privilege_mgr ( ) . get_visibility_checker ( ) . await
299
301
}
300
302
301
303
pub fn get_settings ( self : & Arc < Self > ) -> Arc < Settings > {
@@ -328,9 +330,7 @@ impl Session {
328
330
}
329
331
330
332
pub fn set_query_priority ( & self , priority : u8 ) {
331
- let session_ctx = self . session_ctx . clone ( ) ;
332
-
333
- if let Some ( context_shared) = session_ctx. get_query_context_shared ( ) {
333
+ if let Some ( context_shared) = self . session_ctx . get_query_context_shared ( ) {
334
334
context_shared. set_priority ( priority) ;
335
335
}
336
336
}
0 commit comments