-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserTable.cs
209 lines (183 loc) · 7.84 KB
/
UserTable.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
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
using System.Collections.Generic;
namespace AspNet.Identity.MySQL
{
/// <summary>
/// Class that represents the Users table in the MySQL Database
/// </summary>
public class UserTable
{
private MySQLDatabase _database;
/// <summary>
/// Constructor that takes a MySQLDatabase instance
/// </summary>
/// <param name="database"></param>
public UserTable(MySQLDatabase database)
{
_database = database;
}
/// <summary>
/// Returns the user's name given a user id
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public string GetUserName(string userId)
{
string commandText = "Select Name from Users where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@id", userId } };
return _database.GetStrValue(commandText, parameters);
}
/// <summary>
/// Returns a User ID given a user name
/// </summary>
/// <param name="userName">The user's name</param>
/// <returns></returns>
public string GetUserId(string userName)
{
string commandText = "Select Id from Users where UserName = @name";
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@name", userName } };
return _database.GetStrValue(commandText, parameters);
}
/// <summary>
/// Returns an IdentityUser given the user's id
/// </summary>
/// <param name="userId">The user's id</param>
/// <returns></returns>
public IdentityUser GetUserById(string userId)
{
IdentityUser user = null;
string commandText = "Select * from Users where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@id", userId } };
var rows = _database.Query(commandText, parameters);
if (rows != null && rows.Count == 1)
{
var row = rows[0];
user = new IdentityUser();
user.Id = row["Id"];
user.UserName = row["UserName"];
user.PasswordHash = string.IsNullOrEmpty(row["PasswordHash"]) ? null : row["PasswordHash"];
user.SecurityStamp = string.IsNullOrEmpty(row["SecurityStamp"]) ? null : row["SecurityStamp"];
user.ClientId = int.Parse(row["clientid"]);
}
return user;
}
/// <summary>
/// Returns a list of IdentityUser instances given a user name
/// </summary>
/// <param name="userName">User's name</param>
/// <returns></returns>
public List<IdentityUser> GetUserByName(string userName)
{
List<IdentityUser> users = new List<IdentityUser>();
string commandText = "Select * from Users where UserName = @name";
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@name", userName } };
var rows = _database.Query(commandText, parameters);
foreach(var row in rows)
{
IdentityUser user = new IdentityUser();
user.Id = row["Id"];
user.UserName = row["UserName"];
user.PasswordHash = string.IsNullOrEmpty(row["PasswordHash"]) ? null : row["PasswordHash"];
user.SecurityStamp = string.IsNullOrEmpty(row["SecurityStamp"]) ? null : row["SecurityStamp"];
user.ClientId = int.Parse(row["clientid"]);
users.Add(user);
}
return users;
}
/// <summary>
/// Return the user's password hash
/// </summary>
/// <param name="userId">The user's id</param>
/// <returns></returns>
public string GetPasswordHash(string userId)
{
string commandText = "Select PasswordHash from Users where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@id", userId);
var passHash = _database.GetStrValue(commandText, parameters);
if(string.IsNullOrEmpty(passHash))
{
return null;
}
return passHash;
}
/// <summary>
/// Sets the user's password hash
/// </summary>
/// <param name="userId"></param>
/// <param name="passwordHash"></param>
/// <returns></returns>
public int SetPasswordHash(string userId, string passwordHash)
{
string commandText = "Update Users set PasswordHash = @pwdHash where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@pwdHash", passwordHash);
parameters.Add("@id", userId);
return _database.Execute(commandText, parameters);
}
/// <summary>
/// Returns the user's security stamp
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public string GetSecurityStamp(string userId)
{
string commandText = "Select SecurityStamp from Users where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@id", userId } };
var result = _database.GetStrValue(commandText, parameters);
return result;
}
/// <summary>
/// Inserts a new user in the Users table
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public int Insert(IdentityUser user)
{
string commandText = "Insert into Users (UserName, Id, PasswordHash, SecurityStamp, clientid) values (@name, @id, @pwdHash, @SecStamp, @clientid)";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@name", user.UserName);
parameters.Add("@id", user.Id);
parameters.Add("@pwdHash", user.PasswordHash);
parameters.Add("@SecStamp", user.SecurityStamp);
parameters.Add("@clientid", user.ClientId);
return _database.Execute(commandText, parameters);
}
/// <summary>
/// Deletes a user from the Users table
/// </summary>
/// <param name="userId">The user's id</param>
/// <returns></returns>
private int Delete(string userId)
{
string commandText = "Delete from Users where Id = @userId";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@userId", userId);
return _database.Execute(commandText, parameters);
}
/// <summary>
/// Deletes a user from the Users table
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public int Delete(IdentityUser user)
{
return Delete(user.Id);
}
/// <summary>
/// Updates a user in the Users table
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public int Update(IdentityUser user)
{
string commandText = "Update Users set UserName = @userName, clientid = @clientid, PasswordHash = @pswHash, SecurityStamp = @secStamp WHERE Id = @userId";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@userName", user.UserName);
parameters.Add("@pswHash", user.PasswordHash);
parameters.Add("@secStamp", user.SecurityStamp);
parameters.Add("@userId", user.Id);
parameters.Add("@clientid", user.ClientId);
return _database.Execute(commandText, parameters);
}
}
}