コード例 #1
0
        public async Task <ActionResult> EditToPost(ProfileViewModel profileVM, HttpPostedFileBase image)
        {
            if (profileVM.Id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var locale = LocalizationService.GetAvalaibleLocalization();

            if ((profileVM.Locale == null) || (locale.FirstOrDefault(l => l.LocalizationId == profileVM.Locale) == null))
            {
                profileVM.Locale = LocalizationService.GetDefaultLang().LocalizationId;
            }

            if (profileVM.TimezoneId == null || profileVM.TimezoneId == string.Empty)
            {
                ModelState.AddModelError("TimezoneId", Resource.errTimeZoneNotFound);
            }

            var userDTO = Mapper.Map <Auction.BussinessLogic.Models.UserDTO>(profileVM);

            _logger.Info(string.Format("user editted profile - {0}, username -{1}, timezone - {2}", userDTO.Id, userDTO.FullName, userDTO.TimezoneId));
            if (image != null)
            {
                string extension = System.IO.Path.GetExtension(image.FileName);
                if ((extension == ".jpg") || (extension == ".png"))
                {
                    var imageBase64 = ImageHelper.LoadImage(image);
                    userDTO.Picture = imageBase64;
                }
                else
                {
                    ModelState.AddModelError("Picture", Resource.errPictureFormat);
                }
            }

            try
            {
                if (ModelState.IsValid)
                {
                    await _customUserManager.EditUserAsync(userDTO);

                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError(string.Empty, Resource.errNotUpdate);
            }

            DropdownTimeZone();
            return(View(profileVM));
        }
コード例 #2
0
        public async Task <JsonResult> EditUser(string data)
        {
            dynamic user = new ExpandoObject();

            if (Request.IsAjaxRequest())
            {
                try
                {
                    user = JsonConvert.DeserializeObject(data);
                    string             userModel      = user.userWithPermissions.userModel.ToString();
                    var                dataNvc        = HttpUtility.ParseQueryString(userModel);
                    var                dataCollection = dataNvc.AllKeys.ToDictionary(o => o, o => dataNvc[o]);
                    var                jsonString     = JsonConvert.SerializeObject(dataCollection);
                    AdminUserViewModel userVM         = JsonConvert.DeserializeObject <AdminUserViewModel>(jsonString);
                    var                locale         = LocalizationService.GetAvalaibleLocalization();
                    if ((userVM.Locale == null) || (locale.FirstOrDefault(l => l.LocalizationId == userVM.Locale) == null))
                    {
                        ModelState.AddModelError("Locale", Resource.errLocaleNotFound);
                    }

                    if (userVM.TimezoneId == null || userVM.TimezoneId == string.Empty)
                    {
                        ModelState.AddModelError("TimezoneId", Resource.errTimeZoneNotFound);
                    }

                    userVM.Password = SecurityHelper.Hash(userVM.Password);
                    if (ModelState.IsValid)
                    {
                        string pic = user.userWithPermissions.file.ToString();
                        userVM.Picture = pic;

                        var           userPermissions = (IEnumerable <dynamic>)user.userWithPermissions.permissions;
                        var           userId          = Guid.Parse((string)userPermissions.First().userId);
                        UserViewModel currUser        = null;
                        if (userId != Guid.Empty)
                        {
                            currUser = await _customUserManager.FindByIdAsync(userId.ToString());
                        }

                        if (currUser == null || (!currUser.UserName.Equals(userVM.Login) && !await _customUserManager.IsUniqueLoginAsync(userVM.Login)))
                        {
                            ModelState.AddModelError("Login", Resource.errNotUnique);
                            return(Json(JsonConvert.SerializeObject(Url.Action("Edit", "User"))));
                        }

                        List <PermissionDTO> permissions = new List <PermissionDTO>();

                        foreach (dynamic permission in (IEnumerable <dynamic>)user.userWithPermissions.permissions)
                        {
                            var permissionDTO = new PermissionDTO()
                            {
                                Id     = Guid.NewGuid(),
                                UserId = Guid.Parse((string)permission.userId),
                                Role   = (Auction.BussinessLogic.Models.Role)Enum.Parse(typeof(Auction.BussinessLogic.Models.Role), (string)permission.role)
                            };

                            if (permissionDTO.Role != Auction.BussinessLogic.Models.Role.Admin)
                            {
                                permissionDTO.AuctionId = (string)permission.auctionName;
                                if (permissionDTO.Role == BussinessLogic.Models.Role.Moderator)
                                {
                                    permissionDTO.CategoriesId = new List <Guid>();
                                    foreach (dynamic key in (IEnumerable <dynamic>)permission.categories)
                                    {
                                        permissionDTO.CategoriesId.Add(Guid.Parse((string)key.Value));
                                    }
                                }
                                else
                                {
                                    permissionDTO.CategoriesId = null;
                                }
                            }

                            permissions.Add(permissionDTO);
                        }

                        var userDTO = new UserDTO()
                        {
                            Id = userId
                        };
                        userVM.Id = userDTO.Id;
                        userVM.RegistrationDate = DateTime.Now;
                        userDTO.InjectFrom(userVM);

                        var listOldPermissions = await _customUserManager.GetPermissionsAsync(Guid.Parse(currUser.Id));

                        foreach (var oldPermission in listOldPermissions)
                        {
                            await _customUserManager.RemovePermissionAsync(oldPermission);
                        }

                        foreach (var permission in permissions)
                        {
                            await _customUserManager.AddPermissionAsync(permission);
                        }

                        await _customUserManager.EditUserAsync(userDTO);

                        return(Json(JsonConvert.SerializeObject(Url.Action("Index", "User"))));
                    }
                }
                catch
                {
                    ModelState.AddModelError(string.Empty, Resource.errCreate);
                }
            }

            return(Json(JsonConvert.SerializeObject(Url.Action("Edit", "User"))));
        }
コード例 #3
0
        public async Task <ActionResult> Index(RegistrationViewModel user, HttpPostedFileBase image, Guid id)
        {
            user.Id = id;
            var locale = LocalizationService.GetAvalaibleLocalization();

            if ((user.Locale == null) || (locale.FirstOrDefault(l => l.LocalizationId == user.Locale) == null))
            {
                ModelState.AddModelError("Locale", Resource.errLocaleNotFound);
            }

            if (user.TimezoneId == null || user.TimezoneId == string.Empty)
            {
                ModelState.AddModelError("TimezoneId", Resource.errTimeZoneNotFound);
            }

            if (!await _customUserManager.IsUniqueLoginAsync(user.Login))
            {
                ModelState.AddModelError("Login", Resource.errNotUnique);
            }

            try
            {
                if (ModelState.IsValid)
                {
                    user.Password  = Helpers.SecurityHelper.Hash(user.Password);
                    user.DPassword = Helpers.SecurityHelper.Hash(user.DPassword);
                    if (user.Password != user.DPassword)
                    {
                        ModelState.AddModelError(string.Empty, Resource.errDPassword);
                        throw new DataException();
                    }

                    if (image != null)
                    {
                        string extension = System.IO.Path.GetExtension(image.FileName);
                        if ((extension == ".jpg") || (extension == ".png"))
                        {
                            var imageBase64 = ImageHelper.LoadImage(image);
                            user.Picture = imageBase64;
                        }
                        else
                        {
                            ModelState.AddModelError("Picture", Resource.errPictureFormat);
                        }
                    }

                    user.RegistrationDate = DateTime.Now;
                    var regUser = Mapper.Map <Auction.BussinessLogic.Models.UserDTO>(user);
                    ////default Role = 0 - User
                    await _customUserManager.AddUserAsync(regUser);

                    List <Task> taskList = new List <Task>();
                    foreach (AuctionHouseElement auctionEl in config.AuctionHouses)
                    {
                        PermissionDTO permissionDTO = new PermissionDTO()
                        {
                            AuctionId    = auctionEl.Name,
                            UserId       = regUser.Id,
                            Id           = Guid.NewGuid(),
                            CategoriesId = null,
                            Role         = BussinessLogic.Models.Role.User
                        };
                        await _customUserManager.AddPermissionAsync(permissionDTO);

                        taskList.Add(_customUserManager.AddPermissionAsync(permissionDTO));
                    }
                    //// Task.WaitAll(taskList.ToArray());
                    return(RedirectToAction("Index", "Home"));
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError(string.Empty, "Невозможно сохранить модель, попробуйте ещё раз");
            }

            Dropdown();
            return(View(user));
        }