1use crate::common::OpType;
2use crate::{handle_client_error, GroupOpt, GroupPosix, OutputMode};
3use kanidm_proto::constants::ATTR_GIDNUMBER;
4
5mod account_policy;
6
7impl GroupOpt {
8 pub fn debug(&self) -> bool {
9 match self {
10 GroupOpt::List(copt) | GroupOpt::Search { copt, .. } => copt.debug,
11 GroupOpt::Get(gcopt) => gcopt.copt.debug,
12 GroupOpt::SetEntryManagedBy { copt, .. } | GroupOpt::Create { copt, .. } => copt.debug,
13 GroupOpt::Delete(gcopt) => gcopt.copt.debug,
14 GroupOpt::ListMembers(gcopt) => gcopt.copt.debug,
15 GroupOpt::AddMembers(gcopt) => gcopt.copt.debug,
16 GroupOpt::RemoveMembers(gcopt) => gcopt.copt.debug,
17 GroupOpt::SetMembers(gcopt) => gcopt.copt.debug,
18 GroupOpt::PurgeMembers(gcopt) => gcopt.copt.debug,
19 GroupOpt::SetDescription { copt, .. }
20 | GroupOpt::Rename { copt, .. }
21 | GroupOpt::SetMail { copt, .. } => copt.debug,
22 GroupOpt::Posix { commands } => match commands {
23 GroupPosix::Show(gcopt) => gcopt.copt.debug,
24 GroupPosix::Set(gcopt) => gcopt.copt.debug,
25 GroupPosix::ResetGidnumber { copt, .. } => copt.debug,
26 },
27 GroupOpt::AccountPolicy { commands } => commands.debug(),
28 }
29 }
30
31 pub async fn exec(&self) {
32 match self {
33 GroupOpt::List(copt) => {
34 let client = copt.to_client(OpType::Read).await;
35 match client.idm_group_list().await {
36 Ok(r) => match copt.output_mode {
37 OutputMode::Json => {
38 let r_attrs: Vec<_> = r.iter().map(|entry| &entry.attrs).collect();
39 println!(
40 "{}",
41 serde_json::to_string(&r_attrs).expect("Failed to serialise json")
42 );
43 }
44 OutputMode::Text => r.iter().for_each(|ent| println!("{}", ent)),
45 },
46 Err(e) => handle_client_error(e, copt.output_mode),
47 }
48 }
49 GroupOpt::Search { copt, name } => {
50 let client = copt.to_client(OpType::Read).await;
51 match client.idm_group_search(name).await {
52 Ok(r) => match copt.output_mode {
53 OutputMode::Json => {
54 let r_attrs: Vec<_> = r.iter().map(|entry| &entry.attrs).collect();
55 println!(
56 "{}",
57 serde_json::to_string(&r_attrs).expect("Failed to serialise json")
58 );
59 }
60 OutputMode::Text => r.iter().for_each(|ent| println!("{}", ent)),
61 },
62 Err(e) => handle_client_error(e, copt.output_mode),
63 }
64 }
65 GroupOpt::Get(gcopt) => {
66 let client = gcopt.copt.to_client(OpType::Read).await;
67 match client.idm_group_get(gcopt.name.as_str()).await {
69 Ok(Some(e)) => match gcopt.copt.output_mode {
70 OutputMode::Json => {
71 println!(
72 "{}",
73 serde_json::to_string(&e.attrs).expect("Failed to serialise json")
74 );
75 }
76 OutputMode::Text => println!("{}", e),
77 },
78 Ok(None) => warn!("No matching group '{}'", gcopt.name.as_str()),
79 Err(e) => handle_client_error(e, gcopt.copt.output_mode),
80 }
81 }
82 GroupOpt::Create {
83 copt,
84 name,
85 entry_managed_by,
86 } => {
87 let client = copt.to_client(OpType::Write).await;
88 match client
89 .idm_group_create(name.as_str(), entry_managed_by.as_deref())
90 .await
91 {
92 Err(err) => {
93 error!("Error -> {:?}", err)
94 }
95 Ok(_) => println!("Successfully created group '{}'", name.as_str()),
96 }
97 }
98 GroupOpt::Delete(gcopt) => {
99 let client = gcopt.copt.to_client(OpType::Write).await;
100 match client.idm_group_delete(gcopt.name.as_str()).await {
101 Err(e) => handle_client_error(e, gcopt.copt.output_mode),
102 Ok(_) => println!("Successfully deleted group {}", gcopt.name.as_str()),
103 }
104 }
105 GroupOpt::PurgeMembers(gcopt) => {
106 let client = gcopt.copt.to_client(OpType::Write).await;
107 match client.idm_group_purge_members(gcopt.name.as_str()).await {
108 Err(e) => handle_client_error(e, gcopt.copt.output_mode),
109 Ok(_) => println!(
110 "Successfully purged members of group {}",
111 gcopt.name.as_str()
112 ),
113 }
114 }
115 GroupOpt::ListMembers(gcopt) => {
116 let client = gcopt.copt.to_client(OpType::Read).await;
117 match client.idm_group_get_members(gcopt.name.as_str()).await {
118 Ok(Some(groups)) => groups.iter().for_each(|m| println!("{:?}", m)),
119 Ok(None) => warn!("No members in group {}", gcopt.name.as_str()),
120 Err(e) => handle_client_error(e, gcopt.copt.output_mode),
121 }
122 }
123 GroupOpt::AddMembers(gcopt) => {
124 let client = gcopt.copt.to_client(OpType::Write).await;
125 let new_members: Vec<&str> = gcopt.members.iter().map(String::as_str).collect();
126
127 match client
128 .idm_group_add_members(gcopt.name.as_str(), &new_members)
129 .await
130 {
131 Err(e) => handle_client_error(e, gcopt.copt.output_mode),
132 Ok(_) => println!(
133 "Successfully added {:?} to group \"{}\"",
134 &new_members,
135 gcopt.name.as_str()
136 ),
137 }
138 }
139
140 GroupOpt::RemoveMembers(gcopt) => {
141 let client = gcopt.copt.to_client(OpType::Write).await;
142 let remove_members: Vec<&str> = gcopt.members.iter().map(String::as_str).collect();
143
144 match client
145 .idm_group_remove_members(gcopt.name.as_str(), &remove_members)
146 .await
147 {
148 Err(e) => {
149 error!("Failed to remove members!");
150 handle_client_error(e, gcopt.copt.output_mode)
151 }
152 Ok(_) => println!("Successfully removed members from {}", gcopt.name.as_str()),
153 }
154 }
155 GroupOpt::SetMembers(gcopt) => {
156 let client = gcopt.copt.to_client(OpType::Write).await;
157 let new_members: Vec<&str> = gcopt.members.iter().map(String::as_str).collect();
158
159 match client
160 .idm_group_set_members(gcopt.name.as_str(), &new_members)
161 .await
162 {
163 Err(e) => handle_client_error(e, gcopt.copt.output_mode),
164 Ok(_) => println!("Successfully set members for group {}", gcopt.name.as_str()),
165 }
166 }
167 GroupOpt::SetMail { copt, name, mail } => {
168 let client = copt.to_client(OpType::Write).await;
169
170 let result = if mail.is_empty() {
171 client.idm_group_purge_mail(name.as_str()).await
172 } else {
173 client
174 .idm_group_set_mail(name.as_str(), mail.as_slice())
175 .await
176 };
177
178 match result {
179 Err(e) => handle_client_error(e, copt.output_mode),
180 Ok(_) => println!("Successfully set mail for group {}", name.as_str()),
181 }
182 }
183 GroupOpt::SetDescription {
184 copt,
185 name,
186 description,
187 } => {
188 let client = copt.to_client(OpType::Write).await;
189
190 let result = if let Some(description) = description {
191 client
192 .idm_group_set_description(name.as_str(), description.as_str())
193 .await
194 } else {
195 client.idm_group_purge_description(name.as_str()).await
196 };
197
198 match result {
199 Err(e) => handle_client_error(e, copt.output_mode),
200 Ok(_) => println!("Successfully set description for group {}", name.as_str()),
201 }
202 }
203 GroupOpt::Rename {
204 copt,
205 name,
206 new_name,
207 } => {
208 let client = copt.to_client(OpType::Write).await;
209
210 let result = client.group_rename(name.as_str(), new_name.as_str()).await;
211
212 match result {
213 Err(e) => handle_client_error(e, copt.output_mode),
214 Ok(_) => println!("Successfully renamed group {} to {}", name, new_name),
215 }
216 }
217 GroupOpt::SetEntryManagedBy {
218 name,
219 entry_managed_by,
220 copt,
221 } => {
222 let client = copt.to_client(OpType::Write).await;
223
224 match client
225 .idm_group_set_entry_managed_by(name, entry_managed_by)
226 .await
227 {
228 Err(e) => handle_client_error(e, copt.output_mode),
229 Ok(_) => println!(
230 "Successfully set entry manager to '{}' for group '{}'",
231 entry_managed_by, name
232 ),
233 }
234 }
235 GroupOpt::Posix { commands } => match commands {
236 GroupPosix::Show(gcopt) => {
237 let client = gcopt.copt.to_client(OpType::Read).await;
238 match client.idm_group_unix_token_get(gcopt.name.as_str()).await {
239 Ok(token) => println!("{}", token),
240 Err(e) => handle_client_error(e, gcopt.copt.output_mode),
241 }
242 }
243 GroupPosix::Set(gcopt) => {
244 let client = gcopt.copt.to_client(OpType::Write).await;
245 match client
246 .idm_group_unix_extend(gcopt.name.as_str(), gcopt.gidnumber)
247 .await
248 {
249 Err(e) => handle_client_error(e, gcopt.copt.output_mode),
250 Ok(_) => println!(
251 "Success adding POSIX configuration for group {}",
252 gcopt.name.as_str()
253 ),
254 }
255 }
256 GroupPosix::ResetGidnumber { copt, group_id } => {
257 let client = copt.to_client(OpType::Write).await;
258 if let Err(e) = client
259 .idm_group_purge_attr(group_id.as_str(), ATTR_GIDNUMBER)
260 .await
261 {
262 handle_client_error(e, copt.output_mode)
263 }
264 }
265 },
266 GroupOpt::AccountPolicy { commands } => commands.exec().await,
267 } }
269}