コード例 #1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = _mapper.Map <CertificationPlatformUser>(model);
                user.Id            = _keyGenerator.GenerateNewId();
                user.SecurityStamp = _keyGenerator.GenerateNewGuid();

                var result = await _userManager.CreateAsync(user, model.Password);

                if (!await _roleManager.RoleExistsAsync("Worker"))
                {
                    await _roleManager.CreateAsync(new CertificationPlatformUserRole("Worker"));
                }

                var addToRole = await _userManager.AddToRoleAsync(user, "Worker");

                if (result.Succeeded)
                {
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);

                    var emailToSend = _emailSender.GenerateEmailMessage(model.Email, user.FirstName + " " + user.LastName, "register", callbackUrl);
                    await _emailSender.SendEmailAsync(emailToSend);

                    #region EntityLogs

                    var logInfoRegister = _logger.GenerateLogInformation(this.User.Identity.Name, this.ControllerContext.RouteData.Values["action"].ToString(), LogTypeOfAction.TypesOfActions[0], LogDescriptions.DescriptionOfActionOnEntity["registerUser"]);
                    _logger.AddUserLog(user, logInfoRegister);

                    #endregion

                    #region PersonalUserLogs

                    var createdUser = _context.userRepository.GetUserById(user.Id);
                    _context.personalLogRepository.CreatePersonalUserLog(createdUser);

                    var logInfoPersonalRegister = _context.personalLogRepository.GeneratePersonalLogInformation(this.User.Identity.Name, this.ControllerContext.RouteData.Values["action"].ToString(), LogDescriptions.DescriptionOfPersonalUserLog["registerUser"]);
                    _context.personalLogRepository.AddPersonalUserLog(createdUser.Id, logInfoPersonalRegister);

                    #endregion

                    return(RedirectToAction("Login", "Account", new { message = "Na Twój adres email została wysłana wiadomość z informacją dotyczącą potwierdzenia adresu email." }));
                }

                ModelState.AddModelError(string.Empty, "Użytkownik o podanym adresie email już widnieje w systemie.");
                AddErrors(result);
            }

            return(View(model));
        }