1717 */
1818
1919use actix_web:: { web, HttpRequest , HttpResponse , Responder } ;
20+ use anyhow:: Error ;
2021use bytes:: Bytes ;
2122use itertools:: Itertools ;
22- use relative_path:: RelativePathBuf ;
2323
2424use crate :: rbac:: Users ;
25- use crate :: utils:: user_auth_for_query;
26- use crate :: {
27- option:: CONFIG , storage:: CORRELATIONS_ROOT_DIRECTORY ,
28- utils:: actix:: extract_session_key_from_req,
29- } ;
25+ use crate :: storage:: object_storage:: correlation_path;
26+ use crate :: utils:: { get_hash, get_user_from_request, user_auth_for_query} ;
27+ use crate :: { option:: CONFIG , utils:: actix:: extract_session_key_from_req} ;
3028
3129use crate :: correlation:: { CorrelationConfig , CorrelationError , CorrelationRequest , CORRELATIONS } ;
3230
3331pub async fn list ( req : HttpRequest ) -> Result < impl Responder , CorrelationError > {
3432 let session_key = extract_session_key_from_req ( & req)
35- . map_err ( |err| CorrelationError :: AnyhowError ( anyhow:: Error :: msg ( err. to_string ( ) ) ) ) ?;
33+ . map_err ( |err| CorrelationError :: AnyhowError ( Error :: msg ( err. to_string ( ) ) ) ) ?;
34+
35+ let user_id = get_user_from_request ( & req)
36+ . map ( |s| get_hash ( & s. to_string ( ) ) )
37+ . map_err ( |err| CorrelationError :: AnyhowError ( Error :: msg ( err. to_string ( ) ) ) ) ?;
3638
3739 let correlations = CORRELATIONS
38- . list_correlations_for_user ( & session_key)
40+ . list_correlations_for_user ( & session_key, & user_id )
3941 . await ?;
4042
4143 Ok ( web:: Json ( correlations) )
4244}
4345
4446pub async fn get ( req : HttpRequest ) -> Result < impl Responder , CorrelationError > {
4547 let session_key = extract_session_key_from_req ( & req)
46- . map_err ( |err| CorrelationError :: AnyhowError ( anyhow:: Error :: msg ( err. to_string ( ) ) ) ) ?;
48+ . map_err ( |err| CorrelationError :: AnyhowError ( Error :: msg ( err. to_string ( ) ) ) ) ?;
49+
50+ let user_id = get_user_from_request ( & req)
51+ . map ( |s| get_hash ( & s. to_string ( ) ) )
52+ . map_err ( |err| CorrelationError :: AnyhowError ( Error :: msg ( err. to_string ( ) ) ) ) ?;
4753
4854 let correlation_id = req
4955 . match_info ( )
5056 . get ( "correlation_id" )
5157 . ok_or ( CorrelationError :: Metadata ( "No correlation ID Provided" ) ) ?;
5258
53- let correlation = CORRELATIONS . get_correlation_by_id ( correlation_id) . await ?;
59+ let correlation = CORRELATIONS
60+ . get_correlation ( correlation_id, & user_id)
61+ . await ?;
5462
5563 let permissions = Users . get_permissions ( & session_key) ;
5664
@@ -68,16 +76,24 @@ pub async fn get(req: HttpRequest) -> Result<impl Responder, CorrelationError> {
6876pub async fn post ( req : HttpRequest , body : Bytes ) -> Result < impl Responder , CorrelationError > {
6977 let session_key = extract_session_key_from_req ( & req)
7078 . map_err ( |err| CorrelationError :: AnyhowError ( anyhow:: Error :: msg ( err. to_string ( ) ) ) ) ?;
79+ let user_id = get_user_from_request ( & req)
80+ . map ( |s| get_hash ( & s. to_string ( ) ) )
81+ . map_err ( |err| CorrelationError :: AnyhowError ( Error :: msg ( err. to_string ( ) ) ) ) ?;
7182
7283 let correlation_request: CorrelationRequest = serde_json:: from_slice ( & body) ?;
7384
7485 correlation_request. validate ( & session_key) . await ?;
7586
76- let correlation: CorrelationConfig = correlation_request. into ( ) ;
87+ let mut correlation: CorrelationConfig = correlation_request. into ( ) ;
88+ correlation. user_id . clone_from ( & user_id) ;
89+ let correlation_id = & correlation. id ;
90+ let path = correlation_path ( & user_id, & format ! ( "{}.json" , correlation_id) ) ;
7791
78- // Save to disk
7992 let store = CONFIG . storage ( ) . get_object_store ( ) ;
80- store. put_correlation ( & correlation) . await ?;
93+ let correlation_bytes = serde_json:: to_vec ( & correlation) ?;
94+ store
95+ . put_object ( & path, Bytes :: from ( correlation_bytes) )
96+ . await ?;
8197
8298 // Save to memory
8399 CORRELATIONS . update ( & correlation) . await ?;
@@ -88,14 +104,19 @@ pub async fn post(req: HttpRequest, body: Bytes) -> Result<impl Responder, Corre
88104pub async fn modify ( req : HttpRequest , body : Bytes ) -> Result < impl Responder , CorrelationError > {
89105 let session_key = extract_session_key_from_req ( & req)
90106 . map_err ( |err| CorrelationError :: AnyhowError ( anyhow:: Error :: msg ( err. to_string ( ) ) ) ) ?;
107+ let user_id = get_user_from_request ( & req)
108+ . map ( |s| get_hash ( & s. to_string ( ) ) )
109+ . map_err ( |err| CorrelationError :: AnyhowError ( Error :: msg ( err. to_string ( ) ) ) ) ?;
91110
92111 let correlation_id = req
93112 . match_info ( )
94113 . get ( "correlation_id" )
95114 . ok_or ( CorrelationError :: Metadata ( "No correlation ID Provided" ) ) ?;
96115
97116 // validate whether user has access to this correlation object or not
98- let correlation = CORRELATIONS . get_correlation_by_id ( correlation_id) . await ?;
117+ let correlation = CORRELATIONS
118+ . get_correlation ( correlation_id, & user_id)
119+ . await ?;
99120 let permissions = Users . get_permissions ( & session_key) ;
100121 let tables = & correlation
101122 . table_configs
@@ -108,11 +129,17 @@ pub async fn modify(req: HttpRequest, body: Bytes) -> Result<impl Responder, Cor
108129 let correlation_request: CorrelationRequest = serde_json:: from_slice ( & body) ?;
109130 correlation_request. validate ( & session_key) . await ?;
110131
111- let correlation = correlation_request. generate_correlation_config ( correlation_id. to_owned ( ) ) ;
132+ let correlation =
133+ correlation_request. generate_correlation_config ( correlation_id. to_owned ( ) , user_id. clone ( ) ) ;
134+
135+ let correlation_id = & correlation. id ;
136+ let path = correlation_path ( & user_id, & format ! ( "{}.json" , correlation_id) ) ;
112137
113- // Save to disk
114138 let store = CONFIG . storage ( ) . get_object_store ( ) ;
115- store. put_correlation ( & correlation) . await ?;
139+ let correlation_bytes = serde_json:: to_vec ( & correlation) ?;
140+ store
141+ . put_object ( & path, Bytes :: from ( correlation_bytes) )
142+ . await ?;
116143
117144 // Save to memory
118145 CORRELATIONS . update ( & correlation) . await ?;
@@ -123,13 +150,18 @@ pub async fn modify(req: HttpRequest, body: Bytes) -> Result<impl Responder, Cor
123150pub async fn delete ( req : HttpRequest ) -> Result < impl Responder , CorrelationError > {
124151 let session_key = extract_session_key_from_req ( & req)
125152 . map_err ( |err| CorrelationError :: AnyhowError ( anyhow:: Error :: msg ( err. to_string ( ) ) ) ) ?;
153+ let user_id = get_user_from_request ( & req)
154+ . map ( |s| get_hash ( & s. to_string ( ) ) )
155+ . map_err ( |err| CorrelationError :: AnyhowError ( Error :: msg ( err. to_string ( ) ) ) ) ?;
126156
127157 let correlation_id = req
128158 . match_info ( )
129159 . get ( "correlation_id" )
130160 . ok_or ( CorrelationError :: Metadata ( "No correlation ID Provided" ) ) ?;
131161
132- let correlation = CORRELATIONS . get_correlation_by_id ( correlation_id) . await ?;
162+ let correlation = CORRELATIONS
163+ . get_correlation ( correlation_id, & user_id)
164+ . await ?;
133165
134166 // validate user's query auth
135167 let permissions = Users . get_permissions ( & session_key) ;
@@ -141,12 +173,10 @@ pub async fn delete(req: HttpRequest) -> Result<impl Responder, CorrelationError
141173
142174 user_auth_for_query ( & permissions, tables) ?;
143175
144- // Delete from disk
176+ let correlation_id = & correlation. id ;
177+ let path = correlation_path ( & user_id, & format ! ( "{}.json" , correlation_id) ) ;
178+
145179 let store = CONFIG . storage ( ) . get_object_store ( ) ;
146- let path = RelativePathBuf :: from_iter ( [
147- CORRELATIONS_ROOT_DIRECTORY ,
148- & format ! ( "{}.json" , correlation_id) ,
149- ] ) ;
150180 store. delete_object ( & path) . await ?;
151181
152182 // Delete from memory
0 commit comments