public static UpsertPermissionsCriteria UpdatePermissions(String ExtProjID) { var users = new UserPermission[] { new UserPermission { ID = 3, Role = "SAMPLE_MANAGER", } }; var teams = new TeamPermission[] { new TeamPermission { ID = 109, } }; return(new UpsertPermissionsCriteria { ExtProjectID = ExtProjID, UserPermissions = users, TeamPermissions = teams }); }
public static string CreateTeam(string teamName, string orgName, TeamPermission permission) { string id = ""; try { string api = "https://api.github.com/orgs/" + orgName + "/teams"; var webClient = new System.Net.WebClient(); var cc = new CredentialCache(); cc.Add(new Uri(api), "Basic", new NetworkCredential(_UserName, _Password)); webClient.Credentials = cc; string data = "{\"name\":\""+teamName+"\",\"permission\":\""+(permission==TeamPermission.Read?"pull":"push")+"\"}"; string res = webClient.UploadString(api, data); id = res.Substring(res.LastIndexOf(":") + 1).Replace("}", ""); return id; } catch { } return id; }
public static string CreateTeam(string teamName, string orgName, TeamPermission permission) { string id = ""; try { string api = "https://api.github.com/orgs/" + orgName + "/teams"; var webClient = new System.Net.WebClient(); var cc = new CredentialCache(); cc.Add(new Uri(api), "Basic", new NetworkCredential(_UserName, _Password)); webClient.Credentials = cc; string data = "{\"name\":\"" + teamName + "\",\"permission\":\"" + (permission == TeamPermission.Read?"pull":"push") + "\"}"; string res = webClient.UploadString(api, data); id = res.Substring(res.LastIndexOf(":") + 1).Replace("}", ""); return(id); } catch { } return(id); }
public async Task <IActionResult> CreatePermission(CreateTeamPermissionViewModel model) { var team = await _context.Teams.SingleOrDefaultAsync(m => m.Slug == TeamSlug && m.IsActive); if (team == null) { return(NotFound()); } // find possible user from db var foundUser = await _context.Users.SingleOrDefaultAsync(a => a.CampusKerberos == model.UserLookup.ToLower() || a.NormalizedEmail == model.UserLookup.SafeToUpper()); // otherwise, create the user if (foundUser == null) { // find user Person user = null; if (model.UserLookup.Contains("@")) { user = await _directorySearchService.GetByEmail(model.UserLookup.ToLower()); } else { var directoryUser = await _directorySearchService.GetByKerberos(model.UserLookup.ToLower()); if (directoryUser != null && !directoryUser.IsInvalid) { user = directoryUser.Person; } } // create user if (user != null) { var userToCreate = new User { Email = user.Mail, UserName = user.Mail, CampusKerberos = user.Kerberos, Name = user.FullName }; var userPrincipal = new ClaimsPrincipal(); userPrincipal.AddIdentity(new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, userToCreate.CampusKerberos), new Claim(ClaimTypes.Name, userToCreate.Name) })); var loginInfo = new ExternalLoginInfo(userPrincipal, "UCDavis", userToCreate.CampusKerberos, null); var createResult = await _userManager.CreateAsync(userToCreate); if (createResult.Succeeded) { await _userManager.AddLoginAsync(userToCreate, loginInfo); foundUser = userToCreate; } } } ModelState.Clear(); TryValidateModel(model); // find or create failed if (foundUser == null) { ModelState.AddModelError("UserLookup", "User Not Found"); return(View(model)); } // look for existing permissions if (await _context.TeamPermissions.AnyAsync(a => a.TeamId == team.Id && a.UserId == foundUser.Id && a.RoleId == model.SelectedRole)) { ModelState.AddModelError("UserLookup", "User with selected role already exists."); return(View(model)); } // check model if (!ModelState.IsValid) { return(View(model)); } // create permission and save it var role = await _context.TeamRoles.SingleAsync(a => a.Id == model.SelectedRole); var teamPermission = new TeamPermission { TeamId = team.Id, Role = role, UserId = foundUser.Id }; _context.TeamPermissions.Add(teamPermission); await _context.SaveChangesAsync(); // send user notification try { await _emailService.SendNewTeamMemberNotice(team, foundUser, role); } catch (Exception ex) { Log.Error(ex, "New Member Notice email failed."); } return(RedirectToAction(nameof(Roles), new { team = team.Slug })); }
public async Task SetRolesAsync(string orgId, string teamId, string username, TeamPermission permission) { if (!await organisationRepository.ExistsByAsync(o => o.Id == orgId)) { throw new ArgumentException($"The organisation {orgId} not exists"); } var teamEntity = await this.teamRepository.GetByAsync(t => t.OrganisationId == orgId && t.TeamCode == teamId); if (teamEntity == null) { throw new ArgumentException($"The team {teamId} on organisation {orgId} not exists"); } var userEntity = await this.repository.GetByAsync(u => u.OrganisationId == orgId && u.Username == username); if (userEntity == null) { throw new ArgumentException($"The user {username} on organisation {orgId} not exists"); } userEntity.TeamsPermissions.RemoveAll(p => p.TeamCode == teamId); userEntity.TeamsPermissions.Add(permission.ToEntity()); teamEntity.Users.RemoveAll(u => userEntity.Username.Equals(u.Username)); teamEntity.Users.Add(new Domain.Entities.TeamUser(userEntity)); await this.teamRepository.UpdateAsync(teamEntity); await this.repository.UpdateAsync(userEntity); }