Files
server/src/Core/Models/Api/Request/CipherRequestModel.cs

109 lines
3.2 KiB
C#
Raw Normal View History

2015-12-08 22:57:38 -05:00
using System;
using System.ComponentModel.DataAnnotations;
2017-03-08 21:55:08 -05:00
using Bit.Core.Utilities;
2017-03-08 21:45:08 -05:00
using Bit.Core.Models.Table;
2015-12-08 22:57:38 -05:00
using Bit.Core.Enums;
using Newtonsoft.Json;
using System.Collections.Generic;
2017-04-04 22:07:30 -04:00
using System.Linq;
2015-12-08 22:57:38 -05:00
2017-03-08 21:55:08 -05:00
namespace Bit.Core.Models.Api
2015-12-08 22:57:38 -05:00
{
public class CipherRequestModel
2015-12-08 22:57:38 -05:00
{
public CipherType Type { get; set; }
[Required]
[StringLength(36)]
2015-12-08 22:57:38 -05:00
public string Id { get; set; }
[StringLength(36)]
public string OrganizationId { get; set; }
2015-12-08 22:57:38 -05:00
[Required]
[EncryptedString]
[StringLength(300)]
2015-12-08 22:57:38 -05:00
public string Name { get; set; }
[EncryptedString]
2016-10-13 18:45:33 -04:00
[StringLength(10000)]
2015-12-08 22:57:38 -05:00
public string Uri { get; set; }
[EncryptedString]
2016-10-13 18:45:33 -04:00
[StringLength(300)]
2015-12-08 22:57:38 -05:00
public string Username { get; set; }
[EncryptedString]
[StringLength(300)]
2015-12-08 22:57:38 -05:00
public string Password { get; set; }
[EncryptedString]
2016-10-13 18:45:33 -04:00
[StringLength(10000)]
2015-12-08 22:57:38 -05:00
public string Notes { get; set; }
[EncryptedString]
[StringLength(300)]
public string Totp { get; set; }
2015-12-08 22:57:38 -05:00
public virtual Cipher ToCipher(Guid userId)
2015-12-08 22:57:38 -05:00
{
return ToCipher(new Cipher
2015-12-08 22:57:38 -05:00
{
Id = new Guid(Id),
UserId = string.IsNullOrWhiteSpace(OrganizationId) ? (Guid?)userId : null,
Type = Type
});
}
public Cipher ToCipher(Cipher existingCipher)
{
switch(existingCipher.Type)
2015-12-08 22:57:38 -05:00
{
2017-01-02 21:52:13 -05:00
case CipherType.Login:
2017-04-04 22:07:30 -04:00
existingCipher.Data = JsonConvert.SerializeObject(new LoginDataModel(this),
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
2015-12-08 22:57:38 -05:00
}
return existingCipher;
2015-12-08 22:57:38 -05:00
}
}
2017-04-12 12:42:00 -04:00
public class CipherShareRequestModel : IValidatableObject
{
2017-04-04 22:07:30 -04:00
[Required]
2017-04-27 09:19:30 -04:00
public IEnumerable<string> CollectionIds { get; set; }
[Required]
public CipherRequestModel Cipher { get; set; }
2017-04-04 22:07:30 -04:00
2017-04-12 12:42:00 -04:00
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if(string.IsNullOrWhiteSpace(Cipher.OrganizationId))
{
yield return new ValidationResult("Cipher OrganizationId is required.",
new string[] { nameof(Cipher.OrganizationId) });
}
2017-04-27 09:19:30 -04:00
if(!CollectionIds?.Any() ?? false)
2017-04-12 12:42:00 -04:00
{
2017-04-27 09:19:30 -04:00
yield return new ValidationResult("You must select at least one collection.",
new string[] { nameof(CollectionIds) });
2017-04-12 12:42:00 -04:00
}
}
}
2017-04-27 09:19:30 -04:00
public class CipherCollectionsRequestModel
2017-04-12 12:42:00 -04:00
{
[Required]
2017-04-27 09:19:30 -04:00
public IEnumerable<string> CollectionIds { get; set; }
}
2017-06-09 00:30:59 -04:00
public class CipherBulkDeleteRequestModel
{
[Required]
public IEnumerable<string> Ids { get; set; }
}
public class CipherBulkMoveRequestModel
{
[Required]
public IEnumerable<string> Ids { get; set; }
public string FolderId { get; set; }
}
2015-12-08 22:57:38 -05:00
}