Files
server/src/Core/Models/Api/Response/CipherResponseModel.cs

114 lines
4.1 KiB
C#
Raw Normal View History

using System;
using Core.Models.Data;
using System.Collections.Generic;
using Bit.Core.Models.Table;
using System.Linq;
2017-03-08 21:55:08 -05:00
namespace Bit.Core.Models.Api
{
2017-04-17 17:01:23 -04:00
public class CipherMiniResponseModel : ResponseModel
{
public CipherMiniResponseModel(Cipher cipher, GlobalSettings globalSettings, bool orgUseTotp, string obj = "cipherMini")
: base(obj)
{
if(cipher == null)
{
throw new ArgumentNullException(nameof(cipher));
}
Id = cipher.Id.ToString();
Type = cipher.Type;
RevisionDate = cipher.RevisionDate;
OrganizationId = cipher.OrganizationId?.ToString();
2017-06-30 23:01:41 -04:00
Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings);
OrganizationUseTotp = orgUseTotp;
switch(cipher.Type)
{
case Enums.CipherType.Login:
2017-01-02 21:52:13 -05:00
Data = new LoginDataModel(cipher);
break;
case Enums.CipherType.SecureNote:
Data = new SecureNoteDataModel(cipher);
break;
case Enums.CipherType.Card:
Data = new CardDataModel(cipher);
break;
2017-10-09 11:23:20 -04:00
case Enums.CipherType.Identity:
Data = new IdentityDataModel(cipher);
break;
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
}
}
public string Id { get; set; }
public string OrganizationId { get; set; }
public Enums.CipherType Type { get; set; }
public dynamic Data { get; set; }
2017-06-30 23:01:41 -04:00
public IEnumerable<AttachmentResponseModel> Attachments { get; set; }
public bool OrganizationUseTotp { get; set; }
public DateTime RevisionDate { get; set; }
}
2017-04-17 17:01:23 -04:00
public class CipherResponseModel : CipherMiniResponseModel
{
2017-06-30 23:01:41 -04:00
public CipherResponseModel(CipherDetails cipher, GlobalSettings globalSettings, string obj = "cipher")
: base(cipher, globalSettings, cipher.OrganizationUseTotp, obj)
2017-04-17 17:01:23 -04:00
{
FolderId = cipher.FolderId?.ToString();
Favorite = cipher.Favorite;
2017-05-06 23:23:01 -04:00
Edit = cipher.Edit;
2017-04-17 17:01:23 -04:00
}
public string FolderId { get; set; }
public bool Favorite { get; set; }
2017-05-06 23:23:01 -04:00
public bool Edit { get; set; }
2017-04-17 17:01:23 -04:00
}
public class CipherDetailsResponseModel : CipherResponseModel
{
2017-06-30 23:01:41 -04:00
public CipherDetailsResponseModel(CipherDetails cipher, GlobalSettings globalSettings,
2017-04-27 09:19:30 -04:00
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphers, string obj = "cipherDetails")
2017-06-30 23:01:41 -04:00
: base(cipher, globalSettings, obj)
{
2017-11-22 09:53:14 -05:00
if(collectionCiphers?.ContainsKey(cipher.Id) ?? false)
{
2017-04-27 09:39:21 -04:00
CollectionIds = collectionCiphers[cipher.Id].Select(c => c.CollectionId);
}
else
{
2017-04-27 09:19:30 -04:00
CollectionIds = new Guid[] { };
}
}
2017-06-30 23:01:41 -04:00
public CipherDetailsResponseModel(CipherDetails cipher, GlobalSettings globalSettings,
IEnumerable<CollectionCipher> collectionCiphers, string obj = "cipherDetails")
: base(cipher, globalSettings, obj)
2017-04-04 17:22:47 -04:00
{
2017-11-22 09:53:14 -05:00
CollectionIds = collectionCiphers?.Select(c => c.CollectionId) ?? new List<Guid>();
2017-04-04 17:22:47 -04:00
}
2017-04-27 09:19:30 -04:00
public IEnumerable<Guid> CollectionIds { get; set; }
}
2017-04-04 17:22:47 -04:00
2017-04-17 17:01:23 -04:00
public class CipherMiniDetailsResponseModel : CipherMiniResponseModel
{
2017-06-30 23:01:41 -04:00
public CipherMiniDetailsResponseModel(Cipher cipher, GlobalSettings globalSettings,
2017-04-27 09:19:30 -04:00
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphers, string obj = "cipherMiniDetails")
: base(cipher, globalSettings, false, obj)
2017-04-17 17:01:23 -04:00
{
2017-11-22 09:53:14 -05:00
if(collectionCiphers?.ContainsKey(cipher.Id) ?? false)
2017-04-17 17:01:23 -04:00
{
2017-04-27 09:39:21 -04:00
CollectionIds = collectionCiphers[cipher.Id].Select(c => c.CollectionId);
2017-04-17 17:01:23 -04:00
}
else
{
2017-04-27 09:19:30 -04:00
CollectionIds = new Guid[] { };
2017-04-17 17:01:23 -04:00
}
}
2017-04-27 09:19:30 -04:00
public IEnumerable<Guid> CollectionIds { get; set; }
2017-04-17 17:01:23 -04:00
}
}