Exemple #1
0
        public async Task <ClaimsIdentity> Authenticate(ClientViewModelBLL userDto)
        {
            ClaimsIdentity  claim = null;
            ApplicationUser user  = await UserManager.FindByEmailAsync(userDto.Email);

            if (!ReferenceEquals(user, null))
            {
                if (user.IsPasswordClear && user.PasswordHash.Equals(userDto.Password))
                {
                    ;
                }
                else if (!user.IsPasswordClear)
                {
                    user = await UserManager.FindAsync(userDto.Email, userDto.Password);
                }
                else
                {
                    user = null;
                }
            }

            if (!ReferenceEquals(user, null))
            {
                claim = await UserManager.CreateIdentityAsync(user,
                                                              DefaultAuthenticationTypes.ApplicationCookie);

                HttpContext.Current.Session["isPerformer"] = user.IsPerformer;
                HttpContext.Current.Session["adminStatus"] = user.AdminStatus;
            }
            return(claim);
        }
        public async Task <ActionResult> Login(LoginViewModel model)
        {
            if (Request.IsAuthenticated)
            {
                return(View("~/Views/Error/Forbidden.cshtml"));
            }

            if (ModelState.IsValid)
            {
                ClientViewModelBLL userViewModel = new ClientViewModelBLL
                {
                    Email    = model.Email,
                    Password = model.Password
                };
                ClaimsIdentity claim = await _userService.Authenticate(userViewModel);

                if (claim == null)
                {
                    ModelState.AddModelError("", "Wrong login or password");
                }
                else
                {
                    AuthenticationManager.SignOut();
                    AuthenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = true
                    }, claim);
                    return(RedirectToAction("Index", "Home"));
                }
            }
            return(View(model));
        }
        public async Task <ActionResult> DeleteAccount(DeleteAccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = _userService.FindById(User.Identity.GetUserId <int>());

                var picture = _userService.FindById(user.Id).PictureId;
                if (picture.HasValue)
                {
                    _pictureService.Delete(picture.Value);
                }

                var ordersId = _orderService.GetAll().Where(o => o.UserId == user.Id).Select(o => o.Id).ToList();
                if (ordersId.Any())
                {
                    var responses = _responseService.GetAll()
                                    .Select(r => r.OrderId.Value)
                                    .Where(r => ordersId.Contains(r)).ToList();
                    if (responses.Any())
                    {
                        foreach (var response in responses)
                        {
                            _responseService.Delete(response);
                        }
                    }
                }

                var comments = _commentService.GetAll().Where(c => c.CustomerId == user.Id).ToList();
                if (comments.Any())
                {
                    foreach (var comment in comments)
                    {
                        _commentService.Delete(comment.Id);
                    }
                }

                ClientViewModelBLL userDto = new ClientViewModelBLL
                {
                    Password = model.Password,
                    Email    = User.Identity.Name
                };
                var operationDetails = await _userService.DeleteAccount(userDto);

                if (operationDetails.Succedeed)
                {
                    _unitOfWork.Save();
                    LogOff();
                    return(RedirectToAction("Index", "Home"));
                }

                ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
            }
            return(View(model));
        }
Exemple #4
0
        public async Task <OperationDetails> DeleteAccount(ClientViewModelBLL userDto)
        {
            var user = await UserManager.FindAsync(userDto.Email, userDto.Password);

            if (!ReferenceEquals(user, null))
            {
                _clientRepository.Delete(user.Id);
                return(new OperationDetails(true, "Deleting account succeeded", string.Empty));
            }
            return(new OperationDetails(false, "Incorrect password", "Password"));
        }
