-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsers.cs
74 lines (63 loc) · 2.86 KB
/
Users.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using Microsoft.Extensions.Configuration;
using Microsoft.TokenService.BlockchainNetworkManager;
using Microsoft.TokenService.KeyManagement;
using Microsoft.TokenService.PartyManager;
using Microsoft.TokenService.UserManager.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Solutions.CosmosDB.SQL;
using Microsoft.Solutions.CosmosDB;
using Microsoft.Solutions.CosmosDB.Security.ManagedIdentity;
namespace Microsoft.TokenService.UserManager
{
public class Users : SQLEntityCollectionBase<User>, IUserManager
{
Parties _parties;
BlockchainNetworks _blockchainNetworks;
AzureKeyVaultKeyClient _akvKeyClient;
public Users(IConfiguration Config, CosmosConnectionStrings cosmosConnectionStrings, AzureKeyVaultKeyClient akvKeyClient) : base(cosmosConnectionStrings.PrimaryReadWriteKey, Config["App:ManagementCollection"])
{
_parties = new Parties(cosmosConnectionStrings.PrimaryReadWriteKey, Config["App:ManagementCollection"]);
_blockchainNetworks = new BlockchainNetworks(cosmosConnectionStrings.PrimaryReadWriteKey, Config["App:ManagementCollection"]);
_akvKeyClient = akvKeyClient;
}
public async Task<User> RegisterUser(string Name, string Description, Guid PartyID, Guid BlockchainNetworkID)
{
var newUser = new User()
{
Name = Name,
PublicAddress = "",
Description = Description,
Party = await _parties.GetParty(PartyID),
BlockchainNetwork = await _blockchainNetworks.GetBlockchainNetwork(BlockchainNetworkID)
};
var userIdentifier = newUser.Id.ToString();
var result = await _akvKeyClient.SetKey(userIdentifier);
var publicKey = await _akvKeyClient.GetPublicKey(userIdentifier);
newUser.PublicAddress = publicKey;
await this.EntityCollection.AddAsync(newUser);
return newUser;
}
public async Task<User> GetUser(Guid Id)
{
return await this.EntityCollection.FindAsync(new GenericSpecification<User>(x => x.Id == Id));
}
public async Task<User> GetUserByPublicAddress(string PublicAddress)
{
return await this.EntityCollection.FindAsync(new GenericSpecification<User>(x => x.PublicAddress == PublicAddress));
}
public async Task UnRegistUser(Guid Id)
{
_ = await _akvKeyClient.DeleteKey(Id.ToString());
var user = await GetUser(Id);
await this.EntityCollection.DeleteAsync(user, user.__partitionkey);
}
public async Task<IEnumerable<User>> GetAllUsers()
{
return await this.EntityCollection.GetAllAsync();
}
}
}