Skip to content

Commit 8b08b77

Browse files
committed
Implement SQL create and get role access.
1 parent c44f6b4 commit 8b08b77

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

src/DynamicAuthorization.Mvc.MsSqlServerStore/RoleAccessStore.cs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,51 @@
11
using DynamicAuthorization.Mvc.Core;
2+
using Microsoft.Data.SqlClient;
3+
using Microsoft.Extensions.Logging;
4+
using Newtonsoft.Json;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Data;
28
using System.Threading.Tasks;
39

410
namespace DynamicAuthorization.Mvc.MsSqlServerStore
511
{
612
public class RoleAccessStore : IRoleAccessStore
713
{
8-
public Task<bool> AddRoleAccessAsync(RoleAccess roleAccess)
14+
private readonly SqlOptions _options;
15+
private readonly ILogger<RoleAccessStore> _logger;
16+
17+
public RoleAccessStore(SqlOptions options, ILogger<RoleAccessStore> logger)
918
{
10-
throw new System.NotImplementedException();
19+
_options = options;
20+
_logger = logger;
21+
}
22+
23+
public async Task<bool> AddRoleAccessAsync(RoleAccess roleAccess)
24+
{
25+
try
26+
{
27+
using (var conn = new SqlConnection(_options.ConnectionString))
28+
{
29+
const string insertCommand = "INSERT INTO RoleAccess VALUES(@RoleId, @Access)";
30+
using (var cmd = new SqlCommand(insertCommand, conn))
31+
{
32+
cmd.CommandType = CommandType.Text;
33+
cmd.Parameters.AddWithValue("@RoleId", roleAccess.RoleId);
34+
35+
var access = JsonConvert.SerializeObject(roleAccess.Controllers);
36+
cmd.Parameters.AddWithValue("@Access", access);
37+
38+
conn.Open();
39+
var affectedRows = await cmd.ExecuteNonQueryAsync();
40+
return affectedRows > 0;
41+
}
42+
}
43+
}
44+
catch (Exception ex)
45+
{
46+
_logger.LogError(ex, "An error has occurred while inserting access into RoleAccess table");
47+
return false;
48+
}
1149
}
1250

1351
public Task<bool> EditRoleAccessAsync(RoleAccess roleAccess)
@@ -20,9 +58,37 @@ public Task<bool> RemoveRoleAccessAsync(string roleId)
2058
throw new System.NotImplementedException();
2159
}
2260

23-
public Task<RoleAccess> GetRoleAccessAsync(string roleId)
61+
public async Task<RoleAccess> GetRoleAccessAsync(string roleId)
2462
{
25-
throw new System.NotImplementedException();
63+
try
64+
{
65+
using (var conn = new SqlConnection(_options.ConnectionString))
66+
{
67+
const string query = "SELECT [Id], [RoleId], [Access] FROM [RoleAccess] WHERE [RoleId] = @RoleId";
68+
using (var cmd = new SqlCommand(query, conn))
69+
{
70+
cmd.CommandType = CommandType.Text;
71+
cmd.Parameters.AddWithValue("@RoleId", roleId);
72+
conn.Open();
73+
var reader = await cmd.ExecuteReaderAsync();
74+
if (!reader.Read())
75+
return null;
76+
77+
var roleAccess = new RoleAccess();
78+
roleAccess.Id = int.Parse(reader[0].ToString());
79+
roleAccess.RoleId = reader[1].ToString();
80+
var json = reader[2].ToString();
81+
roleAccess.Controllers = JsonConvert.DeserializeObject<IEnumerable<MvcControllerInfo>>(json);
82+
83+
return roleAccess;
84+
}
85+
}
86+
}
87+
catch (Exception ex)
88+
{
89+
_logger.LogError(ex, "An error has occurred while inserting access into RoleAccess table");
90+
return null;
91+
}
2692
}
2793

2894
public Task<bool> HasAccessToActionAsync(string actionId, params string[] roles)

src/DynamicAuthorization.Mvc.MsSqlServerStore/SqlOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
public class SqlOptions
44
{
55
public string ConnectionString { get; set; }
6+
7+
internal bool IsTableCreated { get; set; }
68
}
79
}

src/DynamicAuthorization.Mvc.MsSqlServerStore/SqlTableCreator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void CreateTable()
2828
{
2929
conn.Open();
3030
cmd.ExecuteNonQuery();
31+
_options.IsTableCreated = true;
3132
}
3233
}
3334
}
@@ -62,10 +63,10 @@ private string GetTableDdl()
6263

6364
private string GetRoleIdType()
6465
{
65-
if (DynamicAuthorizationOptions.RoleType == typeof(Guid))
66+
if (DynamicAuthorizationOptions.KeyType == typeof(Guid))
6667
return "uniqueidentifier";
6768

68-
if (DynamicAuthorizationOptions.RoleType == typeof(string))
69+
if (DynamicAuthorizationOptions.KeyType == typeof(string))
6970
return "nvarchar(450) COLLATE SQL_Latin1_General_CP1_CI_AS";
7071

7172
return "int";

0 commit comments

Comments
 (0)