Exemple #1
0
        public ErrorsCollection Create(User_UserRolePostModel user_userRolePostModel)
        {
            var errors = userRoleValidator.Validate(user_userRolePostModel, context);

            if (errors != null)
            {
                return(errors);
            }

            User user = context.Users
                        .FirstOrDefault(u => u.Id == user_userRolePostModel.UserId);

            if (user != null)
            {
                UserRole userRole = context
                                    .UserRoles
                                    .Include(u_ur => u_ur.User_UserRoles)
                                    .FirstOrDefault(ur => ur.Name == user_userRolePostModel.UserRoleName);

                User_UserRole currentUser_UserRole = context
                                                     .User_UserRoles
                                                     .Include(ur => ur.UserRole)
                                                     .FirstOrDefault(u_ur => u_ur.EndTime == null && u_ur.UserId == user.Id);

                if (currentUser_UserRole == null)
                {
                    //    context.User_UserRoles.Add(new User_UserRole
                    //    {
                    //        User = user,
                    //        UserRole = userRole,
                    //        StartTime = DateTime.Now,
                    //        EndTime = null
                    //    });

                    //    context.SaveChanges();
                    return(null);
                }
                // aici se inchide perioada de activare pentru un anumit rol
                if (!currentUser_UserRole.UserRole.Name.Contains(user_userRolePostModel.UserRoleName))
                {
                    currentUser_UserRole.EndTime = DateTime.Now;

                    context.User_UserRoles.Add(new User_UserRole
                    {
                        User      = user,
                        UserRole  = userRole,
                        StartTime = DateTime.Now,
                        EndTime   = null
                    });

                    context.SaveChanges();
                    return(null);
                }
                else
                {
                    return(null);
                }
            }
            return(null);
        }
        public ErrorsCollection Create(UserUserRolePostDTO userUserRolePostDTO)
        {
            var errors = userRoleValidator.Validate(userUserRolePostDTO, context);

            if (errors != null)
            {
                return(errors);
            }

            User user = context
                        .Users
                        .FirstOrDefault(u => u.Id == userUserRolePostDTO.UserId);

            if (user != null)
            {
                UserRole userRole = context
                                    .UserRoles
                                    .Include(urole => urole.UserUserRoles)
                                    .FirstOrDefault(urole => urole.Name == userUserRolePostDTO.UserRoleName);

                UserUserRole currentUserUserRole = context
                                                   .UserUserRoles
                                                   .Include(uurole => uurole.UserRole)
                                                   .FirstOrDefault(uurole => uurole.UserId == user.Id && uurole.EndTime == null);

                if (currentUserUserRole == null)
                {
                    context.UserUserRoles.Add(new UserUserRole
                    {
                        User      = user,
                        UserRole  = userRole,
                        StartTime = DateTime.Now,
                        EndTime   = null
                    });

                    context.SaveChanges();
                    return(null);
                }


                if (!currentUserUserRole.UserRole.Name.Contains(userUserRolePostDTO.UserRoleName))
                {
                    currentUserUserRole.EndTime = DateTime.Now;

                    context.UserUserRoles.Add(new UserUserRole
                    {
                        User      = user,
                        UserRole  = userRole,
                        StartTime = DateTime.Now,
                        EndTime   = null
                    });

                    context.SaveChanges();
                    return(null);
                }
                else
                {
                    return(null);
                }
            }
            return(null);
        }
        public ErrorsCollection Create(UserUserRolePostModel userUserRolePostModel)
        {
            var errors = userRoleValidator.Validate(userUserRolePostModel, context);

            if (errors != null)
            {
                return(errors);
            }

            User user = context.Users
                        .FirstOrDefault(u => u.Id == userUserRolePostModel.UserId);

            if (user != null)
            {
                UserRole userRole = context
                                    .UserRole
                                    .Include(ur => ur.UserUserRoles)
                                    .FirstOrDefault(ur => ur.Name == userUserRolePostModel.UserRoleName);

                UserUserRole curentUserUserRole = context.UserUserRole
                                                  .Include(uur => uur.UserRole)
                                                  .FirstOrDefault(uur => uur.UserId == user.Id && uur.EndTime == null);

                //discutabil, nu ar trebui sa fie null niciodata, adica la Register nu a fost creat bine un defalut Regular role pentru userul respectiv
                if (curentUserUserRole == null)
                {
                    context.UserUserRole.Add(new UserUserRole
                    {
                        User      = user,
                        UserRole  = userRole,
                        StartTime = DateTime.Now,
                        EndTime   = null
                    });

                    context.SaveChanges();
                    return(null);
                }

                //inchiderea perioadel de activare pentru un anumit rol
                if (!curentUserUserRole.UserRole.Name.Contains(userUserRolePostModel.UserRoleName))
                {
                    curentUserUserRole.EndTime = DateTime.Now;

                    context.UserUserRole.Add(new UserUserRole
                    {
                        User      = user,
                        UserRole  = userRole,
                        StartTime = DateTime.Now,
                        EndTime   = null
                    });

                    context.SaveChanges();
                    return(null);
                }
                else
                {
                    return(null);    //trebuie sa trimit eroare ca modificarea nu poate avea loc, rol nou = rol vechi
                }
            }
            return(null);    //eroare Nu exista User cu Id-ul specificat
        }