예제 #1
0
        public virtual async Task RemoveFromRoleAsync(TUser user, String normalizedRoleName, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (String.IsNullOrWhiteSpace(normalizedRoleName))
            {
                throw new ArgumentException(nameof(normalizedRoleName));
            }

            TRole roleEntity = await this.FindRoleAsync(normalizedRoleName, cancellationToken);

            if (roleEntity != null)
            {
                MongoDBIdentityUserRole userRole = user.Roles.SingleOrDefault(r => r.RoleId == roleEntity.Id);
                if (userRole != null)
                {
                    List <MongoDBIdentityUserRole> newList = new List <MongoDBIdentityUserRole>(user.Roles);
                    newList.Remove(userRole);
                    user.Roles = newList;
                }
            }
        }
예제 #2
0
        protected virtual async Task <UserRole> FindUserRoleAsync(ObjectId userId, ObjectId roleId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            MongoDBIdentityUserRole ur = (await this.userCollection
                                          .Find(u => u.Id == userId && u.Roles.Any(r => r.RoleId == roleId))
                                          .SingleOrDefaultAsync(cancellationToken))
                                         .Roles
                                         .SingleOrDefault(r => r.RoleId == roleId);

            if (ur != null)
            {
                return(new UserRole {
                    RoleId = roleId, UserId = userId
                });
            }

            return(null);
        }