malwaredb_server/db/
admin.rs1use chrono::{DateTime, Utc};
4use std::fmt::{Display, Formatter};
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct User {
9 pub id: u32,
11
12 pub email: String,
14
15 pub uname: String,
17
18 pub fname: String,
20
21 pub lname: String,
23
24 pub org: Option<String>,
26
27 pub phone: Option<String>,
29
30 pub has_password: bool,
32
33 pub has_api_key: bool,
35
36 pub created: DateTime<Utc>,
38
39 pub is_readonly: bool,
41}
42
43impl Display for User {
44 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45 let has_password = {
46 if self.has_password {
47 "password set"
48 } else {
49 "no password"
50 }
51 };
52 let has_api_key = {
53 if self.has_api_key {
54 "API key set"
55 } else {
56 "no API key"
57 }
58 };
59 let org = {
60 if let Some(org) = &self.org {
61 format!(" ({org}) ")
62 } else {
63 String::new()
64 }
65 };
66 let phone = {
67 if let Some(ph) = &self.phone {
68 format!(" {ph} ")
69 } else {
70 String::new()
71 }
72 };
73
74 let readonly = if self.is_readonly { " read only" } else { "" };
75
76 write!(
77 f,
78 "{}: {} {} {}<{}>,{phone}{org}{has_password}, {has_api_key} created {}{readonly}",
79 self.id, self.fname, self.lname, self.uname, self.email, self.created
80 )
81 }
82}
83
84#[derive(Clone, Debug)]
87pub struct Group {
88 pub id: u32,
90
91 pub name: String,
93
94 pub description: Option<String>,
96
97 pub parent: Option<String>,
99
100 pub members: Vec<User>,
102
103 pub sources: Vec<Source>,
105
106 pub files: u32,
108}
109
110impl Display for Group {
111 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
112 let parent = if let Some(p) = &self.parent {
113 format!("Parent: {p}")
114 } else {
115 String::new()
116 };
117
118 let sources_label = match self.sources.len() {
119 0 => "no sources".into(),
120 1 => "one source".into(),
121 x => format!("{x} sources:"),
122 };
123
124 let members_label = match self.members.len() {
125 0 => "no members".into(),
126 1 => "one member".into(),
127 x => format!("{x} members:"),
128 };
129
130 if let Some(desc) = &self.description {
131 writeln!(
132 f,
133 "{}: {} {parent} {} files -- {desc} {members_label} {sources_label}",
134 self.id, self.name, self.files
135 )?;
136 } else {
137 writeln!(
138 f,
139 "{}: {} {parent} {} files -- {members_label} {sources_label}",
140 self.id, self.name, self.files
141 )?;
142 }
143
144 writeln!(f, "Members:")?;
145 for user in &self.members {
146 writeln!(f, "\t{user}")?;
147 }
148
149 writeln!(f, "Sources:")?;
150 for source in &self.sources {
151 writeln!(f, "\t{source}")?;
152 }
153
154 Ok(())
155 }
156}
157
158#[derive(Clone, Debug, PartialEq, Eq)]
160pub struct Source {
161 pub id: u32,
163
164 pub name: String,
166
167 pub description: Option<String>,
169
170 pub url: Option<String>,
172
173 pub date: DateTime<chrono::Local>,
175
176 pub parent: Option<String>,
178
179 pub files: u64,
181
182 pub groups: u32,
184
185 pub malicious: Option<bool>,
187}
188
189impl Display for Source {
190 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
191 write!(f, "{}: {}", self.id, self.name)?;
192 if let Some(url) = &self.url {
193 write!(f, " {url}")?;
194 }
195 if let Some(desc) = &self.description {
196 write!(f, " {desc}")?;
197 }
198 if let Some(is_malicious) = &self.malicious {
199 write!(f, ", known malicious {is_malicious},")?;
200 }
201 if let Some(parent) = &self.parent {
202 write!(f, ", parent {parent},")?;
203 }
204 write!(f, " {} files and {} groups from {}", self.files, self.groups, self.date)?;
205 Ok(())
206 }
207}