mirror of
https://github.com/dani-garcia/vaultwarden.wiki.git
synced 2026-11-06 15:58:36 +02:00
[PR 1] Add experimental Custom Roles foundation (#7847)
This commit is contained in:
20 files changed
+621
-68
No files matched your search
@@ -534,3 +534,74 @@ mod postgresql_migrations {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// A throwaway SQLite database with the real schema, for tests that need to run queries.
|
||||
#[cfg(all(test, sqlite))]
|
||||
pub mod test_db {
|
||||
use super::sqlite_migrations::MIGRATIONS;
|
||||
use diesel::connection::SimpleConnection;
|
||||
use diesel_migrations::MigrationHarness;
|
||||
|
||||
#[test]
|
||||
fn custom_role_migration_preserves_memberships_and_guards_downgrade() {
|
||||
use super::schema::users_organizations::dsl::*;
|
||||
use diesel::{migration::MigrationSource, prelude::*};
|
||||
let mut conn = SqliteConnection::establish(":memory:").unwrap();
|
||||
for migration in conn.pending_migrations(MIGRATIONS).unwrap() {
|
||||
if migration.name().version().to_string() == "20261010120000" {
|
||||
break;
|
||||
}
|
||||
conn.run_migration(&migration).unwrap();
|
||||
}
|
||||
conn.batch_execute(
|
||||
"PRAGMA foreign_keys=OFF;
|
||||
INSERT INTO organizations (uuid,name,billing_email) VALUES ('org','Org','a@b');
|
||||
INSERT INTO users_organizations (uuid,user_uuid,org_uuid,access_all,akey,status,atype) VALUES
|
||||
('0','0','org',TRUE,'key',2,0), ('1','1','org',TRUE,'key',2,1),
|
||||
('2','2','org',FALSE,'key',1,2), ('3','3','org',FALSE,'key',2,3), ('4','4','org',TRUE,'key',2,3);",
|
||||
)
|
||||
.unwrap();
|
||||
let rows = |conn: &mut SqliteConnection| {
|
||||
users_organizations
|
||||
.select((uuid, user_uuid, org_uuid, access_all, akey, status, atype))
|
||||
.order(uuid)
|
||||
.load::<(String, String, String, bool, String, i32, i32)>(conn)
|
||||
.unwrap()
|
||||
};
|
||||
let before = rows(&mut conn);
|
||||
conn.run_pending_migrations(MIGRATIONS).unwrap();
|
||||
assert_eq!(rows(&mut conn), before);
|
||||
let grants = |conn: &mut SqliteConnection| {
|
||||
diesel::select(diesel::dsl::sql::<diesel::sql_types::BigInt>(
|
||||
"(SELECT COUNT(*) FROM users_organizations WHERE manage_users OR manage_groups OR manage_policies
|
||||
OR create_new_collections OR edit_any_collection OR delete_any_collection
|
||||
OR access_event_logs OR access_import_export OR access_reports)",
|
||||
))
|
||||
.get_result::<i64>(conn)
|
||||
.unwrap()
|
||||
};
|
||||
assert_eq!(grants(&mut conn), 0);
|
||||
let migration = MigrationSource::<diesel::sqlite::Sqlite>::migrations(&MIGRATIONS)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.find(|m| m.name().version().to_string() == "20261010120000")
|
||||
.unwrap();
|
||||
for member_status in [1, 2] {
|
||||
conn.batch_execute(&format!(
|
||||
"UPDATE users_organizations SET atype=4,manage_users=TRUE,status={member_status} WHERE uuid='2'"
|
||||
))
|
||||
.unwrap();
|
||||
let custom = rows(&mut conn);
|
||||
assert!(conn.revert_migration(&*migration).is_err());
|
||||
assert_eq!(rows(&mut conn), custom);
|
||||
assert!(users_organizations.filter(uuid.eq("2")).select(manage_users).first::<bool>(&mut conn).unwrap());
|
||||
}
|
||||
conn.batch_execute("UPDATE users_organizations SET atype=2,manage_users=FALSE,status=1 WHERE uuid='2'")
|
||||
.unwrap();
|
||||
conn.revert_migration(&*migration).unwrap();
|
||||
assert!(diesel::sql_query("SELECT manage_users FROM users_organizations").execute(&mut conn).is_err());
|
||||
conn.run_pending_migrations(MIGRATIONS).unwrap();
|
||||
assert_eq!(grants(&mut conn), 0);
|
||||
assert_eq!(rows(&mut conn), before);
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ use macros::UuidFromParam;
|
||||
|
||||
use super::{
|
||||
Archive, Attachment, CollectionCipher, CollectionId, Favorite, FolderCipher, FolderId, Group, Membership,
|
||||
MembershipStatus, MembershipType, OrganizationId, User, UserId,
|
||||
MembershipStatus, OrganizationId, User, UserId, organization::ORG_ADMIN_ATYPES,
|
||||
};
|
||||
|
||||
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
||||
@@ -872,7 +872,7 @@ impl Cipher {
|
||||
|
||||
if !visible_only {
|
||||
query = query.or_filter(
|
||||
users_organizations::atype.le(MembershipType::Admin as i32), // Org admin/owner
|
||||
users_organizations::atype.eq_any(ORG_ADMIN_ATYPES), // Org admin/owner
|
||||
);
|
||||
}
|
||||
|
||||
@@ -907,7 +907,7 @@ impl Cipher {
|
||||
|
||||
if !visible_only {
|
||||
query = query.or_filter(
|
||||
users_organizations::atype.le(MembershipType::Admin as i32), // Org admin/owner
|
||||
users_organizations::atype.eq_any(ORG_ADMIN_ATYPES), // Org admin/owner
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1111,7 +1111,7 @@ impl Cipher {
|
||||
.or(collections_groups::collections_uuid
|
||||
.is_not_null() // Access via groups
|
||||
.and(collections_groups::read_only.eq(false)))
|
||||
.or(users_organizations::atype.le(MembershipType::Admin as i32)), // User is admin or owner
|
||||
.or(users_organizations::atype.eq_any(ORG_ADMIN_ATYPES)), // User is admin or owner
|
||||
)
|
||||
.select(ciphers_collections::collection_uuid)
|
||||
.load::<CollectionId>(conn)
|
||||
@@ -1140,7 +1140,7 @@ impl Cipher {
|
||||
.or(users_collections::user_uuid
|
||||
.eq(user_uuid) // User has access to collection
|
||||
.and(users_collections::read_only.eq(false)))
|
||||
.or(users_organizations::atype.le(MembershipType::Admin as i32)), // User is admin or owner
|
||||
.or(users_organizations::atype.eq_any(ORG_ADMIN_ATYPES)), // User is admin or owner
|
||||
)
|
||||
.select(ciphers_collections::collection_uuid)
|
||||
.load::<CollectionId>(conn)
|
||||
@@ -1182,7 +1182,7 @@ impl Cipher {
|
||||
)
|
||||
.or_filter(users_collections::user_uuid.eq(user_uuid)) // User has access to collection
|
||||
.or_filter(users_organizations::access_all.eq(true)) // User has access all
|
||||
.or_filter(users_organizations::atype.le(MembershipType::Admin as i32)) // User is admin or owner
|
||||
.or_filter(users_organizations::atype.eq_any(ORG_ADMIN_ATYPES)) // 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
|
||||
.filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32))
|
||||
|
||||
@@ -18,7 +18,7 @@ use macros::UuidFromParam;
|
||||
|
||||
use super::{
|
||||
CipherId, CollectionGroup, GroupUser, Membership, MembershipId, MembershipStatus, MembershipType, OrganizationId,
|
||||
User, UserId,
|
||||
User, UserId, organization::ORG_ADMIN_ATYPES,
|
||||
};
|
||||
|
||||
// See (v2026.7.0): https://github.com/bitwarden/server/blob/5d4461aa42cadbacfef8fe2166c5453a5c52773a/src/Core/AdminConsole/Entities/Collection.cs
|
||||
@@ -375,7 +375,7 @@ impl Collection {
|
||||
// Directly accessed collection
|
||||
users_organizations::access_all.eq(true).or(
|
||||
// access_all in Organization
|
||||
users_organizations::atype.le(MembershipType::Admin as i32), // Org admin or owner
|
||||
users_organizations::atype.eq_any(ORG_ADMIN_ATYPES), // Org admin or owner
|
||||
),
|
||||
)
|
||||
.or(
|
||||
@@ -412,7 +412,7 @@ impl Collection {
|
||||
// Directly accessed collection
|
||||
users_organizations::access_all.eq(true).or(
|
||||
// access_all in Organization
|
||||
users_organizations::atype.le(MembershipType::Admin as i32), // Org admin or owner
|
||||
users_organizations::atype.eq_any(ORG_ADMIN_ATYPES), // Org admin or owner
|
||||
),
|
||||
))
|
||||
.select(collections::all_columns)
|
||||
@@ -455,7 +455,7 @@ impl Collection {
|
||||
)
|
||||
.filter(
|
||||
users_organizations::atype
|
||||
.le(MembershipType::Admin as i32) // Org admin or owner
|
||||
.eq_any(ORG_ADMIN_ATYPES) // Org admin or owner
|
||||
.or(users_organizations::access_all.eq(true)) // access_all via membership
|
||||
.or(users_collections::collection_uuid
|
||||
.eq(&self.uuid) // write access given to collection
|
||||
@@ -489,7 +489,7 @@ impl Collection {
|
||||
)
|
||||
.filter(
|
||||
users_organizations::atype
|
||||
.le(MembershipType::Admin as i32) // Org admin or owner
|
||||
.eq_any(ORG_ADMIN_ATYPES) // Org admin or owner
|
||||
.or(users_organizations::access_all.eq(true)) // access_all via membership
|
||||
.or(users_collections::collection_uuid
|
||||
.eq(&self.uuid) // write access given to collection
|
||||
@@ -540,7 +540,7 @@ impl Collection {
|
||||
// Directly accessed collection
|
||||
users_organizations::access_all.eq(true).or(
|
||||
// access_all in Organization
|
||||
users_organizations::atype.le(MembershipType::Admin as i32), // Org admin or owner
|
||||
users_organizations::atype.eq_any(ORG_ADMIN_ATYPES), // Org admin or owner
|
||||
),
|
||||
)
|
||||
.or(
|
||||
@@ -600,7 +600,7 @@ impl Collection {
|
||||
// Directly accessed collection
|
||||
users_organizations::access_all.eq(true).or(
|
||||
// access_all in Organization
|
||||
users_organizations::atype.le(MembershipType::Admin as i32), // Org admin or owner
|
||||
users_organizations::atype.eq_any(ORG_ADMIN_ATYPES), // Org admin or owner
|
||||
),
|
||||
)
|
||||
.or(
|
||||
|
||||
@@ -33,8 +33,8 @@ pub use self::folder::{Folder, FolderCipher, FolderId};
|
||||
pub use self::group::{CollectionGroup, Group, GroupId, GroupUser};
|
||||
pub use self::org_policy::{FillAssistPolicyData, OrgPolicy, OrgPolicyId, OrgPolicyType};
|
||||
pub use self::organization::{
|
||||
Membership, MembershipId, MembershipStatus, MembershipType, OrgApiKeyId, Organization, OrganizationApiKey,
|
||||
OrganizationId,
|
||||
CustomPermission, Membership, MembershipId, MembershipStatus, MembershipType, OrgApiKeyId, Organization,
|
||||
OrganizationApiKey, OrganizationId, custom_roles_enabled,
|
||||
};
|
||||
pub use self::send::{Send, SendFileId, SendId, SendType};
|
||||
pub use self::sso_auth::{OIDCAuthenticatedUser, OIDCCodeResponseError, SsoAuth};
|
||||
|
||||
+319
-24
@@ -1,4 +1,7 @@
|
||||
use std::cmp::Ordering;
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
collections::{HashMap, HashSet},
|
||||
};
|
||||
|
||||
use chrono::{NaiveDateTime, Utc};
|
||||
use derive_more::{AsRef, Deref, Display, From};
|
||||
@@ -21,8 +24,8 @@ use crate::{
|
||||
use macros::UuidFromParam;
|
||||
|
||||
use super::{
|
||||
Cipher, CipherId, Collection, CollectionId, CollectionUser, Group, GroupId, GroupUser, OrgPolicy, OrgPolicyType,
|
||||
TwoFactor, User, UserId,
|
||||
Cipher, CipherId, Collection, CollectionGroup, CollectionId, CollectionUser, Group, GroupId, GroupUser, OrgPolicy,
|
||||
OrgPolicyType, TwoFactor, User, UserId,
|
||||
};
|
||||
|
||||
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
||||
@@ -41,6 +44,7 @@ pub struct Organization {
|
||||
#[diesel(table_name = users_organizations)]
|
||||
#[diesel(treat_none_as_null = true)]
|
||||
#[diesel(primary_key(uuid))]
|
||||
#[expect(clippy::struct_excessive_bools, reason = "Each Custom role permission is a column of its own")]
|
||||
pub struct Membership {
|
||||
pub uuid: MembershipId,
|
||||
pub user_uuid: UserId,
|
||||
@@ -54,6 +58,17 @@ pub struct Membership {
|
||||
pub atype: i32,
|
||||
pub reset_password_key: Option<String>,
|
||||
pub external_id: Option<String>,
|
||||
|
||||
// The permissions of a Custom role, only accessed through `CustomPermission`.
|
||||
manage_users: bool,
|
||||
manage_groups: bool,
|
||||
manage_policies: bool,
|
||||
create_new_collections: bool,
|
||||
edit_any_collection: bool,
|
||||
delete_any_collection: bool,
|
||||
access_event_logs: bool,
|
||||
access_import_export: bool,
|
||||
access_reports: bool,
|
||||
}
|
||||
|
||||
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
||||
@@ -89,42 +104,68 @@ impl MembershipStatus {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, num_derive::FromPrimitive)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, num_derive::FromPrimitive)]
|
||||
pub enum MembershipType {
|
||||
Owner = 0,
|
||||
Admin = 1,
|
||||
User = 2,
|
||||
// The legacy Manager role. The Bitwarden clients no longer know it and are shown a Custom role instead.
|
||||
Manager = 3,
|
||||
// Only granted while EXPERIMENTAL_CUSTOM_ROLES is enabled.
|
||||
Custom = 4,
|
||||
}
|
||||
|
||||
impl MembershipType {
|
||||
pub fn from_str(s: &str) -> Option<Self> {
|
||||
#[expect(
|
||||
clippy::match_same_arms,
|
||||
reason = "Specifically define `4|Custom` since this is a hack, not a default"
|
||||
)]
|
||||
match s {
|
||||
"0" | "Owner" => Some(MembershipType::Owner),
|
||||
"1" | "Admin" => Some(MembershipType::Admin),
|
||||
"2" | "User" => Some(MembershipType::User),
|
||||
"3" | "Manager" => Some(MembershipType::Manager),
|
||||
// HACK: We convert the custom role to a manager role
|
||||
"4" | "Custom" => Some(MembershipType::Manager),
|
||||
"4" | "Custom" => Some(MembershipType::Custom),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The role a member invite or edit from the Bitwarden clients asks for, given the member's current
|
||||
/// role (`None` for an invite).
|
||||
///
|
||||
/// The clients no longer know the legacy Manager role and are shown a Custom role in its place, so a
|
||||
/// requested Custom role is the legacy Manager unless EXPERIMENTAL_CUSTOM_ROLES is enabled. An existing
|
||||
/// Manager or Custom member keeps their role when the request repeats it, so an unrelated edit never
|
||||
/// turns one into the other.
|
||||
pub fn requested(
|
||||
requested: MembershipType,
|
||||
current: Option<&Membership>,
|
||||
custom_roles_enabled: bool,
|
||||
) -> MembershipType {
|
||||
if requested != MembershipType::Custom {
|
||||
return requested;
|
||||
}
|
||||
match current {
|
||||
Some(member) if member.has_type(MembershipType::Custom) => MembershipType::Custom,
|
||||
Some(member) if member.has_type(MembershipType::Manager) => MembershipType::Manager,
|
||||
_ if custom_roles_enabled => MembershipType::Custom,
|
||||
_ => MembershipType::Manager,
|
||||
}
|
||||
}
|
||||
|
||||
/// The authority of a role, which is what roles are compared by. A Custom member gets its authority from
|
||||
/// its permissions, never from its role, so the role itself ranks right above User and below Manager.
|
||||
const fn rank(self) -> u8 {
|
||||
match self {
|
||||
MembershipType::User => 0,
|
||||
MembershipType::Custom => 1,
|
||||
MembershipType::Manager => 2,
|
||||
MembershipType::Admin => 3,
|
||||
MembershipType::Owner => 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for MembershipType {
|
||||
fn cmp(&self, other: &MembershipType) -> Ordering {
|
||||
// For easy comparison, map each variant to an access level (where 0 is lowest).
|
||||
const ACCESS_LEVEL: [i32; 4] = [
|
||||
3, // Owner
|
||||
2, // Admin
|
||||
0, // User
|
||||
1, // Manager && Custom
|
||||
];
|
||||
ACCESS_LEVEL[*self as usize].cmp(&ACCESS_LEVEL[*other as usize])
|
||||
self.rank().cmp(&other.rank())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +221,91 @@ impl PartialOrd<MembershipType> for i32 {
|
||||
}
|
||||
}
|
||||
|
||||
/// The granular permissions of the Custom role.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum CustomPermission {
|
||||
ManageUsers,
|
||||
ManageGroups,
|
||||
ManagePolicies,
|
||||
CreateNewCollections,
|
||||
EditAnyCollection,
|
||||
DeleteAnyCollection,
|
||||
AccessEventLogs,
|
||||
AccessImportExport,
|
||||
AccessReports,
|
||||
}
|
||||
|
||||
/// The permissions this version enforces on every endpoint they open. Only these can be granted, a stored
|
||||
/// grant of any other one grants nothing. They are implemented one at a time, so this grows with them.
|
||||
const SUPPORTED_CUSTOM_PERMISSIONS: &[CustomPermission] = &[];
|
||||
|
||||
impl CustomPermission {
|
||||
pub const ALL: [CustomPermission; 9] = [
|
||||
CustomPermission::ManageUsers,
|
||||
CustomPermission::ManageGroups,
|
||||
CustomPermission::ManagePolicies,
|
||||
CustomPermission::CreateNewCollections,
|
||||
CustomPermission::EditAnyCollection,
|
||||
CustomPermission::DeleteAnyCollection,
|
||||
CustomPermission::AccessEventLogs,
|
||||
CustomPermission::AccessImportExport,
|
||||
CustomPermission::AccessReports,
|
||||
];
|
||||
|
||||
/// The key of the permission in the `permissions` object of the Bitwarden clients.
|
||||
pub const fn json_key(self) -> &'static str {
|
||||
match self {
|
||||
CustomPermission::ManageUsers => "manageUsers",
|
||||
CustomPermission::ManageGroups => "manageGroups",
|
||||
CustomPermission::ManagePolicies => "managePolicies",
|
||||
CustomPermission::CreateNewCollections => "createNewCollections",
|
||||
CustomPermission::EditAnyCollection => "editAnyCollection",
|
||||
CustomPermission::DeleteAnyCollection => "deleteAnyCollection",
|
||||
CustomPermission::AccessEventLogs => "accessEventLogs",
|
||||
CustomPermission::AccessImportExport => "accessImportExport",
|
||||
CustomPermission::AccessReports => "accessReports",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_supported(self) -> bool {
|
||||
SUPPORTED_CUSTOM_PERMISSIONS.contains(&self)
|
||||
}
|
||||
|
||||
/// The permissions a member invite or edit grants a Custom role.
|
||||
///
|
||||
/// A permission is only granted when it is set to `true`, and anything but a boolean is rejected: this
|
||||
/// replaces the member's permissions, so a malformed value must not silently remove one. Granting a
|
||||
/// permission this version does not enforce is rejected as well. The clients also send permissions
|
||||
/// Vaultwarden does not know at all, like `manageSso`, those are ignored.
|
||||
pub fn requested(permissions: &HashMap<String, Value>) -> Result<HashSet<CustomPermission>, crate::Error> {
|
||||
let mut granted = HashSet::new();
|
||||
for permission in CustomPermission::ALL {
|
||||
match permissions.get(permission.json_key()) {
|
||||
None | Some(Value::Bool(false)) => {}
|
||||
Some(Value::Bool(true)) if permission.is_supported() => {
|
||||
granted.insert(permission);
|
||||
}
|
||||
Some(Value::Bool(true)) => {
|
||||
err!(format!("The Custom role permission '{}' is not supported yet", permission.json_key()))
|
||||
}
|
||||
Some(_) => {
|
||||
err!(format!("The Custom role permission '{}' must be true or false", permission.json_key()))
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(granted)
|
||||
}
|
||||
}
|
||||
|
||||
/// The stored roles with authority over the whole organization. Queries match them by value, comparing
|
||||
/// `atype <= Admin` would also match any unknown value below it.
|
||||
pub(super) const ORG_ADMIN_ATYPES: [i32; 2] = [MembershipType::Owner as i32, MembershipType::Admin as i32];
|
||||
|
||||
/// Whether the experimental Custom role is enabled. Nothing is ever granted through a Custom role otherwise.
|
||||
pub fn custom_roles_enabled() -> bool {
|
||||
CONFIG.experimental_custom_roles()
|
||||
}
|
||||
|
||||
/// Local methods
|
||||
impl Organization {
|
||||
pub fn new(name: String, billing_email: &str, private_key: Option<String>, public_key: Option<String>) -> Self {
|
||||
@@ -271,9 +397,92 @@ impl Membership {
|
||||
atype: MembershipType::User as i32,
|
||||
reset_password_key: None,
|
||||
external_id: None,
|
||||
|
||||
manage_users: false,
|
||||
manage_groups: false,
|
||||
manage_policies: false,
|
||||
create_new_collections: false,
|
||||
edit_any_collection: false,
|
||||
delete_any_collection: false,
|
||||
access_event_logs: false,
|
||||
access_import_export: false,
|
||||
access_reports: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn custom_permission_mut(&mut self, permission: CustomPermission) -> &mut bool {
|
||||
match permission {
|
||||
CustomPermission::ManageUsers => &mut self.manage_users,
|
||||
CustomPermission::ManageGroups => &mut self.manage_groups,
|
||||
CustomPermission::ManagePolicies => &mut self.manage_policies,
|
||||
CustomPermission::CreateNewCollections => &mut self.create_new_collections,
|
||||
CustomPermission::EditAnyCollection => &mut self.edit_any_collection,
|
||||
CustomPermission::DeleteAnyCollection => &mut self.delete_any_collection,
|
||||
CustomPermission::AccessEventLogs => &mut self.access_event_logs,
|
||||
CustomPermission::AccessImportExport => &mut self.access_import_export,
|
||||
CustomPermission::AccessReports => &mut self.access_reports,
|
||||
}
|
||||
}
|
||||
|
||||
/// Change the role. Nothing the old role carried survives a role change: the Custom role permissions are
|
||||
/// dropped, so changing the role back later starts from none. A Custom role never keeps the legacy
|
||||
/// `access_all` flag, its reach only comes from its permissions.
|
||||
pub fn set_type(&mut self, new_type: MembershipType) {
|
||||
if self.atype != new_type as i32 {
|
||||
for permission in CustomPermission::ALL {
|
||||
*self.custom_permission_mut(permission) = false;
|
||||
}
|
||||
}
|
||||
if new_type == MembershipType::Custom {
|
||||
self.access_all = false;
|
||||
}
|
||||
self.atype = new_type as i32;
|
||||
}
|
||||
|
||||
/// Grant this Custom member exactly `permissions` out of those this version enforces. A stored grant of a
|
||||
/// permission this version does not enforce is kept as it is: it grants nothing here.
|
||||
pub fn set_custom_permissions(&mut self, permissions: &HashSet<CustomPermission>) {
|
||||
for permission in CustomPermission::ALL.into_iter().filter(|p| p.is_supported()) {
|
||||
*self.custom_permission_mut(permission) = permissions.contains(&permission);
|
||||
}
|
||||
}
|
||||
|
||||
/// Why this legacy Manager can't become a Custom member without losing what they may do now, if they can't. A
|
||||
/// conversion must not silently change their access, and a Custom role can't keep access to all collections
|
||||
/// (which also puts every collection in the Manager's own vault), nor manage any collection yet.
|
||||
pub async fn custom_role_conversion_loss(&self, conn: &DbConn) -> Option<&'static str> {
|
||||
if !self.has_type(MembershipType::Manager) {
|
||||
return None;
|
||||
}
|
||||
if self.access_all {
|
||||
return Some(
|
||||
"This Manager has access to all collections, which a Custom member can't have: the collections they \
|
||||
aren't assigned to would disappear from their vault. Uncheck Manage all collections for them first, \
|
||||
or keep the Manager role.",
|
||||
);
|
||||
}
|
||||
// Whatever their status, the collections they manage directly, through a group or through a group with
|
||||
// access to all collections
|
||||
let mut manages = GroupUser::has_full_access_by_member(&self.org_uuid, &self.uuid, conn).await
|
||||
|| CollectionUser::find_by_organization_and_user_uuid(&self.org_uuid, &self.user_uuid, conn)
|
||||
.await
|
||||
.iter()
|
||||
.any(|assignment| assignment.manage);
|
||||
for group_user in GroupUser::find_by_member(&self.uuid, conn).await {
|
||||
if manages {
|
||||
break;
|
||||
}
|
||||
manages = CollectionGroup::find_by_group(&group_user.groups_uuid, &self.org_uuid, conn)
|
||||
.await
|
||||
.iter()
|
||||
.any(|assignment| assignment.manage);
|
||||
}
|
||||
manages.then_some(
|
||||
"This Manager manages collections, with the manage permission or through a group with access to all \
|
||||
collections, which a Custom member can't do yet. Remove that first, or keep the Manager role.",
|
||||
)
|
||||
}
|
||||
|
||||
pub fn restore(&mut self) -> bool {
|
||||
if self.status < MembershipStatus::Invited as i32 {
|
||||
self.status += ACTIVATE_REVOKE_DIFF;
|
||||
@@ -712,6 +921,12 @@ impl Membership {
|
||||
}
|
||||
|
||||
pub async fn save(&self, conn: &DbConn) -> EmptyResult {
|
||||
// The legacy `access_all` flag reaches every collection, which a Custom role may only do through its
|
||||
// permissions, so it must never be stored on one. `set_type` takes care of this.
|
||||
if self.has_type(MembershipType::Custom) && self.access_all {
|
||||
err!("A Custom role membership can't have access to all collections")
|
||||
}
|
||||
|
||||
User::update_uuid_revision(&self.user_uuid, conn).await;
|
||||
|
||||
db_run! { conn:
|
||||
@@ -1228,13 +1443,93 @@ pub struct OrgApiKeyId(String);
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use MembershipType::{Admin, Custom, Manager, Owner, User};
|
||||
|
||||
fn member(role: MembershipType) -> Membership {
|
||||
let mut member = Membership::new("user".into(), "org".into(), None);
|
||||
member.atype = role as i32;
|
||||
member.status = MembershipStatus::Confirmed as i32;
|
||||
member
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(non_snake_case)]
|
||||
fn partial_cmp_MembershipType() {
|
||||
assert!(MembershipType::Owner > MembershipType::Admin);
|
||||
assert!(MembershipType::Admin > MembershipType::Manager);
|
||||
assert!(MembershipType::Manager > MembershipType::User);
|
||||
assert!(MembershipType::Manager == MembershipType::from_str("4").unwrap());
|
||||
fn membership_type_authority() {
|
||||
assert!(Owner > Admin && Admin > Manager && Manager > Custom && Custom > User);
|
||||
assert!((Custom as i32) >= User && (Custom as i32) < Manager && (Custom as i32) < Admin);
|
||||
for raw in [99, -1] {
|
||||
assert_eq!((raw >= User, raw >= Manager, raw >= Admin, raw < Admin), (false, false, false, true));
|
||||
}
|
||||
for (value, expected) in [
|
||||
("3", Some(Manager)),
|
||||
("Manager", Some(Manager)),
|
||||
("4", Some(Custom)),
|
||||
("Custom", Some(Custom)),
|
||||
("5", None),
|
||||
] {
|
||||
assert_eq!(MembershipType::from_str(value), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requested_membership_type() {
|
||||
for (current, disabled, enabled) in [
|
||||
(None, Manager, Custom),
|
||||
(Some(User), Manager, Custom),
|
||||
(Some(Manager), Manager, Manager),
|
||||
(Some(Custom), Custom, Custom),
|
||||
] {
|
||||
let current = current.map(member);
|
||||
for (flag, expected) in [(false, disabled), (true, enabled)] {
|
||||
assert_eq!(MembershipType::requested(Custom, current.as_ref(), flag), expected);
|
||||
}
|
||||
}
|
||||
assert_eq!(MembershipType::requested(User, Some(&member(Custom)), true), User);
|
||||
assert_eq!(MembershipType::requested(User, Some(&member(Manager)), true), User);
|
||||
for requested in [Owner, Admin, Manager] {
|
||||
assert_eq!(MembershipType::requested(requested, Some(&member(Custom)), true), requested);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_permission_requests() {
|
||||
let ignored = serde_json::from_value(json!({"manageUsers":false,"manageSso":true})).unwrap();
|
||||
assert!(CustomPermission::requested(&ignored).unwrap().is_empty());
|
||||
for permission in CustomPermission::ALL {
|
||||
for value in [json!(true), json!(null), json!("true"), json!(1), json!([true])] {
|
||||
let request = HashMap::from([(permission.json_key().to_owned(), value.clone())]);
|
||||
let result = CustomPermission::requested(&request);
|
||||
if value == json!(true) && SUPPORTED_CUSTOM_PERMISSIONS.contains(&permission) {
|
||||
assert_eq!(result.unwrap(), HashSet::from([permission]));
|
||||
} else {
|
||||
assert!(result.is_err(), "{permission:?}: {value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(SUPPORTED_CUSTOM_PERMISSIONS, []);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn role_changes_drop_custom_permissions() {
|
||||
for (from, to) in [
|
||||
(Custom, Owner),
|
||||
(Custom, Admin),
|
||||
(Custom, Manager),
|
||||
(Custom, User),
|
||||
(User, Custom),
|
||||
(Manager, Custom),
|
||||
(Custom, Custom),
|
||||
(Admin, User),
|
||||
] {
|
||||
let mut member = member(from);
|
||||
member.access_all = true;
|
||||
for permission in CustomPermission::ALL {
|
||||
*member.custom_permission_mut(permission) = true;
|
||||
}
|
||||
member.set_type(to);
|
||||
for permission in CustomPermission::ALL {
|
||||
assert_eq!(*member.custom_permission_mut(permission), from == to, "{from:?} -> {to:?}");
|
||||
}
|
||||
assert_eq!(member.access_all, to != Custom);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -244,6 +244,15 @@ table! {
|
||||
atype -> Integer,
|
||||
reset_password_key -> Nullable<Text>,
|
||||
external_id -> Nullable<Text>,
|
||||
manage_users -> Bool,
|
||||
manage_groups -> Bool,
|
||||
manage_policies -> Bool,
|
||||
create_new_collections -> Bool,
|
||||
edit_any_collection -> Bool,
|
||||
delete_any_collection -> Bool,
|
||||
access_event_logs -> Bool,
|
||||
access_import_export -> Bool,
|
||||
access_reports -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user