Skip to content

Commit 17784aa

Browse files
committed
Impelement edit, delete and has access for SQL provider.
1 parent 8b08b77 commit 17784aa

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

src/DynamicAuthorization.Mvc.MsSqlServerStore/DynamicAuthorization.Mvc.MsSqlServerStore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Dapper" Version="2.0.30" />
98
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.1" />
9+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

src/DynamicAuthorization.Mvc.MsSqlServerStore/RoleAccessStore.cs

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Data;
8+
using System.Linq;
89
using System.Threading.Tasks;
910

1011
namespace DynamicAuthorization.Mvc.MsSqlServerStore
@@ -48,14 +49,67 @@ public async Task<bool> AddRoleAccessAsync(RoleAccess roleAccess)
4849
}
4950
}
5051

51-
public Task<bool> EditRoleAccessAsync(RoleAccess roleAccess)
52+
public async Task<bool> EditRoleAccessAsync(RoleAccess roleAccess)
5253
{
53-
throw new System.NotImplementedException();
54+
try
55+
{
56+
int affectedRows;
57+
using (var conn = new SqlConnection(_options.ConnectionString))
58+
{
59+
const string insertCommand = "UPDATE RoleAccess SET [Access] = @Access WHERE [RoleId] = @RoleId";
60+
using (var cmd = new SqlCommand(insertCommand, conn))
61+
{
62+
cmd.CommandType = CommandType.Text;
63+
cmd.Parameters.AddWithValue("@RoleId", roleAccess.RoleId);
64+
if (roleAccess.Controllers != null)
65+
{
66+
var access = JsonConvert.SerializeObject(roleAccess.Controllers);
67+
cmd.Parameters.AddWithValue("@Access", access);
68+
}
69+
else
70+
cmd.Parameters.AddWithValue("@Access", DBNull.Value);
71+
72+
conn.Open();
73+
affectedRows = await cmd.ExecuteNonQueryAsync();
74+
}
75+
}
76+
77+
if (affectedRows > 0)
78+
return true;
79+
80+
return await AddRoleAccessAsync(roleAccess);
81+
}
82+
catch (Exception ex)
83+
{
84+
_logger.LogError(ex, "An error has occurred while editing access into RoleAccess table");
85+
return false;
86+
}
5487
}
5588

56-
public Task<bool> RemoveRoleAccessAsync(string roleId)
89+
public async Task<bool> RemoveRoleAccessAsync(string roleId)
5790
{
58-
throw new System.NotImplementedException();
91+
try
92+
{
93+
using (var conn = new SqlConnection(_options.ConnectionString))
94+
{
95+
const string insertCommand = "DELETE FROM RoleAccess WHERE [RoleId] = @RoleId";
96+
using (var cmd = new SqlCommand(insertCommand, conn))
97+
{
98+
cmd.CommandType = CommandType.Text;
99+
cmd.Parameters.AddWithValue("@RoleId", roleId);
100+
101+
conn.Open();
102+
var affectedRows = await cmd.ExecuteNonQueryAsync();
103+
104+
return affectedRows > 0;
105+
}
106+
}
107+
}
108+
catch (Exception ex)
109+
{
110+
_logger.LogError(ex, "An error has occurred while deleting access from RoleAccess table");
111+
return false;
112+
}
59113
}
60114

61115
public async Task<RoleAccess> GetRoleAccessAsync(string roleId)
@@ -86,14 +140,54 @@ public async Task<RoleAccess> GetRoleAccessAsync(string roleId)
86140
}
87141
catch (Exception ex)
88142
{
89-
_logger.LogError(ex, "An error has occurred while inserting access into RoleAccess table");
143+
_logger.LogError(ex, "An error has occurred while getting data from RoleAccess table");
90144
return null;
91145
}
92146
}
93147

94-
public Task<bool> HasAccessToActionAsync(string actionId, params string[] roles)
148+
public async Task<bool> HasAccessToActionAsync(string actionId, params string[] roles)
95149
{
96-
throw new System.NotImplementedException();
150+
try
151+
{
152+
using (var conn = new SqlConnection(_options.ConnectionString))
153+
{
154+
using (var cmd = new SqlCommand())
155+
{
156+
var parameters = new string[roles.Length];
157+
for (var i = 0; i < roles.Length; i++)
158+
{
159+
parameters[i] = $"@RoleId{i}";
160+
cmd.Parameters.AddWithValue(parameters[i], roles[i]);
161+
}
162+
var query = $"SELECT [Access] FROM [RoleAccess] WHERE [RoleId] IN ({string.Join(", ", parameters)})";
163+
164+
cmd.CommandType = CommandType.Text;
165+
cmd.CommandText = query;
166+
cmd.Connection = conn;
167+
168+
conn.Open();
169+
var reader = await cmd.ExecuteReaderAsync();
170+
171+
var list = new List<MvcActionInfo>();
172+
while (reader.Read())
173+
{
174+
var json = reader[0].ToString();
175+
if (string.IsNullOrEmpty(json))
176+
continue;
177+
178+
var controllers = JsonConvert.DeserializeObject<IEnumerable<MvcControllerInfo>>(json);
179+
list.AddRange(controllers.SelectMany(c => c.Actions));
180+
}
181+
182+
return list.Any(a => a.Id == actionId);
183+
}
184+
}
185+
}
186+
catch (Exception ex)
187+
{
188+
_logger.LogError(ex, "An error has occurred while getting data from RoleAccess table");
189+
return false;
190+
}
97191
}
98192
}
99193
}

0 commit comments

Comments
 (0)