public async Task <object> Register([FromBody] RegisterEmployeeVM model) { var token = Request.Headers["Authorization"].ToString().Replace("Bearer ", ""); if (!PatManagerVerify.CheckIfManager(token, context)) { //if the user isn't a manager then quit the method. Only managers are able to hire new people return(null); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await userManager.CreateAsync(user, model.Password); if (result.Succeeded) { //add the user to the role of employee straight after being added by the manager UserRoleRepo repo = new UserRoleRepo(service); await repo.AddUserRole(model.Email, "employee"); //now add the user details UserRepo userRepo = new UserRepo(context, service); userRepo.AddUserDetails(user.Id, model.FirstName, model.LastName); //The user is never signed in when they are registered. As they are only ever registered through the manager //creating the new user //await signInManager.SignInAsync(user, false); return(await GenerateJwtToken(model.Email, user)); } throw new ApplicationException("UNKNOWN_ERROR"); }
private void addServicesToEmployee(RegisterEmployeeVM _RegisterEmployee, EmployeeUser _userEmployee) { List <string> serviceList = _RegisterEmployee.listaServiciosId.Split(',').ToList(); foreach (var serviceId in serviceList) { Guid id = Guid.Parse(serviceId); var service = _queries.GetServiceIncludeApplicationUserFirstOrDefault(id); _userEmployee.ServiceLst.Add(service); } }
private EmployeeUser setEmployeesAttributes(RegisterEmployeeVM _RegisterEmployee) { var _userEmployee = new EmployeeUser(); _userEmployee.UserName = _RegisterEmployee.email; _userEmployee.Email = _RegisterEmployee.email; _userEmployee.Name = _RegisterEmployee.Nombre; var Employer = _queries.GetApplicationUserIncludeServiceLst(_RegisterEmployee.idEmpleador); _userEmployee.EmployedBy = Employer; return(_userEmployee); }
private async Task sendRegistrationCodeToEmployee(RegisterEmployeeVM _RegisterEmployee, EmployeeUser _userEmployee) { _logger.LogInformation("EmployedUser created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(_userEmployee); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = _userEmployee.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(_RegisterEmployee.email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _userManager.AddToRoleAsync(_userEmployee, SD.EmployeeEndUser); }
public async Task <IActionResult> PostRegisterEmployee([FromBody] RegisterEmployeeVM _RegisterEmployee) { if (_RegisterEmployee.email == "") { return(NotFound()); } if (_RegisterEmployee.Nombre == "") { return(NotFound()); } if (_RegisterEmployee.pw == "") { return(NotFound()); } if (_RegisterEmployee.idEmpleador == "") { return(NotFound()); } EmployeeUser _userEmployee = setEmployeesAttributes(_RegisterEmployee); addServicesToEmployee(_RegisterEmployee, _userEmployee); var result2 = await _userManager.CreateAsync(_userEmployee, _RegisterEmployee.pw); if (result2.Succeeded) { if (!await _roleManager.RoleExistsAsync(SD.EmployeeEndUser)) { await _roleManager.CreateAsync(new IdentityRole(SD.EmployeeEndUser)); } await sendRegistrationCodeToEmployee(_RegisterEmployee, _userEmployee); return(Ok()); } foreach (var error in result2.Errors) { ModelState.AddModelError(string.Empty, error.Description); } return(NotFound()); }