-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathAccountsDatabaseAccessor.cs
More file actions
289 lines (242 loc) · 12.1 KB
/
AccountsDatabaseAccessor.cs
File metadata and controls
289 lines (242 loc) · 12.1 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#if (!UNITY_WEBGL && !UNITY_IOS) || UNITY_EDITOR
using MasterServerToolkit.Logging;
using MasterServerToolkit.MasterServer;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MasterServerToolkit.Bridges.MongoDB
{
public class AccountsDatabaseAccessor : IAccountsDatabaseAccessor
{
private readonly MongoClient client;
private readonly IMongoDatabase database;
private readonly IMongoCollection<AccountInfoMongoDB> accountsCollection;
private readonly IMongoCollection<ExtraPropertyDataMongoDB> extraPropertiesCollection;
private readonly IMongoCollection<PasswordResetDataMongoDB> resetCodesCollection;
private readonly IMongoCollection<EmailConfirmationDataMongoDB> emailConfirmations;
public MstProperties CustomProperties { get; private set; } = new MstProperties();
public Logger Logger { get; set; }
public AccountsDatabaseAccessor(string connectionString, string databaseName)
: this(new MongoClient(connectionString), databaseName) { }
public AccountsDatabaseAccessor(MongoClient client, string databaseName)
{
this.client = client;
database = this.client.GetDatabase(databaseName);
accountsCollection = database.GetCollection<AccountInfoMongoDB>("accounts");
extraPropertiesCollection = database.GetCollection<ExtraPropertyDataMongoDB>("extraProperties");
resetCodesCollection = database.GetCollection<PasswordResetDataMongoDB>("resetCodes");
emailConfirmations = database.GetCollection<EmailConfirmationDataMongoDB>("emailConfirmationCodes");
// force reindex
accountsCollection.Indexes.DropAll();
accountsCollection.Indexes.CreateOne(
new CreateIndexModel<AccountInfoMongoDB>(
Builders<AccountInfoMongoDB>.IndexKeys.Ascending(e => e.Username), new CreateIndexOptions() {Unique = true}
)
);
extraPropertiesCollection.Indexes.CreateOne(
new CreateIndexModel<ExtraPropertyDataMongoDB>(
Builders<ExtraPropertyDataMongoDB>.IndexKeys
.Ascending(e => e.PropertyKey)
.Ascending(e => e.PropertyValue),
new CreateIndexOptions {Unique = true}
)
);
resetCodesCollection.Indexes.CreateOne(
new CreateIndexModel<PasswordResetDataMongoDB>(
Builders<PasswordResetDataMongoDB>.IndexKeys.Ascending(e => e.Email), new CreateIndexOptions() { Unique = true }
)
);
emailConfirmations.Indexes.CreateOne(
new CreateIndexModel<EmailConfirmationDataMongoDB>(
Builders<EmailConfirmationDataMongoDB>.IndexKeys.Ascending(e => e.Email), new CreateIndexOptions() { Unique = true }
)
);
}
public IAccountInfoData CreateAccountInstance()
{
return new AccountInfoMongoDB();
}
public async Task<IAccountInfoData> GetAccountByIdAsync(string id)
{
var filter = Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Id, id);
var account = await accountsCollection.Find(filter).FirstOrDefaultAsync();
if (account != null)
{
account.LastLogin = DateTime.UtcNow;
await accountsCollection.ReplaceOneAsync(Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Id, account.Id), account);
account.ExtraProperties = await GetExtraPropertiesAsync(account.Id);
}
return account;
}
public async Task<IAccountInfoData> GetAccountByUsernameAsync(string username)
{
var filter = Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Username, username);
var account = await accountsCollection.Find(filter).FirstOrDefaultAsync();
if (account != null)
{
account.LastLogin = DateTime.UtcNow;
await accountsCollection.ReplaceOneAsync(Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Id, account.Id), account);
account.ExtraProperties = await GetExtraPropertiesAsync(account.Id);
}
return account;
}
public async Task<IAccountInfoData> GetAccountByEmailAsync(string email)
{
var filter = Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Email, email);
var account = await accountsCollection.Find(filter).FirstOrDefaultAsync();
if (account != null)
{
account.LastLogin = DateTime.UtcNow;
await accountsCollection.ReplaceOneAsync(Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Id, account.Id), account);
account.ExtraProperties = await GetExtraPropertiesAsync(account.Id);
}
return account;
}
public async Task<IAccountInfoData> GetAccountByExtraPropertyAsync(string phoneNumber)
{
var filter = Builders<AccountInfoMongoDB>.Filter.Eq(e => e.PhoneNumber, phoneNumber);
return await accountsCollection.Find(filter).FirstOrDefaultAsync();
}
public async Task<IAccountInfoData> GetAccountByTokenAsync(string token)
{
var filter = Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Token, token);
var account = await accountsCollection.Find(filter).FirstOrDefaultAsync();
if (account != null)
{
account.LastLogin = DateTime.UtcNow;
await accountsCollection.ReplaceOneAsync(Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Id, account.Id), account);
account.ExtraProperties = await GetExtraPropertiesAsync(account.Id);
}
return account;
}
public async Task<IAccountInfoData> GetAccountByDeviceIdAsync(string deviceId)
{
var filter = Builders<AccountInfoMongoDB>.Filter.Eq(e => e.DeviceId, deviceId.ToLower());
var account = await accountsCollection.Find(filter).FirstOrDefaultAsync();
if (account != null)
{
account.LastLogin = DateTime.UtcNow;
await accountsCollection.ReplaceOneAsync(Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Id, account.Id), account);
account.ExtraProperties = await GetExtraPropertiesAsync(account.Id);
}
return account;
}
public async Task SavePasswordResetCodeAsync(string email, string code)
{
await resetCodesCollection.DeleteManyAsync(i => i.Email == email.ToLower());
await resetCodesCollection.InsertOneAsync(new PasswordResetDataMongoDB()
{
Email = email.ToLower(),
Code = code
});
}
public async Task<string> CheckPasswordResetCodeAsync(string email)
{
PasswordResetDataMongoDB data = default;
var filter = Builders<PasswordResetDataMongoDB>.Filter.Eq(i => i.Email, email.ToLower());
data = await resetCodesCollection.Find(filter).FirstOrDefaultAsync();
return data != null ? data.Code : "";
}
public async Task SaveEmailConfirmationCodeAsync(string email, string code)
{
await emailConfirmations.DeleteManyAsync(i => i.Email == email.ToLower());
await emailConfirmations.InsertOneAsync(new EmailConfirmationDataMongoDB()
{
Code = code,
Email = email
});
}
public async Task<bool> CheckEmailConfirmationCodeAsync(string email, string code)
{
if (string.IsNullOrEmpty(email)) return false;
if (string.IsNullOrEmpty(code)) return false;
var entry = await emailConfirmations.Find(i => i.Email == email).FirstOrDefaultAsync();
if (entry != null && entry.Code == code)
{
await emailConfirmations.DeleteOneAsync(i => i.Email == email.ToLower());
return true;
}
return false;
}
public async Task UpdateAccountAsync(IAccountInfoData account)
{
var filter = Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Id, account.Id);
await accountsCollection.ReplaceOneAsync(filter, account as AccountInfoMongoDB);
await InsertOrUpdateExtraProperties(account.Id, account.ExtraProperties);
}
public async Task<string> InsertAccountAsync(IAccountInfoData account)
{
var acc = account as AccountInfoMongoDB;
await accountsCollection.InsertOneAsync(acc);
await InsertOrUpdateExtraProperties(acc.Id, account.ExtraProperties);
return acc.Id;
}
public async Task InsertOrUpdateTokenAsync(IAccountInfoData account, string token)
{
var filter = Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Id, account.Id);
var update = Builders<AccountInfoMongoDB>.Update.Set(e => e.Token, token);
account.Token = token;
await accountsCollection.UpdateOneAsync(filter, update);
}
public Task<string> GetPhoneNumberConfirmationCodeAsync(string phoneNumber)
{
throw new NotImplementedException();
}
public Task<bool> CheckPhoneNumberConfirmationCodeAsync(string confirmationCode)
{
throw new NotImplementedException();
}
public void Dispose() { }
public Task<IAccountInfoData> GetAccountByPropertyAsync(string propertyKey, string propertyValue)
{
throw new NotImplementedException();
}
public async Task<IAccountInfoData> GetAccountByExtraPropertyAsync(string propertyKey, string propertyValue)
{
var filter = Builders<ExtraPropertyDataMongoDB>.Filter.And(
Builders<ExtraPropertyDataMongoDB>.Filter.Eq(e => e.PropertyKey, propertyKey),
Builders<ExtraPropertyDataMongoDB>.Filter.Eq(e => e.PropertyValue, propertyValue)
);
var extraProperty = await extraPropertiesCollection.Find(filter).FirstOrDefaultAsync();
if (extraProperty == null)
{
return null;
}
var accountFilter = Builders<AccountInfoMongoDB>.Filter.Eq(e => e.Id, extraProperty.AccountId);
return await accountsCollection.Find(accountFilter).FirstOrDefaultAsync();
}
private async Task InsertOrUpdateExtraProperties(string accountId, Dictionary<string, string> properties)
{
foreach (var (key, value) in properties)
{
var filter = Builders<ExtraPropertyDataMongoDB>.Filter.And(
Builders<ExtraPropertyDataMongoDB>.Filter.Eq(e => e.AccountId, accountId),
Builders<ExtraPropertyDataMongoDB>.Filter.Eq(e => e.PropertyKey, key)
);
var update = Builders<ExtraPropertyDataMongoDB>.Update.Set(e => e.PropertyValue, value);
var options = new UpdateOptions {IsUpsert = true};
await extraPropertiesCollection.UpdateOneAsync(filter, update, options);
}
}
public async Task<Dictionary<string, string>> GetExtraPropertiesAsync(string accountId)
{
var filter = Builders<ExtraPropertyDataMongoDB>.Filter.Eq(e => e.AccountId, accountId);
var extraProperties = await extraPropertiesCollection.Find(filter).ToListAsync();
return extraProperties.ToDictionary(e => e.PropertyKey, e => e.PropertyValue);
}
public async Task<bool> CheckPasswordResetCodeAsync(string email, string code)
{
var filter = Builders<PasswordResetDataMongoDB>.Filter.Eq(i => i.Email, email.ToLower());
var entry = await resetCodesCollection.Find(filter).FirstOrDefaultAsync();
if (entry != null && entry.Code == code)
{
await resetCodesCollection.DeleteManyAsync(filter);
return true;
}
return false;
}
}
}
#endif