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 }); } }
public async Task CreateRoleAsync(string roleName, string[] rightNames) { var role = await this.GetEntityAsync <Data.Role>(r => r.Name == roleName); if (role == null) { role = new Data.Role { Name = roleName, CreationBy = this.principalIdProvider.PrincipalId, ModificationBy = this.principalIdProvider.PrincipalId }; this.context.Set <Data.Role>().Add(role); (await SharedQueries.GetModelModificationDateAsync(this.context)).Roles = DateTime.UtcNow; } if (rightNames != null) { foreach (var rightName in rightNames) { await this.CreateRightAsync(rightName); var right = await GetEntityAsync <Data.Right>(r => r.Name == rightName); if (right == null) { throw new InvalidOperationException($"Inconsistency with right : {rightName}. Specified right does not exist."); } role.Rights.Add(new Data.RoleRight { Right = right, Role = role }); (await SharedQueries.GetModelModificationDateAsync(this.context)).Rights = DateTime.UtcNow; } } }