2018-11-12 18:13:25 +01:00
|
|
|
use std::cmp::Ordering;
|
2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
use uuid::Uuid;
|
2018-10-12 16:20:10 +02:00
|
|
|
use super::{User, CollectionUser, Invitation};
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable)]
|
|
|
|
#[table_name = "organizations"]
|
|
|
|
#[primary_key(uuid)]
|
|
|
|
pub struct Organization {
|
|
|
|
pub uuid: String,
|
|
|
|
pub name: String,
|
|
|
|
pub billing_email: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable)]
|
|
|
|
#[table_name = "users_organizations"]
|
|
|
|
#[primary_key(uuid)]
|
|
|
|
pub struct UserOrganization {
|
|
|
|
pub uuid: String,
|
|
|
|
pub user_uuid: String,
|
|
|
|
pub org_uuid: String,
|
|
|
|
|
|
|
|
pub access_all: bool,
|
|
|
|
pub key: String,
|
|
|
|
pub status: i32,
|
|
|
|
pub type_: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum UserOrgStatus {
|
2018-09-10 15:51:40 +02:00
|
|
|
Invited = 0,
|
2018-04-24 22:01:55 +02:00
|
|
|
Accepted = 1,
|
|
|
|
Confirmed = 2,
|
|
|
|
}
|
|
|
|
|
2018-11-12 18:13:25 +01:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
#[derive(Eq)]
|
2018-04-24 22:01:55 +02:00
|
|
|
pub enum UserOrgType {
|
|
|
|
Owner = 0,
|
|
|
|
Admin = 1,
|
|
|
|
User = 2,
|
2018-11-12 18:13:25 +01:00
|
|
|
Manager = 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for UserOrgType {
|
|
|
|
fn cmp(&self, other: &UserOrgType) -> Ordering {
|
|
|
|
if self == other {
|
|
|
|
Ordering::Equal
|
|
|
|
} else {
|
|
|
|
match self {
|
|
|
|
UserOrgType::Owner => Ordering::Greater,
|
|
|
|
UserOrgType::Admin => match other {
|
|
|
|
UserOrgType::Owner => Ordering::Less,
|
|
|
|
_ => Ordering::Greater
|
|
|
|
},
|
|
|
|
UserOrgType::Manager => match other {
|
|
|
|
UserOrgType::Owner | UserOrgType::Admin => Ordering::Less,
|
|
|
|
_ => Ordering::Greater
|
|
|
|
},
|
|
|
|
UserOrgType::User => Ordering::Less
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for UserOrgType {
|
|
|
|
fn partial_cmp(&self, other: &UserOrgType) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<i32> for UserOrgType {
|
|
|
|
fn eq(&self, other: &i32) -> bool {
|
|
|
|
*other == *self as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd<i32> for UserOrgType {
|
|
|
|
fn partial_cmp(&self, other: &i32) -> Option<Ordering> {
|
|
|
|
if let Some(other) = Self::from_i32(other) {
|
|
|
|
return Some(self.cmp(&other))
|
|
|
|
}
|
|
|
|
return None
|
|
|
|
}
|
2018-11-13 17:34:21 +01:00
|
|
|
|
|
|
|
fn gt(&self, other: &i32) -> bool {
|
|
|
|
match self.partial_cmp(other) {
|
2018-11-13 22:38:56 +01:00
|
|
|
Some(Ordering::Less) | Some(Ordering::Equal) => false,
|
2018-11-13 17:34:21 +01:00
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ge(&self, other: &i32) -> bool {
|
|
|
|
match self.partial_cmp(other) {
|
|
|
|
Some(Ordering::Less) => false,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 18:13:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<UserOrgType> for i32 {
|
|
|
|
fn eq(&self, other: &UserOrgType) -> bool {
|
|
|
|
*self == *other as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd<UserOrgType> for i32 {
|
|
|
|
fn partial_cmp(&self, other: &UserOrgType) -> Option<Ordering> {
|
|
|
|
if let Some(self_type) = UserOrgType::from_i32(self) {
|
|
|
|
return Some(self_type.cmp(other))
|
|
|
|
}
|
|
|
|
return None
|
|
|
|
}
|
2018-11-13 17:34:21 +01:00
|
|
|
|
|
|
|
fn lt(&self, other: &UserOrgType) -> bool {
|
|
|
|
match self.partial_cmp(other) {
|
|
|
|
Some(Ordering::Less) | None => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn le(&self, other: &UserOrgType) -> bool {
|
|
|
|
match self.partial_cmp(other) {
|
|
|
|
Some(Ordering::Less) | Some(Ordering::Equal) | None => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
impl UserOrgType {
|
|
|
|
pub fn from_str(s: &str) -> Option<Self> {
|
|
|
|
match s {
|
|
|
|
"0" | "Owner" => Some(UserOrgType::Owner),
|
|
|
|
"1" | "Admin" => Some(UserOrgType::Admin),
|
|
|
|
"2" | "User" => Some(UserOrgType::User),
|
2018-11-12 18:13:25 +01:00
|
|
|
"3" | "Manager" => Some(UserOrgType::Manager),
|
2018-04-25 00:34:40 +02:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2018-11-12 18:13:25 +01:00
|
|
|
|
|
|
|
pub fn from_i32(i: &i32) -> Option<Self> {
|
|
|
|
match i {
|
|
|
|
0 => Some(UserOrgType::Owner),
|
|
|
|
1 => Some(UserOrgType::Admin),
|
|
|
|
2 => Some(UserOrgType::User),
|
|
|
|
3 => Some(UserOrgType::Manager),
|
2018-04-25 00:34:40 +02:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2018-11-12 18:13:25 +01:00
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
/// Local methods
|
|
|
|
impl Organization {
|
2018-10-12 16:20:10 +02:00
|
|
|
pub const VIRTUAL_ID: &'static str = "00000000-0000-0000-0000-000000000000";
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
pub fn new(name: String, billing_email: String) -> Self {
|
|
|
|
Self {
|
|
|
|
uuid: Uuid::new_v4().to_string(),
|
|
|
|
|
|
|
|
name,
|
|
|
|
billing_email,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 16:20:10 +02:00
|
|
|
pub fn new_virtual() -> Self {
|
|
|
|
Self {
|
|
|
|
uuid: String::from(Organization::VIRTUAL_ID),
|
|
|
|
name: String::from("bitwarden_rs"),
|
|
|
|
billing_email: String::from("none@none.none")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json(&self) -> Value {
|
2018-04-24 22:01:55 +02:00
|
|
|
json!({
|
|
|
|
"Id": self.uuid,
|
|
|
|
"Name": self.name,
|
|
|
|
"Seats": 10,
|
|
|
|
"MaxCollections": 10,
|
2018-10-01 17:00:11 +02:00
|
|
|
"MaxStorageGb": 10, // The value doesn't matter, we don't check server-side
|
2018-07-01 15:49:16 +02:00
|
|
|
"Use2fa": true,
|
2018-04-24 22:01:55 +02:00
|
|
|
"UseDirectory": false,
|
|
|
|
"UseEvents": false,
|
|
|
|
"UseGroups": false,
|
2018-07-01 15:49:16 +02:00
|
|
|
"UseTotp": true,
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
"BusinessName": null,
|
|
|
|
"BusinessAddress1": null,
|
|
|
|
"BusinessAddress2": null,
|
|
|
|
"BusinessAddress3": null,
|
|
|
|
"BusinessCountry": null,
|
|
|
|
"BusinessTaxNumber": null,
|
|
|
|
|
|
|
|
"BillingEmail": self.billing_email,
|
2018-07-01 15:49:16 +02:00
|
|
|
"Plan": "TeamsAnnually",
|
|
|
|
"PlanType": 5, // TeamsAnnually plan
|
2018-10-01 17:00:11 +02:00
|
|
|
"UsersGetPremium": true,
|
2018-04-24 22:01:55 +02:00
|
|
|
"Object": "organization",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserOrganization {
|
|
|
|
pub fn new(user_uuid: String, org_uuid: String) -> Self {
|
|
|
|
Self {
|
|
|
|
uuid: Uuid::new_v4().to_string(),
|
|
|
|
|
|
|
|
user_uuid,
|
|
|
|
org_uuid,
|
|
|
|
|
|
|
|
access_all: false,
|
|
|
|
key: String::new(),
|
|
|
|
status: UserOrgStatus::Accepted as i32,
|
|
|
|
type_: UserOrgType::User as i32,
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 16:20:10 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use db::DbConn;
|
2018-08-21 18:31:01 +02:00
|
|
|
use db::schema::{organizations, users_organizations, users_collections, ciphers_collections};
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl Organization {
|
2018-10-14 16:04:23 +02:00
|
|
|
pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> {
|
2018-10-12 16:20:10 +02:00
|
|
|
if self.uuid == Organization::VIRTUAL_ID {
|
2018-10-14 16:04:23 +02:00
|
|
|
return Err(diesel::result::Error::NotFound)
|
2018-10-12 16:20:10 +02:00
|
|
|
}
|
|
|
|
|
2018-08-21 14:26:22 +02:00
|
|
|
UserOrganization::find_by_org(&self.uuid, conn)
|
|
|
|
.iter()
|
|
|
|
.for_each(|user_org| {
|
|
|
|
User::update_uuid_revision(&user_org.user_uuid, conn);
|
|
|
|
});
|
|
|
|
|
2018-10-14 16:04:23 +02:00
|
|
|
diesel::replace_into(organizations::table)
|
|
|
|
.values(&*self).execute(&**conn).and(Ok(()))
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-05-18 17:52:51 +02:00
|
|
|
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
|
|
|
|
use super::{Cipher, Collection};
|
|
|
|
|
2018-10-12 16:20:10 +02:00
|
|
|
if self.uuid == Organization::VIRTUAL_ID {
|
|
|
|
return Err(diesel::result::Error::NotFound)
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:52:51 +02:00
|
|
|
Cipher::delete_all_by_organization(&self.uuid, &conn)?;
|
|
|
|
Collection::delete_all_by_organization(&self.uuid, &conn)?;
|
|
|
|
UserOrganization::delete_all_by_organization(&self.uuid, &conn)?;
|
|
|
|
|
|
|
|
diesel::delete(
|
|
|
|
organizations::table.filter(
|
|
|
|
organizations::uuid.eq(self.uuid)
|
|
|
|
)
|
|
|
|
).execute(&**conn).and(Ok(()))
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
|
2018-10-12 16:20:10 +02:00
|
|
|
if uuid == Organization::VIRTUAL_ID {
|
|
|
|
return Some(Self::new_virtual())
|
|
|
|
};
|
2018-04-24 22:01:55 +02:00
|
|
|
organizations::table
|
|
|
|
.filter(organizations::uuid.eq(uuid))
|
|
|
|
.first::<Self>(&**conn).ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserOrganization {
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json(&self, conn: &DbConn) -> Value {
|
2018-04-24 22:01:55 +02:00
|
|
|
let org = Organization::find_by_uuid(&self.org_uuid, conn).unwrap();
|
|
|
|
|
|
|
|
json!({
|
|
|
|
"Id": self.org_uuid,
|
|
|
|
"Name": org.name,
|
|
|
|
"Seats": 10,
|
|
|
|
"MaxCollections": 10,
|
2018-10-01 17:00:11 +02:00
|
|
|
"UsersGetPremium": true,
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-07-01 15:49:16 +02:00
|
|
|
"Use2fa": true,
|
2018-04-24 22:01:55 +02:00
|
|
|
"UseDirectory": false,
|
|
|
|
"UseEvents": false,
|
|
|
|
"UseGroups": false,
|
2018-07-01 15:49:16 +02:00
|
|
|
"UseTotp": true,
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-06-01 00:50:22 +02:00
|
|
|
"MaxStorageGb": 10, // The value doesn't matter, we don't check server-side
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
// These are per user
|
|
|
|
"Key": self.key,
|
|
|
|
"Status": self.status,
|
|
|
|
"Type": self.type_,
|
|
|
|
"Enabled": true,
|
|
|
|
|
|
|
|
"Object": "profileOrganization",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json_user_details(&self, conn: &DbConn) -> Value {
|
2018-04-24 22:01:55 +02:00
|
|
|
let user = User::find_by_uuid(&self.user_uuid, conn).unwrap();
|
|
|
|
|
|
|
|
json!({
|
|
|
|
"Id": self.uuid,
|
2018-04-25 00:34:40 +02:00
|
|
|
"UserId": self.user_uuid,
|
2018-04-24 22:01:55 +02:00
|
|
|
"Name": user.name,
|
|
|
|
"Email": user.email,
|
|
|
|
|
|
|
|
"Status": self.status,
|
|
|
|
"Type": self.type_,
|
2018-05-11 20:08:02 +02:00
|
|
|
"AccessAll": self.access_all,
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
"Object": "organizationUserUserDetails",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json_collection_user_details(&self, read_only: bool, conn: &DbConn) -> Value {
|
2018-05-29 17:01:38 +02:00
|
|
|
let user = User::find_by_uuid(&self.user_uuid, conn).unwrap();
|
|
|
|
|
|
|
|
json!({
|
|
|
|
"OrganizationUserId": self.uuid,
|
|
|
|
"AccessAll": self.access_all,
|
|
|
|
"Name": user.name,
|
|
|
|
"Email": user.email,
|
|
|
|
"Type": self.type_,
|
|
|
|
"Status": self.status,
|
|
|
|
"ReadOnly": read_only,
|
|
|
|
"Object": "collectionUser",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json_details(&self, conn: &DbConn) -> Value {
|
2018-05-11 20:08:02 +02:00
|
|
|
let coll_uuids = if self.access_all {
|
|
|
|
vec![] // If we have complete access, no need to fill the array
|
|
|
|
} else {
|
2018-05-17 00:05:50 +02:00
|
|
|
let collections = CollectionUser::find_by_organization_and_user_uuid(&self.org_uuid, &self.user_uuid, conn);
|
2018-05-11 20:08:02 +02:00
|
|
|
collections.iter().map(|c| json!({"Id": c.collection_uuid, "ReadOnly": c.read_only})).collect()
|
|
|
|
};
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
json!({
|
|
|
|
"Id": self.uuid,
|
|
|
|
"UserId": self.user_uuid,
|
|
|
|
|
|
|
|
"Status": self.status,
|
|
|
|
"Type": self.type_,
|
2018-05-11 20:08:02 +02:00
|
|
|
"AccessAll": self.access_all,
|
|
|
|
"Collections": coll_uuids,
|
2018-04-25 00:34:40 +02:00
|
|
|
|
|
|
|
"Object": "organizationUserDetails",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:04:23 +02:00
|
|
|
pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> {
|
2018-10-12 16:20:10 +02:00
|
|
|
if self.org_uuid == Organization::VIRTUAL_ID {
|
2018-10-14 16:04:23 +02:00
|
|
|
return Err(diesel::result::Error::NotFound)
|
2018-10-12 16:20:10 +02:00
|
|
|
}
|
2018-08-21 14:26:22 +02:00
|
|
|
User::update_uuid_revision(&self.user_uuid, conn);
|
|
|
|
|
2018-10-14 16:04:23 +02:00
|
|
|
diesel::replace_into(users_organizations::table)
|
|
|
|
.values(&*self).execute(&**conn).and(Ok(()))
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-05-18 17:52:51 +02:00
|
|
|
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
|
2018-10-12 16:20:10 +02:00
|
|
|
if self.org_uuid == Organization::VIRTUAL_ID {
|
|
|
|
return Err(diesel::result::Error::NotFound)
|
|
|
|
}
|
2018-08-21 14:26:22 +02:00
|
|
|
User::update_uuid_revision(&self.user_uuid, conn);
|
2018-05-18 17:52:51 +02:00
|
|
|
|
|
|
|
CollectionUser::delete_all_by_user(&self.user_uuid, &conn)?;
|
|
|
|
|
|
|
|
diesel::delete(
|
|
|
|
users_organizations::table.filter(
|
|
|
|
users_organizations::uuid.eq(self.uuid)
|
|
|
|
)
|
|
|
|
).execute(&**conn).and(Ok(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_all_by_organization(org_uuid: &str, conn: &DbConn) -> QueryResult<()> {
|
|
|
|
for user_org in Self::find_by_org(&org_uuid, &conn) {
|
|
|
|
user_org.delete(&conn)?;
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
2018-05-18 17:52:51 +02:00
|
|
|
Ok(())
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 16:20:10 +02:00
|
|
|
pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> QueryResult<()> {
|
|
|
|
for user_org in Self::find_any_state_by_user(&user_uuid, &conn) {
|
|
|
|
user_org.delete(&conn)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:13:59 +02:00
|
|
|
pub fn has_full_access(self) -> bool {
|
2018-11-12 18:13:25 +01:00
|
|
|
self.access_all || self.type_ >= UserOrgType::Admin
|
2018-05-14 17:13:59 +02:00
|
|
|
}
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::uuid.eq(uuid))
|
|
|
|
.first::<Self>(&**conn).ok()
|
|
|
|
}
|
|
|
|
|
2018-09-04 12:24:53 +02:00
|
|
|
pub fn find_by_uuid_and_org(uuid: &str, org_uuid: &str, conn: &DbConn) -> Option<Self> {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::uuid.eq(uuid))
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
.first::<Self>(&**conn).ok()
|
|
|
|
}
|
|
|
|
|
2018-05-03 18:47:27 +02:00
|
|
|
pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
2018-04-24 22:01:55 +02:00
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
2018-07-13 18:21:19 +02:00
|
|
|
.filter(users_organizations::status.eq(UserOrgStatus::Confirmed as i32))
|
2018-09-13 21:55:23 +02:00
|
|
|
.load::<Self>(&**conn).unwrap_or_default()
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-09-10 15:51:40 +02:00
|
|
|
pub fn find_invited_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.filter(users_organizations::status.eq(UserOrgStatus::Invited as i32))
|
2018-09-13 21:55:23 +02:00
|
|
|
.load::<Self>(&**conn).unwrap_or_default()
|
2018-09-10 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 16:20:10 +02:00
|
|
|
pub fn find_any_state_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
2018-04-24 22:01:55 +02:00
|
|
|
users_organizations::table
|
2018-10-12 16:20:10 +02:00
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.load::<Self>(&**conn).unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
pub fn find_by_org_and_type(org_uuid: &str, type_: i32, conn: &DbConn) -> Vec<Self> {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
.filter(users_organizations::type_.eq(type_))
|
|
|
|
.load::<Self>(&**conn).expect("Error loading user organizations")
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
pub fn find_by_user_and_org(user_uuid: &str, org_uuid: &str, conn: &DbConn) -> Option<Self> {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
.first::<Self>(&**conn).ok()
|
|
|
|
}
|
2018-08-21 18:31:01 +02:00
|
|
|
|
|
|
|
pub fn find_by_cipher_and_org(cipher_uuid: &str, org_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
users_collections::user_uuid.eq(users_organizations::user_uuid)
|
|
|
|
))
|
|
|
|
.left_join(ciphers_collections::table.on(
|
|
|
|
ciphers_collections::collection_uuid.eq(users_collections::collection_uuid).and(
|
|
|
|
ciphers_collections::cipher_uuid.eq(&cipher_uuid)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.filter(
|
|
|
|
users_organizations::access_all.eq(true).or( // AccessAll..
|
|
|
|
ciphers_collections::cipher_uuid.eq(&cipher_uuid) // ..or access to collection with cipher
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.select(users_organizations::all_columns)
|
|
|
|
.load::<Self>(&**conn).expect("Error loading user organizations")
|
|
|
|
}
|
2018-10-01 17:52:36 +02:00
|
|
|
|
|
|
|
pub fn find_by_collection_and_org(collection_uuid: &str, org_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
users_collections::user_uuid.eq(users_organizations::user_uuid)
|
|
|
|
))
|
|
|
|
.filter(
|
|
|
|
users_organizations::access_all.eq(true).or( // AccessAll..
|
|
|
|
users_collections::collection_uuid.eq(&collection_uuid) // ..or access to collection with cipher
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.select(users_organizations::all_columns)
|
|
|
|
.load::<Self>(&**conn).expect("Error loading user organizations")
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|