Esempio n. 1
0
        public void CreateUser(UserViewModel ApplicationUserView)
        {
            ApplicationUser Exists = UserRepository.Get(x => x.UserName == ApplicationUserView.UserName && x.Email == ApplicationUserView.Email).FirstOrDefault();

            if (Exists != null)
            {
                throw new Exception("ApplicationUsername & Email already in use");
            }
            string SecurityStamp  = EncryptionService.CreateSalt();
            string HassedPassword = EncryptionService.EncryptPassword(ApplicationUserView.HashedPassword, SecurityStamp);

            ApplicationUserView.HashedPassword = HassedPassword;
            ApplicationUserView.Salt           = SecurityStamp;

            ApplicationUser ApplicationUser = new ApplicationUser()
            {
                UserName      = ApplicationUserView.UserName,
                Email         = ApplicationUserView.Email,
                PasswordHash  = ApplicationUserView.HashedPassword,
                SecurityStamp = ApplicationUserView.Salt,
                Active        = ApplicationUserView.Active
            };

            UserRepository.Add(ApplicationUser);
            SaveUser();
        }
Esempio n. 2
0
        public async Task <ApplicationUser> Handle(CreateNewApplicationUserCommand cmd, bool useStrongPassword)
        {
            if (cmd.CommandId == null)
            {
                cmd.CommandId = Guid.NewGuid();
            }

            var applicationUser = new ApplicationUser(cmd.UserName, cmd.Password, cmd.ConfirmPassword, useStrongPassword, true);

            foreach (var role in cmd.Roles)
            {
                applicationUser.AddClaim(Constants.CLAIM_ROLE, role);
            }

            _applicationUserRepository.Add(applicationUser);
            _applicationUserRepository.Save();

            //fire event here...
            await _messageProducer.ProduceEventAsync <ApplicationUserCreatedEvent>(new ApplicationUserCreatedEvent
            {
                CorrelationId = (cmd.CommandId == null) ? Guid.NewGuid() : (Guid)cmd.CommandId,
                EntityId      = applicationUser.Id,
                Active        = applicationUser.Active,
                Claims        = cmd.Roles,
                DateCreated   = applicationUser.DateCreated,
                UserName      = applicationUser.UserName
            });

            return(applicationUser);
        }
Esempio n. 3
0
        public IActionResult Register([FromBody] ApplicationUser user)
        {
            if (user == null)
            {
                return(BadRequest(user));
            }
            if (!regex.IsMatch(user.phone))
            {
                return(BadRequest(user));
            }
            if (user.password.Length < 6)
            {
                return(BadRequest(user));
            }
            if (_userRepository.CheckPhone(user.phone))
            {
                return(BadRequest(user));
            }
            Random rd  = new Random();
            int    num = rd.Next(100000, 999999);

            user.code = num.ToString();
            if (_userRepository.Add(user))
            {
                _smsSender.SendSmsAsync(user.phone, user.code);
                return(StatusCode(201, user));
            }
            else
            {
                return(BadRequest(user));
            }
        }
Esempio n. 4
0
        public ActionResult Add([FromBody] UserAddDTO userDTO)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser applicationUser = new ApplicationUser()
                {
                    FullName    = userDTO.Name,
                    Email       = userDTO.Email,
                    UserName    = userDTO.Email,
                    PhoneNumber = userDTO.PhoneNumber
                };

                var result = _userRepository.Add(applicationUser, userDTO.Password);

                if (!result.Succeeded)
                {
                    List <string> errors = new List <string>();

                    foreach (var error in result.Errors)
                    {
                        errors.Add(error.Description);
                    }

                    return(UnprocessableEntity(errors));
                }
                else
                {
                    return(Ok(applicationUser));
                }
            }
            else
            {
                return(UnprocessableEntity(ModelState));
            }
        }
Esempio n. 5
0
        public async Task <Unit> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            var password = PasswordManager.HashPassword(request.Password);

            var applicationUser = ApplicationUser.Create(request.Email, request.FirstName, uniquenessChecker);

            await applicationUserRepository.Add(applicationUser);

            return(Unit.Value);
        }
        public IActionResult Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            _userRepository.Add(model.ToApplicationUser());

            return(LocalRedirect("/"));
        }
Esempio n. 7
0
        public async Task <ApplicationUserDto> Create(ApplicationUserDto model)
        {
            var existingUser = await _applicationUserRepository.Get(x => x.Email == model.Email);

            if (existingUser != null)
            {
                throw new Exception(Constants.Users.UserExisted);
            }

            var data   = _mapper.Map <ApplicationUser>(model);
            var result = await _applicationUserRepository.Add(data);

            return(_mapper.Map <ApplicationUserDto>(result));
        }
