diff --git a/src/Core/Enums/PolicyType.cs b/src/Core/Enums/PolicyType.cs new file mode 100644 index 000000000..6afe384d8 --- /dev/null +++ b/src/Core/Enums/PolicyType.cs @@ -0,0 +1,8 @@ +namespace Bit.Core.Enums +{ + public enum PolicyType : byte + { + TwoStepLogin = 0, + MasterPassword = 1 + } +} diff --git a/src/Core/Models/Table/Policy.cs b/src/Core/Models/Table/Policy.cs new file mode 100644 index 000000000..07c44b6aa --- /dev/null +++ b/src/Core/Models/Table/Policy.cs @@ -0,0 +1,22 @@ +using System; +using Bit.Core.Enums; +using Bit.Core.Utilities; + +namespace Bit.Core.Models.Table +{ + public class Policy : ITableObject + { + public Guid Id { get; set; } + public Guid OrganizationId { get; set; } + public PolicyType Type { get; set; } + public string Data { get; set; } + public bool Enabled { get; set; } + public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; + public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow; + + public void SetNewId() + { + Id = CoreHelpers.GenerateComb(); + } + } +} diff --git a/src/Core/Repositories/IPolicyRepository.cs b/src/Core/Repositories/IPolicyRepository.cs new file mode 100644 index 000000000..c26a90df6 --- /dev/null +++ b/src/Core/Repositories/IPolicyRepository.cs @@ -0,0 +1,12 @@ +using System; +using Bit.Core.Models.Table; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Bit.Core.Repositories +{ + public interface IPolicyRepository : IRepository + { + Task> GetManyByOrganizationIdAsync(Guid organizationId); + } +} diff --git a/src/Core/Repositories/SqlServer/PolicyRepository.cs b/src/Core/Repositories/SqlServer/PolicyRepository.cs new file mode 100644 index 000000000..381b30093 --- /dev/null +++ b/src/Core/Repositories/SqlServer/PolicyRepository.cs @@ -0,0 +1,35 @@ +using System; +using Bit.Core.Models.Table; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Data.SqlClient; +using System.Data; +using Dapper; +using System.Linq; + +namespace Bit.Core.Repositories.SqlServer +{ + public class PolicyRepository : Repository, IPolicyRepository + { + public PolicyRepository(GlobalSettings globalSettings) + : this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString) + { } + + public PolicyRepository(string connectionString, string readOnlyConnectionString) + : base(connectionString, readOnlyConnectionString) + { } + + public async Task> GetManyByOrganizationIdAsync(Guid organizationId) + { + using(var connection = new SqlConnection(ConnectionString)) + { + var results = await connection.QueryAsync( + $"[{Schema}].[{Table}_ReadByOrganizationId]", + new { OrganizationId = organizationId }, + commandType: CommandType.StoredProcedure); + + return results.ToList(); + } + } + } +}