Exemple #5
0
        public ActionResult Edit(EditPerformerViewModel model, int[] selectedCategories, HttpPostedFileBase loadImage)
        {
            if (!(bool)Session["isPerformer"] || !(bool)Session["adminStatus"])
            {
                return(View("~/Views/Error/Forbidden.cshtml"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (ReferenceEquals(selectedCategories, null))
            {
                return(RedirectToAction("Edit", new
                {
                    message = "At least one category is required",
                    company = model.Company,
                    info = model.Info,
                    phoneNumber = model.PhoneNumber
                }));
            }

            PictureViewModelBLL picture = null;

            if (!ReferenceEquals(loadImage, null))
            {
                byte[] image;
                using (var binaryReader = new BinaryReader(loadImage.InputStream))
                {
                    image = binaryReader.ReadBytes(loadImage.ContentLength);
                }
                picture = new PictureViewModelBLL {
                    Image = image
                };

                _pictureService.Create(image);
                _unitOfWork.Save();
            }

            ClientViewModelBLL client = _userService.FindById(User.Identity.GetUserId <int>());

            Mapper.Initialize(cfg => cfg.CreateMap <EditPerformerViewModel, ClientViewModelBLL>()
                              .ForMember("Name", opt => opt.MapFrom(c => client.Name))
                              .ForMember("CategoriesBll", opt => opt.MapFrom(c => c.Categories))
                              .ForMember("PictureId", opt => opt.MapFrom(c => _pictureService.FindByBytes(picture.Image).Value))
                              );
            Mapper.Map(model, client);
            _userService.Update(client, selectedCategories);
            _unitOfWork.Save();

            return(RedirectToAction("Details", new { id = client.Id }));
        }
        public ActionResult BecomePerformer(BecomePerformerViewModel model, int[] selectedCategories, HttpPostedFileBase loadImage)
        {
            if ((bool)Session["isPerformer"] && (bool)Session["adminStatus"])
            {
                return(View("~/Views/Error/Forbidden.cshtml"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (selectedCategories == null)
            {
                ModelState.AddModelError("", "At least one category is required");
                GetCategoriesList();
                return(View(model));
            }

            PictureViewModelBLL picture = null;

            if (!ReferenceEquals(loadImage, null))
            {
                byte[] image;
                using (var binaryReader = new BinaryReader(loadImage.InputStream))
                {
                    image = binaryReader.ReadBytes(loadImage.ContentLength);
                }
                picture = new PictureViewModelBLL {
                    Image = image
                };

                _pictureService.Create(image);
                _unitOfWork.Save();
            }

            ClientViewModelBLL client = _userService.FindById(User.Identity.GetUserId <int>());

            Mapper.Initialize(cfg => cfg.CreateMap <BecomePerformerViewModel, ClientViewModelBLL>()
                              .ForMember("RegistrationDate", opt => opt.MapFrom(c => DateTime.Today))
                              .ForMember("IsPerformer", opt => opt.MapFrom(c => true))
                              .ForMember("AdminStatus", opt => opt.MapFrom(c => false))
                              .ForMember("Rating", opt => opt.MapFrom(c => 0))
                              .ForMember("PictureId", opt => opt.MapFrom(c => _pictureService.FindByBytes(picture.Image).Value))
                              );
            Mapper.Map(model, client);
            _userService.Update(client, selectedCategories);
            _unitOfWork.Save();

            return(RedirectToAction("Index"));
        }
Exemple #7
0
        public OperationDetails Update(ClientViewModelBLL userDto)
        {
            ApplicationUser user = _clientRepository.Get(userDto.Id);

            if (!ReferenceEquals(user, null))
            {
                Mapper.Initialize(cfg => cfg.CreateMap <ClientViewModelBLL, ApplicationUser>()
                                  .IgnoreAllSourcePropertiesWithAnInaccessibleSetter()
                                  .IgnoreAllPropertiesWithAnInaccessibleSetter());
                Mapper.Map(userDto, user);
                _clientRepository.Update(user);

                HttpContext.Current.Session["adminStatus"] = userDto.AdminStatus;
                HttpContext.Current.Session["isPerformer"] = userDto.IsPerformer;

                return(new OperationDetails(true, @"User information updated", string.Empty));
            }
            return(new OperationDetails(false, @"User doesn't exist", "Id"));
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (Request.IsAuthenticated)
            {
                return(View("~/Views/Error/Forbidden.cshtml"));
            }

            if (ModelState.IsValid)
            {
                Mapper.Initialize(cfg => cfg.CreateMap <RegisterViewModel, ClientViewModelBLL>()
                                  .ForMember("UserName", opt => opt.MapFrom(c => c.Email))
                                  .ForMember("EmailConfirmed", opt => opt.MapFrom(c => false))
                                  .ForMember("IsPerformer", opt => opt.MapFrom(c => false))
                                  .ForMember("Rating", opt => opt.MapFrom(c => 0))
                                  .ForMember("RegistrationDate", opt => opt.MapFrom(c => DateTime.Today))
                                  .ForMember("AdminStatus", opt => opt.MapFrom(c => false))
                                  .ForMember("Role", opt => opt.MapFrom(c => "user")));

                ClientViewModelBLL userViewModel = Mapper.Map <RegisterViewModel, ClientViewModelBLL>(model);

                OperationDetails operationDetails = await _userService.Create(userViewModel);

                _unitOfWork.Save();
                if (operationDetails.Succedeed)
                {
                    //int userId = _userService.FindByUserName(userViewModel.UserName).Id;
                    //string code = await _userService.GenerateEmailConfirmationToken(userId);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userViewModel.Id, EmailConfirmed = userViewModel.EmailConfirmed, code = code }, protocol: Request.Url.Scheme);
                    //_userService.SendEmail(userId, callbackUrl);

                    //return View("DisplayEmail");

                    return(View("RegistrationSucceeded"));
                }

                ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
            }
            return(View(model));
        }
Exemple #9
0
        public async Task <OperationDetails> Create(ClientViewModelBLL userDto)
        {
            ApplicationUser user = await UserManager.FindByEmailAsync(userDto.Email);

            if (ReferenceEquals(user, null))
            {
                Mapper.Initialize(cfg => cfg.CreateMap <ClientViewModelBLL, ApplicationUser>());
                user = Mapper.Map <ClientViewModelBLL, ApplicationUser>(userDto);

                var result = await UserManager.CreateAsync(user, userDto.Password);

                if (result.Errors.Any())
                {
                    return(new OperationDetails(false, result.Errors.FirstOrDefault(), ""));
                }

                await UserManager.AddToRoleAsync(user.Id, userDto.Role);

                return(new OperationDetails(true, "Registration succeeded", string.Empty));
            }

            return(new OperationDetails(false, "Login is already taken by another user", "Email"));
        }
        public async Task <ActionResult> ChangePassword(IndexManageViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("ChangePassword"));
            }

            Mapper.Initialize(cfg => cfg.CreateMap <IndexManageViewModel, ClientViewModelBLL>()
                              .ForMember("Id", opt => opt.MapFrom(c => User.Identity.GetUserId <int>()))
                              .ForMember("UserName", opt => opt.MapFrom(c => c.OldPassword))
                              .ForMember("Password", opt => opt.MapFrom(c => c.NewPassword))
                              );
            ClientViewModelBLL client = Mapper.Map <IndexManageViewModel, ClientViewModelBLL>(model);

            var result = await _userService.ChangePassword(client);

            if (result.Succeeded)
            {
                return(RedirectToAction("Index", "Home", new { Message = ManageMessageId.ChangePasswordSuccess }));
            }

            AddErrors(result);
            return(RedirectToAction("ChangePassword"));
        }
Exemple #11
0
 public OperationDetails Update(ClientViewModelBLL userDto, int[] selectedCategories)
 {
     (_clientRepository as IManyToManyResolver)?.Update(userDto.Id, selectedCategories);
     return(Update(userDto));
 }
Exemple #12
0
 public async Task <IdentityResult> ChangePassword(ClientViewModelBLL userDto)
 => await UserManager.ChangePasswordAsync(userDto.Id, userDto.UserName, userDto.Password);