2023-06-02 21:36:15 +02:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2021-03-31 21:18:35 +01:00
|
|
|
use num_traits::FromPrimitive;
|
2024-12-21 07:28:00 +01:00
|
|
|
use rocket::request::FromParam;
|
2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2024-08-15 12:36:00 +02:00
|
|
|
use std::{
|
2024-12-21 07:28:00 +01:00
|
|
|
borrow::Borrow,
|
2024-08-15 12:36:00 +02:00
|
|
|
cmp::Ordering,
|
|
|
|
collections::{HashMap, HashSet},
|
2024-12-21 07:28:00 +01:00
|
|
|
fmt::{Display, Formatter},
|
|
|
|
ops::Deref,
|
2024-08-15 12:36:00 +02:00
|
|
|
};
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2023-02-16 17:29:12 +01:00
|
|
|
use super::{CollectionUser, Group, GroupUser, OrgPolicy, OrgPolicyType, TwoFactor, User};
|
2024-08-15 12:36:00 +02:00
|
|
|
use crate::db::models::{Collection, CollectionGroup};
|
2022-11-20 19:15:45 +01:00
|
|
|
use crate::CONFIG;
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_object! {
|
2021-03-13 22:04:04 +01:00
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
2022-05-20 23:39:47 +02:00
|
|
|
#[diesel(table_name = organizations)]
|
|
|
|
#[diesel(primary_key(uuid))]
|
2020-08-18 17:15:44 +02:00
|
|
|
pub struct Organization {
|
2024-12-21 07:28:00 +01:00
|
|
|
pub uuid: OrganizationId,
|
2020-08-18 17:15:44 +02:00
|
|
|
pub name: String,
|
|
|
|
pub billing_email: String,
|
Added web-vault v2.21.x support + some misc fixes
- The new web-vault v2.21.0+ has support for Master Password Reset. For
this to work it generates a public/private key-pair which needs to be
stored in the database. Currently the Master Password Reset is not
fixed, but there are endpoints which are needed even if we do not
support this feature (yet). This PR fixes those endpoints, and stores
the keys already in the database.
- There was an issue when you want to do a key-rotate when you change
your password, it also called an Emergency Access endpoint, which we do
not yet support. Because this endpoint failed to reply correctly
produced some errors, and also prevent the user from being forced to
logout. This resolves #1826 by adding at least that endpoint.
Because of that extra endpoint check to Emergency Access is done using
an old user stamp, i also modified the stamp exception to allow multiple
rocket routes to be called, and added an expiration timestamp to it.
During these tests i stumbled upon an issue that after my key-change was
done, it triggered the websockets to try and reload my ciphers, because
they were updated. This shouldn't happen when rotating they keys, since
all access should be invalided. Now there will be no websocket
notification for this, which also prevents error toasts.
- Increased Send Size limit to 500MB (with a litle overhead)
As a side note, i tested these changes on both v2.20.4 and v2.21.1 web-vault versions, all keeps working.
2021-07-04 23:02:56 +02:00
|
|
|
pub private_key: Option<String>,
|
|
|
|
pub public_key: Option<String>,
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 22:04:04 +01:00
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
2022-05-20 23:39:47 +02:00
|
|
|
#[diesel(table_name = users_organizations)]
|
|
|
|
#[diesel(primary_key(uuid))]
|
2024-12-20 21:04:58 +01:00
|
|
|
pub struct Membership {
|
2020-08-18 17:15:44 +02:00
|
|
|
pub uuid: String,
|
|
|
|
pub user_uuid: String,
|
2024-12-21 07:28:00 +01:00
|
|
|
pub org_uuid: OrganizationId,
|
2020-08-18 17:15:44 +02:00
|
|
|
|
|
|
|
pub access_all: bool,
|
|
|
|
pub akey: String,
|
|
|
|
pub status: i32,
|
|
|
|
pub atype: i32,
|
2023-01-25 08:06:21 +01:00
|
|
|
pub reset_password_key: Option<String>,
|
2023-09-02 23:57:43 +02:00
|
|
|
pub external_id: Option<String>,
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
2023-06-02 21:36:15 +02:00
|
|
|
|
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
|
|
|
#[diesel(table_name = organization_api_key)]
|
|
|
|
#[diesel(primary_key(uuid, org_uuid))]
|
|
|
|
pub struct OrganizationApiKey {
|
|
|
|
pub uuid: String,
|
2024-12-21 07:28:00 +01:00
|
|
|
pub org_uuid: OrganizationId,
|
2023-06-02 21:36:15 +02:00
|
|
|
pub atype: i32,
|
|
|
|
pub api_key: String,
|
|
|
|
pub revision_date: NaiveDateTime,
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 16:42:36 +02:00
|
|
|
// https://github.com/bitwarden/server/blob/b86a04cef9f1e1b82cf18e49fc94e017c641130c/src/Core/Enums/OrganizationUserStatusType.cs
|
2024-12-20 21:04:58 +01:00
|
|
|
pub enum MembershipStatus {
|
2022-08-20 16:42:36 +02:00
|
|
|
Revoked = -1,
|
2018-09-10 14:51:40 +01:00
|
|
|
Invited = 0,
|
2018-04-24 22:01:55 +02:00
|
|
|
Accepted = 1,
|
|
|
|
Confirmed = 2,
|
|
|
|
}
|
|
|
|
|
2021-03-31 21:18:35 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, num_derive::FromPrimitive)]
|
2024-12-20 21:04:58 +01:00
|
|
|
pub enum MembershipType {
|
2018-04-24 22:01:55 +02:00
|
|
|
Owner = 0,
|
|
|
|
Admin = 1,
|
|
|
|
User = 2,
|
2018-11-12 17:13:25 +00:00
|
|
|
Manager = 3,
|
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
impl MembershipType {
|
2020-09-13 02:03:16 -07:00
|
|
|
pub fn from_str(s: &str) -> Option<Self> {
|
|
|
|
match s {
|
2024-12-20 21:04:58 +01:00
|
|
|
"0" | "Owner" => Some(MembershipType::Owner),
|
|
|
|
"1" | "Admin" => Some(MembershipType::Admin),
|
|
|
|
"2" | "User" => Some(MembershipType::User),
|
|
|
|
"3" | "Manager" => Some(MembershipType::Manager),
|
2020-09-13 02:03:16 -07:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
impl Ord for MembershipType {
|
|
|
|
fn cmp(&self, other: &MembershipType) -> Ordering {
|
2020-09-13 02:03:16 -07:00
|
|
|
// For easy comparison, map each variant to an access level (where 0 is lowest).
|
|
|
|
static ACCESS_LEVEL: [i32; 4] = [
|
|
|
|
3, // Owner
|
|
|
|
2, // Admin
|
|
|
|
0, // User
|
|
|
|
1, // Manager
|
|
|
|
];
|
|
|
|
ACCESS_LEVEL[*self as usize].cmp(&ACCESS_LEVEL[*other as usize])
|
2018-11-12 17:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
impl PartialOrd for MembershipType {
|
|
|
|
fn partial_cmp(&self, other: &MembershipType) -> Option<Ordering> {
|
2018-11-12 17:13:25 +00:00
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
impl PartialEq<i32> for MembershipType {
|
2018-11-12 17:13:25 +00:00
|
|
|
fn eq(&self, other: &i32) -> bool {
|
|
|
|
*other == *self as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
impl PartialOrd<i32> for MembershipType {
|
2018-11-12 17:13:25 +00:00
|
|
|
fn partial_cmp(&self, other: &i32) -> Option<Ordering> {
|
2018-12-07 15:01:29 +01:00
|
|
|
if let Some(other) = Self::from_i32(*other) {
|
2018-12-30 23:34:31 +01:00
|
|
|
return Some(self.cmp(&other));
|
2018-11-12 17:13:25 +00:00
|
|
|
}
|
2018-12-07 15:01:29 +01:00
|
|
|
None
|
2018-11-12 17:13:25 +00:00
|
|
|
}
|
2018-11-13 16:34:21 +00:00
|
|
|
|
|
|
|
fn gt(&self, other: &i32) -> bool {
|
2021-03-28 10:49:29 +01:00
|
|
|
matches!(self.partial_cmp(other), Some(Ordering::Greater))
|
2018-11-13 16:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ge(&self, other: &i32) -> bool {
|
2024-09-23 20:25:32 +02:00
|
|
|
matches!(self.partial_cmp(other), Some(Ordering::Greater | Ordering::Equal))
|
2018-11-13 16:34:21 +00:00
|
|
|
}
|
2018-11-12 17:13:25 +00:00
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
impl PartialEq<MembershipType> for i32 {
|
|
|
|
fn eq(&self, other: &MembershipType) -> bool {
|
2018-11-12 17:13:25 +00:00
|
|
|
*self == *other as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
impl PartialOrd<MembershipType> for i32 {
|
|
|
|
fn partial_cmp(&self, other: &MembershipType) -> Option<Ordering> {
|
|
|
|
if let Some(self_type) = MembershipType::from_i32(*self) {
|
2018-12-30 23:34:31 +01:00
|
|
|
return Some(self_type.cmp(other));
|
2018-11-12 17:13:25 +00:00
|
|
|
}
|
2018-12-07 15:01:29 +01:00
|
|
|
None
|
2018-11-12 17:13:25 +00:00
|
|
|
}
|
2018-11-13 16:34:21 +00:00
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
fn lt(&self, other: &MembershipType) -> bool {
|
2021-03-27 14:03:07 +00:00
|
|
|
matches!(self.partial_cmp(other), Some(Ordering::Less) | None)
|
2018-11-13 16:34:21 +00:00
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
fn le(&self, other: &MembershipType) -> bool {
|
2024-09-23 20:25:32 +02:00
|
|
|
matches!(self.partial_cmp(other), Some(Ordering::Less | Ordering::Equal) | None)
|
2018-11-13 16:34:21 +00:00
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Organization {
|
Added web-vault v2.21.x support + some misc fixes
- The new web-vault v2.21.0+ has support for Master Password Reset. For
this to work it generates a public/private key-pair which needs to be
stored in the database. Currently the Master Password Reset is not
fixed, but there are endpoints which are needed even if we do not
support this feature (yet). This PR fixes those endpoints, and stores
the keys already in the database.
- There was an issue when you want to do a key-rotate when you change
your password, it also called an Emergency Access endpoint, which we do
not yet support. Because this endpoint failed to reply correctly
produced some errors, and also prevent the user from being forced to
logout. This resolves #1826 by adding at least that endpoint.
Because of that extra endpoint check to Emergency Access is done using
an old user stamp, i also modified the stamp exception to allow multiple
rocket routes to be called, and added an expiration timestamp to it.
During these tests i stumbled upon an issue that after my key-change was
done, it triggered the websockets to try and reload my ciphers, because
they were updated. This shouldn't happen when rotating they keys, since
all access should be invalided. Now there will be no websocket
notification for this, which also prevents error toasts.
- Increased Send Size limit to 500MB (with a litle overhead)
As a side note, i tested these changes on both v2.20.4 and v2.21.1 web-vault versions, all keeps working.
2021-07-04 23:02:56 +02:00
|
|
|
pub fn new(name: String, billing_email: String, private_key: Option<String>, public_key: Option<String>) -> Self {
|
2018-04-24 22:01:55 +02:00
|
|
|
Self {
|
2024-12-21 07:28:00 +01:00
|
|
|
uuid: OrganizationId(crate::util::get_uuid()),
|
2018-04-24 22:01:55 +02:00
|
|
|
name,
|
|
|
|
billing_email,
|
Added web-vault v2.21.x support + some misc fixes
- The new web-vault v2.21.0+ has support for Master Password Reset. For
this to work it generates a public/private key-pair which needs to be
stored in the database. Currently the Master Password Reset is not
fixed, but there are endpoints which are needed even if we do not
support this feature (yet). This PR fixes those endpoints, and stores
the keys already in the database.
- There was an issue when you want to do a key-rotate when you change
your password, it also called an Emergency Access endpoint, which we do
not yet support. Because this endpoint failed to reply correctly
produced some errors, and also prevent the user from being forced to
logout. This resolves #1826 by adding at least that endpoint.
Because of that extra endpoint check to Emergency Access is done using
an old user stamp, i also modified the stamp exception to allow multiple
rocket routes to be called, and added an expiration timestamp to it.
During these tests i stumbled upon an issue that after my key-change was
done, it triggered the websockets to try and reload my ciphers, because
they were updated. This shouldn't happen when rotating they keys, since
all access should be invalided. Now there will be no websocket
notification for this, which also prevents error toasts.
- Increased Send Size limit to 500MB (with a litle overhead)
As a side note, i tested these changes on both v2.20.4 and v2.21.1 web-vault versions, all keeps working.
2021-07-04 23:02:56 +02:00
|
|
|
private_key,
|
|
|
|
public_key,
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-20 16:42:36 +02:00
|
|
|
// https://github.com/bitwarden/server/blob/13d1e74d6960cf0d042620b72d85bf583a4236f7/src/Api/Models/Response/Organizations/OrganizationResponseModel.cs
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json(&self) -> Value {
|
2018-04-24 22:01:55 +02:00
|
|
|
json!({
|
2024-06-23 21:31:02 +02:00
|
|
|
"id": self.uuid,
|
|
|
|
"identifier": null, // not supported by us
|
|
|
|
"name": self.name,
|
2024-07-10 17:25:41 +02:00
|
|
|
"seats": null,
|
|
|
|
"maxCollections": null,
|
|
|
|
"maxStorageGb": i16::MAX, // The value doesn't matter, we don't check server-side
|
2024-06-23 21:31:02 +02:00
|
|
|
"use2fa": true,
|
2024-07-10 17:25:41 +02:00
|
|
|
"useCustomPermissions": false,
|
2024-06-23 21:31:02 +02:00
|
|
|
"useDirectory": false, // Is supported, but this value isn't checked anywhere (yet)
|
|
|
|
"useEvents": CONFIG.org_events_enabled(),
|
|
|
|
"useGroups": CONFIG.org_groups_enabled(),
|
|
|
|
"useTotp": true,
|
|
|
|
"usePolicies": true,
|
|
|
|
// "useScim": false, // Not supported (Not AGPLv3 Licensed)
|
|
|
|
"useSso": false, // Not supported
|
|
|
|
// "useKeyConnector": false, // Not supported
|
|
|
|
"selfHost": true,
|
|
|
|
"useApi": true,
|
|
|
|
"hasPublicAndPrivateKeys": self.private_key.is_some() && self.public_key.is_some(),
|
|
|
|
"useResetPassword": CONFIG.mail_enabled(),
|
|
|
|
|
|
|
|
"businessName": null,
|
|
|
|
"businessAddress1": null,
|
|
|
|
"businessAddress2": null,
|
|
|
|
"businessAddress3": null,
|
|
|
|
"businessCountry": null,
|
|
|
|
"businessTaxNumber": null,
|
|
|
|
|
|
|
|
"billingEmail": self.billing_email,
|
2024-07-10 17:25:41 +02:00
|
|
|
"planType": 6, // Custom plan
|
2024-06-23 21:31:02 +02:00
|
|
|
"usersGetPremium": true,
|
|
|
|
"object": "organization",
|
2018-04-24 22:01:55 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 16:42:36 +02:00
|
|
|
// Used to either subtract or add to the current status
|
|
|
|
// The number 128 should be fine, it is well within the range of an i32
|
|
|
|
// The same goes for the database where we only use INTEGER (the same as an i32)
|
|
|
|
// It should also provide enough room for 100+ types, which i doubt will ever happen.
|
|
|
|
static ACTIVATE_REVOKE_DIFF: i32 = 128;
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
impl Membership {
|
2024-12-21 07:28:00 +01:00
|
|
|
pub fn new(user_uuid: String, org_uuid: OrganizationId) -> Self {
|
2018-04-24 22:01:55 +02:00
|
|
|
Self {
|
2018-12-07 14:32:40 +01:00
|
|
|
uuid: crate::util::get_uuid(),
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
user_uuid,
|
|
|
|
org_uuid,
|
|
|
|
|
|
|
|
access_all: false,
|
2019-05-20 21:24:29 +02:00
|
|
|
akey: String::new(),
|
2024-12-20 21:04:58 +01:00
|
|
|
status: MembershipStatus::Accepted as i32,
|
|
|
|
atype: MembershipType::User as i32,
|
2023-01-25 08:06:21 +01:00
|
|
|
reset_password_key: None,
|
2023-09-02 23:57:43 +02:00
|
|
|
external_id: None,
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-20 16:42:36 +02:00
|
|
|
|
2023-09-02 23:57:43 +02:00
|
|
|
pub fn restore(&mut self) -> bool {
|
2024-12-20 21:04:58 +01:00
|
|
|
if self.status < MembershipStatus::Invited as i32 {
|
2022-08-20 16:42:36 +02:00
|
|
|
self.status += ACTIVATE_REVOKE_DIFF;
|
2023-09-02 23:57:43 +02:00
|
|
|
return true;
|
2022-08-20 16:42:36 +02:00
|
|
|
}
|
2023-09-02 23:57:43 +02:00
|
|
|
false
|
2022-08-20 16:42:36 +02:00
|
|
|
}
|
|
|
|
|
2023-09-02 23:57:43 +02:00
|
|
|
pub fn revoke(&mut self) -> bool {
|
2024-12-20 21:04:58 +01:00
|
|
|
if self.status > MembershipStatus::Revoked as i32 {
|
2022-08-20 16:42:36 +02:00
|
|
|
self.status -= ACTIVATE_REVOKE_DIFF;
|
2023-09-02 23:57:43 +02:00
|
|
|
return true;
|
2022-08-20 16:42:36 +02:00
|
|
|
}
|
2023-09-02 23:57:43 +02:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2024-10-19 18:22:21 +02:00
|
|
|
/// Return the status of the user in an unrevoked state
|
|
|
|
pub fn get_unrevoked_status(&self) -> i32 {
|
2024-12-20 21:04:58 +01:00
|
|
|
if self.status <= MembershipStatus::Revoked as i32 {
|
2024-10-19 18:22:21 +02:00
|
|
|
return self.status + ACTIVATE_REVOKE_DIFF;
|
|
|
|
}
|
|
|
|
self.status
|
|
|
|
}
|
|
|
|
|
2023-09-02 23:57:43 +02:00
|
|
|
pub fn set_external_id(&mut self, external_id: Option<String>) -> bool {
|
|
|
|
//Check if external id is empty. We don't want to have
|
|
|
|
//empty strings in the database
|
|
|
|
if self.external_id != external_id {
|
|
|
|
self.external_id = match external_id {
|
|
|
|
Some(external_id) if !external_id.is_empty() => Some(external_id),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
false
|
2022-08-20 16:42:36 +02:00
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2023-06-02 21:36:15 +02:00
|
|
|
impl OrganizationApiKey {
|
2024-12-21 07:28:00 +01:00
|
|
|
pub fn new(org_uuid: OrganizationId, api_key: String) -> Self {
|
2023-06-02 21:36:15 +02:00
|
|
|
Self {
|
|
|
|
uuid: crate::util::get_uuid(),
|
|
|
|
|
|
|
|
org_uuid,
|
|
|
|
atype: 0, // Type 0 is the default and only type we support currently
|
|
|
|
api_key,
|
|
|
|
revision_date: Utc::now().naive_utc(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_valid_api_key(&self, api_key: &str) -> bool {
|
|
|
|
crate::crypto::ct_eq(&self.api_key, api_key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
use crate::db::DbConn;
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
/// Database methods
|
|
|
|
impl Organization {
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn save(&self, conn: &mut DbConn) -> EmptyResult {
|
2022-10-25 22:21:22 +02:00
|
|
|
if !email_address::EmailAddress::is_valid(self.billing_email.trim()) {
|
|
|
|
err!(format!("BillingEmail {} is not a valid email address", self.billing_email.trim()))
|
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
for member in Membership::find_by_org(&self.uuid, conn).await.iter() {
|
|
|
|
User::update_uuid_revision(&member.user_uuid, conn).await;
|
2021-11-16 17:07:55 +01:00
|
|
|
}
|
2019-09-12 16:12:22 -04:00
|
|
|
|
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(organizations::table)
|
2020-08-18 17:15:44 +02:00
|
|
|
.values(OrganizationDb::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(organizations::table)
|
|
|
|
.filter(organizations::uuid.eq(&self.uuid))
|
|
|
|
.set(OrganizationDb::to_db(self))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving organization")
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}.map_res("Error saving organization")
|
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
let value = OrganizationDb::to_db(self);
|
|
|
|
diesel::insert_into(organizations::table)
|
|
|
|
.values(&value)
|
|
|
|
.on_conflict(organizations::uuid)
|
|
|
|
.do_update()
|
|
|
|
.set(&value)
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
.map_res("Error saving organization")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete(self, conn: &mut DbConn) -> EmptyResult {
|
2018-05-18 16:52:51 +01:00
|
|
|
use super::{Cipher, Collection};
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
Cipher::delete_all_by_organization(&self.uuid, conn).await?;
|
|
|
|
Collection::delete_all_by_organization(&self.uuid, conn).await?;
|
2024-12-20 21:04:58 +01:00
|
|
|
Membership::delete_all_by_organization(&self.uuid, conn).await?;
|
2021-11-16 17:07:55 +01:00
|
|
|
OrgPolicy::delete_all_by_organization(&self.uuid, conn).await?;
|
2023-02-16 17:29:12 +01:00
|
|
|
Group::delete_all_by_organization(&self.uuid, conn).await?;
|
2024-05-19 20:33:00 +02:00
|
|
|
OrganizationApiKey::delete_all_by_organization(&self.uuid, conn).await?;
|
2018-05-18 16:52:51 +01:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(organizations::table.filter(organizations::uuid.eq(self.uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving organization")
|
|
|
|
}}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_uuid(uuid: &OrganizationId, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
organizations::table
|
|
|
|
.filter(organizations::uuid.eq(uuid))
|
|
|
|
.first::<OrganizationDb>(conn)
|
|
|
|
.ok().from_db()
|
|
|
|
}}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
2020-05-28 10:42:36 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn get_all(conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
organizations::table.load::<OrganizationDb>(conn).expect("Error loading organizations").from_db()
|
|
|
|
}}
|
2020-05-28 10:42:36 +02:00
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
impl Membership {
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn to_json(&self, conn: &mut DbConn) -> Value {
|
2021-11-16 17:07:55 +01:00
|
|
|
let org = Organization::find_by_uuid(&self.org_uuid, conn).await.unwrap();
|
2020-09-22 12:13:02 +02:00
|
|
|
|
2024-04-27 23:24:04 +02:00
|
|
|
let permissions = json!({
|
|
|
|
// TODO: Add support for Custom User Roles
|
|
|
|
// See: https://bitwarden.com/help/article/user-types-access-control/#custom-role
|
|
|
|
"accessEventLogs": false,
|
|
|
|
"accessImportExport": false,
|
|
|
|
"accessReports": false,
|
|
|
|
"createNewCollections": false,
|
|
|
|
"editAnyCollection": false,
|
|
|
|
"deleteAnyCollection": false,
|
|
|
|
"editAssignedCollections": false,
|
|
|
|
"deleteAssignedCollections": false,
|
|
|
|
"manageGroups": false,
|
|
|
|
"managePolicies": false,
|
|
|
|
"manageSso": false, // Not supported
|
|
|
|
"manageUsers": false,
|
|
|
|
"manageResetPassword": false,
|
|
|
|
"manageScim": false // Not supported (Not AGPLv3 Licensed)
|
|
|
|
});
|
|
|
|
|
2022-08-20 16:42:36 +02:00
|
|
|
// https://github.com/bitwarden/server/blob/13d1e74d6960cf0d042620b72d85bf583a4236f7/src/Api/Models/Response/ProfileOrganizationResponseModel.cs
|
2018-04-24 22:01:55 +02:00
|
|
|
json!({
|
2024-06-23 21:31:02 +02:00
|
|
|
"id": self.org_uuid,
|
|
|
|
"identifier": null, // Not supported
|
|
|
|
"name": org.name,
|
2024-07-10 17:25:41 +02:00
|
|
|
"seats": null,
|
|
|
|
"maxCollections": null,
|
2024-06-23 21:31:02 +02:00
|
|
|
"usersGetPremium": true,
|
|
|
|
"use2fa": true,
|
|
|
|
"useDirectory": false, // Is supported, but this value isn't checked anywhere (yet)
|
|
|
|
"useEvents": CONFIG.org_events_enabled(),
|
|
|
|
"useGroups": CONFIG.org_groups_enabled(),
|
|
|
|
"useTotp": true,
|
|
|
|
"useScim": false, // Not supported (Not AGPLv3 Licensed)
|
|
|
|
"usePolicies": true,
|
|
|
|
"useApi": true,
|
|
|
|
"selfHost": true,
|
|
|
|
"hasPublicAndPrivateKeys": org.private_key.is_some() && org.public_key.is_some(),
|
|
|
|
"resetPasswordEnrolled": self.reset_password_key.is_some(),
|
|
|
|
"useResetPassword": CONFIG.mail_enabled(),
|
|
|
|
"ssoBound": false, // Not supported
|
|
|
|
"useSso": false, // Not supported
|
|
|
|
"useKeyConnector": false,
|
|
|
|
"useSecretsManager": false,
|
|
|
|
"usePasswordManager": true,
|
|
|
|
"useCustomPermissions": false,
|
|
|
|
"useActivateAutofillPolicy": false,
|
|
|
|
|
2024-07-10 17:25:41 +02:00
|
|
|
"organizationUserId": self.uuid,
|
2024-06-23 21:31:02 +02:00
|
|
|
"providerId": null,
|
|
|
|
"providerName": null,
|
|
|
|
"providerType": null,
|
|
|
|
"familySponsorshipFriendlyName": null,
|
|
|
|
"familySponsorshipAvailable": false,
|
2024-07-10 17:25:41 +02:00
|
|
|
"planProductType": 3,
|
|
|
|
"productTierType": 3, // Enterprise tier
|
2024-06-23 21:31:02 +02:00
|
|
|
"keyConnectorEnabled": false,
|
|
|
|
"keyConnectorUrl": null,
|
|
|
|
"familySponsorshipLastSyncDate": null,
|
|
|
|
"familySponsorshipValidUntil": null,
|
|
|
|
"familySponsorshipToDelete": null,
|
|
|
|
"accessSecretsManager": false,
|
2024-10-18 20:37:32 +02:00
|
|
|
"limitCollectionCreationDeletion": false, // This should be set to true only when we can handle roles like createNewCollections
|
2024-06-23 21:31:02 +02:00
|
|
|
"allowAdminAccessToAllCollectionItems": true,
|
2024-07-03 21:11:04 +02:00
|
|
|
"flexibleCollections": false,
|
2021-01-31 21:46:37 +01:00
|
|
|
|
2024-04-27 23:24:04 +02:00
|
|
|
"permissions": permissions,
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2024-07-10 17:25:41 +02:00
|
|
|
"maxStorageGb": i16::MAX, // The value doesn't matter, we don't check server-side
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
// These are per user
|
2024-06-23 21:31:02 +02:00
|
|
|
"userId": self.user_uuid,
|
|
|
|
"key": self.akey,
|
|
|
|
"status": self.status,
|
|
|
|
"type": self.atype,
|
|
|
|
"enabled": true,
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2024-06-23 21:31:02 +02:00
|
|
|
"object": "profileOrganization",
|
2018-04-24 22:01:55 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-27 16:37:58 +01:00
|
|
|
pub async fn to_json_user_details(
|
|
|
|
&self,
|
|
|
|
include_collections: bool,
|
|
|
|
include_groups: bool,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> Value {
|
2021-11-16 17:07:55 +01:00
|
|
|
let user = User::find_by_uuid(&self.user_uuid, conn).await.unwrap();
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2022-08-20 16:42:36 +02:00
|
|
|
// Because BitWarden want the status to be -1 for revoked users we need to catch that here.
|
2023-10-05 20:08:26 +03:00
|
|
|
// We subtract/add a number so we can restore/activate the user to it's previous state again.
|
2024-12-20 21:04:58 +01:00
|
|
|
let status = if self.status < MembershipStatus::Revoked as i32 {
|
|
|
|
MembershipStatus::Revoked as i32
|
2022-08-20 16:42:36 +02:00
|
|
|
} else {
|
|
|
|
self.status
|
|
|
|
};
|
|
|
|
|
2023-01-11 22:13:20 +01:00
|
|
|
let twofactor_enabled = !TwoFactor::find_by_user(&user.uuid, conn).await.is_empty();
|
|
|
|
|
2023-02-27 16:37:58 +01:00
|
|
|
let groups: Vec<String> = if include_groups && CONFIG.org_groups_enabled() {
|
|
|
|
GroupUser::find_by_user(&self.uuid, conn).await.iter().map(|gu| gu.groups_uuid.clone()).collect()
|
|
|
|
} else {
|
|
|
|
// The Bitwarden clients seem to call this API regardless of whether groups are enabled,
|
|
|
|
// so just act as if there are no groups.
|
|
|
|
Vec::with_capacity(0)
|
|
|
|
};
|
|
|
|
|
2024-11-20 17:38:16 +01:00
|
|
|
// Check if a user is in a group which has access to all collections
|
|
|
|
// If that is the case, we should not return individual collections!
|
|
|
|
let full_access_group =
|
|
|
|
CONFIG.org_groups_enabled() && Group::is_in_full_access_group(&self.user_uuid, &self.org_uuid, conn).await;
|
|
|
|
|
|
|
|
// If collections are to be included, only include them if the user does not have full access via a group or defined to the user it self
|
|
|
|
let collections: Vec<Value> = if include_collections && !(full_access_group || self.has_full_access()) {
|
2024-08-15 12:36:00 +02:00
|
|
|
// Get all collections for the user here already to prevent more queries
|
|
|
|
let cu: HashMap<String, CollectionUser> =
|
|
|
|
CollectionUser::find_by_organization_and_user_uuid(&self.org_uuid, &self.user_uuid, conn)
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.map(|cu| (cu.collection_uuid.clone(), cu))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Get all collection groups for this user to prevent there inclusion
|
|
|
|
let cg: HashSet<String> = CollectionGroup::find_by_user(&self.user_uuid, conn)
|
2023-02-27 16:37:58 +01:00
|
|
|
.await
|
2024-08-15 12:36:00 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|cg| cg.collections_uuid)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Collection::find_by_organization_and_user_uuid(&self.org_uuid, &self.user_uuid, conn)
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|c| {
|
|
|
|
let (read_only, hide_passwords, can_manage) = if self.has_full_access() {
|
2024-12-20 21:04:58 +01:00
|
|
|
(false, false, self.atype >= MembershipType::Manager)
|
2024-08-15 12:36:00 +02:00
|
|
|
} else if let Some(cu) = cu.get(&c.uuid) {
|
|
|
|
(
|
|
|
|
cu.read_only,
|
|
|
|
cu.hide_passwords,
|
2024-12-20 21:04:58 +01:00
|
|
|
self.atype == MembershipType::Manager && !cu.read_only && !cu.hide_passwords,
|
2024-08-15 12:36:00 +02:00
|
|
|
)
|
|
|
|
// If previous checks failed it might be that this user has access via a group, but we should not return those elements here
|
|
|
|
// Those are returned via a special group endpoint
|
|
|
|
} else if cg.contains(&c.uuid) {
|
|
|
|
return None;
|
|
|
|
} else {
|
|
|
|
(true, true, false)
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(json!({
|
|
|
|
"id": c.uuid,
|
|
|
|
"readOnly": read_only,
|
|
|
|
"hidePasswords": hide_passwords,
|
|
|
|
"manage": can_manage,
|
|
|
|
}))
|
2023-02-27 16:37:58 +01:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
Vec::with_capacity(0)
|
|
|
|
};
|
|
|
|
|
2024-10-11 18:42:40 +02:00
|
|
|
let permissions = json!({
|
|
|
|
// TODO: Add support for Custom User Roles
|
|
|
|
// See: https://bitwarden.com/help/article/user-types-access-control/#custom-role
|
|
|
|
"accessEventLogs": false,
|
|
|
|
"accessImportExport": false,
|
|
|
|
"accessReports": false,
|
|
|
|
"createNewCollections": false,
|
|
|
|
"editAnyCollection": false,
|
|
|
|
"deleteAnyCollection": false,
|
|
|
|
"editAssignedCollections": false,
|
|
|
|
"deleteAssignedCollections": false,
|
|
|
|
"manageGroups": false,
|
|
|
|
"managePolicies": false,
|
|
|
|
"manageSso": false, // Not supported
|
|
|
|
"manageUsers": false,
|
|
|
|
"manageResetPassword": false,
|
|
|
|
"manageScim": false // Not supported (Not AGPLv3 Licensed)
|
|
|
|
});
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
json!({
|
2024-06-23 21:31:02 +02:00
|
|
|
"id": self.uuid,
|
|
|
|
"userId": self.user_uuid,
|
2024-12-20 21:04:58 +01:00
|
|
|
"name": if self.get_unrevoked_status() >= MembershipStatus::Accepted as i32 { Some(user.name) } else { None },
|
2024-06-23 21:31:02 +02:00
|
|
|
"email": user.email,
|
|
|
|
"externalId": self.external_id,
|
2024-08-15 12:36:00 +02:00
|
|
|
"avatarColor": user.avatar_color,
|
2024-06-23 21:31:02 +02:00
|
|
|
"groups": groups,
|
|
|
|
"collections": collections,
|
|
|
|
|
|
|
|
"status": status,
|
|
|
|
"type": self.atype,
|
|
|
|
"accessAll": self.access_all,
|
|
|
|
"twoFactorEnabled": twofactor_enabled,
|
|
|
|
"resetPasswordEnrolled": self.reset_password_key.is_some(),
|
2024-10-11 18:42:40 +02:00
|
|
|
"hasMasterPassword": !user.password_hash.is_empty(),
|
|
|
|
|
|
|
|
"permissions": permissions,
|
|
|
|
|
|
|
|
"ssoBound": false, // Not supported
|
|
|
|
"usesKeyConnector": false, // Not supported
|
|
|
|
"accessSecretsManager": false, // Not supported (Not AGPLv3 Licensed)
|
2024-06-23 21:31:02 +02:00
|
|
|
|
|
|
|
"object": "organizationUserUserDetails",
|
2018-04-24 22:01:55 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-02 21:51:20 -07:00
|
|
|
pub fn to_json_user_access_restrictions(&self, col_user: &CollectionUser) -> Value {
|
2018-05-29 16:01:38 +01:00
|
|
|
json!({
|
2024-06-23 21:31:02 +02:00
|
|
|
"id": self.uuid,
|
|
|
|
"readOnly": col_user.read_only,
|
|
|
|
"hidePasswords": col_user.hide_passwords,
|
2018-05-29 16:01:38 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn to_json_details(&self, conn: &mut DbConn) -> Value {
|
2018-12-30 23:34:31 +01:00
|
|
|
let coll_uuids = if self.access_all {
|
2018-05-11 20:08:02 +02:00
|
|
|
vec![] // If we have complete access, no need to fill the array
|
|
|
|
} else {
|
2021-11-16 17:07:55 +01:00
|
|
|
let collections =
|
|
|
|
CollectionUser::find_by_organization_and_user_uuid(&self.org_uuid, &self.user_uuid, conn).await;
|
2018-12-30 23:34:31 +01:00
|
|
|
collections
|
|
|
|
.iter()
|
2021-03-31 21:18:35 +01:00
|
|
|
.map(|c| {
|
|
|
|
json!({
|
2024-06-23 21:31:02 +02:00
|
|
|
"id": c.collection_uuid,
|
|
|
|
"readOnly": c.read_only,
|
|
|
|
"hidePasswords": c.hide_passwords,
|
2021-03-31 21:18:35 +01:00
|
|
|
})
|
|
|
|
})
|
2018-12-30 23:34:31 +01:00
|
|
|
.collect()
|
2018-05-11 20:08:02 +02:00
|
|
|
};
|
|
|
|
|
2022-08-20 16:42:36 +02:00
|
|
|
// Because BitWarden want the status to be -1 for revoked users we need to catch that here.
|
2023-10-05 20:08:26 +03:00
|
|
|
// We subtract/add a number so we can restore/activate the user to it's previous state again.
|
2024-12-20 21:04:58 +01:00
|
|
|
let status = if self.status < MembershipStatus::Revoked as i32 {
|
|
|
|
MembershipStatus::Revoked as i32
|
2022-08-20 16:42:36 +02:00
|
|
|
} else {
|
|
|
|
self.status
|
|
|
|
};
|
|
|
|
|
2018-04-25 00:34:40 +02:00
|
|
|
json!({
|
2024-06-23 21:31:02 +02:00
|
|
|
"id": self.uuid,
|
|
|
|
"userId": self.user_uuid,
|
2018-04-25 00:34:40 +02:00
|
|
|
|
2024-06-23 21:31:02 +02:00
|
|
|
"status": status,
|
|
|
|
"type": self.atype,
|
|
|
|
"accessAll": self.access_all,
|
|
|
|
"collections": coll_uuids,
|
2018-04-25 00:34:40 +02:00
|
|
|
|
2024-06-23 21:31:02 +02:00
|
|
|
"object": "organizationUserDetails",
|
2018-04-25 00:34:40 +02:00
|
|
|
})
|
|
|
|
}
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn save(&self, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
User::update_uuid_revision(&self.user_uuid, conn).await;
|
2019-09-12 16:12:22 -04:00
|
|
|
|
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(users_organizations::table)
|
2024-12-20 21:04:58 +01:00
|
|
|
.values(MembershipDb::to_db(self))
|
2020-08-18 17:15:44 +02:00
|
|
|
.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(users_organizations::table)
|
|
|
|
.filter(users_organizations::uuid.eq(&self.uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.set(MembershipDb::to_db(self))
|
2020-09-22 12:13:02 +02:00
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error adding user to organization")
|
2023-06-02 22:28:30 +02:00
|
|
|
},
|
2020-09-22 12:13:02 +02:00
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}.map_res("Error adding user to organization")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
postgresql {
|
2024-12-20 21:04:58 +01:00
|
|
|
let value = MembershipDb::to_db(self);
|
2020-08-18 17:15:44 +02:00
|
|
|
diesel::insert_into(users_organizations::table)
|
|
|
|
.values(&value)
|
|
|
|
.on_conflict(users_organizations::uuid)
|
|
|
|
.do_update()
|
|
|
|
.set(&value)
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
.map_res("Error adding user to organization")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-24 22:01:55 +02: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
|
|
|
User::update_uuid_revision(&self.user_uuid, conn).await;
|
2018-05-18 16:52:51 +01:00
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
CollectionUser::delete_all_by_user_and_org(&self.user_uuid, &self.org_uuid, conn).await?;
|
2024-12-20 21:04:58 +01:00
|
|
|
GroupUser::delete_all_by_member(&self.uuid, conn).await?;
|
2018-05-18 16:52:51 +01:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(users_organizations::table.filter(users_organizations::uuid.eq(self.uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing user from organization")
|
|
|
|
}}
|
2018-05-18 16:52:51 +01:00
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn delete_all_by_organization(org_uuid: &OrganizationId, conn: &mut DbConn) -> EmptyResult {
|
2024-12-20 21:04:58 +01:00
|
|
|
for member in Self::find_by_org(org_uuid, conn).await {
|
|
|
|
member.delete(conn).await?;
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
2018-05-18 16:52:51 +01:00
|
|
|
Ok(())
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_user(user_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2024-12-20 21:04:58 +01:00
|
|
|
for member in Self::find_any_state_by_user(user_uuid, conn).await {
|
|
|
|
member.delete(conn).await?;
|
2018-10-12 15:20:10 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_email_and_org(
|
|
|
|
email: &str,
|
|
|
|
org_uuid: &OrganizationId,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> Option<Membership> {
|
2024-09-23 20:25:32 +02:00
|
|
|
if let Some(user) = User::find_by_mail(email, conn).await {
|
2024-12-21 07:28:00 +01:00
|
|
|
if let Some(member) = Membership::find_by_user_and_org(&user.uuid, org_uuid, conn).await {
|
2024-12-20 21:04:58 +01:00
|
|
|
return Some(member);
|
2021-02-06 18:22:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
pub fn has_status(&self, status: MembershipStatus) -> bool {
|
2020-07-03 10:49:10 -07:00
|
|
|
self.status == status as i32
|
|
|
|
}
|
|
|
|
|
2024-12-20 21:04:58 +01:00
|
|
|
pub fn has_type(&self, user_type: MembershipType) -> bool {
|
2021-01-23 20:50:06 -08:00
|
|
|
self.atype == user_type as i32
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_full_access(&self) -> bool {
|
2024-12-20 21:04:58 +01:00
|
|
|
(self.access_all || self.atype >= MembershipType::Admin) && self.has_status(MembershipStatus::Confirmed)
|
2018-05-14 16:13:59 +01: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: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::uuid.eq(uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.first::<MembershipDb>(conn)
|
2020-08-18 17:15:44 +02:00
|
|
|
.ok().from_db()
|
|
|
|
}}
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_uuid_and_org(uuid: &str, org_uuid: &OrganizationId, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::uuid.eq(uuid))
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.first::<MembershipDb>(conn)
|
2020-08-18 17:15:44 +02:00
|
|
|
.ok().from_db()
|
|
|
|
}}
|
2018-09-04 11:24:53 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_confirmed_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32))
|
|
|
|
.load::<MembershipDb>(conn)
|
2020-08-18 17:15:44 +02:00
|
|
|
.unwrap_or_default().from_db()
|
|
|
|
}}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_invited_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Invited as i32))
|
|
|
|
.load::<MembershipDb>(conn)
|
2020-08-18 17:15:44 +02:00
|
|
|
.unwrap_or_default().from_db()
|
|
|
|
}}
|
2018-09-10 14:51:40 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_any_state_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.load::<MembershipDb>(conn)
|
2020-08-18 17:15:44 +02:00
|
|
|
.unwrap_or_default().from_db()
|
|
|
|
}}
|
2018-10-12 15:20:10 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn count_accepted_and_confirmed_by_user(user_uuid: &str, conn: &mut DbConn) -> i64 {
|
2022-08-20 16:42:36 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Accepted as i32).or(users_organizations::status.eq(MembershipStatus::Confirmed as i32)))
|
2022-08-20 16:42:36 +02:00
|
|
|
.count()
|
|
|
|
.first::<i64>(conn)
|
|
|
|
.unwrap_or(0)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_org(org_uuid: &OrganizationId, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.load::<MembershipDb>(conn)
|
2020-08-18 17:15:44 +02:00
|
|
|
.expect("Error loading user organizations").from_db()
|
|
|
|
}}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_confirmed_by_org(org_uuid: &OrganizationId, conn: &mut DbConn) -> Vec<Self> {
|
2024-01-01 19:41:40 +01:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32))
|
|
|
|
.load::<MembershipDb>(conn)
|
2024-01-01 19:41:40 +01:00
|
|
|
.unwrap_or_default().from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn count_by_org(org_uuid: &OrganizationId, conn: &mut DbConn) -> i64 {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
.count()
|
|
|
|
.first::<i64>(conn)
|
|
|
|
.ok()
|
|
|
|
.unwrap_or(0)
|
|
|
|
}}
|
2020-06-03 20:37:31 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_org_and_type(
|
|
|
|
org_uuid: &OrganizationId,
|
|
|
|
atype: MembershipType,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
2022-08-20 16:42:36 +02:00
|
|
|
.filter(users_organizations::atype.eq(atype as i32))
|
2024-12-20 21:04:58 +01:00
|
|
|
.load::<MembershipDb>(conn)
|
2020-08-18 17:15:44 +02:00
|
|
|
.expect("Error loading user organizations").from_db()
|
|
|
|
}}
|
2018-04-25 00:34:40 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn count_confirmed_by_org_and_type(
|
|
|
|
org_uuid: &OrganizationId,
|
|
|
|
atype: MembershipType,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> i64 {
|
2022-08-20 16:42:36 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
.filter(users_organizations::atype.eq(atype as i32))
|
2024-12-20 21:04:58 +01:00
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32))
|
2022-08-20 16:42:36 +02:00
|
|
|
.count()
|
|
|
|
.first::<i64>(conn)
|
|
|
|
.unwrap_or(0)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_user_and_org(user_uuid: &str, org_uuid: &OrganizationId, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.first::<MembershipDb>(conn)
|
2020-08-18 17:15:44 +02:00
|
|
|
.ok().from_db()
|
|
|
|
}}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
2018-08-21 17:31:01 +01:00
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_confirmed_by_user_and_org(
|
|
|
|
user_uuid: &str,
|
|
|
|
org_uuid: &OrganizationId,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> Option<Self> {
|
2024-08-11 19:39:56 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
.filter(
|
2024-12-20 21:04:58 +01:00
|
|
|
users_organizations::status.eq(MembershipStatus::Confirmed as i32)
|
2024-08-11 19:39:56 +02:00
|
|
|
)
|
2024-12-20 21:04:58 +01:00
|
|
|
.first::<MembershipDb>(conn)
|
2024-08-11 19:39:56 +02:00
|
|
|
.ok().from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2022-05-04 21:13:05 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.load::<MembershipDb>(conn)
|
2022-05-04 21:13:05 +02:00
|
|
|
.expect("Error loading user organizations").from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn get_orgs_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<OrganizationId> {
|
2022-11-20 19:15:45 +01:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.select(users_organizations::org_uuid)
|
2024-12-21 07:28:00 +01:00
|
|
|
.load::<OrganizationId>(conn)
|
2022-11-20 19:15:45 +01:00
|
|
|
.unwrap_or_default()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user_and_policy(user_uuid: &str, policy_type: OrgPolicyType, conn: &mut DbConn) -> Vec<Self> {
|
2021-04-11 22:57:17 -04:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.inner_join(
|
|
|
|
org_policies::table.on(
|
|
|
|
org_policies::org_uuid.eq(users_organizations::org_uuid)
|
|
|
|
.and(users_organizations::user_uuid.eq(user_uuid))
|
|
|
|
.and(org_policies::atype.eq(policy_type as i32))
|
|
|
|
.and(org_policies::enabled.eq(true)))
|
|
|
|
)
|
|
|
|
.filter(
|
2024-12-20 21:04:58 +01:00
|
|
|
users_organizations::status.eq(MembershipStatus::Confirmed as i32)
|
2021-04-11 22:57:17 -04:00
|
|
|
)
|
|
|
|
.select(users_organizations::all_columns)
|
2024-12-20 21:04:58 +01:00
|
|
|
.load::<MembershipDb>(conn)
|
2021-04-11 22:57:17 -04:00
|
|
|
.unwrap_or_default().from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_cipher_and_org(cipher_uuid: &str, org_uuid: &OrganizationId, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
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
|
|
|
|
)
|
2018-08-21 17:31:01 +01:00
|
|
|
)
|
2020-08-18 17:15:44 +02:00
|
|
|
.select(users_organizations::all_columns)
|
2023-03-31 18:01:55 +02:00
|
|
|
.distinct()
|
2024-12-20 21:04:58 +01:00
|
|
|
.load::<MembershipDb>(conn).expect("Error loading user organizations").from_db()
|
2020-08-18 17:15:44 +02:00
|
|
|
}}
|
2018-08-21 17:31:01 +01:00
|
|
|
}
|
2018-10-01 16:52:36 +01:00
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_cipher_and_org_with_group(
|
|
|
|
cipher_uuid: &str,
|
|
|
|
org_uuid: &OrganizationId,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> Vec<Self> {
|
2024-01-01 15:46:03 +01:00
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
.inner_join(groups_users::table.on(
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid)
|
|
|
|
))
|
|
|
|
.left_join(collections_groups::table.on(
|
|
|
|
collections_groups::groups_uuid.eq(groups_users::groups_uuid)
|
|
|
|
))
|
|
|
|
.left_join(groups::table.on(groups::uuid.eq(groups_users::groups_uuid)))
|
|
|
|
.left_join(ciphers_collections::table.on(
|
|
|
|
ciphers_collections::collection_uuid.eq(collections_groups::collections_uuid).and(ciphers_collections::cipher_uuid.eq(&cipher_uuid))
|
|
|
|
|
|
|
|
))
|
|
|
|
.filter(
|
|
|
|
groups::access_all.eq(true).or( // AccessAll via groups
|
|
|
|
ciphers_collections::cipher_uuid.eq(&cipher_uuid) // ..or access to collection via group
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.select(users_organizations::all_columns)
|
|
|
|
.distinct()
|
2024-12-20 21:04:58 +01:00
|
|
|
.load::<MembershipDb>(conn).expect("Error loading user organizations with groups").from_db()
|
2024-01-01 15:46:03 +01:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-11-20 19:15:45 +01:00
|
|
|
pub async fn user_has_ge_admin_access_to_cipher(user_uuid: &str, cipher_uuid: &str, conn: &mut DbConn) -> bool {
|
|
|
|
db_run! { conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.inner_join(ciphers::table.on(ciphers::uuid.eq(cipher_uuid).and(ciphers::organization_uuid.eq(users_organizations::org_uuid.nullable()))))
|
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid))
|
2024-12-20 21:04:58 +01:00
|
|
|
.filter(users_organizations::atype.eq_any(vec![MembershipType::Owner as i32, MembershipType::Admin as i32]))
|
2022-11-20 19:15:45 +01:00
|
|
|
.count()
|
|
|
|
.first::<i64>(conn)
|
|
|
|
.ok().unwrap_or(0) != 0
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_collection_and_org(
|
|
|
|
collection_uuid: &str,
|
|
|
|
org_uuid: &OrganizationId,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
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
|
|
|
|
)
|
2018-10-01 16:52:36 +01:00
|
|
|
)
|
2020-08-18 17:15:44 +02:00
|
|
|
.select(users_organizations::all_columns)
|
2024-12-20 21:04:58 +01:00
|
|
|
.load::<MembershipDb>(conn).expect("Error loading user organizations").from_db()
|
2020-08-18 17:15:44 +02:00
|
|
|
}}
|
2018-10-01 16:52:36 +01:00
|
|
|
}
|
2023-09-02 23:57:43 +02:00
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_external_id_and_org(
|
|
|
|
ext_id: &str,
|
|
|
|
org_uuid: &OrganizationId,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> Option<Self> {
|
2023-09-02 23:57:43 +02:00
|
|
|
db_run! {conn: {
|
|
|
|
users_organizations::table
|
|
|
|
.filter(
|
|
|
|
users_organizations::external_id.eq(ext_id)
|
|
|
|
.and(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
)
|
2024-12-20 21:04:58 +01:00
|
|
|
.first::<MembershipDb>(conn).ok().from_db()
|
2023-09-02 23:57:43 +02:00
|
|
|
}}
|
|
|
|
}
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
2020-09-13 02:03:16 -07:00
|
|
|
|
2023-06-02 21:36:15 +02:00
|
|
|
impl OrganizationApiKey {
|
|
|
|
pub async fn save(&self, conn: &DbConn) -> EmptyResult {
|
|
|
|
db_run! { conn:
|
|
|
|
sqlite, mysql {
|
|
|
|
match diesel::replace_into(organization_api_key::table)
|
|
|
|
.values(OrganizationApiKeyDb::to_db(self))
|
|
|
|
.execute(conn)
|
|
|
|
{
|
|
|
|
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(organization_api_key::table)
|
|
|
|
.filter(organization_api_key::uuid.eq(&self.uuid))
|
|
|
|
.set(OrganizationApiKeyDb::to_db(self))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving organization")
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}.map_res("Error saving organization")
|
|
|
|
|
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
let value = OrganizationApiKeyDb::to_db(self);
|
|
|
|
diesel::insert_into(organization_api_key::table)
|
|
|
|
.values(&value)
|
2023-07-10 15:29:06 +02:00
|
|
|
.on_conflict((organization_api_key::uuid, organization_api_key::org_uuid))
|
2023-06-02 21:36:15 +02:00
|
|
|
.do_update()
|
|
|
|
.set(&value)
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving organization")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn find_by_org_uuid(org_uuid: &OrganizationId, conn: &DbConn) -> Option<Self> {
|
2023-06-02 21:36:15 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
organization_api_key::table
|
|
|
|
.filter(organization_api_key::org_uuid.eq(org_uuid))
|
|
|
|
.first::<OrganizationApiKeyDb>(conn)
|
|
|
|
.ok().from_db()
|
|
|
|
}}
|
|
|
|
}
|
2024-05-19 20:33:00 +02:00
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
pub async fn delete_all_by_organization(org_uuid: &OrganizationId, conn: &mut DbConn) -> EmptyResult {
|
2024-05-19 20:33:00 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(organization_api_key::table.filter(organization_api_key::org_uuid.eq(org_uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing organization api key from organization")
|
|
|
|
}}
|
|
|
|
}
|
2023-06-02 21:36:15 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 07:28:00 +01:00
|
|
|
#[derive(DieselNewType, FromForm, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub struct OrganizationId(String);
|
|
|
|
|
|
|
|
impl AsRef<str> for OrganizationId {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for OrganizationId {
|
|
|
|
type Target = str;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Borrow<str> for OrganizationId {
|
|
|
|
fn borrow(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for OrganizationId {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for OrganizationId {
|
|
|
|
fn from(raw: String) -> Self {
|
|
|
|
Self(raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'r> FromParam<'r> for OrganizationId {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn from_param(param: &'r str) -> Result<Self, Self::Error> {
|
|
|
|
if param.chars().all(|c| matches!(c, 'a'..='z' | 'A'..='Z' |'0'..='9' | '-')) {
|
|
|
|
Ok(OrganizationId(param.to_string()))
|
|
|
|
} else {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(DieselNewType, Clone, Debug, Hash, PartialEq, Eq, Serialize)]
|
|
|
|
pub struct MembershipId(String);
|
|
|
|
|
2020-09-13 02:03:16 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[allow(non_snake_case)]
|
2024-12-20 21:04:58 +01:00
|
|
|
fn partial_cmp_MembershipType() {
|
|
|
|
assert!(MembershipType::Owner > MembershipType::Admin);
|
|
|
|
assert!(MembershipType::Admin > MembershipType::Manager);
|
|
|
|
assert!(MembershipType::Manager > MembershipType::User);
|
2020-09-13 02:03:16 -07:00
|
|
|
}
|
|
|
|
}
|