Esempio n. 8
0
        public async Task <ActionResult> Create(CreateApplicationUser entity)
        {
            if (_user.FindByNameAsync(entity.IdentityUser.UserName).Result == null)
            {
                var email = entity.IdentityUser.UserName;
                var user  = new IdentityUser
                {
                    UserName = email,
                    Email    = email
                };
                var result = _user.CreateAsync(user, entity.Password).Result;
            }
            try
            {
                var newuser   = _user.FindByNameAsync(entity.IdentityUser.UserName).Result;
                var newentity = new ApplicationUser
                {
                    FirstName = entity.FirstName,
                    LastName  = entity.LastName,
                    Address   = entity.Address,
                    Id        = _user.FindByNameAsync(entity.IdentityUser.UserName).Result.Id,
                };
                if (!ModelState.IsValid)
                {
                    return(View(newentity));
                }
                var applicationUser = _mapper.Map <ApplicationUser>(newentity);
                //applicationUser.DateCreated = DateTime.Now;

                var isSuccess = await _repo.Add(applicationUser);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong...");
                    return(View(entity));
                }
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
Esempio n. 9
0
        public async Task <IActionResult> Add(AddUserViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            var user = new ApplicationUser
            {
                Firstname       = vm.Firstname,
                Lastname        = vm.Lastname,
                Email           = vm.Email,
                Password        = vm.Password,
                ProfilePhotoUrl = vm.UserImageUrl
            };
            await userRepository.Add(user);

            TempData["StatusMessage"] = "New User account created successfully";
            return(RedirectToAction(nameof(Add)));
        }
        //Saved de veranderingen in de tabel: Applicationuser
        public void Save(Models.ApplicationUser user)
        {
            if (user == null)
            {
                throw new ArgumentException("user");
            }
            if (UserExists(user.Email, user.UserID))
            {
                throw new ArgumentException("username exists");
            }

            if (user.UserID == 0)
            {
                _repository.Add(user);
            }
            else
            {
                _repository.Edit(user);
            }

            _uow.Commit();
        }
Esempio n. 11
0
        public IActionResult RegisterAdmin()
        {
            var user = _applicationUserRepository.FindByEmail("*****@*****.**");

            if (user.EmailId == null)
            {
                ApplicationUserViewModel AdminUser = new ApplicationUserViewModel()
                {
                    Name    = "Admin",
                    EmailId = "*****@*****.**",
                    //CreatedDateTime = DateTime.Now,
                    MobileNumber = "9423237999",
                    RoleId       = 1
                };
                using (var hmac = new System.Security.Cryptography.HMACSHA512())
                {
                    AdminUser.PasswordSalt = hmac.Key;
                    AdminUser.PasswordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes("Reset1234"));
                }
                _applicationUserRepository.Add(AdminUser);
            }
            return(RedirectToAction("Index", "Account"));
        }
Esempio n. 12
0
 public bool UpdateUser(ApplicationUser user, string password)
 {
     if (user.Id == 0)
     {
         //user.Token = GenerateToken(4);
         //_repository.Add(Token)
         user.Password = Helpers.Hasher.HashString(password);
         //string link = GenerateLink(user.UserName, user.Token);
         //SendVerification(user.WorkEmail, link);
         _repository.Add(user);
         _uow.Commit();
         return(true);
     }
     else
     {
         if (user.Password.ToString() != password)//check whether the password changed
         {
             user.Password = Helpers.Hasher.HashString(password);
         }
         _repository.Edit(user);
         _uow.Commit();
         return(true);
     }
 }
Esempio n. 13
0
 public void CreateApplicationUser(ApplicationUser ApplicationUser)
 {
     ApplicationUsersRepository.Add(ApplicationUser);
 }
Esempio n. 14
0
 public ApplicationUser Add(ApplicationUser applicationUser)
 {
     return(_applicationUserRepository.Add(applicationUser));
 }
