public async Task <bool> CheckAuthorizationAsync(IUser user, Guid permissionId, string resourceId = null, string contextId = null) { var userPermissions = await userPermissionResolver.GetUserPermissionsAsync(user); return(permissionAuthorizationMatcher.CheckAuthorization(userPermissions, new[] { GetPermission(permissionId, resourceId, contextId) })); }
public async Task CheckAuthorizationAsync(bool passes) { var user = Substitute.For <IUser>(); var userPermissions = new List <Permission>() { new Permission(new PermissionType(Guid.Parse("7AD9EEB0-2D74-4F68-BEE1-17C67D687EDF"), "aaa"), "res1", "ctx1") }; userPermissionResolver.GetUserPermissionsAsync(user).Returns(userPermissions); permissionAuthorizationMatcher.CheckAuthorization(userPermissions, Arg.Any <IReadOnlyCollection <Permission> >()) .Returns(ci => { var requiredPermissions = ci.ArgAt <IReadOnlyCollection <Permission> >(1); if (requiredPermissions.Count == 1 && requiredPermissions.First().PermissionType.Equals(permissionTypeRegistry.GetPermissionTypeById( Guid.Parse("0F292EFD-792E-48EC-93DF-CD99EEDB5885"))) && requiredPermissions.First().ContextId == "context_id" && requiredPermissions.First().ResourceId == "resource_id") { return(passes); } throw new ArgumentException(); }); bool result = await sut.CheckAuthorizationAsync(user, "0F292EFD-792E-48EC-93DF-CD99EEDB5885", "resource_id", "context_id"); result.Should().Be(passes); }
public async Task <IReadOnlyCollection <Permission> > GetPermissionsAsync() { if (userPermissions == null) { if (IsAuthenticated) { IIdentityUser user = await GetUserInternalAsync(); userPermissions = await userPermissionResolver.GetUserPermissionsAsync(user.User); } else { userPermissions = new List <Permission>(); } } return(userPermissions); }