using System.ComponentModel.DataAnnotations;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Interfaces;
using Bit.Core.Enums;
using Bit.Core.Models;
using Bit.Core.Models.Data;
using Bit.Core.Utilities;
#nullable enable
namespace Bit.Core.Entities;
///
/// An association table between one and one , representing that user's
/// membership in the organization. "Member" refers to the OrganizationUser object.
///
public class OrganizationUser : ITableObject, IExternal, IOrganizationUser
{
///
/// A unique random identifier.
///
public Guid Id { get; set; }
///
/// The ID of the Organization that the user is a member of.
///
public Guid OrganizationId { get; set; }
///
/// The ID of the User that is the member. This is NULL if the Status is Invited (or Invited and then Revoked), because
/// it is not linked to a specific User yet.
///
public Guid? UserId { get; set; }
///
/// The email address of the user invited to the organization. This is NULL if the Status is not Invited (or
/// Invited and then Revoked), because in that case the OrganizationUser is linked to a User
/// and the email is stored on the User object.
///
[MaxLength(256)]
public string? Email { get; set; }
///
/// The Organization symmetric key encrypted with the User's public key. NULL if the user is not in a Confirmed
/// (or Confirmed and then Revoked) status.
///
public string? Key { get; set; }
///
/// The User's symmetric key encrypted with the Organization's public key. NULL if the OrganizationUser
/// is not enrolled in account recovery.
///
public string? ResetPasswordKey { get; set; }
///
public OrganizationUserStatusType Status { get; set; }
///
/// The User's role in the Organization.
///
public OrganizationUserType Type { get; set; }
///
/// An ID used to identify the OrganizationUser with an external directory service. Used by Directory Connector
/// and SCIM.
///
[MaxLength(300)]
public string? ExternalId { get; set; }
///
/// The date the OrganizationUser was created, i.e. when the User was first invited to the Organization.
///
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
///
/// The last date the OrganizationUser entry was updated.
///
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
///
/// A json blob representing the of the OrganizationUser if they
/// are a Custom user role (i.e. the is Custom). MAY be NULL if they are not
/// a custom user, but this is not guaranteed; do not use this to determine their role.
///
///
/// Avoid using this property directly - instead use the and
/// helper methods.
///
public string? Permissions { get; set; }
///
/// True if the User has access to Secrets Manager for this Organization, false otherwise.
///
public bool AccessSecretsManager { get; set; }
public void SetNewId()
{
Id = CoreHelpers.GenerateComb();
}
public Permissions? GetPermissions()
{
return string.IsNullOrWhiteSpace(Permissions) ? null
: CoreHelpers.LoadClassFromJsonData(Permissions);
}
public void SetPermissions(Permissions permissions)
{
Permissions = CoreHelpers.ClassToJsonData(permissions);
}
}