-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserModel.pas
132 lines (106 loc) · 2.97 KB
/
UserModel.pas
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
unit UserModel;
interface
Uses
TypInfo, Generics.Collections, System.Classes;
type
TUserRole = (urAdmin, urUser);
type
TUser = class(TObject)
private
FId:Integer;
FLogin:String;
FUserName:String;
FPassword:String;
FRole:TUserRole;
FRoleNames: TDictionary<TUserRole, String>;
procedure initRoleNames;
public
constructor Create; overload;
constructor Create(Login, Password:String); overload;
constructor Create(id: integer; Login, Password, UserName:String; Role:TUserRole); overload;
constructor Create(id: integer; Login, Password, UserName, roleName:String); overload;
destructor Destroy; override;
property id:Integer read FId write FId;
property Login:String read FLogin write FLogin;
property Password:String read FPassword write FPassword;
property UserName:String read FUserName write FUserName;
property Role:TUserRole read FRole write FRole;
function roleToString(role: TUserRole):string;
function getRoleName(role:TUserRole):string;
function getAllRoleNames:TStringList;
procedure setRoleFromString(role:String);
end;
implementation
constructor TUser.Create;
begin
inherited;
initRoleNames;
end;
constructor TUser.Create(Login: string; Password: string);
begin
inherited Create;
initRoleNames;
self.FLogin:=login;
Self.FPassword:=Password;
end;
constructor TUser.Create(id: integer; Login: string; Password: string; UserName: string; Role: TUserRole);
begin
inherited Create;
initRoleNames;
self.FLogin:=login;
Self.FPassword:=Password;
Self.FUserName:=UserName;
Self.FRole:=Role;
Self.FId:=id;
end;
constructor TUser.Create(id: Integer; Login: string; Password: string; UserName: string; roleName: string);
begin
initRoleNames;
self.FLogin:=login;
Self.FPassword:=Password;
Self.FUserName:=UserName;
Self.setRoleFromString(roleName);
Self.FId:=id;
end;
destructor TUser.Destroy;
begin
inherited;
FRoleNames.Free;
end;
procedure TUser.initRoleNames;
begin
FRoleNames:= TDictionary<TUserRole, String>.Create;
FRoleNames.Add(urAdmin, 'Àäìèíèñòðàòîð');
FRoleNames.Add(urUser, 'Ïîëüçîâàòåëü');
end;
function TUser.roleToString(role: TUserRole):string;//Äëÿ ñîõðàíåíèÿ â ÁÄ
var i:TUserRole;
s, amp:string;
begin
{s:='';
amp:='';
for i := Low(FRoles) to High(FRoles) do begin
if(i=High(FRoles)) then amp:='' else amp:=', ';
s:=s+GetEnumName(TypeInfo(TUserRole), ord(i))+amp;
end; }
Result:=GetEnumName(TypeInfo(TUserRole), ord(role));
end;
procedure TUser.setRoleFromString(role: string);//Äëÿ çàãðóçêè èç ÁÄ
begin
self.FRole:=TUserrole(GetEnumValue(TypeInfo(TUserrole), role));
end;
function TUser.getRoleName(role: TUserRole):string;//Äëÿ îòîáðàæåíèÿ èìåí ðîëåé ïîëüçîâàòåëþ
begin
Result:=FRoleNames.ExtractPair(role).Value;
end;
function TUser.getAllRoleNames:TStringList;
var i:TUserRole;
sl:TstringList;
begin
sl:=TStringList.Create;
for i := Low(TUserRole) to High(TUserRole) do begin
sl.Add(getRoleName(i));
end;
Result:=sl;
end;
end.