Files
jube-fork/Jube.App/Code/RoleStore.cs
2022-11-04 13:24:24 +02:00

128 lines
5.1 KiB
C#

/* Copyright (C) 2022-present Jube Holdings Limited.
*
* This file is part of Jube™ software.
*
* Jube™ is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Jube™ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License along with Jube™. If not,
* see <https://www.gnu.org/licenses/>.
*/
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
#pragma warning disable 1998
namespace Jube.App.Code
{
public class RoleStore : IRoleStore<ApplicationRole>
{
public async Task<IdentityResult> CreateAsync(ApplicationRole role, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
/*using (var connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync(cancellationToken);
role.Id = await connection.QuerySingleAsync<int>($@"INSERT INTO [ApplicationRole] ([Name], [NormalizedName])
VALUES (@{nameof(ApplicationRole.Name)}, @{nameof(ApplicationRole.NormalizedName)});
SELECT CAST(SCOPE_IDENTITY() as int)", role);
}*/
return IdentityResult.Success;
}
public async Task<IdentityResult> UpdateAsync(ApplicationRole role, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
/*using (var connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync(cancellationToken);
await connection.ExecuteAsync($@"UPDATE [ApplicationRole] SET
[Name] = @{nameof(ApplicationRole.Name)},
[NormalizedName] = @{nameof(ApplicationRole.NormalizedName)}
WHERE [Id] = @{nameof(ApplicationRole.Id)}", role);
}*/
return IdentityResult.Success;
}
public async Task<IdentityResult> DeleteAsync(ApplicationRole role, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
/*using (var connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync(cancellationToken);
await connection.ExecuteAsync($"DELETE FROM [ApplicationRole] WHERE [Id] = @{nameof(ApplicationRole.Id)}", role);
}*/
return IdentityResult.Success;
}
public Task<string> GetRoleIdAsync(ApplicationRole role, CancellationToken cancellationToken)
{
return Task.FromResult(role.Id.ToString());
}
public Task<string> GetRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken)
{
return Task.FromResult(role.Name);
}
public Task SetRoleNameAsync(ApplicationRole role, string roleName, CancellationToken cancellationToken)
{
role.Name = roleName;
return Task.FromResult(0);
}
public Task<string> GetNormalizedRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken)
{
return Task.FromResult(role.NormalizedName);
}
public Task SetNormalizedRoleNameAsync(ApplicationRole role, string normalizedName,
CancellationToken cancellationToken)
{
role.NormalizedName = normalizedName;
return Task.FromResult(0);
}
public async Task<ApplicationRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
/*using (var connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync(cancellationToken);
return await connection.QuerySingleOrDefaultAsync<ApplicationRole>($@"SELECT * FROM [ApplicationRole]
WHERE [Id] = @{nameof(roleId)}", new { roleId });
}*/
return null;
}
public async Task<ApplicationRole> FindByNameAsync(string normalizedRoleName,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
/*using (var connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync(cancellationToken);
return await connection.QuerySingleOrDefaultAsync<ApplicationRole>($@"SELECT * FROM [ApplicationRole]
WHERE [NormalizedName] = @{nameof(normalizedRoleName)}", new { normalizedRoleName });
}*/
return null;
}
public void Dispose()
{
// Nothing to dispose.
}
}
}