Exemplo n.º 1
0
        /// <inheritdoc/>
        public override async Task RemoveFromRoleAsync(TUser user, string normalizedRoleName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            user.ThrowIfNull(nameof(user));
            if (string.IsNullOrEmpty(normalizedRoleName))
            {
                throw new ArgumentException(nameof(normalizedRoleName));
            }
            var roleEntity = await FindRoleAsync(normalizedRoleName, cancellationToken);

            if (roleEntity != null)
            {
                var userRoles = (await UserRolesTable.GetRolesAsync(user.Id))?.Select(x => new TUserRole {
                    UserId = user.Id,
                    RoleId = x.Id
                }).ToList();
                UserRoles = userRoles;
                var userRole = await FindUserRoleAsync(user.Id, roleEntity.Id, cancellationToken);

                if (userRole != null)
                {
                    UserRoles.Remove(userRole);
                }
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public override async Task <IList <string> > GetRolesAsync(TUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            user.ThrowIfNull(nameof(user));
            var userRoles = await UserRolesTable.GetRolesAsync(user.Id);

            return(userRoles.Select(x => x.Name).ToList());
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public override async Task AddToRoleAsync(TUser user, string normalizedRoleName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            user.ThrowIfNull(nameof(user));
            if (string.IsNullOrEmpty(normalizedRoleName))
            {
                throw new ArgumentException($"Parameter {nameof(normalizedRoleName)} cannot be null or empty.");
            }
            var roleEntity = await FindRoleAsync(normalizedRoleName, cancellationToken);

            if (roleEntity == null)
            {
                throw new InvalidOperationException($"Role '{normalizedRoleName}' was not found.");
            }
            var userRoles = (await UserRolesTable.GetRolesAsync(user.Id))?.Select(x => new TUserRole {
                UserId = user.Id,
                RoleId = x.Id
            }).ToList();

            UserRoles = userRoles;
            UserRoles.Add(CreateUserRole(user, roleEntity));
        }