Repository navigation
Expand file tree
/
Copy pathQuick.Core.Identity.Store.Entity.pas
More file actions
342 lines (287 loc) · 11 KB
/
Copy pathQuick.Core.Identity.Store.Entity.pas
File metadata and controls
342 lines (287 loc) · 11 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
{ ***************************************************************************
Copyright (c) 2016-2026 Kike Perez
Unit : Quick.Core.Identity.Store.Entity
Description : Core Identity Store Database
Author : Kike Perez
Version : 1.0
Created : 12/03/2020
Modified : 24/03/2020
This file is part of QuickCore: https://github.com/exilon/QuickCore
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Core.Identity.Store.Entity;
{$i QuickCore.inc}
interface
uses
System.SysUtils,
System.Rtti,
Quick.Commons,
Quick.Collections,
Quick.Value,
Quick.Core.Entity.DAO,
Quick.Core.Linq.Abstractions,
Quick.Core.Identity,
Quick.Core.Entity,
Quick.Core.Entity.Database,
Quick.Core.Entity.Factory.Database,
Quick.Core.Identity.Store.Abstractions;
type
TEntityStoreLinq<T : class, constructor> = class(TInterfacedObject,ILinq<T>)
private
fLinq : IEntityLinqQuery<T>;
public
constructor Create(aLinq : IEntityLinqQuery<T>);
{$IFDEF VALUE_FORMATPARAMS}
function Where(const aWhereClause : string; aWhereValues : array of TValue) : ILinq<T>; overload;
{$ELSE}
function Where(const aWhereClause : string; aWhereValues : array of const) : ILinq<T>; overload;
{$ENDIF}
function Where(const aWhereClause: string): ILinq<T>; overload;
function Where(aPredicate : TPredicate<T>) : ILinq<T>; overload;
function OrderBy(const aFieldNames : string) : ILinq<T>;
function OrderByDescending(const aFieldNames : string) : ILinq<T>;
function SelectFirst : T;
function SelectLast : T;
function SelectTop(aLimit : Integer) : IList<T>;
function Select : IList<T>; overload;
function Select(const aPropertyName : string) : IList<TFlexValue>; overload;
function Count : Integer;
function Update(const aFields : TArray<string>; aValues : array of const) : Boolean;
function Delete : Boolean;
end;
TEntityIdentityStore<TUser,TRole : class, constructor> = class(TInterfacedObject,IIdentityStore<TUser,TRole>)
private
fDBContext : TIdentityDbContext<TUser,TRole>;
function NewLinQ<T : class, constructor>: IEntityLinqQuery<T>;
//function NewQuery<T : class, constructor>: IEntityQuery<T>;
public
constructor Create(aDataBase : TEntityDatabase);
function GetUserById(aId : TFlexValue) : TUser;
function GetRolebyId(aId : TFlexValue) : TRole;
procedure AddUser(aUser : TUser);
function RemoveUser(aUser : TUser) : Boolean;
procedure AddRole(aRole : TRole);
function RemoveRole(aRole : TRole) : Boolean;
function ValidateCredentials(const aUsername, aPassword : string) : Boolean;
function Users : ILinq<TUser>;
function Roles : ILinq<TRole>;
function FindByName(const aUserName : string) : TUser;
function FindByEmail(const aEmail : string) : TUser;
function GetPasswordHash(aUser : TUser) : string;
function SetPasswordHash(aUser : TUser; const aHash : string) : Boolean;
function GetSecurityStamp(aUser : TUser) : string;
function SetSecurityStamp(aUser : TUser; const aStamp : string) : Boolean;
function IsLockedOut(aUser : TUser) : Boolean;
function SetLockoutEnd(aUser : TUser; aEnd : TDateTime) : Boolean;
function GetRolesForUser(aUser : TUser) : TArray<string>;
function AddUserToRole(aUser : TUser; const aRoleName : string) : Boolean;
function RemoveUserFromRole(aUser : TUser; const aRoleName : string) : Boolean;
end;
implementation
{ TEntityIdentityStore<TUser, TRole> }
constructor TEntityIdentityStore<TUser, TRole>.Create(aDataBase: TEntityDatabase);
begin
fDBContext := TIdentityDbContext<TUser,TRole>.Create(aDataBase);
if aDataBase.IsConnected then aDataBase.Disconnect;
fDBContext.Models.PluralizeTableNames := True;
fDBContext.Connect;
end;
function TEntityIdentityStore<TUser, TRole>.NewLinQ<T>: IEntityLinqQuery<T>;
begin
Result := TEntityDatabaseFactory.GetQueryInstance<T>(fDBContext.Database,fDBContext.Database.Models.Get(TEntityClass(T)));
end;
//function TEntityIdentityStore<TUser, TRole>.NewQuery<T>: IEntityQuery<T>;
//begin
// Result := TEntityDatabaseFactory.GetQueryInstance<T>(fDBContext.Database,fDBContext.Database.Models.Get(TEntityClass(T)));
//end;
procedure TEntityIdentityStore<TUser, TRole>.AddRole(aRole: TRole);
begin
fDBContext.Roles.AddOrUpdate(TEntity(aRole));
end;
procedure TEntityIdentityStore<TUser, TRole>.AddUser(aUser: TUser);
begin
fDBContext.Users.AddOrUpdate(TEntity(aUser));
end;
function TEntityIdentityStore<TUser, TRole>.GetUserById(aId: TFlexValue): TUser;
begin
Result := fDBContext.Users.Where('Id = ?',[aId.AsString]).SelectFirst;
end;
function TEntityIdentityStore<TUser, TRole>.GetRolebyId(aId: TFlexValue): TRole;
begin
Result := fDBContext.Roles.Where('Id = ?',[aId.AsString]).SelectFirst;
end;
function TEntityIdentityStore<TUser, TRole>.RemoveUser(aUser: TUser) : Boolean;
begin
Result := fDBContext.Roles.Delete(TEntity(aUser));
end;
function TEntityIdentityStore<TUser, TRole>.RemoveRole(aRole: TRole) : Boolean;
begin
Result := fDBContext.Roles.Delete(TEntity(aRole));
end;
function TEntityIdentityStore<TUser, TRole>.Roles: ILinq<TRole>;
begin
Result := TEntityStoreLinq<TRole>.Create(NewLinq<TRole>);
end;
function TEntityIdentityStore<TUser, TRole>.Users: ILinq<TUser>;
begin
Result := TEntityStoreLinq<TUser>.Create(NewLinq<TUser>);
end;
function TEntityIdentityStore<TUser, TRole>.ValidateCredentials(const aUsername, aPassword: string): Boolean;
begin
Result := fDBContext.Users.Where('UserName = ? AND PasswordHash = ?',[aUserName,aPassword]).Count > 0;
end;
function TEntityIdentityStore<TUser, TRole>.FindByName(const aUserName: string): TUser;
begin
Result := fDBContext.Users.Where('UserName = ?',[aUserName]).SelectFirst;
end;
function TEntityIdentityStore<TUser, TRole>.FindByEmail(const aEmail: string): TUser;
begin
Result := fDBContext.Users.Where('Email = ?',[aEmail]).SelectFirst;
end;
function TEntityIdentityStore<TUser, TRole>.GetPasswordHash(aUser: TUser): string;
var
ctx : TRttiContext;
prop : TRttiProperty;
begin
Result := '';
prop := ctx.GetType(TObject(aUser).ClassType).GetProperty('PasswordHash');
if prop <> nil then Result := prop.GetValue(TObject(aUser)).AsString;
end;
function TEntityIdentityStore<TUser, TRole>.SetPasswordHash(aUser: TUser; const aHash: string): Boolean;
var
ctx : TRttiContext;
prop : TRttiProperty;
begin
Result := False;
prop := ctx.GetType(TObject(aUser).ClassType).GetProperty('PasswordHash');
if prop <> nil then begin prop.SetValue(TObject(aUser), aHash); Result := True; end;
end;
function TEntityIdentityStore<TUser, TRole>.GetSecurityStamp(aUser: TUser): string;
var
ctx : TRttiContext;
prop : TRttiProperty;
begin
Result := '';
prop := ctx.GetType(TObject(aUser).ClassType).GetProperty('SecurityStamp');
if prop <> nil then Result := prop.GetValue(TObject(aUser)).AsString;
end;
function TEntityIdentityStore<TUser, TRole>.SetSecurityStamp(aUser: TUser; const aStamp: string): Boolean;
var
ctx : TRttiContext;
prop : TRttiProperty;
begin
Result := False;
prop := ctx.GetType(TObject(aUser).ClassType).GetProperty('SecurityStamp');
if prop <> nil then begin prop.SetValue(TObject(aUser), aStamp); Result := True; end;
end;
function TEntityIdentityStore<TUser, TRole>.IsLockedOut(aUser: TUser): Boolean;
var
ctx : TRttiContext;
prop : TRttiProperty;
lockoutEnd : TDateTime;
begin
Result := False;
prop := ctx.GetType(TObject(aUser).ClassType).GetProperty('LockoutEnd');
if prop <> nil then begin
lockoutEnd := prop.GetValue(TObject(aUser)).AsExtended;
Result := (lockoutEnd > 0) and (lockoutEnd > Now);
end;
end;
function TEntityIdentityStore<TUser, TRole>.SetLockoutEnd(aUser: TUser; aEnd: TDateTime): Boolean;
var
ctx : TRttiContext;
prop : TRttiProperty;
begin
Result := False;
prop := ctx.GetType(TObject(aUser).ClassType).GetProperty('LockoutEnd');
if prop <> nil then begin prop.SetValue(TObject(aUser), TValue.From<TDateTime>(aEnd)); Result := True; end;
end;
function TEntityIdentityStore<TUser, TRole>.GetRolesForUser(aUser: TUser): TArray<string>;
begin
raise ENotImplemented.Create('GetRolesForUser not implemented in TEntityIdentityStore');
end;
function TEntityIdentityStore<TUser, TRole>.AddUserToRole(aUser: TUser; const aRoleName: string): Boolean;
begin
raise ENotImplemented.Create('AddUserToRole not implemented in TEntityIdentityStore');
end;
function TEntityIdentityStore<TUser, TRole>.RemoveUserFromRole(aUser: TUser; const aRoleName: string): Boolean;
begin
raise ENotImplemented.Create('RemoveUserFromRole not implemented in TEntityIdentityStore');
end;
{ TEntityStoreLinq<T> }
constructor TEntityStoreLinq<T>.Create(aLinq : IEntityLinqQuery<T>);
begin
fLinq := aLinq;
end;
function TEntityStoreLinq<T>.Count: Integer;
begin
Result := fLinq.Count;
end;
function TEntityStoreLinq<T>.Delete: Boolean;
begin
Result := fLinq.Delete;
end;
function TEntityStoreLinq<T>.OrderBy(const aFieldNames: string): ILinq<T>;
begin
Result := Self;
fLinq.OrderBy(aFieldNames);
end;
function TEntityStoreLinq<T>.OrderByDescending(const aFieldNames: string): ILinq<T>;
begin
Result := Self;
fLinq.OrderByDescending(aFieldNames);
end;
function TEntityStoreLinq<T>.Select(const aPropertyName: string): IList<TFlexValue>;
begin
raise ENotImplemented.Create('Feature not implemented yet!');
end;
function TEntityStoreLinq<T>.Select: IList<T>;
begin
Result := TXList<T>.Create;
Result.FromArray(fLinq.Select.ToArray);
end;
function TEntityStoreLinq<T>.SelectFirst: T;
begin
Result := fLinq.SelectFirst;
end;
function TEntityStoreLinq<T>.SelectLast: T;
begin
Result := fLinq.SelectLast;
end;
function TEntityStoreLinq<T>.SelectTop(aLimit: Integer): IList<T>;
begin
Result := TXList<T>.Create;
Result.FromArray(fLinq.SelectTop(aLimit).ToArray);
end;
function TEntityStoreLinq<T>.Update(const aFields: TArray<string>; aValues: array of const): Boolean;
begin
Result := fLinq.Update(CommaText(aFields),aValues);
end;
function TEntityStoreLinq<T>.Where(aPredicate: TPredicate<T>): ILinq<T>;
begin
raise ENotImplemented.Create('Feature not implemented yet!');
end;
function TEntityStoreLinq<T>.Where(const aWhereClause: string): ILinq<T>;
begin
Result := Self;
fLinq.Where(aWhereClause);
end;
{$IFDEF VALUE_FORMATPARAMS}
function TEntityStoreLinq<T>.Where(const aWhereClause: string; aWhereValues: array of TValue): ILinq<T>;
{$ELSE}
function TEntityStoreLinq<T>.Where(const aWhereClause: string; aWhereValues: array of const): ILinq<T>;
{$ENDIF}
begin
Result := Self;
fLinq.Where(aWhereClause,aWhereValues);
end;
end.