示例#1
0
        //Verifies email
        public async Task VerifyEmailAsync(UserModel user, VerificationCodeModel verification)
        {
            if (user.Status == (int)UserStatus.Valid)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("User Already Verified", "User already verified");
                errors.Throw();
            }

            if (user.Status == (int)UserStatus.Banned)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("User Is Banned", "User is banned from application");
                errors.Throw();
            }

            InformationModel EmailVerificationCodeInfo = await _informationRepository.GetInformationByInformationNameAsync("EmailVerificationCode");

            InformationModel EmailVerificationCodeGenerateDateInfo = await _informationRepository.GetInformationByInformationNameAsync("EmailVerificationCodeGenerateDate");

            UserInformationModel EmailVerificationCode = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, EmailVerificationCodeInfo.Id);

            UserInformationModel EmailVerificationCodeGenerateDate = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, EmailVerificationCodeGenerateDateInfo.Id);

            //Bad request
            if (EmailVerificationCode == null)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Email Verification Code Not Exist", "There is no verification code which is generated for you");
                errors.Throw();
            }

            //Generated code timed out
            if (String.Format("{0:u}", DateTime.UtcNow.AddMinutes(-15)).CompareTo(EmailVerificationCodeGenerateDate.Value) > 0)
            {
                _userInformationRepository.Delete(EmailVerificationCode);
                _userInformationRepository.Delete(EmailVerificationCodeGenerateDate);

                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Verification Code Timeout", "Verification code timed out, please request another verification code");
                errors.Throw();
            }

            //Verification code accepted
            if (EmailVerificationCode.Value == verification.VerificationCode)
            {
                user.Status = (int)UserStatus.Valid;
                _userRepository.Update(user);

                _userInformationRepository.Delete(EmailVerificationCode);
                _userInformationRepository.Delete(EmailVerificationCodeGenerateDate);
            }
            //Verification code does not matched
            else
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Verification Code", "Verification code does not matched");
                errors.Throw();
            }
        }
示例#2
0
        public void Delete(UserInformationEntity information)
        {
            if (ReferenceEquals(information, null))
            {
                throw new ArgumentNullException(nameof(information));
            }

            informationRepository.Delete(information.ToDalInformationUsers());
            uow.Commit();
        }
示例#3
0
        public async Task <IActionResult> DeleteConfirmed(string idUser)
        {
            //if the role is admin, do not delete the user
            if (String.IsNullOrEmpty(idUser))
            {
                //it is not in the user information
                ModelState.AddModelError("", "You cannot delete an null user");
                return(View());
            }

            var userToDelete = await _userManager.FindByIdAsync(idUser);

            var isAdmin = await _userManager.IsInRoleAsync(userToDelete, "Admin");

            if (isAdmin == true)
            {
                ModelState.AddModelError("", "You cannot delete an admin");
                return(RedirectToAction(nameof(Delete), new { id = idUser }));
            }

            //the user is not an admin
            foreach (var role in _rolesNames)
            {
                var isInRole = await _userManager.IsInRoleAsync(userToDelete, role);

                if (isInRole)
                {
                    await _userManager.RemoveFromRoleAsync(userToDelete, role);
                }
            }

            //delete from asp.net.users
            await _userManager.DeleteAsync(userToDelete);

            //delete userinformation
            var userInformationBasedOnId = _userInformationRepository.GetUserById(idUser);

            _userInformationRepository.Delete(userInformationBasedOnId);
            _userInformationRepository.Save();

            return(RedirectToAction(nameof(Index), new { searchString = string.Empty }));
        }