1
0
Fork 0
mirror of https://github.com/dani-garcia/vaultwarden.git synced 2025-08-27 13:04:47 +00:00

Finish invite functionality, and remove virtual organization

This commit is contained in:
Daniel García 2018-12-19 22:51:08 +01:00
commit b2fc0499f6
No known key found for this signature in database
GPG key ID: FC8A7D14C3CD543A
6 changed files with 42 additions and 138 deletions

View file

@ -1,7 +1,7 @@
use std::cmp::Ordering;
use serde_json::Value;
use super::{User, CollectionUser, Invitation};
use super::{User, CollectionUser};
#[derive(Debug, Identifiable, Queryable, Insertable)]
#[table_name = "organizations"]
@ -154,8 +154,6 @@ impl UserOrgType {
/// Local methods
impl Organization {
pub const VIRTUAL_ID: &'static str = "00000000-0000-0000-0000-000000000000";
pub fn new(name: String, billing_email: String) -> Self {
Self {
uuid: crate::util::get_uuid(),
@ -165,14 +163,6 @@ impl Organization {
}
}
pub fn new_virtual() -> Self {
Self {
uuid: String::from(Organization::VIRTUAL_ID),
name: String::from("bitwarden_rs"),
billing_email: String::from("none@none.none")
}
}
pub fn to_json(&self) -> Value {
json!({
"Id": self.uuid,
@ -216,20 +206,6 @@ impl UserOrganization {
type_: UserOrgType::User as i32,
}
}
pub fn new_virtual(user_uuid: String, type_: UserOrgType, status: UserOrgStatus) -> Self {
Self {
uuid: user_uuid.clone(),
user_uuid,
org_uuid: String::from(Organization::VIRTUAL_ID),
access_all: true,
key: String::new(),
status: status as i32,
type_: type_ as i32,
}
}
}
@ -244,10 +220,6 @@ use crate::error::MapResult;
/// Database methods
impl Organization {
pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
if self.uuid == Organization::VIRTUAL_ID {
err!("diesel::result::Error::NotFound")
}
UserOrganization::find_by_org(&self.uuid, conn)
.iter()
.for_each(|user_org| {
@ -262,10 +234,6 @@ impl Organization {
pub fn delete(self, conn: &DbConn) -> EmptyResult {
use super::{Cipher, Collection};
if self.uuid == Organization::VIRTUAL_ID {
err!("diesel::result::Error::NotFound")
}
Cipher::delete_all_by_organization(&self.uuid, &conn)?;
Collection::delete_all_by_organization(&self.uuid, &conn)?;
UserOrganization::delete_all_by_organization(&self.uuid, &conn)?;
@ -279,9 +247,6 @@ impl Organization {
}
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
if uuid == Organization::VIRTUAL_ID {
return Some(Self::new_virtual())
};
organizations::table
.filter(organizations::uuid.eq(uuid))
.first::<Self>(&**conn).ok()
@ -371,9 +336,6 @@ impl UserOrganization {
}
pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
if self.org_uuid == Organization::VIRTUAL_ID {
err!("diesel::result::Error::NotFound")
}
User::update_uuid_revision(&self.user_uuid, conn);
diesel::replace_into(users_organizations::table)
@ -382,9 +344,6 @@ impl UserOrganization {
}
pub fn delete(self, conn: &DbConn) -> EmptyResult {
if self.org_uuid == Organization::VIRTUAL_ID {
err!("diesel::result::Error::NotFound")
}
User::update_uuid_revision(&self.user_uuid, conn);
CollectionUser::delete_all_by_user(&self.user_uuid, &conn)?;
@ -449,22 +408,9 @@ impl UserOrganization {
}
pub fn find_by_org(org_uuid: &str, conn: &DbConn) -> Vec<Self> {
if org_uuid == Organization::VIRTUAL_ID {
User::get_all(&*conn).iter().map(|user| {
Self::new_virtual(
user.uuid.clone(),
UserOrgType::User,
if Invitation::find_by_mail(&user.email, &conn).is_some() {
UserOrgStatus::Invited
} else {
UserOrgStatus::Confirmed
})
}).collect()
} else {
users_organizations::table
.filter(users_organizations::org_uuid.eq(org_uuid))
.load::<Self>(&**conn).expect("Error loading user organizations")
}
users_organizations::table
.filter(users_organizations::org_uuid.eq(org_uuid))
.load::<Self>(&**conn).expect("Error loading user organizations")
}
pub fn find_by_org_and_type(org_uuid: &str, type_: i32, conn: &DbConn) -> Vec<Self> {

View file

@ -100,13 +100,6 @@ impl User {
pub fn reset_security_stamp(&mut self) {
self.security_stamp = crate::util::get_uuid();
}
pub fn is_server_admin(&self) -> bool {
match CONFIG.server_admin_email {
Some(ref server_admin_email) => &self.email == server_admin_email,
None => false
}
}
}
use diesel;
@ -121,12 +114,9 @@ use crate::error::MapResult;
/// Database methods
impl User {
pub fn to_json(&self, conn: &DbConn) -> Value {
use super::{UserOrganization, UserOrgType, UserOrgStatus, TwoFactor};
use super::{UserOrganization, TwoFactor};
let mut orgs = UserOrganization::find_by_user(&self.uuid, conn);
if self.is_server_admin() {
orgs.push(UserOrganization::new_virtual(self.uuid.clone(), UserOrgType::Owner, UserOrgStatus::Confirmed));
}
let orgs = UserOrganization::find_by_user(&self.uuid, conn);
let orgs_json: Vec<Value> = orgs.iter().map(|c| c.to_json(&conn)).collect();
let twofactor_enabled = !TwoFactor::find_by_user(&self.uuid, conn).is_empty();
@ -172,7 +162,7 @@ impl User {
Cipher::delete_all_by_user(&self.uuid, &*conn)?;
Folder::delete_all_by_user(&self.uuid, &*conn)?;
Device::delete_all_by_user(&self.uuid, &*conn)?;
//TwoFactor::delete_all_by_user(&self.uuid, &*conn)?;
TwoFactor::delete_all_by_user(&self.uuid, &*conn)?;
Invitation::take(&self.email, &*conn); // Delete invitation if any
diesel::delete(users::table.filter(