Path: redb/redb.Core/Providers/Base/RoleProviderBase.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Providers/Base/RoleProviderBase.cs
----
#pragma warning disable CS0618 // implements or uses the deprecated object permissions and roles (RedbDeprecation)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using redb.Core.Data;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
using redb.Core.Models.Roles;
using redb.Core.Query;
using Microsoft.Extensions.Logging;
namespace redb.Core.Providers.Base;
///
/// Base implementation of role provider with common business logic.
/// Database-specific providers inherit from this class and provide ISqlDialect.
///
/// Usage:
/// public class PostgresRoleProvider : RoleProviderBase
/// {
/// public PostgresRoleProvider(IRedbContext context, IRedbSecurityContext security)
/// : base(context, security, new PostgreSqlDialect()) { }
/// }
///
public abstract class RoleProviderBase : IRoleProvider
{
protected readonly IRedbContext Context;
protected readonly IRedbSecurityContext SecurityContext;
protected readonly ISqlDialect Sql;
protected readonly ILogger? Logger;
protected RoleProviderBase(
IRedbContext context,
IRedbSecurityContext securityContext,
ISqlDialect sql,
ILogger? logger = null)
{
Context = context ?? throw new ArgumentNullException(nameof(context));
SecurityContext = securityContext ?? throw new ArgumentNullException(nameof(securityContext));
Sql = sql ?? throw new ArgumentNullException(nameof(sql));
Logger = logger;
}
// ============================================================
// === CRUD ROLES ===
// ============================================================
///
public virtual async Task CreateRoleAsync(CreateRoleRequest request, IRedbUser? currentUser = null, CancellationToken cancellationToken = default)
{
ValidateRoleName(request.Name);
// Check name uniqueness
var existingRole = await GetRoleByNameInternalAsync(request.Name);
if (existingRole != null)
throw new InvalidOperationException($"Role with name '{request.Name}' already exists");
// Create new role
var newRoleId = await Context.NextObjectIdAsync();
await InsertRoleInternalAsync(newRoleId, request.Name);
var newRole = new RedbRole
{
Id = newRoleId,
Name = request.Name
};
// Assign users to role if specified
if (request.UserLogins?.Length > 0)
{
await AssignUsersByLoginsAsync(newRole, request.UserLogins);
}
else if (request.Users?.Length > 0)
{
await AssignUsersByObjectsAsync(newRole, request.Users);
}
// Lifecycle hook for derived classes
await OnRoleCreatedAsync(newRole, currentUser);
return newRole;
}
///
public virtual async Task UpdateRoleAsync(IRedbRole role, string newName, IRedbUser? currentUser = null, CancellationToken cancellationToken = default)
{
ValidateRoleName(newName);
var dbRole = await GetRoleByIdInternalAsync(role.Id);
if (dbRole == null)
throw new ArgumentException($"Role with ID {role.Id} not found");
// Check name uniqueness if changed
if (dbRole.Name != newName)
{
var existingRole = await Context.ExecuteScalarAsync(
Sql.Roles_ExistsByNameExcluding(), new object[] { newName, role.Id }, cancellationToken);
if (existingRole.HasValue)
throw new InvalidOperationException($"Role with name '{newName}' already exists");
await Context.ExecuteAsync(Sql.Roles_UpdateName(), new object[] { newName, role.Id }, cancellationToken);
dbRole.Name = newName;
}
await OnRoleUpdatedAsync(dbRole, currentUser);
return dbRole;
}
///
public virtual async Task DeleteRoleAsync(IRedbRole role, IRedbUser? currentUser = null, CancellationToken cancellationToken = default)
{
var dbRole = await GetRoleByIdInternalAsync(role.Id);
if (dbRole == null)
return false;
// Cascade delete user-role associations
await Context.ExecuteAsync(Sql.UsersRoles_DeleteByRole(), new object[] { role.Id }, cancellationToken);
// Cascade delete role permissions
await Context.ExecuteAsync(Sql.Permissions_DeleteByRole(), new object[] { role.Id }, cancellationToken);
// Delete role itself
var result = await Context.ExecuteAsync(Sql.Roles_Delete(), new object[] { role.Id }, cancellationToken);
if (result > 0)
await OnRoleDeletedAsync(role, currentUser);
return result > 0;
}
// ============================================================
// === SEARCH ROLES ===
// ============================================================
///
public virtual async Task GetRoleByIdAsync(long roleId, CancellationToken cancellationToken = default)
{
return await GetRoleByIdInternalAsync(roleId);
}
///
public virtual async Task GetRoleByNameAsync(string roleName, CancellationToken cancellationToken = default)
{
return await GetRoleByNameInternalAsync(roleName);
}
///
public virtual async Task LoadRoleAsync(long roleId, CancellationToken cancellationToken = default)
{
var role = await GetRoleByIdAsync(roleId);
if (role == null)
throw new ArgumentException($"Role with ID {roleId} not found");
return role;
}
///
public virtual async Task LoadRoleAsync(string roleName, CancellationToken cancellationToken = default)
{
var role = await GetRoleByNameAsync(roleName);
if (role == null)
throw new ArgumentException($"Role with name '{roleName}' not found");
return role;
}
///
public virtual async Task> GetRolesAsync(CancellationToken cancellationToken = default)
{
var roles = await Context.QueryAsync(Sql.Roles_SelectAll(), System.Array.Empty