Set of administration tools for 42l's infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
11 KiB

use crate::db_config::*;
use crate::db_structs::*;
use diesel::dsl::count;
use diesel::prelude::*;
use chrono::NaiveDate;
impl User {
pub fn get(req_username: &str) -> Option<User> {
use crate::db_schema::user::dsl::*;
let conn = establish_connection();
user.filter(username_plain.eq(req_username))
.first(&conn)
.ok()
}
pub fn get_by_id(req_id: i32) -> Option<User> {
use crate::db_schema::user::dsl::*;
let conn = establish_connection();
user.find(req_id).first(&conn).ok()
}
pub fn get_all() -> Vec<User> {
use crate::db_schema::user::dsl::*;
let conn = establish_connection();
user.order(id)
.get_results(&conn)
.expect("Failed to query the database")
}
pub fn edit_max_emails(&self, updated_max: i16) {
use crate::db_schema::user::dsl::*;
let conn = establish_connection();
diesel::update(user.find(self.id))
.set(max_emails.eq(updated_max))
.get_result::<User>(&conn)
.expect("Failed to query the database");
}
pub fn edit_max_alias(&self, updated_max: i16) {
use crate::db_schema::user::dsl::*;
let conn = establish_connection();
diesel::update(user.find(self.id))
.set(max_alias.eq(updated_max))
.get_result::<User>(&conn)
.expect("Failed to query the database");
}
pub fn delete(&self) {
use crate::db_schema::user::dsl::*;
let conn = establish_connection();
diesel::delete(user.find(self.id))
.execute(&conn)
.expect("Failed to delete the user");
}
pub fn get_sub_newsletter() -> Vec<User> {
use crate::db_schema::user::dsl::*;
let conn = establish_connection();
user.filter(sub_newsletter.eq(true))
.filter(blocked.eq(0))
.order(id)
.get_results(&conn)
.expect("Failed query the database")
}
pub fn get_sub_ag() -> Vec<User> {
use crate::db_schema::user::dsl::*;
let conn = establish_connection();
user.filter(sub_ag.eq(true))
.filter(blocked.eq(0))
.order(id)
.get_results(&conn)
.expect("Failed query the database")
}
}
impl Email {
pub fn get_by_user(req_user: i32) -> Vec<Email> {
use crate::db_schema::email::dsl::*;
let conn = establish_connection();
email
.filter(user_id.eq(req_user))
.get_results(&conn)
.expect("Failed to query the database")
}
pub fn get_by_email(req_addr: &str) -> Option<Email> {
use crate::db_schema::email::dsl::*;
let conn = establish_connection();
email.filter(address.eq(req_addr)).first(&conn).ok()
}
pub fn count_by_user(req_id: i32) -> i64 {
use crate::db_schema::email::dsl::*;
let conn = establish_connection();
email
.select(count(id))
.filter(user_id.eq(req_id))
.first(&conn)
.expect("Failed to query the database")
}
}
impl Alias {
pub fn get_by_user(req_user: i32) -> Vec<Alias> {
use crate::db_schema::alias::dsl::*;
let conn = establish_connection();
alias
.filter(user_id.eq(req_user))
.get_results(&conn)
.expect("Failed to query the database")
}
pub fn get_by_from_email(req_addr: &str) -> Vec<Alias> {
use crate::db_schema::alias::dsl::*;
let conn = establish_connection();
alias
.filter(from_email.eq(req_addr))
.get_results(&conn)
.expect("Failed to query the database")
}
pub fn get_by_to_email(req_addr: &str) -> Vec<Alias> {
use crate::db_schema::alias::dsl::*;
let conn = establish_connection();
alias
.filter(to_email.eq(req_addr))
.get_results(&conn)
.expect("Failed to query the database")
}
pub fn count_by_user(req_id: i32) -> i64 {
use crate::db_schema::alias::dsl::*;
let conn = establish_connection();
alias
.select(count(id))
.filter(user_id.eq(req_id))
.first(&conn)
.expect("Failed to query the database")
}
}
impl Adhesions {
pub fn adhesions_by_user(req_id: i32) -> Vec<Adhesions> {
use crate::db_schema::adhesions::dsl::*;
let conn = establish_connection();
adhesions
.filter(user_id.eq(req_id))
.order(adh_date.asc())
.get_results(&conn)
.expect("Failed to query the database")
}
pub fn add_subscription(req_id: i32, req_sub_date: NaiveDate) {
use crate::db_schema::adhesions::dsl::*;
let conn = establish_connection();
diesel::insert_into(adhesions)
.values((user_id.eq(req_id), adh_date.eq(req_sub_date)))
.get_result::<Adhesions>(&conn)
.expect("Failed to query the database");
}
pub fn del_by_id(req_sub_id: i32) {
use crate::db_schema::adhesions::dsl::*;
let conn = establish_connection();
diesel::delete(adhesions.find(req_sub_id))
.get_result::<Adhesions>(&conn)
.expect("Failed to query the database");
}
}
impl EmailDomains {
pub fn toggle_free_access(&self) {
use crate::db_schema::email_domains::dsl::*;
let conn = establish_connection();
diesel::update(email_domains.find(self.id))
.set(free_access.eq(!self.free_access))
.execute(&conn)
.expect("Failed to query the database");
}
pub fn change_owner(&self, req_owner: i32) {
use crate::db_schema::email_domains::dsl::*;
let conn = establish_connection();
diesel::update(email_domains.find(self.id))
.set(owner.eq(req_owner))
.execute(&conn)
.expect("Failed to query the database");
}
pub fn insert(req_fqdn: String, req_free_access: bool, req_owner: i32) {
use crate::db_schema::email_domains::dsl::*;
let conn = establish_connection();
diesel::insert_into(email_domains)
.values((
fqdn.eq(req_fqdn),
free_access.eq(req_free_access),
owner.eq(req_owner),
))
.get_result::<EmailDomains>(&conn)
.expect("Failed to query the database");
}
pub fn get_created_addresses(&self) -> i64 {
use crate::db_schema::alias::dsl as alias_t;
use crate::db_schema::alias::dsl::*;
use crate::db_schema::email::dsl as email_t;
use crate::db_schema::email::dsl::*;
let match_fqdn = format!("%{}", self.fqdn);
let conn = establish_connection();
let count_email: i64 = email
.select(count(email_t::id))
.filter(address.like(&match_fqdn))
.first(&conn)
.expect("Failed to query the database");
let count_alias: i64 = alias
.select(count(alias_t::id))
.filter(from_email.like(&match_fqdn))
.first(&conn)
.expect("Failed to query the database");
count_email + count_alias
}
pub fn get_by_id(req_id: i32) -> Option<EmailDomains> {
use crate::db_schema::email_domains::dsl::*;
let conn = establish_connection();
email_domains.find(req_id).first(&conn).ok()
}
pub fn get_all() -> Vec<EmailDomains> {
use crate::db_schema::email_domains::dsl::*;
let conn = establish_connection();
email_domains
.order(id)
.get_results(&conn)
.expect("Failed to query the database")
}
pub fn owned_by_user(req_id: i32) -> Vec<EmailDomains> {
use crate::db_schema::email_domains::dsl::*;
let conn = establish_connection();
email_domains
.filter(owner.eq(req_id))
.order(id.desc())
.get_results(&conn)
.expect("Failed to query the database")
}
pub fn get_allowed_domains(req_id: i32) -> Vec<EmailDomains> {
use crate::db_schema::email_domains::dsl::*;
use crate::db_schema::email_groups::dsl::*;
// FOREACH domain
// IF the user has permission in EmailGroups --> add to list
// *DO NOT add free_access domains to list*
// END
let conn = establish_connection();
email_domains
.select((id, fqdn, free_access, owner))
.left_join(email_groups)
.filter(user_id.eq(req_id))
.filter(free_access.eq(false))
.order(fqdn.asc())
.get_results(&conn)
.expect("Failed to query the database")
}
pub fn delete(&self) {
use crate::db_schema::email_domains::dsl::*;
let conn = establish_connection();
diesel::delete(email_domains.find(self.id))
.execute(&conn)
.expect("Failed to delete the user");
}
}
impl EmailGroups {
pub fn check_exists(req_domain_id: i32, req_id: i32) -> bool {
use crate::db_schema::email_groups::dsl::*;
let conn = establish_connection();
email_groups
.filter(user_id.eq(req_id))
.filter(email_domains_id.eq(req_domain_id))
.first::<EmailGroups>(&conn)
.ok()
.is_some()
}
pub fn add_user(req_domain_id: i32, req_id: i32) {
use crate::db_schema::email_groups::dsl::*;
let conn = establish_connection();
diesel::insert_into(email_groups)
.values((user_id.eq(req_id), email_domains_id.eq(req_domain_id)))
.get_result::<EmailGroups>(&conn)
.expect("Failed to query the database");
}
pub fn del_by_id(req_domain_id: i32, req_id: i32) {
use crate::db_schema::email_groups::dsl::*;
let conn = establish_connection();
diesel::delete(
email_groups
.filter(user_id.eq(req_id))
.filter(email_domains_id.eq(req_domain_id)),
)
.get_result::<EmailGroups>(&conn)
.expect("Failed to query the database");
}
pub fn del_all(req_domain_id: i32) {
use crate::db_schema::email_groups::dsl::*;
let conn = establish_connection();
diesel::delete(email_groups.filter(email_domains_id.eq(req_domain_id)))
.get_result::<EmailGroups>(&conn)
.expect("Failed to query the database");
}
pub fn get_allowed_users(req_domain_id: i32) -> Vec<User> {
use crate::db_schema::email_groups::dsl::*;
use crate::db_schema::user::dsl::*;
// todo: find a way to not explicit those fields
let conn = establish_connection();
user.select((
id,
username,
recovery_email,
notify_email,
password,
sub_newsletter,
sub_ag,
blocked,
max_emails,
max_alias,
gitea_id,
nextcloud_username,
username_plain,
))
.left_join(email_groups)
.filter(email_domains_id.eq(req_domain_id))
.get_results(&conn)
.expect("Failed to query the database")
}
}