public async Task <IActionResult> AddUserToTeam(AddUserRequestModel model) { if (!ModelState.IsValid) { return(BadRequest()); } //check if user has account on system var password = string.Empty; var applicationUser = await _userManager.FindByEmailAsync(model.Email); if (applicationUser == null) { applicationUser = new ApplicationUser { Email = model.Email, UserName = model.Email }; password = PasswordHelper.CreatePassword(7); var result = await _userManager.CreateAsync(applicationUser, password); if (!result.Succeeded) { return(BadRequest(new ErrorResponse { ErrorDescription = "Your Email or Password is Incorrect" })); } //email password to user } //we have an applicationUser var teamUser = await _tenantRepository.GetUserByEmailAsync(model.Email, TenantId); if (teamUser != null) { return(new BadRequestObjectResult(new { error = "a user with that email already exists in this team" })); } teamUser = new User { ApplicationUserId = applicationUser.Id, Email = model.Email, UserRole = model.Role }; await _tenantRepository.AddUserToTeam(teamUser, TenantId); return(Ok(new { Message = $"advice user to check email for confirmation", password })); }
public async Task <IActionResult> Post([FromBody] CreateTeamModel model) { //TODO: Implement Detailed Error Checking if (ModelState.IsValid) { try { //TenantServerConfig tenantServerConfig, DatabaseConfig databaseConfig, CatalogConfig catalogConfig var databaseConfig = new DatabaseConfig { DatabasePassword = _configuration["DatabaseOptions:DatabasePassword"], DatabaseUser = _configuration["DatabaseOptions:DatabaseUser"], DatabaseServerPort = Int32.Parse(_configuration["DatabaseOptions:DatabaseServerPort"]), SqlProtocol = SqlProtocol.Tcp, ConnectionTimeOut = Int32.Parse(_configuration["DatabaseOptions:ConnectionTimeOut"]), }; var catalogConfig = new CatalogConfig { ServicePlan = _configuration["DatabaseOptions:ServicePlan"], CatalogDatabase = _configuration["DatabaseOptions:CatalogDatabase"], CatalogServer = _configuration["DatabaseOptions:CatalogServer"], // + ".database.windows.net" }; var tenantServerConfig = new TenantServerConfig { TenantServer = _configuration["DatabaseOptions:CatalogServer"],// + ".database.windows.net", TenantDatabase = _configuration["DatabaseOptions:TenantDatabase"], }; var team = new Team { Id = _utilities.GetTenantKey(model.TenantName), Name = model.TenantName, LogoLink = model.TenantLogoLink, }; //Create Shard, Add Team and Register Tenant against shard var shard = Sharding.CreateNewShard(tenantServerConfig.TenantDatabase, tenantServerConfig.TenantServer, databaseConfig.DatabaseServerPort, null); await _tenantRepository.AddTeam(team); var x = await Sharding.RegisterNewShard(team.Id, "", shard); //Add first user to team. Team Owner! var applicationUser = await _userService.GetApplicationUserAsync(); var user = new User { ApplicationUserId = applicationUser.Id, Email = applicationUser.Email, UserRole = Role.SuperAdministrator }; await _tenantRepository.AddUserToTeam(user, team.Id); return(Ok(new { team_id = team.Id, team_name = team.Name })); } catch (Exception ex) { //TODO: Log Error return(BadRequest(new { Error = ex.Message })); } } return(BadRequest(ModelState)); }