public async Task <IActionResult> RegisterAdmin([FromBody] AdminRegistrationDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userIdentity = Mapper.Map <AppUser>(model); var result = await _userManager.CreateAsync(userIdentity, model.Password); if (!result.Succeeded) { if (result.Errors.FirstOrDefault(o => o.Code == "DuplicateUserName") != null) { ModelState.AddModelError(nameof(CustomerRegistrationDto), "User name already taken"); } return(BadRequest(ModelState)); } var admin = Mapper.Map <Admin>(model); admin.IdentityId = userIdentity.Id; var addres = await _usersRepository.AddAdmin(admin); var customerFromAdmin = Mapper.Map <Customer>(admin); var addDbres = await _usersRepository.AddCustomer(customerFromAdmin); if (!addDbres || !addres) { return(Conflict()); } var adminDto = Mapper.Map <AdminDto>(model); adminDto.Id = admin.Id; if (!userIdentity.EmailConfirmed) { var confirmToken = await _userManager.GenerateEmailConfirmationTokenAsync(userIdentity); var emailConfirmUrl = Url.RouteUrl("ConfirmEmail", new { uid = userIdentity.Id, token = confirmToken }, this.Request.Scheme); try { await _emailSender.SendEmailAsync(userIdentity.Email, "Confirm your account", $"Please confirm your account by this ref <a href=\"{emailConfirmUrl}\">link</a>"); } catch { ModelState.AddModelError("email", "Failed to send confirmation letter"); return(BadRequest(ModelState)); } } return(CreatedAtRoute("GetAdmin", new { id = admin.Id }, adminDto)); }
public async Task <IHttpActionResult> RegisterAdmin(AdminRegistrationDto userModel) { var userData = IdentityHelper.GetLoggedInUser(RequestContext); logger.Info("Register Admin {@adminReg} by {@userData}", userModel, userData); var result = await service.RegisterAdmin(userModel); logger.Info("Created Admin {@userId}", result.AdminId); return(CreatedAtRoute("GetAdminById", new { adminId = result.AdminId }, result)); }
/// <summary> /// Register an admin /// </summary> /// <param name="userModel"></param> /// <returns></returns> public async Task <AdminDto> RegisterAdmin(AdminRegistrationDto userModel) { logger.Trace("layer={0} class={1} method={2} stage={3}", "service", "users", "registerAdmin", "init"); AdminUser user = Converters.AdminsConverter.AdminRegistrationDtoToAdmin(userModel); var result = await db.AuthRepository.RegisterAdminUser(user, userModel.Password); if (!result.Succeeded) { var ex = new UserRegistrationException(result.Errors.ToArray()); ex.Data.Add("IdentityResultErrors", result.Errors.ToArray()); throw ex; } user = await db.AuthRepository.FindUserByUserName(userModel.UserName) as AdminUser; return(Converters.AdminsConverter.AdminToAdminDto(user)); }
public ActionResult <Admin> RegisterMethod(AdminRegistrationDto _NewAdmin) { //Garbage collect invalid admins. Here until a timed action is set on the hosted server. dbQuery.DeleteOutOfDateInvalidAdmins(); Admin NewAdmin = new Admin() { username = _NewAdmin.username, email = _NewAdmin.email, password = _NewAdmin.password, token = _tGen.GenerateToken() }; string verdict = validator.ValidateAdmin(NewAdmin); if (verdict == "pass") { string unhashed_password = _NewAdmin.password; NewAdmin.password = authenticator.HashString(_NewAdmin.password); Admin RegisteredAdmin; try{ RegisteredAdmin = dbQuery.SaveNewAdmin(NewAdmin); }catch (System.ArgumentException e) { return(StatusCode(400, e.Message)); } dbQuery.CreateNewDataPlan(RegisteredAdmin.admin_id); mailer.SendRegistrationConfirmationEmail(RegisteredAdmin.email, RegisteredAdmin.admin_id, RegisteredAdmin.token, _currentDomain); return(RegisteredAdmin); } else { return(StatusCode(400, verdict)); } }
public ActionResult <Admin> Register([FromBody] AdminRegistrationDto NewAdmin) { return(methods.RegisterMethod(NewAdmin)); }