mirror of
https://github.com/bitwarden/server.git
synced 2026-02-05 16:43:14 +08:00
initial commit of source
This commit is contained in:
85
src/Api/Models/Request/Ciphers/CipherRequestModel.cs
Normal file
85
src/Api/Models/Request/Ciphers/CipherRequestModel.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Api.Utilities;
|
||||
using Bit.Core.Domains;
|
||||
using System.Linq;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Api.Models
|
||||
{
|
||||
public class CipherRequestModel : IValidatableObject
|
||||
{
|
||||
public CipherType Type { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Id { get; set; }
|
||||
public string FolderId { get; set; }
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Name { get; set; }
|
||||
[EncryptedString]
|
||||
public string Uri { get; set; }
|
||||
[EncryptedString]
|
||||
public string Username { get; set; }
|
||||
[EncryptedString]
|
||||
public string Password { get; set; }
|
||||
[EncryptedString]
|
||||
public string Notes { get; set; }
|
||||
|
||||
public virtual Site ToSite(string userId = null)
|
||||
{
|
||||
return new Site
|
||||
{
|
||||
Id = Id,
|
||||
UserId = userId,
|
||||
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : FolderId,
|
||||
Name = Name,
|
||||
Uri = Uri,
|
||||
Username = Username,
|
||||
Password = Password,
|
||||
Notes = string.IsNullOrWhiteSpace(Notes) ? null : Notes
|
||||
};
|
||||
}
|
||||
|
||||
public Folder ToFolder(string userId = null)
|
||||
{
|
||||
return new Folder
|
||||
{
|
||||
Id = Id,
|
||||
UserId = userId,
|
||||
Name = Name
|
||||
};
|
||||
}
|
||||
|
||||
public static IEnumerable<dynamic> ToDynamicCiphers(CipherRequestModel[] models, string userId)
|
||||
{
|
||||
var sites = models.Where(m => m.Type == CipherType.Site).Select(m => m.ToSite(userId)).ToList();
|
||||
var folders = models.Where(m => m.Type == CipherType.Folder).Select(m => m.ToFolder(userId)).ToList();
|
||||
|
||||
var ciphers = new List<dynamic>();
|
||||
ciphers.AddRange(sites);
|
||||
ciphers.AddRange(folders);
|
||||
return ciphers;
|
||||
}
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if(Type == CipherType.Site)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(Uri))
|
||||
{
|
||||
yield return new ValidationResult("Uri is required for a site cypher.", new[] { "Uri" });
|
||||
}
|
||||
if(string.IsNullOrWhiteSpace(Username))
|
||||
{
|
||||
yield return new ValidationResult("Username is required for a site cypher.", new[] { "Username" });
|
||||
}
|
||||
if(string.IsNullOrWhiteSpace(Password))
|
||||
{
|
||||
yield return new ValidationResult("Password is required for a site cypher.", new[] { "Password" });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user