Esempio n. 1
0
        public async Task AddToRoleAsync(ApplicationUser user, string roleName, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            user.ThrowIfNull(nameof(user));
            roleName.ThrowIfNull(nameof(roleName));
            var role = await _rolesTable.FindByNameAsync(roleName);

            if (role == null)
            {
                return;
            }

            user.Roles ??= (await _usersRolesTable.GetRolesAsync(user)).ToList();

            if (await IsInRoleAsync(user, roleName, cancellationToken))
            {
                return;
            }

            user.Roles.Add(new UserRole
            {
                RoleName = roleName,
                RoleId   = role.Id
            });
        }
Esempio n. 2
0
        public override async Task AddToRoleAsync(ApplicationUser <TKey> 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 role = await FindRoleAsync(normalizedRoleName, cancellationToken);

            if (role == null)
            {
                throw new InvalidOperationException($"Role '{normalizedRoleName}' was not found.");
            }

            var userRoles = (await UserRolesTable.GetRolesAsync(user.Id))?.Select(x => new IdentityUserRole <TKey> {
                UserId = user.Id,
                RoleId = x.Id
            }).ToList();

            UserRoles = userRoles;
            UserRoles.Add(CreateUserRole(user, role));
        }
Esempio n. 3
0
        public override async Task RemoveFromRoleAsync(ApplicationUser <TKey> 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 IdentityUserRole <TKey> {
                    UserId = user.Id,
                    RoleId = x.Id
                }).ToList();
                UserRoles = userRoles;
                var userRole = await FindUserRoleAsync(user.Id, roleEntity.Id, cancellationToken);

                if (userRole != null)
                {
                    UserRoles.Remove(userRole);
                }
            }
        }
Esempio n. 4
0
        public override async Task <IList <string> > GetRolesAsync(ApplicationUser <TKey> user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            user.ThrowIfNull(nameof(user));
            var userLogins = await UserRolesTable.GetRolesAsync(user.Id);

            return(userLogins.Select(x => x.Name).ToList());
        }