Beispiel #1
0
        public async Task RegisterStudent(RegisterNewStudentUserAccountView viewModel)
        {
            var companies = _companyRepository.GetAll();

            if (companies.Result == null || companies.Result.ToList().Count == 0)
            {
                Company company = new Company()
                {
                    Name = "Test"
                };
                await _companyRepository.Create(company);

                ApplicationConstants.CurrentCompanyId = company.Id;
            }

            var teacher = new Student();

            teacher.Email     = viewModel.Email;
            teacher.UserName  = viewModel.Username;
            teacher.CompanyId = ApplicationConstants.CurrentCompanyId;

            if (!viewModel.Password.Equals(viewModel.ConfirmPassword))
            {
                throw new AdminException("Passwords are different");
            }

            if (!(await _userManager.FindByEmailAsync(viewModel.Email) is null) || !(await _userManager.FindByNameAsync(viewModel.Username) is null))
            {
                throw new AdminException("Account with such Email or UserID already exists");
            }

            teacher.BirthDate = _dateParseHelper.ParseStringToDatetime(viewModel.BirthDate);

            _accountMapper.MapStudentViewModelToModel(viewModel, teacher);
            /// TODOOOOOOOOOOO
            teacher.EmailConfirmed = true;
            ///
            IdentityResult result = await _userManager.CreateAsync(teacher, viewModel.Password);

            if (!result.Succeeded)
            {
                string responseError = result.GetFirstError();

                throw new AdminException(responseError);
            }

            ApplicationUser registeredTeacher = await _userManager.FindByNameAsync(viewModel.Username);

            await _userManager.AddToRoleAsync(registeredTeacher, ApplicationConstants.USER_ROLE);
        }
Beispiel #2
0
        public async Task RegisterNewUser(RegisterNewUserAccountView viewModel)
        {
            var user = new AppUser();

            user.Email    = viewModel.Email;
            user.UserName = viewModel.Username;

            if (!viewModel.Password.Equals(viewModel.ConfirmPassword))
            {
                throw new AccountException("Passwords are different");
            }

            if (!(await _userManager.FindByEmailAsync(viewModel.Email) is null) || !(await _userManager.FindByNameAsync(viewModel.Username) is null))
            {
                throw new AccountException("Account with such Email or UserID already exists");
            }

            IdentityResult result = await _userManager.CreateAsync(user, viewModel.Password);

            if (!result.Succeeded)
            {
                string responseError = result.GetFirstError();

                throw new AccountException(responseError);
            }



            AppUser userRegistered = await _userManager.FindByEmailAsync(viewModel.Email);

            if (viewModel.Type.ToString() == AppConstants.CLIENT_ROLE)
            {
                await _userManager.AddToRoleAsync(userRegistered, AppConstants.CLIENT_ROLE);
            }
            else if (viewModel.Type.ToString() == AppConstants.DRIVER_ROLE)
            {
                await _userManager.AddToRoleAsync(userRegistered, AppConstants.DRIVER_ROLE);
            }
            else
            {
                throw new AccountException("Cant register this type");
            }
            //TO DO
            //await EmailConfirmation(userRegistered, viewModel.CurrentUrl);
        }
Beispiel #3
0
        public async Task CreateDriver(CreateNewDriverAdminView viewModel)
        {
            var driver = new Driver();

            driver.Email    = viewModel.Email;
            driver.UserName = viewModel.Username;

            if (!viewModel.Password.Equals(viewModel.ConfirmPassword))
            {
                throw new AdminException("Passwords are different");
            }

            if (!(await _userManager.FindByEmailAsync(viewModel.Email) is null) || !(await _userManager.FindByNameAsync(viewModel.Username) is null))
            {
                throw new AdminException("Account with such Email or UserID already exists");
            }

            driver.BirthDate = _dateParseHelper.ParseStringToDatetime(viewModel.BirthDate);

            _accountMapper.MapDriverViewModelToModel(viewModel, driver);
            /// TODOOOOOOOOOOO
            driver.EmailConfirmed = true;
            ///
            IdentityResult result = await _userManager.CreateAsync(driver, viewModel.Password);

            if (!result.Succeeded)
            {
                string responseError = result.GetFirstError();

                throw new AdminException(responseError);
            }

            AppUser registeredDriver = await _userManager.FindByNameAsync(viewModel.Username);

            await _userManager.AddToRoleAsync(registeredDriver, AppConstants.DRIVER_ROLE);
        }