17
17
*/
18
18
19
19
use actix_web:: { web, HttpRequest , HttpResponse , Responder } ;
20
+ use anyhow:: Error ;
20
21
use bytes:: Bytes ;
21
22
use itertools:: Itertools ;
22
- use relative_path:: RelativePathBuf ;
23
23
24
24
use 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} ;
30
28
31
29
use crate :: correlation:: { CorrelationConfig , CorrelationError , CorrelationRequest , CORRELATIONS } ;
32
30
33
31
pub async fn list ( req : HttpRequest ) -> Result < impl Responder , CorrelationError > {
34
32
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 ( ) ) ) ) ?;
36
38
37
39
let correlations = CORRELATIONS
38
- . list_correlations_for_user ( & session_key)
40
+ . list_correlations_for_user ( & session_key, & user_id )
39
41
. await ?;
40
42
41
43
Ok ( web:: Json ( correlations) )
42
44
}
43
45
44
46
pub async fn get ( req : HttpRequest ) -> Result < impl Responder , CorrelationError > {
45
47
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 ( ) ) ) ) ?;
47
53
48
54
let correlation_id = req
49
55
. match_info ( )
50
56
. get ( "correlation_id" )
51
57
. ok_or ( CorrelationError :: Metadata ( "No correlation ID Provided" ) ) ?;
52
58
53
- let correlation = CORRELATIONS . get_correlation_by_id ( correlation_id) . await ?;
59
+ let correlation = CORRELATIONS
60
+ . get_correlation ( correlation_id, & user_id)
61
+ . await ?;
54
62
55
63
let permissions = Users . get_permissions ( & session_key) ;
56
64
@@ -68,16 +76,24 @@ pub async fn get(req: HttpRequest) -> Result<impl Responder, CorrelationError> {
68
76
pub async fn post ( req : HttpRequest , body : Bytes ) -> Result < impl Responder , CorrelationError > {
69
77
let session_key = extract_session_key_from_req ( & req)
70
78
. 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 ( ) ) ) ) ?;
71
82
72
83
let correlation_request: CorrelationRequest = serde_json:: from_slice ( & body) ?;
73
84
74
85
correlation_request. validate ( & session_key) . await ?;
75
86
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) ) ;
77
91
78
- // Save to disk
79
92
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 ?;
81
97
82
98
// Save to memory
83
99
CORRELATIONS . update ( & correlation) . await ?;
@@ -88,14 +104,19 @@ pub async fn post(req: HttpRequest, body: Bytes) -> Result<impl Responder, Corre
88
104
pub async fn modify ( req : HttpRequest , body : Bytes ) -> Result < impl Responder , CorrelationError > {
89
105
let session_key = extract_session_key_from_req ( & req)
90
106
. 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 ( ) ) ) ) ?;
91
110
92
111
let correlation_id = req
93
112
. match_info ( )
94
113
. get ( "correlation_id" )
95
114
. ok_or ( CorrelationError :: Metadata ( "No correlation ID Provided" ) ) ?;
96
115
97
116
// 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 ?;
99
120
let permissions = Users . get_permissions ( & session_key) ;
100
121
let tables = & correlation
101
122
. table_configs
@@ -108,11 +129,17 @@ pub async fn modify(req: HttpRequest, body: Bytes) -> Result<impl Responder, Cor
108
129
let correlation_request: CorrelationRequest = serde_json:: from_slice ( & body) ?;
109
130
correlation_request. validate ( & session_key) . await ?;
110
131
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) ) ;
112
137
113
- // Save to disk
114
138
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 ?;
116
143
117
144
// Save to memory
118
145
CORRELATIONS . update ( & correlation) . await ?;
@@ -123,13 +150,18 @@ pub async fn modify(req: HttpRequest, body: Bytes) -> Result<impl Responder, Cor
123
150
pub async fn delete ( req : HttpRequest ) -> Result < impl Responder , CorrelationError > {
124
151
let session_key = extract_session_key_from_req ( & req)
125
152
. 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 ( ) ) ) ) ?;
126
156
127
157
let correlation_id = req
128
158
. match_info ( )
129
159
. get ( "correlation_id" )
130
160
. ok_or ( CorrelationError :: Metadata ( "No correlation ID Provided" ) ) ?;
131
161
132
- let correlation = CORRELATIONS . get_correlation_by_id ( correlation_id) . await ?;
162
+ let correlation = CORRELATIONS
163
+ . get_correlation ( correlation_id, & user_id)
164
+ . await ?;
133
165
134
166
// validate user's query auth
135
167
let permissions = Users . get_permissions ( & session_key) ;
@@ -141,12 +173,10 @@ pub async fn delete(req: HttpRequest) -> Result<impl Responder, CorrelationError
141
173
142
174
user_auth_for_query ( & permissions, tables) ?;
143
175
144
- // Delete from disk
176
+ let correlation_id = & correlation. id ;
177
+ let path = correlation_path ( & user_id, & format ! ( "{}.json" , correlation_id) ) ;
178
+
145
179
let store = CONFIG . storage ( ) . get_object_store ( ) ;
146
- let path = RelativePathBuf :: from_iter ( [
147
- CORRELATIONS_ROOT_DIRECTORY ,
148
- & format ! ( "{}.json" , correlation_id) ,
149
- ] ) ;
150
180
store. delete_object ( & path) . await ?;
151
181
152
182
// Delete from memory
0 commit comments