private bool CreateIzendaUser(CreateUserBindingModel model) { bool ret = false; try { //check if the tenant name provided if (!string.IsNullOrWhiteSpace(model.Tenant)) { //check if the tenant exists / create new if not Tenant tn = TenantHandler.GetTenantByName(model.Tenant); if (tn == null) { CreateTenantBindingModel tm = new CreateTenantBindingModel() { TenantName = model.Tenant, TenantId = model.Tenant }; TenantHandler th = new TenantHandler(); if (!string.IsNullOrEmpty(th.CreateTenant(tm))) { return(false); } } } string adminToken = IzendaTokenAuthorization.GetIzendaAdminToken(); string assignedRole = String.IsNullOrEmpty(model.SelectedRole) ? "Employee" : model.SelectedRole; Task <bool> createdUser = IzendaUtilities.CreateIzendaUser( model.Tenant, model.UserID, model.LastName, model.FirstName, model.IsAdmin, assignedRole, adminToken); // launch the task async and wait for the result. ret = createdUser.Result; } catch (Exception ex) { throw ex; } return(ret); }
public async Task <ActionResult> CreateUser(CreateUserViewModel model, string returnUrl) { var izendaAdminAuthToken = IzendaTokenAuthorization.GetIzendaAdminToken(); model.Tenants = IzendaUtilities.GetAllTenants(); // prevent null exception when redirected if (ModelState.IsValid) { int?tenantId = null; if (model.SelectedTenant != null) { tenantId = IzendaUtilities.GetTenantByName(model.SelectedTenant).Id; model.IsAdmin = false; } var user = new ApplicationUser { UserName = model.UserID, Email = model.UserID, Tenant_Id = tenantId, }; var result = await UserManager.CreateAsync(user); // Save new user into client DB if (result.Succeeded) // if successful, then start creating a user at Izenda DB { var assignedRole = !string.IsNullOrEmpty(model.SelectedRole) ? model.SelectedRole : "Employee"; // set default role if required. As an example, Employee is set by default if (!RoleManager.RoleExists(assignedRole)) // check assigned role exist in client DB. if not, assigned role is null { try { await RoleManager.CreateAsync(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(assignedRole)); result = await UserManager.AddToRoleAsync(user.Id, assignedRole); } catch (Exception e) { Debug.WriteLine(e); } } if (result.Succeeded) { user.Tenant = IzendaUtilities.GetTenantByName(model.SelectedTenant); // set client DB application user's tenant // Create a new user at Izenda DB var success = await IzendaUtilities.CreateIzendaUser( model.SelectedTenant, model.UserID, model.LastName, model.FirstName, model.IsAdmin, assignedRole, izendaAdminAuthToken); if (success) { return(RedirectToAction(returnUrl)); } else { FailedUserCreateAction(_unknownFailureMessage); } } } else { FailedUserCreateAction(_defaultUserFailureMessage); } AddErrors(result); } return(FailedUserCreateAction(_defaultUserFailureMessage)); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { int?tenantId = null; if (Input.SelectedTenantId != null) { tenantId = Input.SelectedTenantId; Input.IsAdmin = false; } var user = new IzendaUser { UserName = Input.UserID, Email = Input.UserID, TenantId = tenantId, }; var result = await _userManager.CreateAsync(user); // Save new user into client DB if (result.Succeeded) // if successful, then start creating a user at Izenda DB { var assignedRole = !string.IsNullOrEmpty(Input.SelectedRole) ? Input.SelectedRole : "Employee"; // set default role if required. As an example, Employee is set by default var isRoleExisting = _roleManager.FindByNameAsync(assignedRole); // check assigned role exist in client DB. if not, assigned role is null if (isRoleExisting == null) { try { await _roleManager.CreateAsync(new Microsoft.AspNetCore.Identity.IdentityRole(assignedRole)); result = await _userManager.AddToRoleAsync(user, assignedRole); } catch (Exception e) { Debug.WriteLine(e); } } else { result = await _userManager.AddToRoleAsync(user, assignedRole); } if (result.Succeeded) { var izendaAdminAuthToken = IzendaTokenAuthorization.GetIzendaAdminToken(); user.Tenant = _tenantManager.GetTenantById(Input.SelectedTenantId); // set client DB application user's tenant var tenantName = user.Tenant?.Name ?? null; // Create a new user at Izenda DB var success = await IzendaUtilities.CreateIzendaUser( tenantName, Input.UserID, Input.LastName, Input.FirstName, Input.IsAdmin, assignedRole, izendaAdminAuthToken); if (success) { return(LocalRedirect(returnUrl)); } } ModelState.AddModelError(string.Empty, "Failed to create a new user. User already exists in DB."); return(Page()); } } ModelState.AddModelError(string.Empty, "Failed to create a new user. Invalid model."); return(Page()); }
protected async void Submit(object sender, EventArgs e) { if (IsValid) { int? tenantId = null; bool isAdmin = IsAdminCheckBox.Checked; var selectedTenant = TenantList.SelectedValue; var userManager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>(); var roleManager = Context.GetOwinContext().Get <ApplicationRoleManager>(); var izendaAdminAuthToken = IzendaTokenAuthorization.GetIzendaAdminToken(); if (!string.IsNullOrEmpty(selectedTenant)) { tenantId = IzendaUtilities.GetTenantByName(selectedTenant)?.Id; isAdmin = false; if (tenantId == null) { return; } } var appUser = new ApplicationUser { UserName = UserID.Text, Email = UserID.Text, Tenant_Id = tenantId, }; var result = await userManager.CreateAsync(appUser); // attempt to create a new user in client DB if (result.Succeeded) // if successful, then start creating a user at Izenda DB { var assignedRole = !string.IsNullOrEmpty(RoleList.SelectedValue) ? RoleList.SelectedValue : "Employee"; // set default role if required. As an example, Employee is set by default if (!roleManager.RoleExists(assignedRole)) // check assigned role exist in client DB. if not, assigned role is null { try { await roleManager.CreateAsync(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(assignedRole)); result = await userManager.AddToRoleAsync(appUser.Id, assignedRole); } catch (Exception ex) { Debug.WriteLine(ex); } } if (result.Succeeded) { appUser.Tenant = IzendaUtilities.GetTenantByName(selectedTenant); // set client DB application user's tenant // Create a new user at Izenda DB var success = await IzendaUtilities.CreateIzendaUser( selectedTenant, UserID.Text, LastName.Text, FirstName.Text, isAdmin, assignedRole, izendaAdminAuthToken); if (success) { RedirectResultPage(true); } else { RedirectResultPage(false); } } else { RedirectResultPage(false); } } else { RedirectResultPage(false); } } }