فایل +Role.cs کاملاً خالی است. شش متد این منطقه باید پیادهسازی شوند. الگوی کلی: متدهای خواندنی (GetRolesAsync، GetRoleAsync) مستقیماً به IXRoleProvider سپرده میشوند؛ متدهای نوشتنی (Add، Update، Remove) ابتدا در پایگاهدادهی داخلی ذخیره میکنند و سپس با roleSync.UpsertAsync یا roleSync.RemoveAsync نقش را در IdentityServer (RoleManager<IdentityRole>) همگام میکنند؛ CountUsersInRoleAsync مستقیماً به roleSync.CountUsersInRoleAsync میرود. یک متد کمکی FindRoleOrThrowAsync نیز بهمنظور یکپارچگی با FindPolicyOrThrowAsync اضافه میشود.
// File: xIdentityService/Partial/XAuthorizationProvider+Role.cs
// (the partial shipped empty - only a `#region Role` stub)
public async Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
CancellationToken cancellationToken = default
)
{
//
return await roles.GetAllAsync(cancellationToken: cancellationToken);
}
public async Task<XRoleDefinitionDto> GetRoleAsync(
string name,
CancellationToken cancellationToken = default
)
{
//
return await FindRoleOrThrowAsync(name, cancellationToken);
}
public async Task<XRoleDefinitionDto> AddRoleAsync(
XRoleDefinitionDto item,
XUserClaimsInfoDto userInfo,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
if (item.IsNull() || item.Name.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
//
// Duplicate check ...
var normalized = item.Name.ToNormalString();
var exists = await roles.FindOneAsync(
predicate: r => r.Name.ToLower() == normalized,
cancellationToken: cancellationToken
);
if (!exists.IsNull())
{
XException.Duplicate.Throw();
}
//
// Persist ...
item.IsSystem = false;
var result = await roles.AddAsync(
item: item,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
// Sync to IdentityServer (RoleManager<IdentityRole>) ...
var entity = roles.AsQueryable()
.FirstOrDefault(r => r.Name.ToLower() == normalized);
if (!entity.IsNull())
{
await roleSync.UpsertAsync(entity, cancellationToken);
}
//
await InvalidateAsync();
return result;
}
public async Task<XRoleDefinitionDto> UpdateRoleAsync(
string name,
XRoleDefinitionDto item,
XUserClaimsInfoDto userInfo,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
if (name.IsNullOrEmpty() || item.IsNull())
{
XException.InvalidArgs.Throw();
}
//
var existing = await FindRoleOrThrowAsync(name, cancellationToken);
if (existing.IsSystem)
{
XException.NotAllowed.Throw();
}
//
var updated = await roles.UpdateAsync(
id: existing.Id,
item: item,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
// Sync to IdentityServer ...
var entity = roles.AsQueryable()
.FirstOrDefault(r => r.Name.ToLower() == name.ToNormalString());
if (!entity.IsNull())
{
await roleSync.UpsertAsync(entity, cancellationToken);
}
//
await InvalidateAsync();
return updated;
}
public async Task<bool> RemoveRoleAsync(
string name,
XUserClaimsInfoDto userInfo,
CancellationToken cancellationToken = default
)
{
//
var entity = await FindRoleOrThrowAsync(name, cancellationToken);
if (entity.IsSystem)
{
XException.NotAllowed.Throw();
}
//
await roles.RemoveAsync(
id: entity.Id,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
// Remove from IdentityServer ...
await roleSync.RemoveAsync(name, cancellationToken);
//
await InvalidateAsync();
return true;
}
public async Task<int> CountUsersInRoleAsync(
string name,
CancellationToken cancellationToken = default
)
{
//
// Fail fast if the role itself does not exist ...
await FindRoleOrThrowAsync(name, cancellationToken);
//
return await roleSync.CountUsersInRoleAsync(name, cancellationToken);
}
//
#region Private ...
private async Task<XRoleDefinitionDto> FindRoleOrThrowAsync(
string name,
CancellationToken cancellationToken
)
{
//
if (name.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
//
var normalized = name.ToNormalString();
var result = await roles.FindOneAsync(
predicate: r => r.Name.ToLower() == normalized,
cancellationToken: cancellationToken
);
if (result.IsNull())
{
XException.NotFound.Throw();
}
//
return result;
}
#endregion
نکتهی همگامسازی: چون roleSync.UpsertAsync یک موجودیت XRoleDefinition (نه DTO) میپذیرد، پس از Add/Update موجودیت معادل را با roles.AsQueryable().FirstOrDefault(...) بازیابی میکنیم و به Sync میسپاریم. این الگو از آنجا میآید که IXRoleProvider.AsQueryable مستقیماً روی DbContext پرسوجو میزند و موجودیت ردیف پایگاهداده را برمیگرداند. در صورت فعال بودن چرخهی حیات کوتاهتر، میتوان بهجای آن یک نگاشت DTO→Entity بهوسیلهی ToEntity() استفاده کرد.
✓ نتیجهی گام ۳: هر شش متد Role پیادهسازی و همگامسازی دوطرفهی نقشها با IdentityServer فعال میشود. CountUsersInRoleAsync پیش از شمارش، وجود خود نقش را با FindRoleOrThrowAsync بررسی میکند تا خطای معنادار NotFound برگردد.