Add user group filter endpoint (#16087)
This commit is contained in:
@@ -62,6 +62,19 @@ public interface IUserGroupService
|
||||
|
||||
Task<IEnumerable<IUserGroup>> GetAsync(IEnumerable<Guid> keys);
|
||||
|
||||
/// <summary>
|
||||
/// Performs filtering for user groups
|
||||
/// </summary>
|
||||
/// <param name="userKey">The key of the performing (current) user.</param>
|
||||
/// <param name="filter">The filter to apply.</param>
|
||||
/// <param name="skip">The amount of user groups to skip.</param>
|
||||
/// <param name="take">The amount of user groups to take.</param>
|
||||
/// <returns>All matching user groups as an enumerable list of <see cref="IUserGroup"/>.</returns>
|
||||
/// <remarks>
|
||||
/// If the performing user is not an administrator, this method only returns groups that the performing user is a member of.
|
||||
/// </remarks>
|
||||
Task<Attempt<PagedModel<IUserGroup>, UserGroupOperationStatus>> FilterAsync(Guid userKey, string? filter, int skip, int take);
|
||||
|
||||
/// <summary>
|
||||
/// Persists a new user group.
|
||||
/// </summary>
|
||||
|
||||
@@ -134,6 +134,35 @@ internal sealed class UserGroupService : RepositoryService, IUserGroupService
|
||||
return Task.FromResult<IEnumerable<IUserGroup>>(result);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Attempt<PagedModel<IUserGroup>, UserGroupOperationStatus>> FilterAsync(Guid userKey, string? filter, int skip, int take)
|
||||
{
|
||||
IUser? requestingUser = await _userService.GetAsync(userKey);
|
||||
if (requestingUser is null)
|
||||
{
|
||||
return Attempt.FailWithStatus(UserGroupOperationStatus.MissingUser, new PagedModel<IUserGroup>());
|
||||
}
|
||||
|
||||
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
|
||||
var groups = _userGroupRepository
|
||||
.GetMany()
|
||||
.Where(group => filter.IsNullOrWhiteSpace() || group.Name?.InvariantContains(filter) is true)
|
||||
.OrderBy(group => group.Name)
|
||||
.ToList();
|
||||
|
||||
if (requestingUser.IsAdmin() is false)
|
||||
{
|
||||
var requestingUserGroups = requestingUser.Groups.Select(group => group.Alias).ToArray();
|
||||
groups.RemoveAll(group =>
|
||||
group.Alias is Constants.Security.AdminGroupAlias
|
||||
|| requestingUserGroups.Contains(group.Alias) is false);
|
||||
}
|
||||
|
||||
return Attempt.SucceedWithStatus(
|
||||
UserGroupOperationStatus.Success,
|
||||
new PagedModel<IUserGroup> { Items = groups.Skip(skip).Take(take), Total = groups.Count });
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Attempt<UserGroupOperationStatus>> DeleteAsync(ISet<Guid> keys)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user