12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- use std::str::FromStr;
- use crate::{client_management, key, schema, UIDCError};
- use microrm::{prelude::*, schema::entity::EntityID};
- #[derive(Debug)]
- pub struct ClientInterface;
- #[derive(Debug, clap::Subcommand)]
- pub enum ClientCommands {
- Create { name: String, key_type: String },
- RotateSecret { name: String },
- AddRedirect { name: String, pattern: String },
- }
- impl microrm::cli::EntityInterface for ClientInterface {
- type Error = UIDCError;
- type Entity = schema::Client;
- type Context = microrm::schema::Stored<schema::Realm>;
- type CustomCommand = ClientCommands;
- fn run_custom(
- ctx: &Self::Context,
- cmd: Self::CustomCommand,
- _query_ctx: impl Queryable<EntityOutput = Self::Entity> + Insertable<Self::Entity>,
- ) -> Result<(), Self::Error> {
- match cmd {
- ClientCommands::Create { name, key_type } => {
- let kt = key::KeyType::from_str(key_type.as_str())?;
- client_management::create(ctx, &name, kt)?;
- }
- ClientCommands::RotateSecret { name } => {
- client_management::rotate_secret(ctx, &name)?;
- }
- ClientCommands::AddRedirect { name, pattern } => {
- client_management::add_redirect(ctx, name.as_str(), pattern.as_str())?;
- }
- }
- Ok(())
- }
- fn should_override(
- _entity: &'static str,
- field: &'static str,
- _role: microrm::cli::ValueRole,
- ) -> bool {
- if field == "realm" {
- true
- } else {
- false
- }
- }
- fn override_for(
- ctx: &Self::Context,
- _entity: &'static str,
- field: &'static str,
- _role: microrm::cli::ValueRole,
- ) -> String {
- if field == "realm" {
- format!("{}", ctx.id().into_raw())
- } else {
- unreachable!()
- }
- }
- }
|