1
0
mirror of https://github.com/bitwarden/server synced 2026-02-26 01:13:35 +00:00

Add mapping methods

This commit is contained in:
Thomas Rittson
2026-01-03 14:27:22 +10:00
parent 4315f5a759
commit 1d42b336f4
3 changed files with 222 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
using Bit.Core.AdminConsole.Interfaces;
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces;
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Models;
@@ -55,4 +56,83 @@ public class AcceptedOrganizationUser : IExternal, IOrganizationUserPermissions
/// True if the User has access to Secrets Manager for this Organization, false otherwise.
/// </summary>
public bool AccessSecretsManager { get; set; }
/// <summary>
/// Transitions this accepted user to a confirmed state when an organization admin confirms them.
/// </summary>
/// <param name="key">The Organization symmetric key encrypted with the User's public key.</param>
/// <returns>A new <see cref="ConfirmedOrganizationUser"/> instance.</returns>
public ConfirmedOrganizationUser ToConfirmed(string key)
{
return new ConfirmedOrganizationUser
{
Id = Id,
OrganizationId = OrganizationId,
UserId = UserId,
Key = key,
ResetPasswordKey = null,
Type = Type,
ExternalId = ExternalId,
CreationDate = CreationDate,
RevisionDate = DateTime.UtcNow,
Permissions = Permissions,
AccessSecretsManager = AccessSecretsManager
};
}
/// <summary>
/// Converts this model to an <see cref="OrganizationUser"/> entity.
/// </summary>
/// <returns>An <see cref="OrganizationUser"/> entity with Status set to Accepted.</returns>
public OrganizationUser ToEntity()
{
return new OrganizationUser
{
Id = Id,
OrganizationId = OrganizationId,
UserId = UserId,
Email = null,
Key = null,
ResetPasswordKey = null,
Status = OrganizationUserStatusType.Accepted,
Type = Type,
ExternalId = ExternalId,
CreationDate = CreationDate,
RevisionDate = RevisionDate,
Permissions = Permissions,
AccessSecretsManager = AccessSecretsManager
};
}
/// <summary>
/// Creates an <see cref="AcceptedOrganizationUser"/> from an <see cref="OrganizationUser"/> entity.
/// </summary>
/// <param name="entity">The entity to convert from. Must have Status = Accepted and UserId must not be null.</param>
/// <returns>A new <see cref="AcceptedOrganizationUser"/> instance.</returns>
/// <exception cref="InvalidOperationException">Thrown if the entity is not in Accepted status or UserId is null.</exception>
public static AcceptedOrganizationUser FromEntity(OrganizationUser entity)
{
if (entity.Status != OrganizationUserStatusType.Accepted)
{
throw new InvalidOperationException($"Cannot create AcceptedOrganizationUser from entity with status {entity.Status}");
}
if (!entity.UserId.HasValue)
{
throw new InvalidOperationException("Cannot create AcceptedOrganizationUser from entity with null UserId");
}
return new AcceptedOrganizationUser
{
Id = entity.Id,
OrganizationId = entity.OrganizationId,
UserId = entity.UserId.Value,
Type = entity.Type,
ExternalId = entity.ExternalId,
CreationDate = entity.CreationDate,
RevisionDate = entity.RevisionDate,
Permissions = entity.Permissions,
AccessSecretsManager = entity.AccessSecretsManager
};
}
}

View File