Esempio n. 15
0
        public async Task <IActionResult> Create(CreateClientViewModel entity)

        {
            if (_user.FindByNameAsync(entity.ApplicationUser.IdentityUser.UserName).Result == null)
            {
                var email = entity.ApplicationUser.IdentityUser.UserName;
                var user  = new IdentityUser
                {
                    UserName       = email,
                    Email          = email,
                    EmailConfirmed = true
                };
                var result = _user.CreateAsync(user, entity.Password).Result;
                var role   = _user.AddToRoleAsync(user, "Client");
            }
            try
            {
                int companyId;
                var newentity = new ApplicationUser
                {
                    FirstName = entity.ApplicationUser.FirstName,
                    LastName  = entity.ApplicationUser.LastName,
                    Address   = entity.ApplicationUser.Address,
                    Id        = _user.FindByNameAsync(entity.ApplicationUser.IdentityUser.UserName).Result.Id,
                };

                if (!ModelState.IsValid)
                {
                    return(View(newentity));
                }
                var applicationUser  = _mapper.Map <ApplicationUser>(newentity);
                var isAppUserSuccess = await _appUserRepo.Add(applicationUser);

                if (!isAppUserSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong...");
                    return(View(entity));
                }
                if (!ModelState.IsValid)
                {
                    return(View(entity));
                }
                var centity = new Company
                {
                    CompanyName      = entity.Company.CompanyName,
                    CompanyContactNo = entity.Company.CompanyContactNo,
                    CompanyEmail     = entity.Company.CompanyEmail
                };
                var newCompany = _mapper.Map <Company>(centity);

                /* using (var memoryStream = new MemoryStream())
                 * {
                 *   entity.Company.CompanyLogo.CopyToAsync(memoryStream);
                 *   newCompany.CompanyLogo = memoryStream.ToArray();
                 * }*/
                var isCompanySuccess = await _companyRepo.Add(newCompany);

                if (!isCompanySuccess)
                {
                    ModelState.AddModelError("", "Something went wrong...");
                    return(View(entity));
                }

                companyId = await _companyRepo.FindIdByDetails(centity.CompanyName, centity.CompanyEmail);

                var clentity = new Client
                {
                    ApplicationUserId = newentity.ApplicationUserId,
                    CompanyId         = companyId
                };
                var client = _mapper.Map <Client>(clentity);

                var isSuccess = await _repo.Add(clentity);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong...");
                    return(View(entity));
                }
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
Esempio n. 16
0
 public void RegisterUser(ApplicationUser user)
 {
     _userRepository.Add(user);
 }
Esempio n. 17
0
        public async Task <ActionResult> Create(CreateDraughtsmanViewModel entity)
        {
            try
            {
                if (_user.FindByNameAsync(entity.ApplicationUser.IdentityUser.UserName).Result == null)
                {
                    var email = entity.ApplicationUser.IdentityUser.UserName;
                    var user  = new IdentityUser
                    {
                        UserName       = email,
                        Email          = email,
                        EmailConfirmed = true
                    };
                    var result = _user.CreateAsync(user, entity.Password).Result;
                    var role   = _user.AddToRoleAsync(user, "Draughtsman");
                }


                int companyId;
                var newuser   = _user.FindByNameAsync(entity.ApplicationUser.IdentityUser.UserName).Result;
                var newentity = new ApplicationUser
                {
                    FirstName = entity.ApplicationUser.FirstName,
                    LastName  = entity.ApplicationUser.LastName,
                    Address   = entity.ApplicationUser.Address,
                    Id        = _user.FindByNameAsync(entity.ApplicationUser.IdentityUser.UserName).Result.Id,
                };
                if (!ModelState.IsValid)
                {
                    return(View(newentity));
                }
                var applicationUser = _mapper.Map <ApplicationUser>(newentity);
                //applicationUser.DateCreated = DateTime.Now;

                var isAppUserSuccess = await _appUserRepo.Add(applicationUser);

                if (!isAppUserSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong...");
                    return(View(entity));
                }
                if (!ModelState.IsValid)
                {
                    return(View(entity));
                }

                var centity = new CreateCompanyViewModel
                {
                    CompanyName      = entity.Company.CompanyName,
                    CompanyContactNo = entity.Company.CompanyContactNo,
                    CompanyEmail     = entity.Company.CompanyEmail
                };
                var newCompany = _mapper.Map <Company>(centity);
                using (var memoryStream = new MemoryStream())
                {
                    await centity.CompanyLogo.CopyToAsync(memoryStream);

                    newCompany.CompanyLogo = memoryStream.ToArray();
                }
                var exists = await _companyRepo.DoesExist(centity.CompanyEmail);

                if (!exists)
                {
                    await _companyRepo.Add(newCompany);
                }

                if (!ModelState.IsValid)
                {
                    return(View(entity));
                }
                companyId = await _companyRepo.FindIdByDetails(centity.CompanyName, centity.CompanyEmail);

                var dentity = new Draughtsman
                {
                    DraughtsmanRegNo  = entity.DraughtsmanRegNo,
                    ApplicationUserId = newentity.ApplicationUserId,
                    CompanyId         = companyId
                };
                var draughtsman = _mapper.Map <Draughtsman>(dentity);

                var isSuccess = await _repo.Add(draughtsman);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong...");
                    return(View(entity));
                }
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }