-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoleTable.cs
163 lines (135 loc) · 4.32 KB
/
RoleTable.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
using BusinessObjects;
using System;
using System.Collections.Generic;
namespace Tiraggo.AspNet.Identity
{
/// <summary>
/// Class that represents the Role table
/// </summary>
public class RoleTable : IdentityBaseTable
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="database"></param>
public RoleTable(string connectionName = null)
{
ConnectionName = connectionName;
}
/// <summary>
/// Deltes a role from the Roles table
/// </summary>
/// <param name="roleId">The role Id</param>
/// <returns></returns>
public int Delete(string roleId)
{
try
{
ConnectionService.ThreadVanityUrl = ConnectionName;
AspNetRoles role = new AspNetRoles();
SetConnection(role);
role.Id = roleId;
role.AcceptChanges();
role.MarkAsDeleted();
role.Save();
}
catch { }
return 1;
}
/// <summary>
/// Inserts a new Role in the Roles table
/// </summary>
/// <param name="roleName">The role's name</param>
/// <returns></returns>
public int Insert(IdentityRole role)
{
ConnectionService.ThreadVanityUrl = ConnectionName;
AspNetRoles newRole = new AspNetRoles();
SetConnection(newRole);
newRole.Id = role.Id;
newRole.Name = role.Name;
newRole.Save();
return 1;
}
/// <summary>
/// Returns a role name given the roleId
/// </summary>
/// <param name="roleId">The role Id</param>
/// <returns>Role name</returns>
public string GetRoleName(string roleId)
{
ConnectionService.ThreadVanityUrl = ConnectionName;
string name = null;
AspNetRoles role = new AspNetRoles();
SetConnection(role);
if(role.LoadByPrimaryKey(roleId))
{
name = role.Name;
}
return role.Name;
}
/// <summary>
/// Returns the role Id given a role name
/// </summary>
/// <param name="roleName">Role's name</param>
/// <returns>Role's Id</returns>
public string GetRoleId(string roleName)
{
ConnectionService.ThreadVanityUrl = ConnectionName;
string roleId = null;
AspNetRolesQuery q = new AspNetRolesQuery();
q.Select(q.Id);
q.Where(q.Name == roleName);
AspNetRoles role = new AspNetRoles();
SetConnection(role);
if(role.Load(q))
{
roleId = role.Id;
}
return roleId;
}
/// <summary>
/// Gets the IdentityRole given the role Id
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public IdentityRole GetRoleById(string roleId)
{
ConnectionService.ThreadVanityUrl = ConnectionName;
var roleName = GetRoleName(roleId);
IdentityRole role = null;
if(roleName != null)
{
role = new IdentityRole(roleName, roleId);
}
return role;
}
/// <summary>
/// Gets the IdentityRole given the role name
/// </summary>
/// <param name="roleName"></param>
/// <returns></returns>
public IdentityRole GetRoleByName(string roleName)
{
ConnectionService.ThreadVanityUrl = ConnectionName;
var roleId = GetRoleId(roleName);
IdentityRole role = null;
if (roleId != null)
{
role = new IdentityRole(roleName, roleId);
}
return role;
}
public int Update(IdentityRole role)
{
ConnectionService.ThreadVanityUrl = ConnectionName;
AspNetRoles roleToUpdate = new AspNetRoles();
SetConnection(roleToUpdate);
roleToUpdate.AcceptChanges();
roleToUpdate.Id = role.Id;
roleToUpdate.Name = role.Name;
roleToUpdate.Save();
return 1;
}
}
}