2022-05-04 21:13:05 +02:00
|
|
|
use crate::CONFIG;
|
2024-03-19 19:47:30 +01:00
|
|
|
use chrono::{NaiveDateTime, TimeDelta, Utc};
|
2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2022-10-20 15:31:53 +02:00
|
|
|
use super::{
|
|
|
|
Attachment, CollectionCipher, Favorite, FolderCipher, Group, User, UserOrgStatus, UserOrgType, UserOrganization,
|
|
|
|
};
|
2021-04-03 05:52:15 +02:00
|
|
|
|
2023-02-27 16:37:58 +01:00
|
|
|
use crate::api::core::{CipherData, CipherSyncData, CipherSyncType};
|
2022-05-04 21:13:05 +02:00
|
|
|
|
|
|
|
use std::borrow::Cow;
|
2018-02-15 00:40:34 +01:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_object! {
|
2022-05-04 21:13:05 +02:00
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
2022-05-20 23:39:47 +02:00
|
|
|
#[diesel(table_name = ciphers)]
|
|
|
|
#[diesel(treat_none_as_null = true)]
|
|
|
|
#[diesel(primary_key(uuid))]
|
2020-08-18 17:15:44 +02:00
|
|
|
pub struct Cipher {
|
|
|
|
pub uuid: String,
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
|
|
|
|
pub user_uuid: Option<String>,
|
|
|
|
pub organization_uuid: Option<String>,
|
|
|
|
|
2023-10-23 00:18:14 +02:00
|
|
|
pub key: Option<String>,
|
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
/*
|
|
|
|
Login = 1,
|
|
|
|
SecureNote = 2,
|
|
|
|
Card = 3,
|
2023-11-04 03:08:45 +01:00
|
|
|
Identity = 4
|
2020-08-18 17:15:44 +02:00
|
|
|
*/
|
|
|
|
pub atype: i32,
|
|
|
|
pub name: String,
|
|
|
|
pub notes: Option<String>,
|
|
|
|
pub fields: Option<String>,
|
|
|
|
|
|
|
|
pub data: String,
|
|
|
|
|
|
|
|
pub password_history: Option<String>,
|
|
|
|
pub deleted_at: Option<NaiveDateTime>,
|
2021-05-01 10:06:06 +02:00
|
|
|
pub reprompt: Option<i32>,
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 10:06:06 +02:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum RepromptType {
|
|
|
|
None = 0,
|
|
|
|
Password = 1, // not currently used in server
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-10-23 00:18:14 +02:00
|
|
|
key: None,
|
|
|
|
|
2019-05-20 21:12:41 +02:00
|
|
|
atype,
|
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,
|
2020-04-17 22:35:27 +02:00
|
|
|
deleted_at: None,
|
2021-05-01 10:06:06 +02:00
|
|
|
reprompt: None,
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-01 15:09:10 +01:00
|
|
|
|
|
|
|
pub fn validate_notes(cipher_data: &[CipherData]) -> EmptyResult {
|
|
|
|
let mut validation_errors = serde_json::Map::new();
|
|
|
|
for (index, cipher) in cipher_data.iter().enumerate() {
|
|
|
|
if let Some(note) = &cipher.Notes {
|
|
|
|
if note.len() > 10_000 {
|
|
|
|
validation_errors.insert(
|
|
|
|
format!("Ciphers[{index}].Notes"),
|
|
|
|
serde_json::to_value([
|
|
|
|
"The field Notes exceeds the maximum encrypted value length of 10000 characters.",
|
|
|
|
])
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !validation_errors.is_empty() {
|
|
|
|
let err_json = json!({
|
|
|
|
"message": "The model state is invalid.",
|
|
|
|
"validationErrors" : validation_errors,
|
|
|
|
"object": "error"
|
|
|
|
});
|
|
|
|
err_json!(err_json, "Import validation errors")
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
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::DbConn;
|
|
|
|
|
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
2018-02-15 00:40:34 +01:00
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl Cipher {
|
2022-05-04 21:13:05 +02:00
|
|
|
pub async fn to_json(
|
|
|
|
&self,
|
|
|
|
host: &str,
|
|
|
|
user_uuid: &str,
|
|
|
|
cipher_sync_data: Option<&CipherSyncData>,
|
2023-02-27 16:37:58 +01:00
|
|
|
sync_type: CipherSyncType,
|
2022-05-20 23:39:47 +02:00
|
|
|
conn: &mut DbConn,
|
2022-05-04 21:13:05 +02:00
|
|
|
) -> Value {
|
2018-12-19 21:52:53 +01:00
|
|
|
use crate::util::format_date;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
let mut attachments_json: Value = Value::Null;
|
|
|
|
if let Some(cipher_sync_data) = cipher_sync_data {
|
|
|
|
if let Some(attachments) = cipher_sync_data.cipher_attachments.get(&self.uuid) {
|
|
|
|
attachments_json = attachments.iter().map(|c| c.to_json(host)).collect();
|
|
|
|
}
|
2021-01-31 21:46:37 +01:00
|
|
|
} else {
|
2022-05-04 21:13:05 +02:00
|
|
|
let attachments = Attachment::find_by_cipher(&self.uuid, conn).await;
|
|
|
|
if !attachments.is_empty() {
|
|
|
|
attachments_json = attachments.iter().map(|c| c.to_json(host)).collect()
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2021-04-06 22:54:42 +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);
|
2021-03-31 22:18:35 +02:00
|
|
|
|
2023-02-27 16:37:58 +01:00
|
|
|
// We don't need these values at all for Organizational syncs
|
|
|
|
// Skip any other database calls if this is the case and just return false.
|
|
|
|
let (read_only, hide_passwords) = if sync_type == CipherSyncType::User {
|
|
|
|
match self.get_access_restrictions(user_uuid, cipher_sync_data, conn).await {
|
|
|
|
Some((ro, hp)) => (ro, hp),
|
|
|
|
None => {
|
|
|
|
error!("Cipher ownership assertion failure");
|
|
|
|
(true, true)
|
|
|
|
}
|
2021-03-31 22:18:35 +02:00
|
|
|
}
|
2023-02-27 16:37:58 +01:00
|
|
|
} else {
|
|
|
|
(false, false)
|
2021-03-31 22:18:35 +02:00
|
|
|
};
|
2020-07-03 06:51:20 +02:00
|
|
|
|
2021-01-31 21:46:37 +01:00
|
|
|
// Get the type_data or a default to an empty json object '{}'.
|
|
|
|
// If not passing an empty object, mobile clients will crash.
|
2023-03-09 16:31:28 +01:00
|
|
|
let mut type_data_json: Value =
|
|
|
|
serde_json::from_str(&self.data).unwrap_or_else(|_| Value::Object(serde_json::Map::new()));
|
2021-01-31 21:46:37 +01:00
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
// NOTE: This was marked as *Backwards Compatibility Code*, but as of January 2021 this is still being used by upstream
|
2021-01-31 21:46:37 +01:00
|
|
|
// Set the first element of the Uris array as Uri, this is needed several (mobile) clients.
|
|
|
|
if self.atype == 1 {
|
|
|
|
if type_data_json["Uris"].is_array() {
|
|
|
|
let uri = type_data_json["Uris"][0]["Uri"].clone();
|
|
|
|
type_data_json["Uri"] = uri;
|
|
|
|
} else {
|
|
|
|
// Upstream always has an Uri key/value
|
|
|
|
type_data_json["Uri"] = Value::Null;
|
|
|
|
}
|
2018-03-06 00:02:36 +01:00
|
|
|
}
|
2021-01-31 21:46:37 +01:00
|
|
|
|
|
|
|
// Clone the type_data and add some default value.
|
|
|
|
let mut data_json = type_data_json.clone();
|
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
// NOTE: This was marked as *Backwards Compatibility Code*, but as of January 2021 this is still being used by upstream
|
2021-01-31 21:46:37 +01:00
|
|
|
// data_json should always contain the following keys with every atype
|
2023-03-09 16:31:28 +01:00
|
|
|
data_json["Fields"] = fields_json.clone();
|
2021-01-31 21:46:37 +01:00
|
|
|
data_json["Name"] = json!(self.name);
|
|
|
|
data_json["Notes"] = json!(self.notes);
|
2023-03-09 16:31:28 +01:00
|
|
|
data_json["PasswordHistory"] = password_history_json.clone();
|
2018-03-06 00:02:36 +01:00
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
let collection_ids = if let Some(cipher_sync_data) = cipher_sync_data {
|
|
|
|
if let Some(cipher_collections) = cipher_sync_data.cipher_collections.get(&self.uuid) {
|
|
|
|
Cow::from(cipher_collections)
|
|
|
|
} else {
|
|
|
|
Cow::from(Vec::with_capacity(0))
|
|
|
|
}
|
|
|
|
} else {
|
2022-05-20 23:39:47 +02:00
|
|
|
Cow::from(self.get_collections(user_uuid.to_string(), conn).await)
|
2022-05-04 21:13:05 +02:00
|
|
|
};
|
|
|
|
|
2020-07-03 06:51:20 +02:00
|
|
|
// There are three types of cipher response models in upstream
|
|
|
|
// Bitwarden: "cipherMini", "cipher", and "cipherDetails" (in order
|
2021-04-27 23:18:32 +02:00
|
|
|
// of increasing level of detail). vaultwarden currently only
|
2020-07-03 06:51:20 +02:00
|
|
|
// supports the "cipherDetails" type, though it seems like the
|
|
|
|
// Bitwarden clients will ignore extra fields.
|
|
|
|
//
|
|
|
|
// Ref: https://github.com/bitwarden/server/blob/master/src/Core/Models/Api/Response/CipherResponseModel.cs
|
2018-03-06 00:02:36 +01:00
|
|
|
let mut json_object = json!({
|
2020-07-03 06:51:20 +02:00
|
|
|
"Object": "cipherDetails",
|
2018-02-10 01:00:55 +01:00
|
|
|
"Id": self.uuid,
|
2019-05-20 21:12:41 +02:00
|
|
|
"Type": self.atype,
|
2022-10-12 09:17:09 +02:00
|
|
|
"CreationDate": format_date(&self.created_at),
|
2018-02-10 01:00:55 +01:00
|
|
|
"RevisionDate": format_date(&self.updated_at),
|
2020-04-17 22:35:27 +02:00
|
|
|
"DeletedDate": self.deleted_at.map_or(Value::Null, |d| Value::String(format_date(&d))),
|
2021-05-01 10:06:06 +02:00
|
|
|
"Reprompt": self.reprompt.unwrap_or(RepromptType::None as i32),
|
2018-04-27 13:49:34 +02:00
|
|
|
"OrganizationId": self.organization_uuid,
|
2023-10-23 00:18:14 +02:00
|
|
|
"Key": self.key,
|
2018-02-15 00:40:34 +01:00
|
|
|
"Attachments": attachments_json,
|
2021-01-31 21:46:37 +01:00
|
|
|
// We have UseTotp set to true by default within the Organization model.
|
|
|
|
// This variable together with UsersGetPremium is used to show or hide the TOTP counter.
|
2018-07-01 15:49:16 +02:00
|
|
|
"OrganizationUseTotp": true,
|
2020-07-03 06:51:20 +02:00
|
|
|
|
|
|
|
// This field is specific to the cipherDetails type.
|
2022-05-04 21:13:05 +02:00
|
|
|
"CollectionIds": collection_ids,
|
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-08-27 23:08:58 +02:00
|
|
|
"PasswordHistory": password_history_json,
|
2021-01-31 21:46:37 +01:00
|
|
|
|
|
|
|
// All Cipher types are included by default as null, but only the matching one will be populated
|
|
|
|
"Login": null,
|
|
|
|
"SecureNote": null,
|
|
|
|
"Card": null,
|
|
|
|
"Identity": null,
|
2018-03-06 00:02:36 +01:00
|
|
|
});
|
|
|
|
|
2023-02-27 16:37:58 +01:00
|
|
|
// These values are only needed for user/default syncs
|
|
|
|
// Not during an organizational sync like `get_org_details`
|
|
|
|
// Skip adding these fields in that case
|
|
|
|
if sync_type == CipherSyncType::User {
|
|
|
|
json_object["FolderId"] = json!(if let Some(cipher_sync_data) = cipher_sync_data {
|
|
|
|
cipher_sync_data.cipher_folders.get(&self.uuid).map(|c| c.to_string())
|
|
|
|
} else {
|
|
|
|
self.get_folder_uuid(user_uuid, conn).await
|
|
|
|
});
|
|
|
|
json_object["Favorite"] = json!(if let Some(cipher_sync_data) = cipher_sync_data {
|
|
|
|
cipher_sync_data.cipher_favorites.contains(&self.uuid)
|
|
|
|
} else {
|
|
|
|
self.is_favorite(user_uuid, conn).await
|
|
|
|
});
|
|
|
|
// These values are true by default, but can be false if the
|
|
|
|
// cipher belongs to a collection or group where the org owner has enabled
|
|
|
|
// the "Read Only" or "Hide Passwords" restrictions for the user.
|
|
|
|
json_object["Edit"] = json!(!read_only);
|
|
|
|
json_object["ViewPassword"] = json!(!hide_passwords);
|
|
|
|
}
|
|
|
|
|
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"),
|
|
|
|
};
|
|
|
|
|
2021-01-31 21:46:37 +01:00
|
|
|
json_object[key] = type_data_json;
|
2018-03-06 00:02:36 +01:00
|
|
|
json_object
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn update_users_revision(&self, conn: &mut DbConn) -> Vec<String> {
|
2018-09-01 17:59:13 +02:00
|
|
|
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) => {
|
2021-11-16 17:07:55 +01:00
|
|
|
User::update_uuid_revision(user_uuid, conn).await;
|
2018-09-01 17:59:13 +02:00
|
|
|
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 {
|
2024-01-01 15:46:03 +01:00
|
|
|
// users having access to the collection
|
|
|
|
let mut collection_users =
|
|
|
|
UserOrganization::find_by_cipher_and_org(&self.uuid, org_uuid, conn).await;
|
|
|
|
if CONFIG.org_groups_enabled() {
|
|
|
|
// members of a group having access to the collection
|
|
|
|
let group_users =
|
|
|
|
UserOrganization::find_by_cipher_and_org_with_group(&self.uuid, org_uuid, conn).await;
|
|
|
|
collection_users.extend(group_users);
|
|
|
|
}
|
|
|
|
for user_org in collection_users {
|
2021-11-16 17:07:55 +01:00
|
|
|
User::update_uuid_revision(&user_org.user_uuid, conn).await;
|
2021-04-06 22:54:42 +02:00
|
|
|
user_uuids.push(user_org.user_uuid.clone())
|
2021-11-16 17:07:55 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn save(&mut self, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
self.update_users_revision(conn).await;
|
2018-02-15 01:07:57 +01:00
|
|
|
self.updated_at = Utc::now().naive_utc();
|
2020-09-22 12:13:02 +02:00
|
|
|
|
|
|
|
db_run! { conn:
|
2020-08-18 17:15:44 +02:00
|
|
|
sqlite, mysql {
|
2020-09-22 12:13:02 +02:00
|
|
|
match diesel::replace_into(ciphers::table)
|
2020-08-18 17:15:44 +02:00
|
|
|
.values(CipherDb::to_db(self))
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
{
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
|
|
|
|
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
|
|
|
|
diesel::update(ciphers::table)
|
|
|
|
.filter(ciphers::uuid.eq(&self.uuid))
|
|
|
|
.set(CipherDb::to_db(self))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving cipher")
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}.map_res("Error saving cipher")
|
|
|
|
}
|
2020-08-18 17:15:44 +02:00
|
|
|
postgresql {
|
|
|
|
let value = CipherDb::to_db(self);
|
|
|
|
diesel::insert_into(ciphers::table)
|
|
|
|
.values(&value)
|
|
|
|
.on_conflict(ciphers::uuid)
|
|
|
|
.do_update()
|
|
|
|
.set(&value)
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving cipher")
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete(&self, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
self.update_users_revision(conn).await;
|
2018-08-21 18:31:01 +02:00
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
FolderCipher::delete_all_by_cipher(&self.uuid, conn).await?;
|
|
|
|
CollectionCipher::delete_all_by_cipher(&self.uuid, conn).await?;
|
|
|
|
Attachment::delete_all_by_cipher(&self.uuid, conn).await?;
|
|
|
|
Favorite::delete_all_by_cipher(&self.uuid, conn).await?;
|
2018-05-15 18:27:53 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_organization(org_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
// TODO: Optimize this by executing a DELETE directly on the database, instead of first fetching.
|
|
|
|
for cipher in Self::find_by_org(org_uuid, conn).await {
|
|
|
|
cipher.delete(conn).await?;
|
2018-05-18 17:52:51 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_user(user_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
for cipher in Self::find_owned_by_user(user_uuid, conn).await {
|
|
|
|
cipher.delete(conn).await?;
|
2018-10-12 16:20:10 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-03 05:52:15 +02:00
|
|
|
/// Purge all ciphers that are old enough to be auto-deleted.
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn purge_trash(conn: &mut DbConn) {
|
2021-04-03 05:52:15 +02:00
|
|
|
if let Some(auto_delete_days) = CONFIG.trash_auto_delete_days() {
|
|
|
|
let now = Utc::now().naive_utc();
|
2024-03-19 19:47:30 +01:00
|
|
|
let dt = now - TimeDelta::try_days(auto_delete_days).unwrap();
|
2021-11-16 17:07:55 +01:00
|
|
|
for cipher in Self::find_deleted_before(&dt, conn).await {
|
|
|
|
cipher.delete(conn).await.ok();
|
2021-04-03 05:52:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn move_to_folder(&self, folder_uuid: Option<String>, user_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
User::update_uuid_revision(user_uuid, conn).await;
|
2019-01-28 00:39:14 +01:00
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
match (self.get_folder_uuid(user_uuid, conn).await, folder_uuid) {
|
2019-01-28 00:39:14 +01:00
|
|
|
// No changes
|
|
|
|
(None, None) => Ok(()),
|
|
|
|
(Some(ref old), Some(ref new)) if old == new => Ok(()),
|
|
|
|
|
|
|
|
// Add to folder
|
2021-11-16 17:07:55 +01:00
|
|
|
(None, Some(new)) => FolderCipher::new(&new, &self.uuid).save(conn).await,
|
2019-01-28 00:39:14 +01:00
|
|
|
|
|
|
|
// Remove from folder
|
2021-11-16 17:07:55 +01:00
|
|
|
(Some(old), None) => match FolderCipher::find_by_folder_and_cipher(&old, &self.uuid, conn).await {
|
|
|
|
Some(old) => old.delete(conn).await,
|
2019-01-28 00:39:14 +01:00
|
|
|
None => err!("Couldn't move from previous folder"),
|
|
|
|
},
|
|
|
|
|
|
|
|
// Move to another folder
|
|
|
|
(Some(old), Some(new)) => {
|
2021-11-16 17:07:55 +01:00
|
|
|
if let Some(old) = FolderCipher::find_by_folder_and_cipher(&old, &self.uuid, conn).await {
|
|
|
|
old.delete(conn).await?;
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
2021-11-16 17:07:55 +01:00
|
|
|
FolderCipher::new(&new, &self.uuid).save(conn).await
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 06:51:20 +02:00
|
|
|
/// Returns whether this cipher is directly owned by the user.
|
2020-07-03 18:00:33 +02:00
|
|
|
pub fn is_owned_by_user(&self, user_uuid: &str) -> bool {
|
|
|
|
self.user_uuid.is_some() && self.user_uuid.as_ref().unwrap() == user_uuid
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 06:51:20 +02:00
|
|
|
/// Returns whether this cipher is owned by an org in which the user has full access.
|
2022-10-20 15:31:53 +02:00
|
|
|
async fn is_in_full_access_org(
|
2022-05-04 21:13:05 +02:00
|
|
|
&self,
|
|
|
|
user_uuid: &str,
|
|
|
|
cipher_sync_data: Option<&CipherSyncData>,
|
2022-05-20 23:39:47 +02:00
|
|
|
conn: &mut DbConn,
|
2022-05-04 21:13:05 +02:00
|
|
|
) -> bool {
|
2020-07-03 19:49:10 +02:00
|
|
|
if let Some(ref org_uuid) = self.organization_uuid {
|
2022-05-04 21:13:05 +02:00
|
|
|
if let Some(cipher_sync_data) = cipher_sync_data {
|
|
|
|
if let Some(cached_user_org) = cipher_sync_data.user_organizations.get(org_uuid) {
|
|
|
|
return cached_user_org.has_full_access();
|
|
|
|
}
|
|
|
|
} else if let Some(user_org) = UserOrganization::find_by_user_and_org(user_uuid, org_uuid, conn).await {
|
2020-07-03 19:49:10 +02:00
|
|
|
return user_org.has_full_access();
|
|
|
|
}
|
2020-07-03 18:00:33 +02:00
|
|
|
}
|
2020-07-03 19:49:10 +02:00
|
|
|
false
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 15:31:53 +02:00
|
|
|
/// Returns whether this cipher is owned by an group in which the user has full access.
|
|
|
|
async fn is_in_full_access_group(
|
|
|
|
&self,
|
|
|
|
user_uuid: &str,
|
|
|
|
cipher_sync_data: Option<&CipherSyncData>,
|
2022-05-20 23:39:47 +02:00
|
|
|
conn: &mut DbConn,
|
2022-10-20 15:31:53 +02:00
|
|
|
) -> bool {
|
2024-01-25 22:02:07 +01:00
|
|
|
if !CONFIG.org_groups_enabled() {
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-20 15:31:53 +02:00
|
|
|
if let Some(ref org_uuid) = self.organization_uuid {
|
|
|
|
if let Some(cipher_sync_data) = cipher_sync_data {
|
2024-04-06 13:55:10 +02:00
|
|
|
return cipher_sync_data.user_group_full_access_for_organizations.contains(org_uuid);
|
2022-10-20 15:31:53 +02:00
|
|
|
} else {
|
|
|
|
return Group::is_in_full_access_group(user_uuid, org_uuid, conn).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-07-03 06:51:20 +02:00
|
|
|
/// Returns the user's access restrictions to this cipher. A return value
|
|
|
|
/// of None means that this cipher does not belong to the user, and is
|
|
|
|
/// not in any collection the user has access to. Otherwise, the user has
|
|
|
|
/// access to this cipher, and Some(read_only, hide_passwords) represents
|
|
|
|
/// the access restrictions.
|
2022-05-04 21:13:05 +02:00
|
|
|
pub async fn get_access_restrictions(
|
|
|
|
&self,
|
|
|
|
user_uuid: &str,
|
|
|
|
cipher_sync_data: Option<&CipherSyncData>,
|
2022-05-20 23:39:47 +02:00
|
|
|
conn: &mut DbConn,
|
2022-05-04 21:13:05 +02:00
|
|
|
) -> Option<(bool, bool)> {
|
2020-07-03 06:51:20 +02:00
|
|
|
// Check whether this cipher is directly owned by the user, or is in
|
|
|
|
// a collection that the user has full access to. If so, there are no
|
|
|
|
// access restrictions.
|
2022-10-20 15:31:53 +02:00
|
|
|
if self.is_owned_by_user(user_uuid)
|
|
|
|
|| self.is_in_full_access_org(user_uuid, cipher_sync_data, conn).await
|
|
|
|
|| self.is_in_full_access_group(user_uuid, cipher_sync_data, conn).await
|
|
|
|
{
|
2020-07-03 06:51:20 +02:00
|
|
|
return Some((false, false));
|
|
|
|
}
|
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
let rows = if let Some(cipher_sync_data) = cipher_sync_data {
|
|
|
|
let mut rows: Vec<(bool, bool)> = Vec::new();
|
|
|
|
if let Some(collections) = cipher_sync_data.cipher_collections.get(&self.uuid) {
|
|
|
|
for collection in collections {
|
2022-10-20 15:31:53 +02:00
|
|
|
//User permissions
|
2022-05-04 21:13:05 +02:00
|
|
|
if let Some(uc) = cipher_sync_data.user_collections.get(collection) {
|
|
|
|
rows.push((uc.read_only, uc.hide_passwords));
|
|
|
|
}
|
2022-10-20 15:31:53 +02:00
|
|
|
|
|
|
|
//Group permissions
|
|
|
|
if let Some(cg) = cipher_sync_data.user_collections_groups.get(collection) {
|
|
|
|
rows.push((cg.read_only, cg.hide_passwords));
|
|
|
|
}
|
2022-05-04 21:13:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rows
|
|
|
|
} else {
|
2022-10-20 15:31:53 +02:00
|
|
|
let mut access_flags = self.get_user_collections_access_flags(user_uuid, conn).await;
|
|
|
|
access_flags.append(&mut self.get_group_collections_access_flags(user_uuid, conn).await);
|
|
|
|
access_flags
|
2022-05-04 21:13:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if rows.is_empty() {
|
|
|
|
// This cipher isn't in any collections accessible to the user.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A cipher can be in multiple collections with inconsistent access flags.
|
|
|
|
// For example, a cipher could be in one collection where the user has
|
|
|
|
// read-only access, but also in another collection where the user has
|
|
|
|
// read/write access. For a flag to be in effect for a cipher, upstream
|
|
|
|
// requires all collections the cipher is in to have that flag set.
|
|
|
|
// Therefore, we do a boolean AND of all values in each of the `read_only`
|
|
|
|
// and `hide_passwords` columns. This could ideally be done as part of the
|
|
|
|
// query, but Diesel doesn't support a min() or bool_and() function on
|
|
|
|
// booleans and this behavior isn't portable anyway.
|
|
|
|
let mut read_only = true;
|
|
|
|
let mut hide_passwords = true;
|
|
|
|
for (ro, hp) in rows.iter() {
|
|
|
|
read_only &= ro;
|
|
|
|
hide_passwords &= hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some((read_only, hide_passwords))
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
async fn get_user_collections_access_flags(&self, user_uuid: &str, conn: &mut DbConn) -> Vec<(bool, bool)> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
// Check whether this cipher is in any collections accessible to the
|
|
|
|
// user. If so, retrieve the access flags for each collection.
|
2022-05-04 21:13:05 +02:00
|
|
|
ciphers::table
|
2020-08-18 17:15:44 +02:00
|
|
|
.filter(ciphers::uuid.eq(&self.uuid))
|
|
|
|
.inner_join(ciphers_collections::table.on(
|
|
|
|
ciphers::uuid.eq(ciphers_collections::cipher_uuid)))
|
|
|
|
.inner_join(users_collections::table.on(
|
|
|
|
ciphers_collections::collection_uuid.eq(users_collections::collection_uuid)
|
|
|
|
.and(users_collections::user_uuid.eq(user_uuid))))
|
2021-10-29 20:45:43 +02:00
|
|
|
.select((users_collections::read_only, users_collections::hide_passwords))
|
|
|
|
.load::<(bool, bool)>(conn)
|
2022-10-20 15:31:53 +02:00
|
|
|
.expect("Error getting user access restrictions")
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
async fn get_group_collections_access_flags(&self, user_uuid: &str, conn: &mut DbConn) -> Vec<(bool, bool)> {
|
2024-01-25 22:02:07 +01:00
|
|
|
if !CONFIG.org_groups_enabled() {
|
|
|
|
return Vec::new();
|
|
|
|
}
|
2022-10-20 15:31:53 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::uuid.eq(&self.uuid))
|
|
|
|
.inner_join(ciphers_collections::table.on(
|
|
|
|
ciphers::uuid.eq(ciphers_collections::cipher_uuid)
|
|
|
|
))
|
|
|
|
.inner_join(collections_groups::table.on(
|
|
|
|
collections_groups::collections_uuid.eq(ciphers_collections::collection_uuid)
|
|
|
|
))
|
|
|
|
.inner_join(groups_users::table.on(
|
|
|
|
groups_users::groups_uuid.eq(collections_groups::groups_uuid)
|
|
|
|
))
|
|
|
|
.inner_join(users_organizations::table.on(
|
|
|
|
users_organizations::uuid.eq(groups_users::users_organizations_uuid)
|
|
|
|
))
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.select((collections_groups::read_only, collections_groups::hide_passwords))
|
|
|
|
.load::<(bool, bool)>(conn)
|
|
|
|
.expect("Error getting group access restrictions")
|
2020-08-18 17:15:44 +02:00
|
|
|
}}
|
2020-07-03 06:51:20 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn is_write_accessible_to_user(&self, user_uuid: &str, conn: &mut DbConn) -> bool {
|
2022-05-04 21:13:05 +02:00
|
|
|
match self.get_access_restrictions(user_uuid, None, conn).await {
|
2020-07-03 06:51:20 +02:00
|
|
|
Some((read_only, _hide_passwords)) => !read_only,
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn is_accessible_to_user(&self, user_uuid: &str, conn: &mut DbConn) -> bool {
|
2022-05-04 21:13:05 +02:00
|
|
|
self.get_access_restrictions(user_uuid, None, conn).await.is_some()
|
2020-07-03 06:51:20 +02:00
|
|
|
}
|
|
|
|
|
2020-08-19 11:16:27 +02:00
|
|
|
// Returns whether this cipher is a favorite of the specified user.
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn is_favorite(&self, user_uuid: &str, conn: &mut DbConn) -> bool {
|
2021-11-16 17:07:55 +01:00
|
|
|
Favorite::is_favorite(&self.uuid, user_uuid, conn).await
|
2020-08-19 11:16:27 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 10:27:38 +02:00
|
|
|
// Sets whether this cipher is a favorite of the specified user.
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn set_favorite(&self, favorite: Option<bool>, user_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2020-08-26 10:27:38 +02:00
|
|
|
match favorite {
|
|
|
|
None => Ok(()), // No change requested.
|
2021-11-16 17:07:55 +01:00
|
|
|
Some(status) => Favorite::set_favorite(status, &self.uuid, user_uuid, conn).await,
|
2020-08-19 11:16:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn get_folder_uuid(&self, user_uuid: &str, conn: &mut DbConn) -> Option<String> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
folders_ciphers::table
|
|
|
|
.inner_join(folders::table)
|
|
|
|
.filter(folders::user_uuid.eq(&user_uuid))
|
|
|
|
.filter(folders_ciphers::cipher_uuid.eq(&self.uuid))
|
|
|
|
.select(folders_ciphers::folder_uuid)
|
|
|
|
.first::<String>(conn)
|
|
|
|
.ok()
|
|
|
|
}}
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_uuid(uuid: &str, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::uuid.eq(uuid))
|
|
|
|
.first::<CipherDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2020-09-01 09:49:48 +02:00
|
|
|
// Find all ciphers accessible or visible to the specified user.
|
|
|
|
//
|
|
|
|
// "Accessible" means the user has read access to the cipher, either via
|
2022-10-20 15:31:53 +02:00
|
|
|
// direct ownership, collection or via group access.
|
2020-09-01 09:49:48 +02:00
|
|
|
//
|
|
|
|
// "Visible" usually means the same as accessible, except when an org
|
2022-10-20 15:31:53 +02:00
|
|
|
// owner/admin sets their account or group to have access to only selected
|
2020-09-01 09:49:48 +02:00
|
|
|
// collections in the org (presumably because they aren't interested in
|
|
|
|
// the other collections in the org). In this case, if `visible_only` is
|
|
|
|
// true, then the non-interesting ciphers will not be returned. As a
|
|
|
|
// result, those ciphers will not appear in "My Vault" for the org
|
|
|
|
// owner/admin, but they can still be accessed via the org vault view.
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user(user_uuid: &str, visible_only: bool, conn: &mut DbConn) -> Vec<Self> {
|
2024-01-25 22:02:07 +01:00
|
|
|
if CONFIG.org_groups_enabled() {
|
|
|
|
db_run! {conn: {
|
|
|
|
let mut query = ciphers::table
|
|
|
|
.left_join(ciphers_collections::table.on(
|
|
|
|
ciphers::uuid.eq(ciphers_collections::cipher_uuid)
|
|
|
|
))
|
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
ciphers::organization_uuid.eq(users_organizations::org_uuid.nullable())
|
|
|
|
.and(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.and(users_organizations::status.eq(UserOrgStatus::Confirmed as i32))
|
|
|
|
))
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
ciphers_collections::collection_uuid.eq(users_collections::collection_uuid)
|
|
|
|
// Ensure that users_collections::user_uuid is NULL for unconfirmed users.
|
|
|
|
.and(users_organizations::user_uuid.eq(users_collections::user_uuid))
|
|
|
|
))
|
|
|
|
.left_join(groups_users::table.on(
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid)
|
|
|
|
))
|
|
|
|
.left_join(groups::table.on(
|
|
|
|
groups::uuid.eq(groups_users::groups_uuid)
|
|
|
|
))
|
|
|
|
.left_join(collections_groups::table.on(
|
|
|
|
collections_groups::collections_uuid.eq(ciphers_collections::collection_uuid).and(
|
|
|
|
collections_groups::groups_uuid.eq(groups::uuid)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid)) // Cipher owner
|
|
|
|
.or_filter(users_organizations::access_all.eq(true)) // access_all in org
|
|
|
|
.or_filter(users_collections::user_uuid.eq(user_uuid)) // Access to collection
|
|
|
|
.or_filter(groups::access_all.eq(true)) // Access via groups
|
|
|
|
.or_filter(collections_groups::collections_uuid.is_not_null()) // Access via groups
|
|
|
|
.into_boxed();
|
|
|
|
|
|
|
|
if !visible_only {
|
|
|
|
query = query.or_filter(
|
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32) // Org admin/owner
|
|
|
|
);
|
|
|
|
}
|
2020-09-01 09:49:48 +02:00
|
|
|
|
2024-01-25 22:02:07 +01:00
|
|
|
query
|
|
|
|
.select(ciphers::all_columns)
|
|
|
|
.distinct()
|
|
|
|
.load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
|
|
|
|
}}
|
|
|
|
} else {
|
|
|
|
db_run! {conn: {
|
|
|
|
let mut query = ciphers::table
|
|
|
|
.left_join(ciphers_collections::table.on(
|
|
|
|
ciphers::uuid.eq(ciphers_collections::cipher_uuid)
|
|
|
|
))
|
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
ciphers::organization_uuid.eq(users_organizations::org_uuid.nullable())
|
|
|
|
.and(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.and(users_organizations::status.eq(UserOrgStatus::Confirmed as i32))
|
|
|
|
))
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
ciphers_collections::collection_uuid.eq(users_collections::collection_uuid)
|
|
|
|
// Ensure that users_collections::user_uuid is NULL for unconfirmed users.
|
|
|
|
.and(users_organizations::user_uuid.eq(users_collections::user_uuid))
|
|
|
|
))
|
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid)) // Cipher owner
|
|
|
|
.or_filter(users_organizations::access_all.eq(true)) // access_all in org
|
|
|
|
.or_filter(users_collections::user_uuid.eq(user_uuid)) // Access to collection
|
|
|
|
.into_boxed();
|
|
|
|
|
|
|
|
if !visible_only {
|
|
|
|
query = query.or_filter(
|
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32) // Org admin/owner
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
query
|
|
|
|
.select(ciphers::all_columns)
|
|
|
|
.distinct()
|
|
|
|
.load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
|
|
|
|
}}
|
|
|
|
}
|
2018-05-12 00:53:37 +02:00
|
|
|
}
|
|
|
|
|
2020-09-01 09:49:48 +02:00
|
|
|
// Find all ciphers visible to the specified user.
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user_visible(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2021-11-16 17:07:55 +01:00
|
|
|
Self::find_by_user(user_uuid, true, conn).await
|
2020-09-01 09:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find all ciphers directly owned by the specified user.
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_owned_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
ciphers::table
|
2021-01-31 21:46:37 +01:00
|
|
|
.filter(
|
|
|
|
ciphers::user_uuid.eq(user_uuid)
|
|
|
|
.and(ciphers::organization_uuid.is_null())
|
|
|
|
)
|
2020-08-18 17:15:44 +02:00
|
|
|
.load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
|
|
|
|
}}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
2018-02-16 00:32:26 +01:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn count_owned_by_user(user_uuid: &str, conn: &mut DbConn) -> i64 {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid))
|
|
|
|
.count()
|
|
|
|
.first::<i64>(conn)
|
|
|
|
.ok()
|
|
|
|
.unwrap_or(0)
|
|
|
|
}}
|
2020-05-28 10:42:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_org(org_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::organization_uuid.eq(org_uuid))
|
|
|
|
.load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
|
|
|
|
}}
|
2018-04-27 13:49:34 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn count_by_org(org_uuid: &str, conn: &mut DbConn) -> i64 {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::organization_uuid.eq(org_uuid))
|
|
|
|
.count()
|
|
|
|
.first::<i64>(conn)
|
|
|
|
.ok()
|
|
|
|
.unwrap_or(0)
|
|
|
|
}}
|
2020-06-03 20:37:31 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_folder(folder_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
folders_ciphers::table.inner_join(ciphers::table)
|
|
|
|
.filter(folders_ciphers::folder_uuid.eq(folder_uuid))
|
|
|
|
.select(ciphers::all_columns)
|
|
|
|
.load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
|
|
|
|
}}
|
2018-02-16 00:32:26 +01:00
|
|
|
}
|
2018-05-09 12:55:05 +02:00
|
|
|
|
2021-04-03 05:52:15 +02:00
|
|
|
/// Find all ciphers that were deleted before the specified datetime.
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_deleted_before(dt: &NaiveDateTime, conn: &mut DbConn) -> Vec<Self> {
|
2021-04-03 05:52:15 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::deleted_at.lt(dt))
|
|
|
|
.load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn get_collections(&self, user_id: String, conn: &mut DbConn) -> Vec<String> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
ciphers_collections::table
|
|
|
|
.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(
|
2022-05-20 23:39:47 +02:00
|
|
|
users_organizations::user_uuid.eq(user_id.clone())
|
2020-08-18 17:15:44 +02:00
|
|
|
)
|
|
|
|
))
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
users_collections::collection_uuid.eq(ciphers_collections::collection_uuid).and(
|
2022-05-20 23:39:47 +02:00
|
|
|
users_collections::user_uuid.eq(user_id.clone())
|
2020-08-18 17:15:44 +02:00
|
|
|
)
|
|
|
|
))
|
|
|
|
.filter(ciphers_collections::cipher_uuid.eq(&self.uuid))
|
|
|
|
.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
|
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32) // User is admin or owner
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.select(ciphers_collections::collection_uuid)
|
|
|
|
.load::<String>(conn).unwrap_or_default()
|
|
|
|
}}
|
2018-05-09 12:55:05 +02:00
|
|
|
}
|
2022-05-04 21:13:05 +02:00
|
|
|
|
|
|
|
/// Return a Vec with (cipher_uuid, collection_uuid)
|
|
|
|
/// This is used during a full sync so we only need one query for all collections accessible.
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn get_collections_with_cipher_by_user(user_id: String, conn: &mut DbConn) -> Vec<(String, String)> {
|
2022-05-04 21:13:05 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
ciphers_collections::table
|
|
|
|
.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(
|
2022-05-20 23:39:47 +02:00
|
|
|
users_organizations::user_uuid.eq(user_id.clone())
|
2022-05-04 21:13:05 +02:00
|
|
|
)
|
|
|
|
))
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
users_collections::collection_uuid.eq(ciphers_collections::collection_uuid).and(
|
2022-05-20 23:39:47 +02:00
|
|
|
users_collections::user_uuid.eq(user_id.clone())
|
2022-05-04 21:13:05 +02:00
|
|
|
)
|
|
|
|
))
|
2022-10-20 15:31:53 +02:00
|
|
|
.left_join(groups_users::table.on(
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid)
|
|
|
|
))
|
|
|
|
.left_join(groups::table.on(
|
|
|
|
groups::uuid.eq(groups_users::groups_uuid)
|
|
|
|
))
|
|
|
|
.left_join(collections_groups::table.on(
|
|
|
|
collections_groups::collections_uuid.eq(ciphers_collections::collection_uuid).and(
|
|
|
|
collections_groups::groups_uuid.eq(groups::uuid)
|
2022-05-04 21:13:05 +02:00
|
|
|
)
|
|
|
|
))
|
2022-10-20 15:31:53 +02:00
|
|
|
.or_filter(users_collections::user_uuid.eq(user_id)) // User has access to collection
|
|
|
|
.or_filter(users_organizations::access_all.eq(true)) // User has access all
|
|
|
|
.or_filter(users_organizations::atype.le(UserOrgType::Admin as i32)) // User is admin or owner
|
|
|
|
.or_filter(groups::access_all.eq(true)) //Access via group
|
|
|
|
.or_filter(collections_groups::collections_uuid.is_not_null()) //Access via group
|
2022-05-04 21:13:05 +02:00
|
|
|
.select(ciphers_collections::all_columns)
|
2023-02-27 16:37:58 +01:00
|
|
|
.distinct()
|
2022-05-04 21:13:05 +02:00
|
|
|
.load::<(String, String)>(conn).unwrap_or_default()
|
|
|
|
}}
|
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|