public async Task<IHttpActionResult> UserSetRole(UserSetRoleViewModel userSetRoleViewModel) { IdentityResult result; IdentityUser user = await userManager.FindByIdAsync(userSetRoleViewModel.IdentityId); var exists = await userManager.IsInRoleAsync(user.Id, userSetRoleViewModel.RoleName); if (exists) { result = await userManager.RemoveFromRoleAsync(user.Id, userSetRoleViewModel.RoleName); } else { result = await userManager.AddToRoleAsync(user.Id, userSetRoleViewModel.RoleName); } var errorResult = GetErrorResult(result); if (errorResult != null) { return errorResult; } return Ok(); }
public async Task<IHttpActionResult> RegisterUserAndEmployee( RegisterUserAndEmployeeViewModel userAndEmployeeViewModel) { userAndEmployeeViewModel.StaffId = Guid.NewGuid(); if (!ModelState.IsValid) { return BadRequest(ModelState); } var user = new IdentityEmployee { StaffId = userAndEmployeeViewModel.StaffId, UserName = userAndEmployeeViewModel.EmailAddress }; var result = await userManager.CreateAsync(user, "123123"); var errorResult = GetErrorResult(result); if (errorResult != null) { return errorResult; } var userSetRoleViewModel = new UserSetRoleViewModel(); userSetRoleViewModel.IdentityId = user.Id; userSetRoleViewModel.RoleName = "user"; await UserSetRole(userSetRoleViewModel); var employee = new Employee { FirstName = userAndEmployeeViewModel.FirstName, LastName = userAndEmployeeViewModel.LastName, HolidayAllowance = 25, RemainingAllowance = 25, HolidayBookings = new List<EmployeeHolidayBooking>(), StaffId = userAndEmployeeViewModel.StaffId, TeamId = userAndEmployeeViewModel.TeamId }; employeeDataService.Create(employee); return Ok(); }