forked from raquelsa/AspNet.Identity.MySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserStore.cs
157 lines (136 loc) · 4.48 KB
/
UserStore.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
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace AspNet.Identity.MySQL
{
/// <summary>
/// Class that implements the key ASP.NET Identity user store iterfaces
/// </summary>
public partial class UserStore<TUser, TRole> :
IUserStore<TUser>,
IQueryableUserStore<TUser>
where TUser : IdentityUser
where TRole : IdentityRole
{
private UserTable<TUser> userTable;
private RoleTable<TRole> roleTable;
private UserRolesTable userRolesTable;
private UserClaimsTable userClaimsTable;
private UserLoginsTable userLoginsTable;
public MySQLDatabase Database { get; private set; }
/// <summary>
/// Default constructor that initializes a new MySQLDatabase
/// instance using the Default Connection string
/// </summary>
public UserStore()
: this(new MySQLDatabase())
{
}
/// <summary>
/// Constructor that takes a MySQLDatabase as argument
/// </summary>
/// <param name="database"></param>
public UserStore(MySQLDatabase database)
{
Database = database;
userTable = new UserTable<TUser>(database);
roleTable = new RoleTable<TRole>(database);
userRolesTable = new UserRolesTable(database);
userClaimsTable = new UserClaimsTable(database);
userLoginsTable = new UserLoginsTable(database);
}
public void Dispose()
{
if (Database != null)
{
Database.Dispose();
Database = null;
}
}
public IQueryable<TUser> Users
{
get { return userTable.GetUsers().AsQueryable(); }
}
/// <summary>
/// Insert a new TUser in the UserTable
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public Task CreateAsync(TUser user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
userTable.Insert(user);
return Task.FromResult<object>(null);
}
/// <summary>
/// Deletes a user
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public Task DeleteAsync(TUser user)
{
if (user != null)
{
userTable.Delete(user);
}
return Task.FromResult<Object>(null);
}
/// <summary>
/// Returns an TUser instance based on a userId query
/// </summary>
/// <param name="userId">The user's Id</param>
/// <returns></returns>
public Task<TUser> FindByIdAsync(string userId)
{
if (string.IsNullOrEmpty(userId))
{
throw new ArgumentException("Null or empty argument: userId");
}
TUser result = userTable.GetUserById(userId) as TUser;
if (result != null)
{
return Task.FromResult<TUser>(result);
}
return Task.FromResult<TUser>(null);
}
/// <summary>
/// Returns an TUser instance based on a userName query
/// </summary>
/// <param name="userName">The user's name</param>
/// <returns></returns>
public Task<TUser> FindByNameAsync(string userName)
{
if (string.IsNullOrEmpty(userName))
{
throw new ArgumentException("Null or empty argument: userName");
}
List<TUser> result = userTable.GetUserByName(userName) as List<TUser>;
// Should I throw if > 1 user?
if (result != null && result.Count == 1)
{
return Task.FromResult<TUser>(result[0]);
}
return Task.FromResult<TUser>(null);
}
/// <summary>
/// Updates the UsersTable with the TUser instance values
/// </summary>
/// <param name="user">TUser to be updated</param>
/// <returns></returns>
public Task UpdateAsync(TUser user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
userTable.Update(user);
return Task.FromResult<object>(null);
}
}
}