-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathstorages.rs
More file actions
145 lines (124 loc) · 4.35 KB
/
Copy pathstorages.rs
File metadata and controls
145 lines (124 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use sqlx::PgPool;
use uuid::Uuid;
use crate::{
common::{access::check_access, jwt_manager::AuthUser},
errors::{PentaractError, PentaractResult},
models::{
access::{AccessType, UserWithAccess},
storages::{InStorage, Storage, StorageWithInfo},
},
repositories::{access::AccessRepository, storages::StoragesRepository},
schemas::{
access::{GrantAccess, RestrictAccess},
storages::InStorageSchema,
},
};
pub struct StoragesService<'d> {
repo: StoragesRepository<'d>,
access_repo: AccessRepository<'d>,
}
impl<'d> StoragesService<'d> {
pub fn new(db: &'d PgPool) -> Self {
let repo = StoragesRepository::new(db);
let access_repo = AccessRepository::new(db);
Self { repo, access_repo }
}
pub async fn create(
&self,
in_schema: InStorageSchema,
user: &AuthUser,
) -> PentaractResult<Storage> {
// checking if user already has a storage with such name
if let Ok(_) = self
.repo
.get_by_name_and_user_id(&in_schema.name, user.id)
.await
{
return Err(PentaractError::StorageNameConflict);
}
// creating storage
let in_model = InStorage::new(in_schema.name, in_schema.chat_id);
let storage = self.repo.create(in_model).await?;
tracing::debug!(
"[STORAGES SERVICE] Created storage id={}, name={}, chat_id={}",
storage.id,
storage.name,
storage.chat_id
);
// setting user as the storage admin
let access_schema = GrantAccess::new(user.email.clone(), AccessType::A);
let result = self
.access_repo
.create_or_update(storage.id, access_schema)
.await;
match &result {
Ok(_) => {
tracing::debug!(
"[STORAGES SERVICE] Successfully granted access to user {} for storage {}",
user.email,
storage.id
);
}
Err(e) => {
tracing::error!(
"[STORAGES SERVICE] Failed to grant access to user {} for storage {}: {:?}. Rolling back storage creation.",
user.email,
storage.id,
e
);
// fallback
let _ = self.repo.delete_storage(storage.id).await;
}
}
result.map(|_| storage)
}
pub async fn list(&self, user: &AuthUser) -> PentaractResult<Vec<StorageWithInfo>> {
let storages = self.repo.list_by_user_id(user.id).await?;
tracing::debug!(
"[STORAGES SERVICE] Listed {} storages for user_id={}",
storages.len(),
user.id
);
Ok(storages)
}
pub async fn get(&self, id: Uuid, user: &AuthUser) -> PentaractResult<Storage> {
check_access(&self.access_repo, user.id, id, &AccessType::R).await?;
self.repo.get_by_id(id).await
}
pub async fn delete(&self, id: Uuid, user: &AuthUser) -> PentaractResult<()> {
check_access(&self.access_repo, user.id, id, &AccessType::A).await?;
self.repo.delete_storage(id).await
}
pub async fn grant_access(
&self,
id: Uuid,
in_schema: GrantAccess,
user: &AuthUser,
) -> PentaractResult<()> {
check_access(&self.access_repo, user.id, id, &AccessType::A).await?;
if in_schema.user_email == user.email {
return Err(PentaractError::CannotManageAccessOfYourself);
}
self.access_repo.create_or_update(id, in_schema).await
}
pub async fn list_users_with_access(
&self,
id: Uuid,
user: &AuthUser,
) -> PentaractResult<Vec<UserWithAccess>> {
check_access(&self.access_repo, user.id, id, &AccessType::A).await?;
self.access_repo.list_users_with_access(id).await
}
pub async fn restrict_access(
&self,
id: Uuid,
in_schema: RestrictAccess,
user: &AuthUser,
) -> PentaractResult<()> {
check_access(&self.access_repo, user.id, id, &AccessType::A).await?;
if in_schema.user_id == user.id {
return Err(PentaractError::CannotManageAccessOfYourself);
}
self.access_repo.delete_access(in_schema.user_id, id).await
}
}