using System; using System.Linq; using xModels.Dtos; using System.Threading; using xCommons.Extensions; using xIdentityModels.Dtos; using xExceptions.Constants; using System.Threading.Tasks; using xIdentityModels.Models; using System.Collections.Generic; namespace xIdentityService.Providers { public partial class XAuthorizationProvider { // #region Scope ... public async Task> QueryScopesAsync( XQuery query, CancellationToken cancellationToken = default ) { // if (query.IsNull()) { XException.InvalidArgs.Throw(); } // var result = await scopes.QueryAsync( query: query, cancellationToken: cancellationToken ); // return result; } public async Task GetScopeAsync( string name, CancellationToken cancellationToken = default ) { // var result = await FindScopeOrThrowAsync(name, cancellationToken); // return result; } public async Task AddScopeAsync( XScopeDefinitionDto 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 scopes.FindOneAsync( cancellationToken: cancellationToken, predicate: s => s.Name.ToLower() == normalized ); if (!exists.IsNull()) { XException.Duplicate.Throw(); } // // Persist ... item.IsSystem = false; var result = await scopes.AddAsync( item: item, userInfo: userInfo, cancellationToken: cancellationToken ); // // Sync to IdentityServer (ConfigurationDbContext) ... var entity = scopes.AsQueryable() .FirstOrDefault(s => s.Name.ToLower() == normalized); if (!entity.IsNull()) { // await scopeSync .UpsertAsync(entity, cancellationToken); } // await InvalidateAsync(); return result; } public async Task UpdateScopeAsync( string name, XScopeDefinitionDto item, XUserClaimsInfoDto userInfo, CancellationToken cancellationToken = default ) { // // Validate ... if (name.IsNullOrEmpty() || item.IsNull()) { XException.InvalidArgs.Throw(); } // var existing = await FindScopeOrThrowAsync(name, cancellationToken); if (existing.IsSystem) { XException.NotAllowed.Throw(); } // var updated = await scopes.UpdateAsync( id: existing.Id, item: item, userInfo: userInfo, cancellationToken: cancellationToken ); // // Sync to IdentityServer ... var entity = scopes.AsQueryable() .FirstOrDefault(s => s.Name.ToLower() == name.ToNormalString()); if (!entity.IsNull()) { await scopeSync.UpsertAsync(entity, cancellationToken); } // await InvalidateAsync(); return updated; } public async Task RemoveScopeAsync( string name, XUserClaimsInfoDto userInfo, CancellationToken cancellationToken = default ) { // var entity = await FindScopeOrThrowAsync(name, cancellationToken); if (entity.IsSystem) { XException.NotAllowed.Throw(); } // await scopes.RemoveAsync( id: entity.Id, userInfo: userInfo, cancellationToken: cancellationToken ); // // Remove from IdentityServer ... await scopeSync.RemoveAsync(name, cancellationToken); // await InvalidateAsync(); return true; } #endregion } }