コード例 #1
0
        public ActionResult Edit(KAssets.Areas.Admin.Models.EditUserViewModel model)
        {
            if (model.SelectedOrganisationId != null)
            {
                if (model.SelectedSiteId == null)
                {
                    var securityGroups = this.securityGroupService.GetAll().ToList()
                                         .ConvertAll(x => new SecurityGroupViewModel
                    {
                        Id   = x.Id,
                        Name = x.Name
                    });

                    //Set all locations
                    if (model.SelectedOrganisationId == null || model.SelectedOrganisationId == 0)
                    {
                        model.Locations = new List <DropDownLocationViewModel>();
                    }
                    else
                    {
                        model.Locations = this.locationService.GetAll()
                                          .Where(x => x.OrganisationId == model.SelectedOrganisationId)
                                          .ToList().
                                          ConvertAll(x => new DropDownLocationViewModel
                        {
                            Code     = x.Code,
                            Location = x.Country + ", " + x.Town + ", " + x.Street + ", " + x.StreetNumber.Value
                        });
                    }

                    model.SecurityGroups = securityGroups.ConvertAll(
                        x => new SelectSecurityGroupViewModel
                    {
                        IsSelected    = false,
                        SecurityGroup = x
                    });

                    model.Organisations = this.organisationService.GetAll().ToList()
                                          .ConvertAll(x => new OrganisationDropDownViewModel
                    {
                        Id   = x.Id,
                        Name = x.Name
                    });

                    this.ModelState.AddModelError("", Resources.Translation.AdminArea.Admin.OrgOrNothing);
                    return(View(model));
                }
            }
            //Check if the email is same as baseadmin email
            if (model.Email == BaseAdmin.Email || !ModelState.IsValid)
            {
                var securityGroupsS = this.securityGroupService.GetAll().ToList()
                                      .ConvertAll(x => new SecurityGroupViewModel
                {
                    Id   = x.Id,
                    Name = x.Name
                });

                //Set all locations
                if (model.SelectedOrganisationId == null || model.SelectedOrganisationId == 0)
                {
                    model.Locations = new List <DropDownLocationViewModel>();
                }
                else
                {
                    model.Locations = this.locationService.GetAll()
                                      .Where(x => x.OrganisationId == model.SelectedOrganisationId)
                                      .ToList().
                                      ConvertAll(x => new DropDownLocationViewModel
                    {
                        Code     = x.Code,
                        Location = x.Country + ", " + x.Town + ", " + x.Street + ", " + x.StreetNumber.Value
                    });
                }

                model.SecurityGroups = securityGroupsS.ConvertAll(
                    x => new SelectSecurityGroupViewModel
                {
                    IsSelected    = false,
                    SecurityGroup = x
                });

                model.Organisations = this.organisationService.GetAll().ToList()
                                      .ConvertAll(x => new OrganisationDropDownViewModel
                {
                    Id   = x.Id,
                    Name = x.Name
                });
                return(View(model));
            }


            var user = new ApplicationUser();

            user.AboutMe    = model.AboutMe;
            user.Email      = model.Email;
            user.FirstName  = model.FirstName;
            user.LastName   = model.LastName;
            user.SecondName = model.SecondName;
            user.Skype      = model.Skype;
            user.Id         = model.Id;

            //Remova all security groups of user
            this.securityGroupService.RemoveUserFromAllSecurityGroups(model.Id);

            //Add a new security groups to user
            this.securityGroupService.AddUserToSecurityGroups(
                model.Email, model.SecurityGroups.Where(x => x.IsSelected)
                .Select(x => x.SecurityGroup.Id).ToList());

            //Add locations to user
            this.locationService.AddLocationToUser(model.Email, model.SelectedLocationCode);

            //Create a new password of user
            if (model.Password != null && model.Password != "")
            {
                var passToken = UserManager.GeneratePasswordResetToken(model.Id);
                UserManager.ResetPassword(model.Id, passToken, model.Password);
            }

            //Change a location of user
            if (model.SelectedOrganisationId != null)
            {
                if (model.SelectedSiteId != null)
                {
                    this.userService.UserSiteAndOrgUpdate(model.Email, model.SelectedOrganisationId.Value, model.SelectedSiteId.Value);
                }
                else
                {
                    this.userService.UserSiteAndOrgUpdate(model.Email, model.SelectedOrganisationId.Value, 0);
                }
            }
            else
            {
                this.userService.UserSiteAndOrgUpdate(model.Email, 0, 0);
            }

            user.PasswordHash = this.userService.GetById(model.Id).PasswordHash;
            this.userService.Update(user);

            return(Redirect("/Admin/User/Details/" + model.Id));
        }
コード例 #2
0
        public ActionResult Edit(string id)
        {
            var user = this.userService.GetById(id);

            if (user.Email == BaseAdmin.Email)
            {
                return(View(user));
            }

            var securityGroups = this.securityGroupService.GetAll().ToList()
                                 .ConvertAll(x => new SecurityGroupViewModel
            {
                Id   = x.Id,
                Name = x.Name
            });

            var viewModel = new KAssets.Areas.Admin.Models.EditUserViewModel();

            viewModel.SecurityGroups = securityGroups.ConvertAll(
                x => new SelectSecurityGroupViewModel
            {
                IsSelected    = user.SecurityGroups.Any(y => y.Id == x.Id) ? true : false,
                SecurityGroup = x
            });

            //Set all locations
            if (user.Site != null)
            {
                viewModel.Locations = this.locationService.GetAll()
                                      .Where(x => x.OrganisationId == user.Site.OrganisationId)
                                      .ToList().
                                      ConvertAll(x => new DropDownLocationViewModel
                {
                    Code     = x.Code,
                    Location = x.Country + ", " + x.Town + ", " + x.Street + ", " + x.StreetNumber.Value
                });
            }
            else
            {
                viewModel.Locations = new List <DropDownLocationViewModel>();
            }

            viewModel.Organisations = this.organisationService.GetAll().ToList()
                                      .ConvertAll(x => new OrganisationDropDownViewModel
            {
                Id    = x.Id,
                Name  = x.Name,
                Sites = x.Sites.ToList().ConvertAll(
                    y => new SiteViewModel
                {
                    Id   = y.Id,
                    Name = y.Name
                })
            });

            viewModel.SelectedOrganisationId = user.Site != null ? user.Site.OrganisationId : 0;
            viewModel.SelectedSiteId         = user.SiteId;
            viewModel.SelectedLocationCode   = user.LocationId;
            viewModel.AboutMe    = user.AboutMe;
            viewModel.Email      = user.Email;
            viewModel.FirstName  = user.FirstName;
            viewModel.LastName   = user.LastName;
            viewModel.SecondName = user.SecondName;
            viewModel.Skype      = user.Skype;
            viewModel.Id         = user.Id;

            return(View(viewModel));
        }