Skip to content

Commit 669da03

Browse files
committed
server: Add Delete item implementation
Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
1 parent 62375dc commit 669da03

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

server/src/collection.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ impl Collection {
170170
) -> zbus::Result<()>;
171171

172172
#[zbus(signal, name = "ItemDeleted")]
173-
async fn item_deleted(
173+
pub async fn item_deleted(
174174
signal_emitter: &SignalEmitter<'_>,
175-
item: OwnedObjectPath,
175+
item: &OwnedObjectPath,
176176
) -> zbus::Result<()>;
177177

178178
#[zbus(signal, name = "ItemChanged")]
@@ -300,4 +300,39 @@ impl Collection {
300300

301301
Ok(())
302302
}
303+
304+
pub async fn delete_item(&self, path: &OwnedObjectPath) -> Result<(), ServiceError> {
305+
let Some(item) = self.item_from_path(path).await else {
306+
return Err(ServiceError::NoSuchObject(format!(
307+
"Item `{}` does not exist.",
308+
path
309+
)));
310+
};
311+
312+
if item.is_locked().await {
313+
return Err(ServiceError::IsLocked(format!(
314+
"Cannot delete a locked item `{}`",
315+
path
316+
)));
317+
}
318+
319+
let attributes = item.attributes().await;
320+
match self.keyring.delete(&attributes).await {
321+
Ok(_) => tracing::debug!("Item `{}` deleted.", path),
322+
Err(err) => {
323+
return Err(ServiceError::ZBus(zbus::Error::FDO(Box::new(
324+
zbus::fdo::Error::Failed(format!("Failed to deleted item {err}.")),
325+
))))
326+
}
327+
};
328+
329+
let mut items = self.items.lock().await;
330+
items.retain(|item| item.path() != path);
331+
drop(items);
332+
333+
let signal_emitter = self.service.signal_emitter(&self.path)?;
334+
self.items_changed(&signal_emitter).await?;
335+
336+
Ok(())
337+
}
303338
}

server/src/item.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use oo7::{
1010
portal,
1111
};
1212
use tokio::sync::Mutex;
13-
use zbus::zvariant::{ObjectPath, OwnedObjectPath};
13+
use zbus::zvariant::OwnedObjectPath;
1414

1515
use crate::{collection::Collection, Service};
1616

@@ -28,8 +28,30 @@ pub struct Item {
2828
#[zbus::interface(name = "org.freedesktop.Secret.Item")]
2929
impl Item {
3030
#[zbus(out_args("prompt"))]
31-
pub async fn delete(&self) -> Result<ObjectPath, ServiceError> {
32-
todo!()
31+
pub async fn delete(
32+
&self,
33+
#[zbus(object_server)] object_server: &zbus::ObjectServer,
34+
) -> Result<OwnedObjectPath, ServiceError> {
35+
let Some(collection) = self
36+
.service
37+
.collection_from_path(&self.collection_path)
38+
.await
39+
else {
40+
return Err(ServiceError::NoSuchObject(format!(
41+
"Collection `{}` does not exist.",
42+
&self.collection_path
43+
)));
44+
};
45+
46+
collection.delete_item(&self.path).await?;
47+
object_server.remove::<Self, _>(&self.path).await?;
48+
49+
let signal_emitter = self.service.signal_emitter(&self.collection_path)?;
50+
Collection::item_deleted(&signal_emitter, &self.path).await?;
51+
52+
tracing::info!("Item `{}` deleted.", &self.path);
53+
54+
Ok(OwnedObjectPath::default())
3355
}
3456

3557
#[zbus(out_args("secret"))]

server/src/service.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,18 @@ impl Service {
358358
self.connection.object_server()
359359
}
360360

361+
pub async fn collection_from_path(&self, path: &OwnedObjectPath) -> Option<Collection> {
362+
let collections = self.collections.lock().await;
363+
364+
for collection in collections.iter() {
365+
if collection.path() == path {
366+
return Some(collection.clone());
367+
}
368+
}
369+
370+
None
371+
}
372+
361373
pub async fn session(&self, path: &OwnedObjectPath) -> Option<Arc<Session>> {
362374
self.sessions.lock().await.get(path).map(Arc::clone)
363375
}

0 commit comments

Comments
 (0)