Define or replace a scope.
(
&self,
name: &str,
grants: Vec<(String, String)>,
includes: Vec<String>,
created_by: &str,
)
| 81 | |
| 82 | /// Define or replace a scope. |
| 83 | pub fn define( |
| 84 | &self, |
| 85 | name: &str, |
| 86 | grants: Vec<(String, String)>, |
| 87 | includes: Vec<String>, |
| 88 | created_by: &str, |
| 89 | ) -> crate::Result<()> { |
| 90 | // Validate included scopes exist. |
| 91 | { |
| 92 | let scopes = self.scopes.read().unwrap_or_else(|p| p.into_inner()); |
| 93 | for inc in &includes { |
| 94 | if !scopes.contains_key(inc) { |
| 95 | return Err(crate::Error::BadRequest { |
| 96 | detail: format!("included scope '{inc}' does not exist"), |
| 97 | }); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | let def = ScopeDefinition { |
| 103 | name: name.into(), |
| 104 | grants, |
| 105 | includes, |
| 106 | created_by: created_by.into(), |
| 107 | created_at: now_secs(), |
| 108 | }; |
| 109 | |
| 110 | if let Some(ref catalog) = self.catalog { |
| 111 | catalog.put_scope(&def.to_stored())?; |
| 112 | } |
| 113 | |
| 114 | let mut scopes = self.scopes.write().unwrap_or_else(|p| p.into_inner()); |
| 115 | scopes.insert(name.into(), def); |
| 116 | info!(scope = %name, "scope defined"); |
| 117 | Ok(()) |
| 118 | } |
| 119 | |
| 120 | /// Alter a scope's grants and/or includes. |
| 121 | pub fn alter( |