using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Models.Business;
using Bit.Core.Models.Business;
using Bit.Core.Utilities;
namespace Bit.Api.AdminConsole.Public.Models.Request;
public class OrganizationImportRequestModel
{
///
/// Groups to import.
///
public OrganizationImportGroupRequestModel[] Groups { get; set; }
///
/// Members to import.
///
public OrganizationImportMemberRequestModel[] Members { get; set; }
///
/// Determines if the data in this request should overwrite or append to the existing organization data.
///
[Required]
public bool? OverwriteExisting { get; set; }
///
/// Indicates an import of over 2000 users and/or groups is expected
///
public bool LargeImport { get; set; } = false;
public class OrganizationImportGroupRequestModel
{
///
/// The name of the group.
///
/// Development Team
[Required]
[StringLength(100)]
public string Name { get; set; }
///
/// External identifier for reference or linking this group to another system, such as a user directory.
///
/// external_id_123456
[Required]
[StringLength(300)]
[JsonConverter(typeof(PermissiveStringConverter))]
public string ExternalId { get; set; }
///
/// The associated external ids for members in this group.
///
[JsonConverter(typeof(PermissiveStringEnumerableConverter))]
public IEnumerable MemberExternalIds { get; set; }
public ImportedGroup ToImportedGroup(Guid organizationId)
{
var importedGroup = new ImportedGroup
{
Group = new Group
{
OrganizationId = organizationId,
Name = Name,
ExternalId = ExternalId
},
ExternalUserIds = new HashSet(MemberExternalIds)
};
return importedGroup;
}
}
public class OrganizationImportMemberRequestModel : IValidatableObject
{
///
/// The member's email address. Required for non-deleted users.
///
/// jsmith@example.com
[EmailAddress]
[StringLength(256)]
public string Email { get; set; }
///
/// External identifier for reference or linking this member to another system, such as a user directory.
///
/// external_id_123456
[Required]
[StringLength(300)]
[JsonConverter(typeof(PermissiveStringConverter))]
public string ExternalId { get; set; }
///
/// Determines if this member should be removed from the organization during import.
///
public bool Deleted { get; set; }
public ImportedOrganizationUser ToImportedOrganizationUser()
{
var importedUser = new ImportedOrganizationUser
{
Email = Email.ToLowerInvariant(),
ExternalId = ExternalId
};
return importedUser;
}
public IEnumerable Validate(ValidationContext validationContext)
{
if (string.IsNullOrWhiteSpace(Email) && !Deleted)
{
yield return new ValidationResult("Email is required for enabled members.",
new string[] { nameof(Email) });
}
}
}
}