-
Notifications
You must be signed in to change notification settings - Fork 33
/
UserClaimsTable.cs
91 lines (80 loc) · 3.33 KB
/
UserClaimsTable.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
using System.Collections.Generic;
using System.Security.Claims;
namespace AspNet.Identity.MySQL
{
/// <summary>
/// Class that represents the UserClaims table in the MySQL Database
/// </summary>
public class UserClaimsTable
{
private MySQLDatabase _database;
/// <summary>
/// Constructor that takes a MySQLDatabase instance
/// </summary>
/// <param name="database"></param>
public UserClaimsTable(MySQLDatabase database)
{
_database = database;
}
/// <summary>
/// Returns a ClaimsIdentity instance given a userId
/// </summary>
/// <param name="userId">The user's id</param>
/// <returns></returns>
public ClaimsIdentity FindByUserId(string userId)
{
ClaimsIdentity claims = new ClaimsIdentity();
string commandText = "Select * from UserClaims where UserId = @userId";
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@UserId", userId } };
var rows = _database.Query(commandText, parameters);
foreach (var row in rows)
{
Claim claim = new Claim(row["ClaimType"], row["ClaimValue"]);
claims.AddClaim(claim);
}
return claims;
}
/// <summary>
/// Deletes all claims from a user given a userId
/// </summary>
/// <param name="userId">The user's id</param>
/// <returns></returns>
public int Delete(string userId)
{
string commandText = "Delete from UserClaims where UserId = @userId";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("userId", userId);
return _database.Execute(commandText, parameters);
}
/// <summary>
/// Inserts a new claim in UserClaims table
/// </summary>
/// <param name="userClaim">User's claim to be added</param>
/// <param name="userId">User's id</param>
/// <returns></returns>
public int Insert(Claim userClaim, string userId)
{
string commandText = "Insert into UserClaims (ClaimValue, ClaimType, UserId) values (@value, @type, @userId)";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("value", userClaim.Value);
parameters.Add("type", userClaim.Type);
parameters.Add("userId", userId);
return _database.Execute(commandText, parameters);
}
/// <summary>
/// Deletes a claim from a user
/// </summary>
/// <param name="user">The user to have a claim deleted</param>
/// <param name="claim">A claim to be deleted from user</param>
/// <returns></returns>
public int Delete(IdentityUser user, Claim claim)
{
string commandText = "Delete from UserClaims where UserId = @userId and @ClaimValue = @value and ClaimType = @type";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("userId", user.Id);
parameters.Add("value", claim.Value);
parameters.Add("type", claim.Type);
return _database.Execute(commandText, parameters);
}
}
}