|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +use anyhow::{bail, Ok, Result}; |
| 6 | +use clap::{Parser, Subcommand}; |
| 7 | +use rfd_sdk::types::RfdPermission; |
| 8 | + |
| 9 | +use crate::Context; |
| 10 | + |
| 11 | +#[derive(Debug, Parser)] |
| 12 | +pub struct AccessShortcut { |
| 13 | + #[clap(subcommand)] |
| 14 | + pub access: AccessShortcuts, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, Subcommand)] |
| 18 | +pub enum AccessShortcuts { |
| 19 | + /// Grant and revoke access to RFDs |
| 20 | + Rfd(RfdAccessShortcut), |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Parser)] |
| 24 | +pub struct RfdAccessShortcut { |
| 25 | + #[clap(subcommand)] |
| 26 | + pub rfd: RfdAccessShortcuts, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Subcommand)] |
| 30 | +pub enum RfdAccessShortcuts { |
| 31 | + /// Grant access to an RFD |
| 32 | + Add(AddRfdAccessShortcut), |
| 33 | + /// Revoke access to an RFD |
| 34 | + Remove(RemoveRfdAccessShortcut), |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Parser)] |
| 38 | +pub struct AddRfdAccessShortcut { |
| 39 | + /// Group name or id to grant access to |
| 40 | + pub group: String, |
| 41 | + /// RFD to grant access to |
| 42 | + pub number: i32, |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Debug, Parser)] |
| 46 | +pub struct RemoveRfdAccessShortcut { |
| 47 | + /// Group name or id to revoke access to |
| 48 | + pub group: String, |
| 49 | + /// RFD to revoke access to |
| 50 | + pub number: i32, |
| 51 | +} |
| 52 | + |
| 53 | +impl AddRfdAccessShortcut { |
| 54 | + pub async fn run(&self, ctx: &mut Context) -> Result<()> { |
| 55 | + let client = ctx.client()?; |
| 56 | + let groups = client.get_groups().send().await?.into_inner(); |
| 57 | + let group = groups |
| 58 | + .iter() |
| 59 | + .find(|g| g.id.to_string() == self.group) |
| 60 | + .or_else(|| groups.iter().find(|g| g.name == self.group)); |
| 61 | + |
| 62 | + if let Some(mut group) = group.cloned() { |
| 63 | + group.permissions.0.push(RfdPermission::GetRfd(self.number)); |
| 64 | + let response = client |
| 65 | + .update_group() |
| 66 | + .group_id(group.id) |
| 67 | + .body_map(|body| body.name(group.name).permissions(group.permissions)) |
| 68 | + .send() |
| 69 | + .await? |
| 70 | + .into_inner(); |
| 71 | + |
| 72 | + println!("Added access to RFD {} to {}", self.number, self.group); |
| 73 | + Ok(()) |
| 74 | + } else { |
| 75 | + bail!("Unable to find requested group") |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl RemoveRfdAccessShortcut { |
| 81 | + pub async fn run(&self, ctx: &mut Context) -> Result<()> { |
| 82 | + let client = ctx.client()?; |
| 83 | + let groups = client.get_groups().send().await?.into_inner(); |
| 84 | + let group = groups |
| 85 | + .iter() |
| 86 | + .find(|g| g.id.to_string() == self.group) |
| 87 | + .or_else(|| groups.iter().find(|g| g.name == self.group)); |
| 88 | + |
| 89 | + if let Some(mut group) = group.cloned() { |
| 90 | + group.permissions.0 = group |
| 91 | + .permissions |
| 92 | + .0 |
| 93 | + .into_iter() |
| 94 | + .filter(|permission| match permission { |
| 95 | + RfdPermission::GetRfd(number) if *number == self.number => false, |
| 96 | + _ => true, |
| 97 | + }) |
| 98 | + .collect::<Vec<_>>(); |
| 99 | + |
| 100 | + let response = client |
| 101 | + .update_group() |
| 102 | + .group_id(group.id) |
| 103 | + .body_map(|body| body.name(group.name).permissions(group.permissions)) |
| 104 | + .send() |
| 105 | + .await? |
| 106 | + .into_inner(); |
| 107 | + |
| 108 | + println!("Removed access to RFD {} from {}", self.number, self.group); |
| 109 | + Ok(()) |
| 110 | + } else { |
| 111 | + bail!("Unable to find requested group") |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments