diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/AcceptedOrganizationUser.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/AcceptedOrganizationUser.cs index cc62994361..a223c13990 100644 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/AcceptedOrganizationUser.cs +++ b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/AcceptedOrganizationUser.cs @@ -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. /// public bool AccessSecretsManager { get; set; } + + /// + /// Transitions this accepted user to a confirmed state when an organization admin confirms them. + /// + /// The Organization symmetric key encrypted with the User's public key. + /// A new instance. + 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 + }; + } + + /// + /// Converts this model to an entity. + /// + /// An entity with Status set to Accepted. + 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 + }; + } + + /// + /// Creates an from an entity. + /// + /// The entity to convert from. Must have Status = Accepted and UserId must not be null. + /// A new instance. + /// Thrown if the entity is not in Accepted status or UserId is null. + 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 + }; + } } diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/ConfirmedOrganizationUser.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/ConfirmedOrganizationUser.cs index 50b1f76a08..b6e1bda29c 100644 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/ConfirmedOrganizationUser.cs +++ b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/ConfirmedOrganizationUser.cs @@ -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. /// public bool AccessSecretsManager { get; set; } + + /// + /// Converts this model to an entity. + /// + /// An entity with Status set to Confirmed. + 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 + }; + } + + /// + /// Creates a from an entity. + /// + /// The entity to convert from. Must have Status = Confirmed, UserId and Key must not be null. + /// A new instance. + /// Thrown if the entity is not in Confirmed status, or UserId or Key is null. + 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 + }; + } } diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/InvitedOrganizationUser.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/InvitedOrganizationUser.cs index a9318cbe0f..2383dd9a86 100644 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/InvitedOrganizationUser.cs +++ b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Models/InvitedOrganizationUser.cs @@ -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(); } + + /// + /// Transitions this invited user to an accepted state when the user accepts the invitation. + /// + /// The ID of the User who accepted the invitation. + /// A new instance. + 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 + }; + } + + /// + /// Converts this model to an entity. + /// + /// An entity with Status set to Invited. + 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 + }; + } + + /// + /// Creates an from an entity. + /// + /// The entity to convert from. Must have Status = Invited and Email must not be null. + /// A new instance. + /// Thrown if the entity is not in Invited status or Email is null. + 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 + }; + } }