@@ -1,5 +1,6 @@
using Bit.Core.AdminConsole.Interfaces;
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces;
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Models;
@@ -67,4 +68,67 @@ public class ConfirmedOrganizationUser : IExternal, IOrganizationUserPermissions
/// True if the User has access to Secrets Manager for this Organization, false otherwise.
/// </summary>
public bool AccessSecretsManager { get; set; }
/// <summary>
/// Converts this model to an <see cref="OrganizationUser"/> entity.
/// </summary>
/// <returns>An <see cref="OrganizationUser"/> entity with Status set to Confirmed.</returns>
public OrganizationUser ToEntity()
{
return new OrganizationUser
{
Id = Id,
OrganizationId = OrganizationId,
UserId = UserId,
Email = null,
Key = Key,
ResetPasswordKey = ResetPasswordKey,
Status = OrganizationUserStatusType.Confirmed,
Type = Type,
ExternalId = ExternalId,
CreationDate = CreationDate,
RevisionDate = RevisionDate,
Permissions = Permissions,
AccessSecretsManager = AccessSecretsManager
};
}
/// <summary>
/// Creates a <see cref="ConfirmedOrganizationUser"/> from an <see cref="OrganizationUser"/> entity.
/// </summary>
/// <param name="entity">The entity to convert from. Must have Status = Confirmed, UserId and Key must not be null.</param>
/// <returns>A new <see cref="ConfirmedOrganizationUser"/> instance.</returns>
/// <exception cref="InvalidOperationException">Thrown if the entity is not in Confirmed status, or UserId or Key is null.</exception>
public static ConfirmedOrganizationUser FromEntity(OrganizationUser entity)
{
if (entity.Status != OrganizationUserStatusType.Confirmed)
{
throw new InvalidOperationException($"Cannot create ConfirmedOrganizationUser from entity with status {entity.Status}");
}
if (!entity.UserId.HasValue)
{
throw new InvalidOperationException("Cannot create ConfirmedOrganizationUser from entity with null UserId");
}
if (string.IsNullOrEmpty(entity.Key))
{
throw new InvalidOperationException("Cannot create ConfirmedOrganizationUser from entity with null Key");
}
return new ConfirmedOrganizationUser
{
Id = entity.Id,
OrganizationId = entity.OrganizationId,
UserId = entity.UserId.Value,
Key = entity.Key,
ResetPasswordKey = entity.ResetPasswordKey,
Type = entity.Type,
ExternalId = entity.ExternalId,
CreationDate = entity.CreationDate,
RevisionDate = entity.RevisionDate,
Permissions = entity.Permissions,
AccessSecretsManager = entity.AccessSecretsManager
};
}
}

View File

@@ -1,4 +1,5 @@
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces;
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Models;
using Bit.Core.Utilities;
@@ -60,4 +61,81 @@ public class InvitedOrganizationUser : IExternal, IOrganizationUserPermissions
{
Id = CoreHelpers.GenerateComb();
}
/// <summary>
/// Transitions this invited user to an accepted state when the user accepts the invitation.
/// </summary>
/// <param name="userId">The ID of the User who accepted the invitation.</param>
/// <returns>A new <see cref="AcceptedOrganizationUser"/> instance.</returns>
public AcceptedOrganizationUser ToAccepted(Guid userId)
{
return new AcceptedOrganizationUser
{
Id = Id,
OrganizationId = OrganizationId,
UserId = userId,
Type = Type,
ExternalId = ExternalId,
CreationDate = CreationDate,
RevisionDate = DateTime.UtcNow,
Permissions = Permissions,
AccessSecretsManager = AccessSecretsManager
};
}
/// <summary>
/// Converts this model to an <see cref="OrganizationUser"/> entity.
/// </summary>
/// <returns>An <see cref="OrganizationUser"/> entity with Status set to Invited.</returns>
public OrganizationUser ToEntity()
{
return new OrganizationUser
{
Id = Id,
OrganizationId = OrganizationId,
UserId = null,
Email = Email,
Key = null,
ResetPasswordKey = null,
Status = OrganizationUserStatusType.Invited,
Type = Type,
ExternalId = ExternalId,
CreationDate = CreationDate,
RevisionDate = RevisionDate,
Permissions = Permissions,
AccessSecretsManager = AccessSecretsManager
};
}
/// <summary>
/// Creates an <see cref="InvitedOrganizationUser"/> from an <see cref="OrganizationUser"/> entity.
/// </summary>
/// <param name="entity">The entity to convert from. Must have Status = Invited and Email must not be null.</param>
/// <returns>A new <see cref="InvitedOrganizationUser"/> instance.</returns>
/// <exception cref="InvalidOperationException">Thrown if the entity is not in Invited status or Email is null.</exception>
public static InvitedOrganizationUser FromEntity(OrganizationUser entity)
{
if (entity.Status != OrganizationUserStatusType.Invited)
{
throw new InvalidOperationException($"Cannot create InvitedOrganizationUser from entity with status {entity.Status}");
}
if (string.IsNullOrEmpty(entity.Email))
{
throw new InvalidOperationException("Cannot create InvitedOrganizationUser from entity with null Email");
}
return new InvitedOrganizationUser
{
Id = entity.Id,
OrganizationId = entity.OrganizationId,
Email = entity.Email,
Type = entity.Type,
ExternalId = entity.ExternalId,
CreationDate = entity.CreationDate,
RevisionDate = entity.RevisionDate,
Permissions = entity.Permissions,
AccessSecretsManager = entity.AccessSecretsManager
};
}
}