1use crate::OpType;
2use crate::{handle_client_error, DomainOpt, KanidmClientParser};
3use anyhow::{Context, Error};
4use kanidm_proto::internal::ImageValue;
5use std::fs::read;
6
7impl DomainOpt {
8 pub async fn exec(&self, opt: KanidmClientParser) {
9 match self {
10 DomainOpt::SetDisplayname(dopt) => {
11 eprintln!(
12 "Attempting to set the domain's display name to: {:?}",
13 dopt.new_display_name
14 );
15 let client = opt.to_client(OpType::Write).await;
16 match client
17 .idm_domain_set_display_name(&dopt.new_display_name)
18 .await
19 {
20 Ok(_) => println!("Success"),
21 Err(e) => handle_client_error(e, opt.output_mode),
22 }
23 }
24 DomainOpt::SetLdapMaxQueryableAttrs {
25 new_max_queryable_attrs,
26 } => {
27 eprintln!(
28 "Attempting to set the maximum number of queryable LDAP attributes to: {new_max_queryable_attrs:?}"
29 );
30 let client = opt.to_client(OpType::Write).await;
31 match client
32 .idm_domain_set_ldap_max_queryable_attrs(*new_max_queryable_attrs)
33 .await
34 {
35 Ok(_) => println!("Success"),
36 Err(e) => handle_client_error(e, opt.output_mode),
37 }
38 }
39 DomainOpt::SetLdapBasedn { new_basedn } => {
40 eprintln!("Attempting to set the domain's ldap basedn to: {new_basedn:?}");
41 let client = opt.to_client(OpType::Write).await;
42 match client.idm_domain_set_ldap_basedn(new_basedn).await {
43 Ok(_) => println!("Success"),
44 Err(e) => handle_client_error(e, opt.output_mode),
45 }
46 }
47 DomainOpt::SetLdapAllowUnixPasswordBind { enable } => {
48 let client = opt.to_client(OpType::Write).await;
49 match client.idm_set_ldap_allow_unix_password_bind(*enable).await {
50 Ok(_) => println!("Success"),
51 Err(e) => handle_client_error(e, opt.output_mode),
52 }
53 }
54 DomainOpt::SetAllowAccountRecovery { enable } => {
55 let client = opt.to_client(OpType::Write).await;
56 match client.idm_set_domain_allow_account_recovery(*enable).await {
57 Ok(_) => {
58 if *enable {
59 println!("Success ✉️")
60 } else {
61 println!("Success")
62 }
63 }
64 Err(e) => handle_client_error(e, opt.output_mode),
65 }
66 }
67 DomainOpt::SetAllowEasterEggs { enable } => {
68 let client = opt.to_client(OpType::Write).await;
69 match client.idm_set_domain_allow_easter_eggs(*enable).await {
70 Ok(_) => {
71 if *enable {
72 println!("Success 🎉 🥚 🎉")
73 } else {
74 println!("Success")
75 }
76 }
77 Err(e) => handle_client_error(e, opt.output_mode),
78 }
79 }
80 DomainOpt::Show => {
81 let client = opt.to_client(OpType::Read).await;
82 match client.idm_domain_get().await {
83 Ok(e) => println!("{e}"),
84 Err(e) => handle_client_error(e, opt.output_mode),
85 }
86 }
87 DomainOpt::RevokeKey { key_id } => {
88 let client = opt.to_client(OpType::Write).await;
89 match client.idm_domain_revoke_key(key_id).await {
90 Ok(_) => println!("Success"),
91 Err(e) => handle_client_error(e, opt.output_mode),
92 }
93 }
94 DomainOpt::SetImage { path, image_type } => {
95 let client = opt.to_client(OpType::Write).await;
96 let img_res: Result<ImageValue, Error> = (move || {
97 let file_name = path
98 .file_name()
99 .context("Please pass a file")?
100 .to_str()
101 .context("Path contains non utf-8")?
102 .to_string();
103
104 let image_type = match image_type {
105 Some(val) => val.clone(),
106 None => {
107 path
108 .extension().context("Path has no extension so we can't infer the imageType, or you could pass the optional imageType argument yourself.")?
109 .to_str().context("Path contains invalid utf-8")?
110 .try_into()
111 .map_err(Error::msg)?
112 }
113 };
114
115 let read_res = read(path);
116 match read_res {
117 Ok(data) => Ok(ImageValue::new(file_name, image_type, data)),
118 Err(err) => {
119 if opt.debug {
120 eprintln!(
121 "{}",
122 kanidm_lib_file_permissions::diagnose_path(path.as_ref())
123 );
124 }
125 Err(err).context(format!("Failed to read file at '{}'", path.display()))
126 }
127 }
128 })();
129
130 let img = match img_res {
131 Ok(img) => img,
132 Err(err) => {
133 eprintln!("{err}");
134 return;
135 }
136 };
137
138 match client.idm_domain_update_image(img).await {
139 Ok(_) => println!("Success"),
140 Err(e) => handle_client_error(e, opt.output_mode),
141 }
142 }
143 DomainOpt::RemoveImage => {
144 let client = opt.to_client(OpType::Write).await;
145
146 match client.idm_domain_delete_image().await {
147 Ok(_) => println!("Success"),
148 Err(e) => handle_client_error(e, opt.output_mode),
149 }
150 }
151 }
152 }
153}