1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
use crate::errors::*; use crate::fmt::colors::*; use diesel; use diesel::prelude::*; use crate::models::*; #[derive(Identifiable, Queryable, Serialize, Deserialize, PartialEq, Debug)] #[table_name="breaches"] pub struct Breach { pub id: i32, pub value: String, pub unscoped: bool, } impl Model for Breach { type ID = str; fn to_string(&self) -> String { self.value.to_owned() } fn list(db: &Database) -> Result<Vec<Self>> { use crate::schema::breaches::dsl::*; let results = breaches.load::<Self>(db.db())?; Ok(results) } fn filter(db: &Database, filter: &Filter) -> Result<Vec<Self>> { use crate::schema::breaches::dsl::*; let query = breaches.filter(filter.sql()); let results = query.load::<Self>(db.db())?; Ok(results) } fn delete(db: &Database, filter: &Filter) -> Result<usize> { use crate::schema::breaches::dsl::*; diesel::delete(breaches.filter(filter.sql())) .execute(db.db()) .map_err(Error::from) } fn delete_id(db: &Database, my_id: i32) -> Result<usize> { use crate::schema::breaches::dsl::*; diesel::delete(breaches.filter(id.eq(my_id))) .execute(db.db()) .map_err(Error::from) } fn id(&self) -> i32 { self.id } fn value(&self) -> &Self::ID { &self.value } fn by_id(db: &Database, my_id: i32) -> Result<Self> { use crate::schema::breaches::dsl::*; let domain = breaches.filter(id.eq(my_id)) .first::<Self>(db.db())?; Ok(domain) } fn get(db: &Database, query: &Self::ID) -> Result<Self> { use crate::schema::breaches::dsl::*; let breach = breaches.filter(value.eq(query)) .first::<Self>(db.db())?; Ok(breach) } fn get_opt(db: &Database, query: &Self::ID) -> Result<Option<Self>> { use crate::schema::breaches::dsl::*; let breach = breaches.filter(value.eq(query)) .first::<Self>(db.db()) .optional()?; Ok(breach) } } impl Scopable for Breach { fn scoped(&self) -> bool { !self.unscoped } fn scope(db: &Database, filter: &Filter) -> Result<usize> { use crate::schema::breaches::dsl::*; diesel::update(breaches.filter(filter.sql())) .set(unscoped.eq(false)) .execute(db.db()) .map_err(Error::from) } fn noscope(db: &Database, filter: &Filter) -> Result<usize> { use crate::schema::breaches::dsl::*; diesel::update(breaches.filter(filter.sql())) .set(unscoped.eq(true)) .execute(db.db()) .map_err(Error::from) } } impl Breach { fn emails(&self, db: &Database) -> Result<Vec<(Email, Option<String>)>> { use std::result; let email_id_pws = BreachEmail::belonging_to(self) .select((breach_emails::email_id, breach_emails::password)) .load::<(i32, Option<String>)>(db.db())?; email_id_pws.into_iter() .map(|(email_id, password)| { emails::table .filter(emails::id.eq(email_id)) .first::<Email>(db.db()) .map(|email| (email, password)) }) .collect::<result::Result<Vec<_>, _>>() .map_err(Error::from) } } pub struct PrintableBreach { value: String, } impl fmt::Display for PrintableBreach { fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { write!(w, "{:?}", self.value) } } impl Printable<PrintableBreach> for Breach { fn printable(&self, _db: &Database) -> Result<PrintableBreach> { Ok(PrintableBreach { value: self.value.to_string(), }) } } pub struct EmailWithPassword { email: PrintableEmail, password: Option<String>, } impl fmt::Display for EmailWithPassword { fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { write!(w, "{}", self.email)?; if let Some(password) = &self.password { write!(w, " ({:?})", password)?; } Ok(()) } } pub struct DetailedBreach { id: i32, value: String, emails: Vec<EmailWithPassword>, unscoped: bool, } impl DisplayableDetailed for DetailedBreach { #[inline] fn scoped(&self) -> bool { !self.unscoped } #[inline] fn print(&self, w: &mut fmt::DetailFormatter) -> fmt::Result { w.id(self.id)?; w.debug::<Green, _>(&self.value)?; Ok(()) } #[inline] fn children(&self, w: &mut fmt::DetailFormatter) -> fmt::Result { for email in &self.emails { w.child(email)?; } Ok(()) } } display_detailed!(DetailedBreach); impl Detailed for Breach { type T = DetailedBreach; fn detailed(&self, db: &Database) -> Result<Self::T> { let emails = self.emails(db)?.into_iter() .map(|(sd, password)| Ok(EmailWithPassword { email: sd.printable(db)?, password, })) .collect::<Result<_>>()?; Ok(DetailedBreach { id: self.id, value: self.value.to_string(), emails, unscoped: self.unscoped, }) } } #[derive(Debug, Clone, Insertable, Serialize, Deserialize)] #[table_name="breaches"] pub struct NewBreach { pub value: String, pub unscoped: bool, } impl InsertableStruct<Breach> for NewBreach { fn value(&self) -> &str { &self.value } fn set_scoped(&mut self, scoped: bool) { self.unscoped = !scoped; } fn insert(&self, db: &Database) -> Result<()> { diesel::insert_into(breaches::table) .values(self) .execute(db.db())?; Ok(()) } } impl Upsertable<Breach> for NewBreach { type Update = NullUpdate; fn upsert(self, existing: &Breach) -> Self::Update { Self::Update { id: existing.id, } } } impl Printable<PrintableBreach> for NewBreach { fn printable(&self, _db: &Database) -> Result<PrintableBreach> { Ok(PrintableBreach { value: self.value.to_string(), }) } } #[derive(Debug, Serialize, Deserialize)] pub struct InsertBreach { pub value: String, } impl InsertToNew for InsertBreach { type Target = NewBreach; fn try_into_new(self) -> Result<NewBreach> { Ok(NewBreach { value: self.value, unscoped: false, }) } }