|
| 1 | +use std::{collections::HashSet, fmt::Display}; |
| 2 | + |
| 3 | +use anyhow::{anyhow, bail}; |
| 4 | +use clap::{Args, Subcommand}; |
| 5 | +use editoast_authz::{ |
| 6 | + authorizer::{StorageDriver, UserInfo}, |
| 7 | + roles::BuiltinRoleSet, |
| 8 | + BuiltinRole, |
| 9 | +}; |
| 10 | +use editoast_models::DbConnection; |
| 11 | +use itertools::Itertools as _; |
| 12 | +use strum::IntoEnumIterator; |
| 13 | +use tracing::info; |
| 14 | + |
| 15 | +use crate::models::auth::PgAuthDriver; |
| 16 | + |
| 17 | +#[derive(Debug, Subcommand)] |
| 18 | +pub enum RolesCommand { |
| 19 | + /// Lists the builtin roles supported by editoast |
| 20 | + ListRoles, |
| 21 | + /// Lists the roles assigned to a subject |
| 22 | + List(ListArgs), |
| 23 | + /// Grants builtin roles to a subject |
| 24 | + Add(AddArgs), |
| 25 | + /// Revokes builtin roles from a subject |
| 26 | + Remove(RemoveArgs), |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Args)] |
| 30 | +pub struct ListArgs { |
| 31 | + /// A subject ID or user identity |
| 32 | + subject: String, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Args)] |
| 36 | +pub struct AddArgs { |
| 37 | + /// A subject ID or user identity |
| 38 | + subject: String, |
| 39 | + /// A non-empty list of builtin roles |
| 40 | + roles: Vec<String>, |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug, Args)] |
| 44 | +pub struct RemoveArgs { |
| 45 | + /// A subject ID or user identity |
| 46 | + subject: String, |
| 47 | + /// A non-empty list of builtin roles |
| 48 | + roles: Vec<String>, |
| 49 | +} |
| 50 | + |
| 51 | +pub fn list_roles() { |
| 52 | + BuiltinRole::iter().for_each(|role| println!("{role}")); |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug)] |
| 56 | +struct Subject { |
| 57 | + id: i64, |
| 58 | + info: UserInfo, |
| 59 | +} |
| 60 | + |
| 61 | +impl Display for Subject { |
| 62 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 63 | + let Self { |
| 64 | + id, |
| 65 | + info: UserInfo { identity, name }, |
| 66 | + } = self; |
| 67 | + write!(f, "{identity}#{id} ({name})") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +async fn parse_and_fetch_subject( |
| 72 | + subject: &String, |
| 73 | + driver: &PgAuthDriver<BuiltinRole>, |
| 74 | +) -> anyhow::Result<Subject> { |
| 75 | + let id = if let Ok(id) = subject.parse::<i64>() { |
| 76 | + id |
| 77 | + } else { |
| 78 | + let uid = driver.get_user_id(subject).await?; |
| 79 | + uid.ok_or_else(|| anyhow!("No subject with identity '{subject}' found"))? |
| 80 | + }; |
| 81 | + if let Some(info) = driver.get_user_info(id).await? { |
| 82 | + let subject = Subject { id, info }; |
| 83 | + info!("Subject {subject}"); |
| 84 | + Ok(subject) |
| 85 | + } else { |
| 86 | + bail!("No subject found with ID {id}"); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +pub async fn list_subject_roles( |
| 91 | + ListArgs { subject }: ListArgs, |
| 92 | + conn: DbConnection, |
| 93 | +) -> anyhow::Result<()> { |
| 94 | + let driver = PgAuthDriver::<BuiltinRole>::new(conn); |
| 95 | + let subject = parse_and_fetch_subject(&subject, &driver).await?; |
| 96 | + let roles = driver.fetch_subject_roles(subject.id).await?; |
| 97 | + if roles.is_empty() { |
| 98 | + info!("{subject} has no roles assigned"); |
| 99 | + return Ok(()); |
| 100 | + } |
| 101 | + for role in roles { |
| 102 | + println!("{role}"); |
| 103 | + } |
| 104 | + Ok(()) |
| 105 | +} |
| 106 | + |
| 107 | +fn parse_role_case_insensitive(tag: &String) -> anyhow::Result<BuiltinRole> { |
| 108 | + let tag = tag.to_lowercase(); |
| 109 | + for role in BuiltinRole::iter() { |
| 110 | + if role.as_str().to_lowercase() == tag { |
| 111 | + return Ok(role); |
| 112 | + } |
| 113 | + } |
| 114 | + bail!("Invalid role tag '{tag}'"); |
| 115 | +} |
| 116 | + |
| 117 | +pub async fn add_roles( |
| 118 | + AddArgs { subject, roles }: AddArgs, |
| 119 | + conn: DbConnection, |
| 120 | +) -> anyhow::Result<()> { |
| 121 | + let driver = PgAuthDriver::<BuiltinRole>::new(conn); |
| 122 | + let subject = parse_and_fetch_subject(&subject, &driver).await?; |
| 123 | + let roles = roles |
| 124 | + .iter() |
| 125 | + .map(parse_role_case_insensitive) |
| 126 | + .collect::<Result<HashSet<_>, _>>()?; |
| 127 | + info!( |
| 128 | + "Adding roles {} to {subject}", |
| 129 | + roles |
| 130 | + .iter() |
| 131 | + .map(|role| role.to_string()) |
| 132 | + .collect_vec() |
| 133 | + .join(", "), |
| 134 | + ); |
| 135 | + driver.ensure_subject_roles(subject.id, roles).await?; |
| 136 | + Ok(()) |
| 137 | +} |
| 138 | + |
| 139 | +pub async fn remove_roles( |
| 140 | + RemoveArgs { subject, roles }: RemoveArgs, |
| 141 | + conn: DbConnection, |
| 142 | +) -> anyhow::Result<()> { |
| 143 | + let driver = PgAuthDriver::<BuiltinRole>::new(conn); |
| 144 | + let subject = parse_and_fetch_subject(&subject, &driver).await?; |
| 145 | + let roles = roles |
| 146 | + .iter() |
| 147 | + .map(parse_role_case_insensitive) |
| 148 | + .collect::<Result<HashSet<_>, _>>()?; |
| 149 | + info!( |
| 150 | + "Removing roles {} from {subject}", |
| 151 | + roles |
| 152 | + .iter() |
| 153 | + .map(|role| role.to_string()) |
| 154 | + .collect_vec() |
| 155 | + .join(", "), |
| 156 | + ); |
| 157 | + driver.remove_subject_roles(subject.id, roles).await?; |
| 158 | + Ok(()) |
| 159 | +} |
0 commit comments