|
@@ -43,7 +43,7 @@ enum Command {
|
|
|
|
|
|
struct RunArgs {
|
|
|
db: microrm::DB,
|
|
|
- realm: RealmID,
|
|
|
+ realm_id: RealmID,
|
|
|
}
|
|
|
|
|
|
impl RootArgs {
|
|
@@ -52,18 +52,22 @@ impl RootArgs {
|
|
|
return self.init().await;
|
|
|
}
|
|
|
|
|
|
- let storage = microrm::DB::new(schema::schema(), &self.db, microrm::CreateMode::MustExist).map_err(|_| UIDCError::Abort("Error accessing database"))?;
|
|
|
+ let db = microrm::DB::new(schema::schema(), &self.db, microrm::CreateMode::MustExist).map_err(|_| UIDCError::Abort("Error accessing database"))?;
|
|
|
+
|
|
|
+ let realm_id = db.query_interface().get().by(schema::Realm::Shortname, self.realm.as_str()).one()?.ok_or(UIDCError::Abort("no such realm"))?.id();
|
|
|
+
|
|
|
+ let ra = RunArgs { db: db, realm_id };
|
|
|
|
|
|
match &self.command {
|
|
|
Command::Init => unreachable!(),
|
|
|
- Command::Config(v) => v.run(&self, storage).await,
|
|
|
- Command::Client(v) => v.run(&self, storage).await,
|
|
|
- Command::Group(v) => v.run(&self, storage).await,
|
|
|
- Command::Key(v) => v.run(&self, storage).await,
|
|
|
- Command::Server(v) => v.run(&self, storage).await,
|
|
|
- Command::Token(v) => v.run(&self, storage).await,
|
|
|
- Command::Role(v) => v.run(&self, storage).await,
|
|
|
- Command::User(v) => v.run(&self, storage).await,
|
|
|
+ Command::Config(v) => v.run(ra).await,
|
|
|
+ Command::Client(v) => v.run(ra).await,
|
|
|
+ Command::Group(v) => v.run(ra).await,
|
|
|
+ Command::Key(v) => v.run(ra).await,
|
|
|
+ Command::Server(v) => v.run(ra).await,
|
|
|
+ Command::Token(v) => v.run(ra).await,
|
|
|
+ Command::Role(v) => v.run(ra).await,
|
|
|
+ Command::User(v) => v.run(ra).await,
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -106,13 +110,13 @@ struct KeyArgs {
|
|
|
}
|
|
|
|
|
|
impl KeyArgs {
|
|
|
- async fn run(&self, root: &RootArgs, db: microrm::DB) -> Result<(), UIDCError> {
|
|
|
+ async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
|
|
|
match &self.command {
|
|
|
KeyCommand::Inspect => {
|
|
|
- key::inspect(&db, &root.realm);
|
|
|
+ key::inspect(&args.db, args.realm_id);
|
|
|
}
|
|
|
KeyCommand::Generate => {
|
|
|
- key::generate(&db, &root.realm);
|
|
|
+ key::generate(&args.db, args.realm_id);
|
|
|
}
|
|
|
}
|
|
|
Ok(())
|
|
@@ -133,14 +137,14 @@ struct ClientArgs {
|
|
|
}
|
|
|
|
|
|
impl ClientArgs {
|
|
|
- async fn run(&self, root: &RootArgs, db: microrm::DB) -> Result<(), UIDCError> {
|
|
|
+ async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
|
|
|
match &self.command {
|
|
|
ClientCommand::Create { name } => {
|
|
|
- client_management::create(&db, root.realm.as_str(), name);
|
|
|
+ client_management::create(&args.db, args.realm_id, name);
|
|
|
}
|
|
|
ClientCommand::List => {}
|
|
|
ClientCommand::Inspect { name } => {
|
|
|
- client_management::inspect(&db, name);
|
|
|
+ client_management::inspect(&args.db, args.realm_id, name);
|
|
|
}
|
|
|
}
|
|
|
Ok(())
|
|
@@ -161,10 +165,11 @@ struct ConfigArgs {
|
|
|
}
|
|
|
|
|
|
impl ConfigArgs {
|
|
|
- async fn run(&self, root: &RootArgs, db: microrm::DB) -> Result<(), UIDCError> {
|
|
|
+ async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
|
|
|
+ let qi = args.db.query_interface();
|
|
|
+
|
|
|
match &self.command {
|
|
|
ConfigCommand::Dump => {
|
|
|
- let qi = db.query_interface();
|
|
|
let config = config::Config::build_from(&qi, None);
|
|
|
println!("config: {:?}", config);
|
|
|
}
|
|
@@ -172,15 +177,8 @@ impl ConfigArgs {
|
|
|
todo!()
|
|
|
}
|
|
|
ConfigCommand::Load { toml_path } => {
|
|
|
- let config = {
|
|
|
- let qi = db.query_interface();
|
|
|
- config::Config::build_from(&qi, Some(toml_path))
|
|
|
- };
|
|
|
- {
|
|
|
- let qi = db.query_interface();
|
|
|
- config.save(&qi);
|
|
|
- drop(config);
|
|
|
- }
|
|
|
+ let config = config::Config::build_from(&qi, Some(toml_path));
|
|
|
+ config.save(&qi);
|
|
|
}
|
|
|
}
|
|
|
Ok(())
|
|
@@ -204,13 +202,12 @@ struct GroupArgs {
|
|
|
}
|
|
|
|
|
|
impl GroupArgs {
|
|
|
- async fn run(&self, root: &RootArgs, db: microrm::DB) -> Result<(), UIDCError> {
|
|
|
- let qi = db.query_interface();
|
|
|
- let realm_id = qi.get().by(schema::Realm::Shortname, root.realm.as_str()).one().unwrap().expect("no such realm").id();
|
|
|
+ async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
|
|
|
+ let qi = args.db.query_interface();
|
|
|
match &self.command {
|
|
|
GroupCommand::Create { group_name } => {
|
|
|
match qi.add(&schema::Group {
|
|
|
- realm: realm_id,
|
|
|
+ realm: args.realm_id,
|
|
|
shortname: group_name.clone(),
|
|
|
}) {
|
|
|
Ok(_) => {
|
|
@@ -225,8 +222,8 @@ impl GroupArgs {
|
|
|
todo!()
|
|
|
},
|
|
|
GroupCommand::AttachRole { group_name, role_name } => {
|
|
|
- let group = qi.get().by(schema::Group::Realm, &realm_id).by(schema::Group::Shortname, group_name).one().unwrap();
|
|
|
- let role = qi.get().by(schema::Role::Realm, &realm_id).by(schema::Role::Shortname, role_name).one().unwrap();
|
|
|
+ let group = qi.get().by(schema::Group::Realm, &args.realm_id).by(schema::Group::Shortname, group_name).one().unwrap();
|
|
|
+ let role = qi.get().by(schema::Role::Realm, &args.realm_id).by(schema::Role::Shortname, role_name).one().unwrap();
|
|
|
|
|
|
match (group, role) {
|
|
|
(None, _) => {
|
|
@@ -255,8 +252,8 @@ impl GroupArgs {
|
|
|
|
|
|
},
|
|
|
GroupCommand::AttachUser { group_name, username } => {
|
|
|
- let group = qi.get().by(schema::Group::Realm, &realm_id).by(schema::Group::Shortname, group_name).one().unwrap();
|
|
|
- let user = qi.get().by(schema::User::Realm, &realm_id).by(schema::User::Username, username).one().unwrap();
|
|
|
+ let group = qi.get().by(schema::Group::Realm, &args.realm_id).by(schema::Group::Shortname, group_name).one().unwrap();
|
|
|
+ let user = qi.get().by(schema::User::Realm, &args.realm_id).by(schema::User::Username, username).one().unwrap();
|
|
|
|
|
|
match (group, user) {
|
|
|
(None, _) => {
|
|
@@ -295,9 +292,9 @@ struct ServerArgs {
|
|
|
}
|
|
|
|
|
|
impl ServerArgs {
|
|
|
- async fn run(&self, root: &RootArgs, db: microrm::DB) -> Result<(), UIDCError> {
|
|
|
- let config = config::Config::build_from(&db.query_interface(), None);
|
|
|
- server::run_server(db, config, self.port.unwrap_or(2114)).await
|
|
|
+ async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
|
|
|
+ let config = config::Config::build_from(&args.db.query_interface(), None);
|
|
|
+ server::run_server(args.db, config, self.port.unwrap_or(2114)).await
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -331,35 +328,28 @@ struct TokenArgs {
|
|
|
}
|
|
|
|
|
|
impl TokenArgs {
|
|
|
- async fn run(&self, root: &RootArgs, db: microrm::DB) -> Result<(), UIDCError> {
|
|
|
- let config = config::Config::build_from(&db.query_interface(), None);
|
|
|
+ async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
|
|
|
+ let config = config::Config::build_from(&args.db.query_interface(), None);
|
|
|
match &self.command {
|
|
|
TokenCommand::GenerateAuth {
|
|
|
client,
|
|
|
username,
|
|
|
scopes,
|
|
|
} => {
|
|
|
- let qi = db.query_interface();
|
|
|
- let realm_id = qi
|
|
|
- .get()
|
|
|
- .by(schema::Realm::Shortname, &root.realm)
|
|
|
- .one()
|
|
|
- .unwrap()
|
|
|
- .expect("no such realm")
|
|
|
- .id();
|
|
|
+ let qi = args.db.query_interface();
|
|
|
let token = token::generate_auth_token(
|
|
|
&config,
|
|
|
&qi,
|
|
|
- realm_id,
|
|
|
+ args.realm_id,
|
|
|
qi.get()
|
|
|
- .by(schema::Client::Realm, &realm_id)
|
|
|
+ .by(schema::Client::Realm, &args.realm_id)
|
|
|
.by(schema::Client::Shortname, client.as_str())
|
|
|
.one()
|
|
|
.unwrap()
|
|
|
.expect("no such client")
|
|
|
.id(),
|
|
|
qi.get()
|
|
|
- .by(schema::User::Realm, &realm_id)
|
|
|
+ .by(schema::User::Realm, &args.realm_id)
|
|
|
.by(schema::User::Username, username.as_str())
|
|
|
.one()
|
|
|
.unwrap()
|
|
@@ -401,17 +391,16 @@ struct RoleArgs {
|
|
|
}
|
|
|
|
|
|
impl RoleArgs {
|
|
|
- async fn run(&self, root: &RootArgs, db: microrm::DB) -> Result<(), UIDCError> {
|
|
|
- let config = config::Config::build_from(&db.query_interface(), None);
|
|
|
+ async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
|
|
|
+ let qi = args.db.query_interface();
|
|
|
+ // let config = config::Config::build_from(&qi, None);
|
|
|
match &self.command {
|
|
|
RoleCommand::List => {
|
|
|
todo!()
|
|
|
},
|
|
|
RoleCommand::Create { name } => {
|
|
|
- let qi = db.query_interface();
|
|
|
- let realm = qi.get().by(schema::Realm::Shortname, &root.realm).one().unwrap().unwrap();
|
|
|
let add_result = qi.add(&schema::Role {
|
|
|
- realm: realm.id(),
|
|
|
+ realm: args.realm_id,
|
|
|
shortname: name.clone()
|
|
|
});
|
|
|
|
|
@@ -425,9 +414,7 @@ impl RoleArgs {
|
|
|
}
|
|
|
},
|
|
|
RoleCommand::Delete { name } => {
|
|
|
- let qi = db.query_interface();
|
|
|
- let realm = qi.get().by(schema::Realm::Shortname, &root.realm).one().unwrap().unwrap();
|
|
|
- qi.delete().by(schema::Role::Realm, &realm.id()).by(schema::Role::Shortname, name.as_str()).exec().unwrap();
|
|
|
+ qi.delete().by(schema::Role::Realm, &args.realm_id).by(schema::Role::Shortname, name.as_str()).exec().unwrap();
|
|
|
},
|
|
|
}
|
|
|
Ok(())
|
|
@@ -458,19 +445,20 @@ struct UserArgs {
|
|
|
}
|
|
|
|
|
|
impl UserArgs {
|
|
|
- async fn run(&self, root: &RootArgs, db: microrm::DB) -> Result<(), UIDCError> {
|
|
|
+ async fn run(&self, args: RunArgs) -> Result<(), UIDCError> {
|
|
|
+ let qi = args.db.query_interface();
|
|
|
match &self.command {
|
|
|
- UserCommand::List => user_management::list(&root.realm, db),
|
|
|
+ UserCommand::List => user_management::list(&qi, args.realm_id),
|
|
|
UserCommand::Create { username } => {
|
|
|
- user_management::create(&root.realm, db, username.as_str())
|
|
|
+ user_management::create(&qi, args.realm_id, username.as_str())
|
|
|
}
|
|
|
UserCommand::Auth { username, change_password } => user_management::change_auth(
|
|
|
- &root.realm,
|
|
|
- db,
|
|
|
+ &qi,
|
|
|
+ args.realm_id,
|
|
|
username.as_str(),
|
|
|
*change_password > 0,
|
|
|
),
|
|
|
- UserCommand::Inspect { username } => user_management::inspect(&root.realm, db, username.as_str()),
|
|
|
+ UserCommand::Inspect { username } => user_management::inspect(&qi, args.realm_id, username.as_str()),
|
|
|
}
|
|
|
}
|
|
|
}
|