Align policy enforcement and status codes with Bitwarden (#7970)

This commit is contained in:
Tom authored and GitHub committed 2026-11-15 21:20:46 +01:00
1 parent 00611ea80d
commit 4051a4d3a6
6 files changed
+95 -41

No files matched your search

+13 -5
View File
@@ -342,12 +342,20 @@ impl OrgPolicy {
if let Some(p) = Self::find_by_org_and_type(&m.org_uuid, OrgPolicyType::SingleOrg, conn).await
&& p.enabled
&& Membership::count_accepted_and_confirmed_by_user(&m.user_uuid, &m.org_uuid, conn).await > 0
{
err!(format!(
"Cannot {} because the organization policy forbids being part of other organization (membership {})",
action, m.uuid
));
// Joining, confirming and restoring count memberships revoked elsewhere too, as in Bitwarden. Bitwarden
// does not check a modification at all, so that keeps counting only the active ones.
let other_memberships = if action == "modify" {
Membership::count_accepted_and_confirmed_by_user(&m.user_uuid, &m.org_uuid, conn).await
} else {
Membership::count_accepted_confirmed_or_revoked_by_user(&m.user_uuid, &m.org_uuid, conn).await
};
if other_memberships > 0 {
err!(format!(
"Cannot {} because the organization policy forbids being part of other organization (membership {})",
action, m.uuid
));
}
}
}
+31 -12
View File
@@ -1241,6 +1241,31 @@ impl Membership {
.await
}
/// Counts the user's accepted and confirmed memberships in other organizations, revoked ones included, which is
/// what Bitwarden's Single organization policy counts, both when it is enabled and when a member joins.
pub async fn count_accepted_confirmed_or_revoked_by_user(
user_uuid: &UserId,
excluded_org: &OrganizationId,
conn: &DbConn,
) -> i64 {
let statuses = [
MembershipStatus::Accepted as i32,
MembershipStatus::Confirmed as i32,
MembershipStatus::Accepted as i32 - ACTIVATE_REVOKE_DIFF,
MembershipStatus::Confirmed as i32 - ACTIVATE_REVOKE_DIFF,
];
conn.run(move |conn| {
users_organizations::table
.filter(users_organizations::user_uuid.eq(user_uuid))
.filter(users_organizations::org_uuid.ne(excluded_org))
.filter(users_organizations::status.eq_any(statuses))
.count()
.first::<i64>(conn)
.unwrap_or(0)
})
.await
}
pub async fn find_by_org(org_uuid: &OrganizationId, conn: &DbConn) -> Vec<Self> {
conn.run(move |conn| {
users_organizations::table
@@ -1251,17 +1276,6 @@ impl Membership {
.await
}
pub async fn find_confirmed_by_org(org_uuid: &OrganizationId, conn: &DbConn) -> Vec<Self> {
conn.run(move |conn| {
users_organizations::table
.filter(users_organizations::org_uuid.eq(org_uuid))
.filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32))
.load::<Self>(conn)
.unwrap_or_default()
})
.await
}
// Get all users which are either owner or admin, or a manager which can manage/access all
// A Custom member with Edit any collection is not listed: the Admin Console sends this list back when a collection
// is saved, which would assign them to it for good, while the others are skipped there for their access_all.
@@ -1444,6 +1458,7 @@ impl Membership {
.await
}
/// The user's accepted and confirmed memberships in organizations that have enabled `policy_type`.
pub async fn find_by_user_and_policy(user_uuid: &UserId, policy_type: OrgPolicyType, conn: &DbConn) -> Vec<Self> {
conn.run(move |conn| {
users_organizations::table
@@ -1454,7 +1469,11 @@ impl Membership {
.and(org_policies::atype.eq(policy_type as i32))
.and(org_policies::enabled.eq(true))),
)
.filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32))
.filter(
users_organizations::status
.eq(MembershipStatus::Accepted as i32)
.or(users_organizations::status.eq(MembershipStatus::Confirmed as i32)),
)
.select(users_organizations::all_columns)
.load::<Self>(conn)
.unwrap_or_default()