2018-02-15 00:53:11 +01:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
use super::{
|
|
|
|
Attachment, CollectionCipher, FolderCipher, Organization, User, UserOrgStatus, UserOrgType, UserOrganization,
|
|
|
|
};
|
2018-02-15 00:40:34 +01:00
|
|
|
|
2019-09-12 22:12:22 +02:00
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations, AsChangeset)]
|
2018-02-10 01:00:55 +01:00
|
|
|
#[table_name = "ciphers"]
|
2020-04-15 16:49:33 +02:00
|
|
|
#[changeset_options(treat_none_as_null="true")]
|
2018-02-15 00:40:34 +01:00
|
|
|
#[belongs_to(User, foreign_key = "user_uuid")]
|
2018-04-30 11:52:15 +02:00
|
|
|
#[belongs_to(Organization, foreign_key = "organization_uuid")]
|
2018-02-10 01:00:55 +01:00
|
|
|
#[primary_key(uuid)]
|
|
|
|
pub struct Cipher {
|
|
|
|
pub uuid: String,
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
|
2018-04-30 11:52:15 +02:00
|
|
|
pub user_uuid: Option<String>,
|
2018-02-10 01:00:55 +01:00
|
|
|
pub organization_uuid: Option<String>,
|
|
|
|
|
2018-03-06 00:02:36 +01:00
|
|
|
/*
|
|
|
|
Login = 1,
|
|
|
|
SecureNote = 2,
|
|
|
|
Card = 3,
|
|
|
|
Identity = 4
|
|
|
|
*/
|
2019-05-20 21:12:41 +02:00
|
|
|
pub atype: i32,
|
2018-03-06 00:02:36 +01:00
|
|
|
pub name: String,
|
|
|
|
pub notes: Option<String>,
|
|
|
|
pub fields: Option<String>,
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
pub data: String,
|
2018-03-06 00:02:36 +01:00
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
pub favorite: bool,
|
2018-08-27 23:08:58 +02:00
|
|
|
pub password_history: Option<String>,
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Cipher {
|
2019-05-20 21:12:41 +02:00
|
|
|
pub fn new(atype: i32, name: String) -> Self {
|
2018-02-10 01:00:55 +01:00
|
|
|
let now = Utc::now().naive_utc();
|
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
Self {
|
2018-12-07 14:32:40 +01:00
|
|
|
uuid: crate::util::get_uuid(),
|
2018-02-10 01:00:55 +01:00
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
|
|
|
|
2018-05-04 19:02:19 +02:00
|
|
|
user_uuid: None,
|
|
|
|
organization_uuid: None,
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2019-05-20 21:12:41 +02:00
|
|
|
atype,
|
2018-05-04 19:02:19 +02:00
|
|
|
favorite: false,
|
2018-03-06 00:02:36 +01:00
|
|
|
name,
|
|
|
|
|
|
|
|
notes: None,
|
|
|
|
fields: None,
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
data: String::new(),
|
2018-08-27 23:08:58 +02:00
|
|
|
password_history: None,
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
use crate::db::schema::*;
|
|
|
|
use crate::db::DbConn;
|
2018-02-15 00:40:34 +01:00
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
2018-12-19 21:52:53 +01:00
|
|
|
|
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
2018-02-15 00:40:34 +01:00
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl Cipher {
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json(&self, host: &str, user_uuid: &str, conn: &DbConn) -> Value {
|
2018-12-19 21:52:53 +01:00
|
|
|
use crate::util::format_date;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
let attachments = Attachment::find_by_cipher(&self.uuid, conn);
|
2018-10-10 20:40:39 +02:00
|
|
|
let attachments_json: Vec<Value> = attachments.iter().map(|c| c.to_json(host)).collect();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2019-06-14 22:51:50 +02:00
|
|
|
let fields_json = self.fields.as_ref().and_then(|s| serde_json::from_str(s).ok()).unwrap_or(Value::Null);
|
|
|
|
let password_history_json = self.password_history.as_ref().and_then(|s| serde_json::from_str(s).ok()).unwrap_or(Value::Null);
|
|
|
|
|
2020-03-30 22:19:50 +02:00
|
|
|
// Get the data or a default empty value to avoid issues with the mobile apps
|
|
|
|
let mut data_json: Value = serde_json::from_str(&self.data).unwrap_or_else(|_| json!({
|
|
|
|
"Fields":null,
|
|
|
|
"Name": self.name,
|
|
|
|
"Notes":null,
|
|
|
|
"Password":null,
|
|
|
|
"PasswordHistory":null,
|
|
|
|
"PasswordRevisionDate":null,
|
|
|
|
"Response":null,
|
|
|
|
"Totp":null,
|
|
|
|
"Uris":null,
|
|
|
|
"Username":null
|
|
|
|
}));
|
2018-03-06 00:02:36 +01:00
|
|
|
|
|
|
|
// TODO: ******* Backwards compat start **********
|
|
|
|
// To remove backwards compatibility, just remove this entire section
|
|
|
|
// and remove the compat code from ciphers::update_cipher_from_data
|
2019-05-20 21:12:41 +02:00
|
|
|
if self.atype == 1 && data_json["Uris"].is_array() {
|
2018-06-13 14:25:50 +02:00
|
|
|
let uri = data_json["Uris"][0]["Uri"].clone();
|
2018-03-06 00:02:36 +01:00
|
|
|
data_json["Uri"] = uri;
|
|
|
|
}
|
|
|
|
// TODO: ******* Backwards compat end **********
|
|
|
|
|
|
|
|
let mut json_object = json!({
|
2018-02-10 01:00:55 +01:00
|
|
|
"Id": self.uuid,
|
2019-05-20 21:12:41 +02:00
|
|
|
"Type": self.atype,
|
2018-02-10 01:00:55 +01:00
|
|
|
"RevisionDate": format_date(&self.updated_at),
|
2018-04-30 11:52:15 +02:00
|
|
|
"FolderId": self.get_folder_uuid(&user_uuid, &conn),
|
2018-02-10 01:00:55 +01:00
|
|
|
"Favorite": self.favorite,
|
2018-04-27 13:49:34 +02:00
|
|
|
"OrganizationId": self.organization_uuid,
|
2018-02-15 00:40:34 +01:00
|
|
|
"Attachments": attachments_json,
|
2018-07-01 15:49:16 +02:00
|
|
|
"OrganizationUseTotp": true,
|
2018-05-11 15:24:41 +02:00
|
|
|
"CollectionIds": self.get_collections(user_uuid, &conn),
|
2018-03-06 00:02:36 +01:00
|
|
|
|
|
|
|
"Name": self.name,
|
|
|
|
"Notes": self.notes,
|
|
|
|
"Fields": fields_json,
|
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
"Data": data_json,
|
2018-03-06 00:02:36 +01:00
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
"Object": "cipher",
|
|
|
|
"Edit": true,
|
2018-08-27 23:08:58 +02:00
|
|
|
|
|
|
|
"PasswordHistory": password_history_json,
|
2018-03-06 00:02:36 +01:00
|
|
|
});
|
|
|
|
|
2019-05-20 21:12:41 +02:00
|
|
|
let key = match self.atype {
|
2018-03-06 00:02:36 +01:00
|
|
|
1 => "Login",
|
|
|
|
2 => "SecureNote",
|
|
|
|
3 => "Card",
|
|
|
|
4 => "Identity",
|
|
|
|
_ => panic!("Wrong type"),
|
|
|
|
};
|
|
|
|
|
|
|
|
json_object[key] = data_json;
|
|
|
|
json_object
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-01 17:59:13 +02:00
|
|
|
pub fn update_users_revision(&self, conn: &DbConn) -> Vec<String> {
|
|
|
|
let mut user_uuids = Vec::new();
|
2018-08-21 18:31:01 +02:00
|
|
|
match self.user_uuid {
|
2018-09-01 17:59:13 +02:00
|
|
|
Some(ref user_uuid) => {
|
|
|
|
User::update_uuid_revision(&user_uuid, conn);
|
|
|
|
user_uuids.push(user_uuid.clone())
|
2018-12-30 23:34:31 +01:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// Belongs to Organization, need to update affected users
|
2018-08-21 18:31:01 +02:00
|
|
|
if let Some(ref org_uuid) = self.organization_uuid {
|
|
|
|
UserOrganization::find_by_cipher_and_org(&self.uuid, &org_uuid, conn)
|
2018-12-30 23:34:31 +01:00
|
|
|
.iter()
|
|
|
|
.for_each(|user_org| {
|
|
|
|
User::update_uuid_revision(&user_org.user_uuid, conn);
|
|
|
|
user_uuids.push(user_org.user_uuid.clone())
|
|
|
|
});
|
2018-08-21 18:31:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-09-01 17:59:13 +02:00
|
|
|
user_uuids
|
2018-08-21 18:31:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 22:12:22 +02:00
|
|
|
#[cfg(feature = "postgresql")]
|
|
|
|
pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
|
|
|
|
self.update_users_revision(conn);
|
|
|
|
self.updated_at = Utc::now().naive_utc();
|
|
|
|
|
|
|
|
diesel::insert_into(ciphers::table)
|
|
|
|
.values(&*self)
|
|
|
|
.on_conflict(ciphers::uuid)
|
|
|
|
.do_update()
|
|
|
|
.set(&*self)
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error saving cipher")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "postgresql"))]
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
|
2018-08-21 18:31:01 +02:00
|
|
|
self.update_users_revision(conn);
|
2018-02-15 01:07:57 +01:00
|
|
|
self.updated_at = Utc::now().naive_utc();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-10-06 14:13:49 +02:00
|
|
|
diesel::replace_into(ciphers::table)
|
2018-02-15 01:07:57 +01:00
|
|
|
.values(&*self)
|
2018-10-06 14:13:49 +02:00
|
|
|
.execute(&**conn)
|
2018-12-19 21:52:53 +01:00
|
|
|
.map_res("Error saving cipher")
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn delete(&self, conn: &DbConn) -> EmptyResult {
|
2018-08-21 18:31:01 +02:00
|
|
|
self.update_users_revision(conn);
|
|
|
|
|
2018-05-15 18:27:53 +02:00
|
|
|
FolderCipher::delete_all_by_cipher(&self.uuid, &conn)?;
|
|
|
|
CollectionCipher::delete_all_by_cipher(&self.uuid, &conn)?;
|
|
|
|
Attachment::delete_all_by_cipher(&self.uuid, &conn)?;
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
diesel::delete(ciphers::table.filter(ciphers::uuid.eq(&self.uuid)))
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error deleting cipher")
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn delete_all_by_organization(org_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-05-18 17:52:51 +02:00
|
|
|
for cipher in Self::find_by_org(org_uuid, &conn) {
|
|
|
|
cipher.delete(&conn)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-10-12 16:20:10 +02:00
|
|
|
for cipher in Self::find_owned_by_user(user_uuid, &conn) {
|
|
|
|
cipher.delete(&conn)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn move_to_folder(&self, folder_uuid: Option<String>, user_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2019-01-28 00:39:14 +01:00
|
|
|
User::update_uuid_revision(user_uuid, &conn);
|
|
|
|
|
|
|
|
match (self.get_folder_uuid(&user_uuid, &conn), folder_uuid) {
|
|
|
|
// No changes
|
|
|
|
(None, None) => Ok(()),
|
|
|
|
(Some(ref old), Some(ref new)) if old == new => Ok(()),
|
|
|
|
|
|
|
|
// Add to folder
|
|
|
|
(None, Some(new)) => FolderCipher::new(&new, &self.uuid).save(&conn),
|
|
|
|
|
|
|
|
// Remove from folder
|
|
|
|
(Some(old), None) => match FolderCipher::find_by_folder_and_cipher(&old, &self.uuid, &conn) {
|
|
|
|
Some(old) => old.delete(&conn),
|
|
|
|
None => err!("Couldn't move from previous folder"),
|
|
|
|
},
|
|
|
|
|
|
|
|
// Move to another folder
|
|
|
|
(Some(old), Some(new)) => {
|
|
|
|
if let Some(old) = FolderCipher::find_by_folder_and_cipher(&old, &self.uuid, &conn) {
|
|
|
|
old.delete(&conn)?;
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
2019-01-28 00:39:14 +01:00
|
|
|
FolderCipher::new(&new, &self.uuid).save(&conn)
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_write_accessible_to_user(&self, user_uuid: &str, conn: &DbConn) -> bool {
|
2018-12-07 15:01:29 +01:00
|
|
|
ciphers::table
|
2018-12-30 23:34:31 +01:00
|
|
|
.filter(ciphers::uuid.eq(&self.uuid))
|
|
|
|
.left_join(
|
|
|
|
users_organizations::table.on(ciphers::organization_uuid
|
|
|
|
.eq(users_organizations::org_uuid.nullable())
|
|
|
|
.and(users_organizations::user_uuid.eq(user_uuid))),
|
2018-05-31 18:25:52 +02:00
|
|
|
)
|
2018-12-30 23:34:31 +01:00
|
|
|
.left_join(ciphers_collections::table)
|
|
|
|
.left_join(
|
|
|
|
users_collections::table
|
|
|
|
.on(ciphers_collections::collection_uuid.eq(users_collections::collection_uuid)),
|
2018-05-31 18:25:52 +02:00
|
|
|
)
|
2018-12-30 23:34:31 +01:00
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid).or(
|
|
|
|
// Cipher owner
|
|
|
|
users_organizations::access_all.eq(true).or(
|
|
|
|
// access_all in Organization
|
2019-05-20 21:12:41 +02:00
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32).or(
|
2018-12-30 23:34:31 +01:00
|
|
|
// Org admin or owner
|
|
|
|
users_collections::user_uuid.eq(user_uuid).and(
|
|
|
|
users_collections::read_only.eq(false), //R/W access to collection
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
))
|
|
|
|
.select(ciphers::all_columns)
|
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
|
|
|
.is_some()
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_accessible_to_user(&self, user_uuid: &str, conn: &DbConn) -> bool {
|
2018-12-07 15:01:29 +01:00
|
|
|
ciphers::table
|
2018-12-30 23:34:31 +01:00
|
|
|
.filter(ciphers::uuid.eq(&self.uuid))
|
|
|
|
.left_join(
|
|
|
|
users_organizations::table.on(ciphers::organization_uuid
|
|
|
|
.eq(users_organizations::org_uuid.nullable())
|
|
|
|
.and(users_organizations::user_uuid.eq(user_uuid))),
|
2018-05-31 18:25:52 +02:00
|
|
|
)
|
2018-12-30 23:34:31 +01:00
|
|
|
.left_join(ciphers_collections::table)
|
|
|
|
.left_join(
|
|
|
|
users_collections::table
|
|
|
|
.on(ciphers_collections::collection_uuid.eq(users_collections::collection_uuid)),
|
2018-05-31 18:25:52 +02:00
|
|
|
)
|
2018-12-30 23:34:31 +01:00
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid).or(
|
|
|
|
// Cipher owner
|
|
|
|
users_organizations::access_all.eq(true).or(
|
|
|
|
// access_all in Organization
|
2019-05-20 21:12:41 +02:00
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32).or(
|
2018-12-30 23:34:31 +01:00
|
|
|
// Org admin or owner
|
|
|
|
users_collections::user_uuid.eq(user_uuid), // Access to Collection
|
|
|
|
),
|
|
|
|
),
|
|
|
|
))
|
|
|
|
.select(ciphers::all_columns)
|
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
|
|
|
.is_some()
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_folder_uuid(&self, user_uuid: &str, conn: &DbConn) -> Option<String> {
|
2018-12-30 23:34:31 +01:00
|
|
|
folders_ciphers::table
|
|
|
|
.inner_join(folders::table)
|
2018-04-30 11:52:15 +02:00
|
|
|
.filter(folders::user_uuid.eq(&user_uuid))
|
|
|
|
.filter(folders_ciphers::cipher_uuid.eq(&self.uuid))
|
|
|
|
.select(folders_ciphers::folder_uuid)
|
2018-12-30 23:34:31 +01:00
|
|
|
.first::<String>(&**conn)
|
|
|
|
.ok()
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
|
2018-02-10 01:00:55 +01:00
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::uuid.eq(uuid))
|
2018-12-30 23:34:31 +01:00
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-12-28 15:25:51 +01:00
|
|
|
// Find all ciphers accessible to user
|
2018-02-15 00:40:34 +01:00
|
|
|
pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
2018-02-10 01:00:55 +01:00
|
|
|
ciphers::table
|
2018-05-12 00:53:37 +02:00
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
ciphers::organization_uuid.eq(users_organizations::org_uuid.nullable()).and(
|
2018-07-12 23:45:41 +02:00
|
|
|
users_organizations::user_uuid.eq(user_uuid).and(
|
|
|
|
users_organizations::status.eq(UserOrgStatus::Confirmed as i32)
|
|
|
|
)
|
2018-05-12 00:53:37 +02:00
|
|
|
)
|
|
|
|
))
|
2018-12-28 15:25:51 +01:00
|
|
|
.left_join(ciphers_collections::table.on(
|
|
|
|
ciphers::uuid.eq(ciphers_collections::cipher_uuid)
|
|
|
|
))
|
2018-05-12 00:53:37 +02:00
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
ciphers_collections::collection_uuid.eq(users_collections::collection_uuid)
|
|
|
|
))
|
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid).or( // Cipher owner
|
|
|
|
users_organizations::access_all.eq(true).or( // access_all in Organization
|
2019-05-20 21:12:41 +02:00
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32).or( // Org admin or owner
|
2018-11-16 15:21:26 +01:00
|
|
|
users_collections::user_uuid.eq(user_uuid).and( // Access to Collection
|
|
|
|
users_organizations::status.eq(UserOrgStatus::Confirmed as i32)
|
|
|
|
)
|
2018-05-12 00:53:37 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.select(ciphers::all_columns)
|
2018-05-13 14:20:00 +02:00
|
|
|
.distinct()
|
2018-05-12 00:53:37 +02:00
|
|
|
.load::<Self>(&**conn).expect("Error loading ciphers")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all ciphers directly owned by user
|
|
|
|
pub fn find_owned_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid))
|
|
|
|
.load::<Self>(&**conn).expect("Error loading ciphers")
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
2018-02-16 00:32:26 +01:00
|
|
|
|
2018-04-27 13:49:34 +02:00
|
|
|
pub fn find_by_org(org_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::organization_uuid.eq(org_uuid))
|
|
|
|
.load::<Self>(&**conn).expect("Error loading ciphers")
|
|
|
|
}
|
|
|
|
|
2018-02-16 00:32:26 +01:00
|
|
|
pub fn find_by_folder(folder_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
2018-04-30 11:52:15 +02:00
|
|
|
folders_ciphers::table.inner_join(ciphers::table)
|
|
|
|
.filter(folders_ciphers::folder_uuid.eq(folder_uuid))
|
|
|
|
.select(ciphers::all_columns)
|
2018-02-16 00:32:26 +01:00
|
|
|
.load::<Self>(&**conn).expect("Error loading ciphers")
|
|
|
|
}
|
2018-05-09 12:55:05 +02:00
|
|
|
|
2018-05-11 15:24:41 +02:00
|
|
|
pub fn get_collections(&self, user_id: &str, conn: &DbConn) -> Vec<String> {
|
2018-05-09 12:55:05 +02:00
|
|
|
ciphers_collections::table
|
2018-05-11 15:24:41 +02:00
|
|
|
.inner_join(collections::table.on(
|
|
|
|
collections::uuid.eq(ciphers_collections::collection_uuid)
|
|
|
|
))
|
|
|
|
.inner_join(users_organizations::table.on(
|
|
|
|
users_organizations::org_uuid.eq(collections::org_uuid).and(
|
|
|
|
users_organizations::user_uuid.eq(user_id)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.left_join(users_collections::table.on(
|
2018-12-28 15:25:51 +01:00
|
|
|
users_collections::collection_uuid.eq(ciphers_collections::collection_uuid).and(
|
|
|
|
users_collections::user_uuid.eq(user_id)
|
|
|
|
)
|
2018-05-11 15:24:41 +02:00
|
|
|
))
|
2018-05-09 12:55:05 +02:00
|
|
|
.filter(ciphers_collections::cipher_uuid.eq(&self.uuid))
|
2018-05-11 15:24:41 +02:00
|
|
|
.filter(users_collections::user_uuid.eq(user_id).or( // User has access to collection
|
|
|
|
users_organizations::access_all.eq(true).or( // User has access all
|
2019-05-20 21:12:41 +02:00
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32) // User is admin or owner
|
2018-05-11 15:24:41 +02:00
|
|
|
)
|
|
|
|
))
|
2018-05-09 12:55:05 +02:00
|
|
|
.select(ciphers_collections::collection_uuid)
|
2018-09-13 21:55:23 +02:00
|
|
|
.load::<String>(&**conn).unwrap_or_default()
|
2018-05-09 12:55:05 +02:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|