forked from raquelsa/AspNet.Identity.MySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserClaimsTable.cs
93 lines (81 loc) · 3.44 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
92
93
using System.Collections.Generic;
using System.Data.Common;
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 claimsIdentity = new ClaimsIdentity();
string commandText = "Select * from userclaims where UserId = @UserId";
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@UserId", userId } };
var claims = _database.ExecuteReader(commandText, parameters, this.ReadClaim);
claimsIdentity.AddClaims(claims);
return claimsIdentity;
}
private Claim ReadClaim(DbDataReader dbReader)
{
return new Claim(dbReader.GetString("ClaimType"), dbReader.GetString("ClaimValue"));
}
/// <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);
}
}
}