Exemplo n.º 1
0
        public User Create(PostUserDto user)
        {
            User toAdd = PostUserDto.ToUser(user);

            context.Users.Add(toAdd);
            context.SaveChanges();
            return(toAdd);
        }
Exemplo n.º 2
0
 public static User ToUser(PostUserDto userModel)
 {
     return(new User
     {
         FullName = userModel.FullName,
         Username = userModel.UserName,
         Email = userModel.Email,
     });
 }
Exemplo n.º 3
0
        public User Upsert(int id, PostUserDto user, User addedBy)
        {
            var existing = context.Users.AsNoTracking().FirstOrDefault(u => u.Id == id);

            if (existing == null)
            {
                User toAdd = PostUserDto.ToUser(user);
                user.Password = ComputeSha256Hash(user.Password);
                context.Users.Add(toAdd);
                context.SaveChanges();
                return(toAdd);
            }

            User toUpdate = PostUserDto.ToUser(user);

            toUpdate.Password  = existing.Password;
            toUpdate.CreatedAt = existing.CreatedAt;
            toUpdate.Id        = id;

            if (user.UserRole.Equals("Admin") && !addedBy.UserRole.Equals(UserRole.Admin))
            {
                return(null);
            }
            else if ((existing.UserRole.Equals(UserRole.Regular) &&
                      addedBy.UserRole.Equals(UserRole.UserManager)) ||

                     (existing.UserRole.Equals(UserRole.UserManager) &&
                      addedBy.UserRole.Equals(UserRole.UserManager) &&
                      addedBy.CreatedAt.AddMonths(6) <= DateTime.Now))
            {
                context.Users.Update(toUpdate);
                context.SaveChanges();
                return(toUpdate);
            }
            else if (addedBy.UserRole.Equals(UserRole.Admin))
            {
                context.Users.Update(toUpdate);
                context.SaveChanges();
                return(toUpdate);
            }


            return(null);
        }
Exemplo n.º 4
0
        public User Upsert(int id, PostUserDto user)
        {
            var existing = context.Users.AsNoTracking().FirstOrDefault(u => u.Id == id);

            if (existing == null)
            {
                User toAdd = PostUserDto.ToUser(user);
                user.Password = ComputeSha256Hash(user.Password);
                context.Users.Add(toAdd);
                context.SaveChanges();
                return(toAdd);
            }

            User toUpdate = PostUserDto.ToUser(user);

            toUpdate.Password  = existing.Password;
            toUpdate.CreatedAt = existing.CreatedAt;
            toUpdate.Id        = id;

            context.Users.Update(toUpdate);
            context.SaveChanges();
            return(toUpdate);
        }
Exemplo n.º 5
0
        // public User Upsert(int id, User user)
        //{
        //   var existing = context.Users.AsNoTracking().FirstOrDefault(ex => ex.Id == id);

        //  if (existing == null)
        // {
        //      context.Users.Add(user);
        //     context.SaveChanges();
        //     return user;
        // }

        // user.Id = id;
        // context.Users.Update(user);
        // context.SaveChanges();
        // return user;
        // }
        public User Upsert(int id, PostUserDto UserPostDto, User addedBy)
        {
            var existing = context.Users.AsNoTracking()
                           .Include(u => u.UserUserRoles)
                           .ThenInclude(us => us.UserRole)
                           .FirstOrDefault(u => u.Id == id);

            if (existing == null)
            {
                User toAdd = PostUserDto.ToUser(UserPostDto);
                context.Users.Add(toAdd);
                context.SaveChanges();
                return(toAdd);
            }

            String existingCurrentRole = GetLatestUserUserRole(existing.UserUserRoles).UserRole.Name;
            String addedByCurrentRole  = GetLatestUserUserRole(addedBy.UserUserRoles).UserRole.Name;

            UserUserRole currentUserUserRole = GetLatestUserUserRole(existing.UserUserRoles);

            User toUpdate = PostUserDto.ToUser(UserPostDto);

            toUpdate.Password  = existing.Password;
            toUpdate.CreatedAt = existing.CreatedAt;
            toUpdate.Id        = id;

            if (existingCurrentRole.Equals(RoleConstants.USER_MANAGER) && addedByCurrentRole.Equals(RoleConstants.USER_MANAGER) && addedBy.CreatedAt.AddMonths(6) >= DateTime.Now)
            {
                return(null);
            }
            if (((!existingCurrentRole.Equals(RoleConstants.ADMIN) ||
                  (!existingCurrentRole.Equals(RoleConstants.USER_MANAGER)) &&
                  (addedByCurrentRole.Equals(RoleConstants.USER_MANAGER) || addedByCurrentRole.Equals(RoleConstants.ADMIN)))) ||
                (existingCurrentRole.Equals(RoleConstants.USER_MANAGER) &&
                 addedByCurrentRole.Equals(RoleConstants.USER_MANAGER) &&
                 addedBy.CreatedAt.AddMonths(6) <= DateTime.Now))
            {
                toUpdate.UserUserRoles = existing.UserUserRoles;
                context.Users.Update(toUpdate);
                context.SaveChanges();
                context.Users.Attach(toUpdate);

                if (existingCurrentRole != UserPostDto.UserRole)
                {
                    IEnumerable <UserRole> allRoles = context.UserRoles;
                    List <String>          list     = new List <string>();
                    foreach (UserRole role in allRoles)
                    {
                        list.Add(role.Name);
                    }
                    if (list.Contains(UserPostDto.UserRole))
                    {
                        UserRole     role    = SearchForRoleByTitle(UserPostDto.UserRole);
                        UserUserRole history = new UserUserRole
                        {
                            UserRole  = role,
                            StartTime = DateTime.Now
                        };

                        currentUserUserRole.EndTime = DateTime.Now;

                        context.UserRoles.Attach(role);
                        toUpdate.UserUserRoles.Add(history);
                        context.SaveChanges();
                    }
                    else
                    {
                        return(null);
                    }
                }

                return(toUpdate);
            }
            return(null);
        }