1use crate::provider_access::{adopt, finish, read, retake};
3use crate::{
4 DbError, NewOperation, OperationId, PendingValue, ProviderAccess, ProviderAccessChange,
5 ProviderAccessRequest,
6};
7use coven_crypto::SecretBytes;
8
9impl crate::StoreDatabase {
10 pub async fn provider_access(
12 &self,
13 account: String,
14 ) -> Result<Option<ProviderAccess>, DbError> {
15 self.call(move |inner| inner.access.with_writer(|db, _| read(db, &account))).await
16 }
17
18 pub async fn start_operation_with_access<P: PendingValue>(
20 &self,
21 operation: NewOperation,
22 changes: Vec<ProviderAccessChange>,
23 ) -> Result<OperationId, DbError> {
24 self.call(move |inner| {
25 inner.access.with_writer(|db, _| {
26 db.transaction(|db| {
27 let id = crate::operation::insert(db, &operation)?;
28 adopt::<P>(db, &changes)?;
29 Ok(id)
30 })
31 })
32 })
33 .await
34 }
35
36 pub async fn advance_operation_with_access<P: PendingValue>(
41 &self,
42 update: crate::OperationUpdate,
43 changes: Vec<ProviderAccessChange>,
44 child: Option<OperationId>,
45 ) -> Result<(), DbError> {
46 self.call(move |inner| {
47 inner.access.with_writer(|db, _| {
48 db.transaction(|db| {
49 crate::operation::advance(db, &update)?;
50 if let Some(child) = child {
51 crate::operation::finish_with_outcome(db, child, None)?;
52 }
53 adopt::<P>(db, &changes)
54 })
55 })
56 })
57 .await
58 }
59
60 pub async fn retake_provider_access<P: PendingValue>(
63 &self,
64 changes: Vec<ProviderAccessChange>,
65 ) -> Result<(), DbError> {
66 self.call(move |inner| {
67 inner.access.with_writer(|db, _| db.transaction(|db| retake::<P>(db, &changes)))
68 })
69 .await
70 }
71
72 pub async fn start_provider_request<P: PendingValue>(
75 &self,
76 account: String,
77 id: OperationId,
78 ) -> Result<bool, DbError> {
79 self.call(move |inner| {
80 inner.access.with_writer(|db, _| {
81 db.transaction(|db| {
82 let state = read(db, &account)?.ok_or(DbError::OperationChanged(id))?;
83 if state.request.as_ref().map(|r| r.operation) != Some(id) {
84 return Err(DbError::OperationChanged(id));
85 }
86 let record =
87 crate::operation::find(db, id)?.ok_or(DbError::OperationChanged(id))?;
88 if record.last_step == crate::provider_access::REQUEST_FIXED_STEP {
89 return Err(DbError::OperationChanged(id));
90 }
91 crate::provider_access::fix_target(db, &account)?;
92 crate::operation::set_step(db, id, crate::provider_access::REQUEST_FIXED_STEP)?;
93 crate::retry::clear(db, crate::RetrySubject::Operation(id))?;
94 crate::pending::save(db, &P::provider_access(id, account, state.intended))?;
95 Ok(state.intended)
96 })
97 })
98 })
99 .await
100 }
101
102 pub async fn fail_provider_request<P: PendingValue>(
109 &self,
110 account: String,
111 id: OperationId,
112 data: Vec<u8>,
113 pending: P,
114 ) -> Result<bool, DbError> {
115 let update = crate::OperationUpdate {
116 id,
117 previous: crate::provider_access::REQUEST_FIXED_STEP,
118 last_step: crate::provider_access::REQUEST_FAILED_STEP,
119 data,
120 };
121 self.call(move |inner| {
122 inner.access.with_writer(|db, _| {
123 db.transaction(|db| {
124 let state = read(db, &account)?.ok_or(DbError::OperationChanged(update.id))?;
125 let request = state.request.ok_or(DbError::OperationChanged(update.id))?;
126 if request.operation != update.id || pending.operation() != Some(update.id) {
127 return Err(DbError::Invariant {
128 what: "a provider request's condition names its own operation",
129 });
130 }
131 let target = request.target.ok_or(DbError::OperationChanged(update.id))?;
132 if state.intended != target {
133 crate::provider_access::requeue(db, &account, update.id)?;
134 crate::pending::save(
135 db,
136 &P::provider_access(update.id, account, state.intended),
137 )?;
138 return Ok(false);
139 }
140 crate::operation::advance(db, &update)?;
141 crate::pending::save(db, &pending)?;
142 Ok(true)
143 })
144 })
145 })
146 .await
147 }
148
149 pub async fn advance_operation_refusing_grant(
156 &self,
157 update: crate::OperationUpdate,
158 account: String,
159 grant: OperationId,
160 ) -> Result<(), DbError> {
161 self.call(move |inner| {
162 inner.access.with_writer(|db, _| {
163 db.transaction(|db| {
164 crate::operation::advance(db, &update)?;
165 let state = read(db, &account)?.ok_or(DbError::OperationChanged(grant))?;
166 if state.request
167 != Some(ProviderAccessRequest { operation: grant, target: Some(true) })
168 {
169 return Err(DbError::OperationChanged(grant));
170 }
171 crate::provider_access::confirm(db, &account, false, None)?;
172 finish(db, &account, grant)
173 })
174 })
175 })
176 .await
177 }
178
179 pub async fn complete_provider_request<P: PendingValue>(
182 &self,
183 account: String,
184 id: OperationId,
185 target: bool,
186 invitation: Option<SecretBytes>,
187 ) -> Result<bool, DbError> {
188 self.call(move |inner| {
189 inner.access.with_writer(|db, _| {
190 db.transaction(|db| {
191 let state = read(db, &account)?.ok_or(DbError::OperationChanged(id))?;
192 if state.request
193 != Some(ProviderAccessRequest { operation: id, target: Some(target) })
194 {
195 return Err(DbError::OperationChanged(id));
196 }
197 crate::provider_access::confirm(db, &account, target, invitation.as_ref())?;
198 if state.intended == target {
199 finish(db, &account, id)?;
200 Ok(true)
201 } else {
202 crate::provider_access::requeue(db, &account, id)?;
203 crate::pending::save(db, &P::provider_access(id, account, state.intended))?;
204 Ok(false)
205 }
206 })
207 })
208 })
209 .await
210 }
211}
212
213#[cfg(test)]
214#[path = "database_provider_access_tests.rs"]
215mod tests;