Beispiel #1
0
        public DTOUser GetUserById(int id)
        {
            using (var context = new IdentityDbContext(_serviceProvider.GetRequiredService <DbContextOptions <IdentityDbContext> >()))
            {
                var newUser   = context.User.Where(u => u.Id == id).FirstOrDefault();
                var userRoles = context.UserRole.Include(u => u.Role).Where(u => u.UserId == id);

                DTOUser dtoUser = DTOConverter(newUser);

                dtoUser.Roles = userRoles.Select(c => c.Role).ToList();
                return(dtoUser);
            }
        }
Beispiel #2
0
 public bool ValidatePassword(User user, string plainTextPassword)
 {
     using (var context = new IdentityDbContext(_serviceProvider.GetRequiredService <DbContextOptions <IdentityDbContext> >()))
     {
         var newUser = context.User.Where(u => String.Equals(u.Email, user.Username)).FirstOrDefault();
         if (user == null)
         {
             return(false);
         }
         if (String.Equals(plainTextPassword, user.Password))
         {
             return(true);
         }
         return(false);
     }
 }
Beispiel #3
0
 public bool DeleteUser(int id)
 {
     using (var context = new IdentityDbContext(_serviceProvider.GetRequiredService <DbContextOptions <IdentityDbContext> >()))
     {
         try {
             var user = context.User.Where(u => u.Id == id).FirstOrDefault();
             user.IsDeleted = true;
             context.User.Update(user);
             context.SaveChanges();
             return(true);
         }
         catch
         {
             return(false);
         }
     }
 }
Beispiel #4
0
        public DTOUser UpdateUser(DTOUser dtoUser)
        {
            using (var context = new IdentityDbContext(_serviceProvider.GetRequiredService <DbContextOptions <IdentityDbContext> >()))
            {
                List <Role> roles  = new List <Role>(dtoUser.Roles);
                User        user   = UserConverter(dtoUser);
                DTOUser     newDto = new DTOUser();
                context.User.Update(user);
                context.SaveChanges();

                var newUser = context.User.Where(u => u.Id == user.Id).FirstOrDefault();
                if (newUser.Id == dtoUser.Id)
                {
                    newDto       = DTOConverter(newUser);
                    newDto.Roles = roles;
                }
                return(newDto);
            }
        }
Beispiel #5
0
        public List <DTOUser> GetAllUsers()
        {
            using (var context = new IdentityDbContext(_serviceProvider.GetRequiredService <DbContextOptions <IdentityDbContext> >()))
            {
                var newUser   = context.User;
                var userRoles = context.UserRole.Include(u => u.Role);

                List <DTOUser> dtoUsers = new List <DTOUser>();
                foreach (var user in newUser)
                {
                    dtoUsers.Add(DTOConverter(user));
                }

                foreach (var users in dtoUsers)
                {
                    users.Roles = userRoles.Where(c => c.UserId == users.Id).Select(c => c.Role).ToList();
                }

                return(dtoUsers);
            }
        }