mirror of
https://github.com/bitwarden/server.git
synced 2025-04-05 05:00:19 -05:00
SM-365: Fix Import Secrets Bug (#2703)
This commit is contained in:
parent
9a484bec07
commit
e6635ff590
@ -154,41 +154,32 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
|
|||||||
|
|
||||||
public async Task<IEnumerable<Core.SecretsManager.Entities.Secret>> ImportAsync(IEnumerable<Core.SecretsManager.Entities.Secret> secrets)
|
public async Task<IEnumerable<Core.SecretsManager.Entities.Secret>> ImportAsync(IEnumerable<Core.SecretsManager.Entities.Secret> secrets)
|
||||||
{
|
{
|
||||||
try
|
using (var scope = ServiceScopeFactory.CreateScope())
|
||||||
{
|
{
|
||||||
using (var scope = ServiceScopeFactory.CreateScope())
|
var dbContext = GetDatabaseContext(scope);
|
||||||
|
var entities = new List<Secret>();
|
||||||
|
var projects = secrets
|
||||||
|
.SelectMany(s => s.Projects ?? Enumerable.Empty<Core.SecretsManager.Entities.Project>())
|
||||||
|
.DistinctBy(p => p.Id)
|
||||||
|
.Select(p => Mapper.Map<Project>(p))
|
||||||
|
.ToDictionary(p => p.Id, p => p);
|
||||||
|
|
||||||
|
dbContext.AttachRange(projects.Values);
|
||||||
|
|
||||||
|
foreach (var s in secrets)
|
||||||
{
|
{
|
||||||
var dbContext = GetDatabaseContext(scope);
|
var entity = Mapper.Map<Secret>(s);
|
||||||
var entities = new List<Secret>();
|
|
||||||
var projects = secrets
|
|
||||||
.SelectMany(s => s.Projects ?? Enumerable.Empty<Core.SecretsManager.Entities.Project>())
|
|
||||||
.DistinctBy(p => p.Id)
|
|
||||||
.Select(p => Mapper.Map<Project>(p))
|
|
||||||
.ToDictionary(p => p.Id, p => p);
|
|
||||||
|
|
||||||
dbContext.AttachRange(projects);
|
if (s.Projects?.Count > 0)
|
||||||
|
|
||||||
foreach (var s in secrets)
|
|
||||||
{
|
{
|
||||||
var entity = Mapper.Map<Secret>(s);
|
entity.Projects = s.Projects.Select(p => projects[p.Id]).ToList();
|
||||||
|
|
||||||
if (s.Projects?.Count > 0)
|
|
||||||
{
|
|
||||||
entity.Projects = s.Projects.Select(p => projects[p.Id]).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
entities.Add(entity);
|
|
||||||
}
|
}
|
||||||
await GetDbSet(dbContext).AddRangeAsync(entities);
|
|
||||||
await dbContext.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
return secrets;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Console.WriteLine(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
entities.Add(entity);
|
||||||
|
}
|
||||||
|
await GetDbSet(dbContext).AddRangeAsync(entities);
|
||||||
|
await dbContext.SaveChangesAsync();
|
||||||
|
}
|
||||||
return secrets;
|
return secrets;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user