-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserRoleTable.cs
72 lines (63 loc) · 2.45 KB
/
UserRoleTable.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
using System.Collections.Generic;
namespace AspNet.Identity.MySQL
{
/// <summary>
/// Class that represents the UserRoles table in the MySQL Database
/// </summary>
public class UserRolesTable
{
private MySQLDatabase _database;
/// <summary>
/// Constructor that takes a MySQLDatabase instance
/// </summary>
/// <param name="database"></param>
public UserRolesTable(MySQLDatabase database)
{
_database = database;
}
/// <summary>
/// Returns a list of user's roles
/// </summary>
/// <param name="userId">The user's id</param>
/// <returns></returns>
public List<string> FindByUserId(string userId)
{
List<string> roles = new List<string>();
string commandText = "Select Roles.Name from UserRoles, Roles where UserRoles.UserId = @userId and UserRoles.RoleId = Roles.Id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@userId", userId);
var rows = _database.Query(commandText, parameters);
foreach(var row in rows)
{
roles.Add(row["Name"]);
}
return roles;
}
/// <summary>
/// Deletes all roles from a user in the UserRoles table
/// </summary>
/// <param name="userId">The user's id</param>
/// <returns></returns>
public int Delete(string userId)
{
string commandText = "Delete from UserRoles where UserId = @userId";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("UserId", userId);
return _database.Execute(commandText, parameters);
}
/// <summary>
/// Inserts a new role for a user in the UserRoles table
/// </summary>
/// <param name="user">The User</param>
/// <param name="roleId">The Role's id</param>
/// <returns></returns>
public int Insert(IdentityUser user, string roleId)
{
string commandText = "Insert into UserRoles (UserId, RoleId) values (@userId, @roleId)";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("userId", user.Id);
parameters.Add("roleId", roleId);
return _database.Execute(commandText, parameters);
}
}
}