public async Task UnaffectRoleFromPrincipalOnScopeAsync(string roleName, Guid principalId, string scopeName) { Data.Role role = await this.GetEntityAsync <Data.Role>(r => r.Name == roleName); if (role != null) { Data.Scope scope = await this.GetEntityAsync <Data.Scope>(s => s.Name == scopeName); if (scope != null) { await this.UnaffectFromPrincipalAsync(principalId, role.Id, scope.Id); } } }
public async Task AffectRoleToPrincipalOnScopeAsync(string roleName, Guid principalId, string scopeName) { Data.Role role = await this.GetEntityAsync <Data.Role>(r => r.Name == roleName); if (role == null) { throw new EntityNotFoundException(roleName); } Data.Scope scope = await this.GetEntityAsync <Data.Scope>(s => s.Name == scopeName); if (scope == null) { throw new EntityNotFoundException(scopeName); } Data.Principal principal = await this.GetEntityAsync <Data.Principal>(s => s.Id == principalId); if (principal == null) { throw new EntityNotFoundException($"Principal '{principalId}'"); } var localAuthorization = context.ChangeTracker.Entries <Data.Authorization>() .FirstOrDefault(e => e.Entity.RoleId == role.Id && e.Entity.ScopeId == scope.Id && e.Entity.PrincipalId == principalId); var authorization = await this.context.Set <Data.Authorization>() .FirstOrDefaultAsync(a => a.PrincipalId == principalId && a.RoleId == role.Id && a.ScopeId == scope.Id); if (localAuthorization != null) { localAuthorization.State = authorization == null ? EntityState.Added : EntityState.Unchanged; } else if (authorization == null) { this.context.Set <Data.Authorization>().Add(new Data.Authorization { Role = role, Scope = scope, Principal = principal, CreationBy = this.principalIdProvider.PrincipalId, ModificationBy = this.principalIdProvider.PrincipalId }); } }
private async Task DeleteScopeAsync(Data.Scope scope) { if (scope != null) { var childrenScopes = await this.context.Set <Data.Scope>() .Join( this.context.Set <Data.ScopeHierarchy>(), s => s.Name, sh => sh.Parent.Name, (s, sh) => new { Scope = s, ScopeHierarchy = sh }) .Where(r => r.ScopeHierarchy.ParentId == scope.Id) .Select(r => r.ScopeHierarchy.Child) .ToListAsync(); foreach (var childrenScope in childrenScopes) { await DeleteScopeAsync(childrenScope); } this.context .ScopeHierarchies() .RemoveRange(await this.context.ScopeHierarchies() .Where(sh => sh.ParentId == scope.Id) .ToListAsync()); this.context .ScopeHierarchies() .RemoveRange(await this.context.ScopeHierarchies() .Where(sh => sh.ChildId == scope.Id) .ToListAsync()); this.context .Authorizations() .RemoveRange(await this.context.Authorizations() .Where(a => a.ScopeId == scope.Id) .ToListAsync()); this.context.Scopes().Remove(scope); (await SharedQueries.GetModelModificationDateAsync(this.context)).Scopes = DateTime.UtcNow; } }
public async Task CreateScopeAsync(string scopeName, string description, params string[] parents) { var scope = await this.GetEntityAsync <Data.Scope>(s => s.Name == scopeName); if (scope == null) { scope = new Data.Scope { Name = scopeName, Description = description, CreationBy = this.principalIdProvider.PrincipalId, ModificationBy = this.principalIdProvider.PrincipalId }; this.context.Set <Data.Scope>().Add(scope); (await SharedQueries.GetModelModificationDateAsync(this.context)).Scopes = DateTime.UtcNow; } if (parents != null) { foreach (var parentName in parents) { await this.CreateScopeAsync(parentName, parentName); var parentScope = await this.GetEntityAsync <Data.Scope>(s => s.Name == parentName); var scopeHierarchy = await this.GetEntityAsync <Data.ScopeHierarchy>(sh => sh.ChildId == scope.Id && sh.ParentId == parentScope.Id); if (scopeHierarchy == null) { this.context.Set <Data.ScopeHierarchy>().Add(new Data.ScopeHierarchy { Child = scope, Parent = parentScope }); } } } }