-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSendRepository.cs
More file actions
180 lines (164 loc) · 7.19 KB
/
SendRepository.cs
File metadata and controls
180 lines (164 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#nullable enable
using AutoMapper;
using Bit.Core.KeyManagement.UserKey;
using Bit.Core.Tools.Enums;
using Bit.Core.Tools.Repositories;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.Tools.Repositories;
/// <inheritdoc cref="ISendRepository"/>
public class SendRepository : Repository<Core.Tools.Entities.Send, Send, Guid>, ISendRepository
{
/// <summary>
/// Initializes the <see cref="SendRepository"/>
/// </summary>
/// <param name="serviceScopeFactory">An IoC service locator.</param>
/// <param name="mapper">An automapper service.</param>
public SendRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Sends)
{ }
/// <summary>
/// Saves a <see cref="Send"/> in the database.
/// </summary>
/// <param name="send">
/// The send being saved.
/// </param>
/// <returns>
/// A task that completes once the save is complete.
/// The task result contains the saved <see cref="Send"/>.
/// </returns>
public override async Task<Core.Tools.Entities.Send> CreateAsync(Core.Tools.Entities.Send send)
{
send = await base.CreateAsync(send);
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
if (send.UserId.HasValue)
{
await UserUpdateStorage(send.UserId.Value);
await dbContext.UserBumpAccountRevisionDateAsync(send.UserId.Value);
await dbContext.SaveChangesAsync();
}
}
return send;
}
/// <inheritdoc />
public async Task<ICollection<Core.Tools.Entities.Send>> GetManyByDeletionDateAsync(DateTime deletionDateBefore)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Sends.Where(s => s.DeletionDate < deletionDateBefore).ToListAsync();
return Mapper.Map<List<Core.Tools.Entities.Send>>(results);
}
}
/// <inheritdoc />
public async Task<ICollection<Core.Tools.Entities.Send>> GetManyByUserIdAsync(Guid userId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Sends.Where(s => s.UserId == userId).ToListAsync();
return Mapper.Map<List<Core.Tools.Entities.Send>>(results);
}
}
/// <inheritdoc />
public async Task<ICollection<Core.Tools.Entities.Send>> GetManyByOrganizationIdAsync(Guid organizationId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Sends.Where(s => s.OrganizationId == organizationId).ToListAsync();
return Mapper.Map<List<Core.Tools.Entities.Send>>(results);
}
}
/// <inheritdoc />
public async Task<ICollection<Core.Tools.Entities.Send>> GetManyFileSendsByUserIdAsync(Guid userId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Sends
.Where(s => s.UserId == userId && s.Type == SendType.File)
.ToListAsync();
return Mapper.Map<List<Core.Tools.Entities.Send>>(results);
}
}
/// <inheritdoc />
public async Task<ICollection<Core.Tools.Entities.Send>> GetManyFileSendsByOrganizationIdAsync(Guid organizationId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Sends
.Where(s => s.OrganizationId == organizationId && s.Type == SendType.File)
.ToListAsync();
return Mapper.Map<List<Core.Tools.Entities.Send>>(results);
}
}
/// <inheritdoc />
public UpdateEncryptedDataForKeyRotation UpdateForKeyRotation(Guid userId,
IEnumerable<Core.Tools.Entities.Send> sends)
{
return async (_, _) =>
{
var newSends = sends.ToDictionary(s => s.Id);
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var userSends = await GetDbSet(dbContext)
.Where(s => s.UserId == userId)
.ToListAsync();
var validSends = userSends
.Where(send => newSends.ContainsKey(send.Id));
foreach (var send in validSends)
{
send.Key = newSends[send.Id].Key;
}
await dbContext.SaveChangesAsync();
};
}
/// <inheritdoc />
public async Task UpdateManyDisabledAsync(IEnumerable<Guid> ids, bool disabled)
{
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var sends = dbContext.Sends.Where(s => ids.Contains(s.Id));
await sends.ExecuteUpdateAsync(setters => setters
.SetProperty(s => s.Disabled, disabled)
.SetProperty(s => s.RevisionDate, DateTime.UtcNow)
);
var userIds = await sends.Select(s => s.User.Id).ToArrayAsync() ?? [];
await dbContext.UserBumpManyAccountRevisionDatesAsync(userIds);
await dbContext.SaveChangesAsync();
}
public async Task<IEnumerable<Guid>> GetIdsByOrganizationIdAsync(Guid organizationId)
{
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var orgUsers = await dbContext.OrganizationUsers.Where(ou => ou.OrganizationId == organizationId).ToListAsync();
var orgUserSendIds = await dbContext.Sends.Where(s => orgUsers.Any(ou => ou.UserId == s.UserId)).Select(s => s.Id).ToListAsync();
return Mapper.Map<List<Guid>>(orgUserSendIds);
}
public async Task UpdateManyDeletionDatesByIdsAsync(IEnumerable<Guid> ids, int deletionHours)
{
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var sends = dbContext.Sends.Where(s => ids.Contains(s.Id));
await sends.ExecuteUpdateAsync(setters => setters
.SetProperty(s => s.DeletionDate, s => s.CreationDate.AddHours(deletionHours))
.SetProperty(s => s.RevisionDate, DateTime.UtcNow)
);
var userIds = await sends.Select(s => s.User.Id).ToArrayAsync() ?? [];
await dbContext.UserBumpManyAccountRevisionDatesAsync(userIds);
await dbContext.SaveChangesAsync();
}
public async Task<ICollection<Core.Tools.Entities.Send>> GetManyByIdsAsync(IEnumerable<Guid> ids)
{
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Sends.Where(s => ids.Contains(s.Id)).ToListAsync();
return Mapper.Map<List<Core.Tools.Entities.Send>>(results);
}
}