public bool Create(TeamModel model) { if (model == null) throw new ArgumentException("team"); if (model.Name == null) throw new ArgumentException("name"); using (var database = new BonoboGitServerContext()) { var team = new Team { Name = model.Name, Description = model.Description }; database.Teams.Add(team); if (model.Members != null) { AddMembers(model.Members, team, database); } try { database.SaveChanges(); } catch (UpdateException) { return false; } } return true; }
public override string[] GetAllRoles() { using (var database = new BonoboGitServerContext()) { return database.Roles.Select(i => i.Name).ToArray(); } }
public bool Create(RepositoryModel model) { if (model == null) throw new ArgumentException("model"); if (model.Name == null) throw new ArgumentException("name"); using (var database = new BonoboGitServerContext()) { var repository = new Repository { Name = model.Name, Logo = model.Logo, Group = model.Group, Description = model.Description, Anonymous = model.AnonymousAccess, AuditPushUser = model.AuditPushUser, }; database.Repositories.Add(repository); AddMembers(model.Users, model.Administrators, model.Teams, repository, database); try { database.SaveChanges(); } catch (UpdateException) { return false; } } return true; }
public IList<RepositoryModel> GetAllRepositories() { using (var db = new BonoboGitServerContext()) { var dbrepos = db.Repositories.Select(repo => new { Name = repo.Name, Group = repo.Group, Description = repo.Description, AnonymousAccess = repo.Anonymous, Users = repo.Users.Select(i => i.Username), Teams = repo.Teams.Select(i => i.Name), Administrators = repo.Administrators.Select(i => i.Username), AuditPushUser = repo.AuditPushUser, Logo = repo.Logo }).ToList(); return dbrepos.Select(repo => new RepositoryModel { Name = repo.Name, Group = repo.Group, Description = repo.Description, AnonymousAccess = repo.AnonymousAccess, Users = repo.Users.ToArray(), Teams = repo.Teams.ToArray(), Administrators = repo.Administrators.ToArray(), AuditPushUser = repo.AuditPushUser, Logo = repo.Logo }).ToList(); } }
public bool CreateUser(string username, string password, string name, string surname, string email) { if (String.IsNullOrEmpty(username)) throw new ArgumentException("Value cannot be null or empty.", "userName"); if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password"); if (String.IsNullOrEmpty(name)) throw new ArgumentException("Value cannot be null or empty.", "name"); if (String.IsNullOrEmpty(surname)) throw new ArgumentException("Value cannot be null or empty.", "surname"); if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); username = username.ToLowerInvariant(); using (var database = new BonoboGitServerContext()) { var user = new User { Username = username, Password = EncryptPassword(password), Name = name, Surname = surname, Email = email, }; database.Users.Add(user); try { database.SaveChanges(); } catch (UpdateException) { return false; } } return true; }
public bool AllowsAnonymous(string project) { using (var database = new BonoboGitServerContext()) { var repo = database.Repositories.FirstOrDefault(i => i.Name == project); return (repo != null && repo.Anonymous); } }
public bool AllowsAnonymous(string project) { using (var database = new BonoboGitServerContext()) { var isAllowsAnonymous = database.Repositories.Any(repo => repo.Name == project && repo.Anonymous); return isAllowsAnonymous; } }
public RepositoryModel GetRepository(string name) { if (name == null) throw new ArgumentException("name"); using (var db = new BonoboGitServerContext()) { return ConvertToModel(db.Repositories.FirstOrDefault(i => i.Name == name)); } }
public override void CreateRole(string roleName) { using (var database = new BonoboGitServerContext()) { database.Roles.Add(new Role { Name = roleName, }); } }
public override string[] FindUsersInRole(string roleName, string usernameToMatch) { using (var database = new BonoboGitServerContext()) { var users = database.Users .Where(us => us.Username.Contains(usernameToMatch) && us.Roles.Any(role => role.Name == roleName)) .Select(us => us.Username) .ToArray(); return users; } }
public IList<Models.RepositoryModel> GetAllRepositories() { using (var db = new BonoboGitServerContext()) { var result = new List<RepositoryModel>(); foreach (var item in db.Repositories) { result.Add(ConvertToModel(item)); } return result; } }
public IList<UserModel> GetAllUsers() { using (var db = new BonoboGitServerContext()) { return db.Users.Include("Roles").ToList().Select(item => new UserModel { Username = item.Username, Name = item.Name, Surname = item.Surname, Email = item.Email, Roles = item.Roles.Select(i => i.Name).ToArray(), }).ToList(); } }
public bool IsRepositoryAdministrator(string username, string project) { using (var database = new BonoboGitServerContext()) { username = username.ToLowerInvariant(); var isRepoAdmin = database.Users.Where(us => us.Username == username) .Any( us => (us.Roles.Any(role => role.Name == Definitions.Roles.Administrator) || us.AdministratedRepositories.Any(ar => ar.Name == project))); return isRepoAdmin; } }
public TeamModel GetTeam(string name) { if (name == null) throw new ArgumentException("name"); using (var db = new BonoboGitServerContext()) { var team = db.Teams.FirstOrDefault(i => i.Name == name); return team == null ? null : new TeamModel { Name = team.Name, Description = team.Description, Members = team.Users.Select(m => m.Username).ToArray(), }; } }
public void DeleteUser(string username) { using (var database = new BonoboGitServerContext()) { var user = database.Users.FirstOrDefault(i => i.Username == username); if (user != null) { user.AdministratedRepositories.Clear(); user.Roles.Clear(); user.Repositories.Clear(); user.Teams.Clear(); database.Users.Remove(user); database.SaveChanges(); } } }
public void Delete(string name) { if (name == null) throw new ArgumentException("name"); using (var db = new BonoboGitServerContext()) { var team = db.Teams.FirstOrDefault(i => i.Name == name); if (team != null) { team.Repositories.Clear(); team.Users.Clear(); db.Teams.Remove(team); db.SaveChanges(); } } }
public bool IsRepositoryAdministrator(string username, string project) { using (var database = new BonoboGitServerContext()) { var user = database.Users.FirstOrDefault(i => i.Username == username); if (user != null) { if (user.Roles.FirstOrDefault(i => i.Name == Definitions.Roles.Administrator) != null || user.AdministratedRepositories.FirstOrDefault(i => i.Name == project) != null) { return true; } } } return false; }
public override string[] FindUsersInRole(string roleName, string usernameToMatch) { var result = new HashSet<string>(); using (var database = new BonoboGitServerContext()) { var matchingRoles = database.Roles.Where(i => i.Name == roleName && i.Users.Where(user => user.Username.Contains(usernameToMatch)).Count() > 0); var usernames = matchingRoles.Select(i => i.Users.Select(u => u.Username)); foreach (var roleUsernames in usernames) { foreach (var username in roleUsernames) { result.Add(username); } } } return result.ToArray(); }
public void Delete(string name) { if (name == null) throw new ArgumentException("name"); using (var db = new BonoboGitServerContext()) { var repo = db.Repositories.FirstOrDefault(i => i.Name == name); if (repo != null) { repo.Administrators.Clear(); repo.Users.Clear(); repo.Teams.Clear(); db.Repositories.Remove(repo); db.SaveChanges(); } } }
public override void AddUsersToRoles(string[] usernames, string[] roleNames) { using (var database = new BonoboGitServerContext()) { var roles = database.Roles.Where(i => roleNames.Contains(i.Name)); var users = database.Users.Where(i => usernames.Contains(i.Username)); foreach (var role in roles) { foreach (var user in users) { role.Users.Add(user); } } database.SaveChanges(); } }
public UserModel GetUser(string username) { if (String.IsNullOrEmpty(username)) throw new ArgumentException("Value cannot be null or empty.", "username"); username = username.ToLowerInvariant(); using (var db = new BonoboGitServerContext()) { var user = db.Users.FirstOrDefault(i => i.Username == username); return user == null ? null : new UserModel { Username = user.Username, Name = user.Name, Surname = user.Surname, Email = user.Email, Roles = user.Roles.Select(i => i.Name).ToArray(), }; } }
public IList<UserModel> GetAllUsers() { using (var db = new BonoboGitServerContext()) { var result = new List<UserModel>(); foreach (var item in db.Users) { result.Add(new UserModel { Username = item.Username, Name = item.Name, Surname = item.Surname, Email = item.Email, Roles = item.Roles.Select(i => i.Name).ToArray(), }); } return result; } }
public bool HasPermission(string username, string project) { using (var database = new BonoboGitServerContext()) { var user = database.Users.FirstOrDefault(i => i.Username == username); var repository = database.Repositories.FirstOrDefault(i => i.Name == project); if (user != null && project != null) { if (user.Roles.FirstOrDefault(i => i.Name == Definitions.Roles.Administrator) != null || user.Repositories.FirstOrDefault(i => i.Name == project) != null || user.AdministratedRepositories.FirstOrDefault(i => i.Name == project) != null || user.Teams.Select(i => i.Name).FirstOrDefault(t => repository.Teams.Select(i => i.Name).Contains(t)) != null) { return true; } } } return false; }
public IList<TeamModel> GetAllTeams() { using (var db = new BonoboGitServerContext()) { var result = new List<TeamModel>(); foreach (var item in db.Teams) { result.Add(new TeamModel { Name = item.Name, Description = item.Description, Members = item.Users.Select(i => i.Username).ToArray(), Repositories = item.Repositories.Select(m => m.Name).ToArray(), }); } return result; } }
public void AddUsersToRoles(string[] usernames, string[] roleNames) { using (var database = new BonoboGitServerContext()) { usernames = usernames.Select(i => i.ToLowerInvariant()).ToArray(); var roles = database.Roles.Where(i => roleNames.Contains(i.Name)).ToList(); var users = database.Users.Where(i => usernames.Contains(i.Username)).ToList(); foreach (var role in roles) { foreach (var user in users) { role.Users.Add(user); } } database.SaveChanges(); } }
public ActionResult ResetPassword(string digest) { string username; digest = HttpUtility.UrlDecode(digest); var cacheObj = MvcApplication.Cache[digest]; if ( cacheObj != null ) { using (var db = new BonoboGitServerContext()) { username = cacheObj.ToString(); var user = db.Users.FirstOrDefault(x => x.Username.Equals(username, StringComparison.OrdinalIgnoreCase)); return View(new ResetPasswordModel { Username = username }); } } else { ModelState.AddModelError("", "Password reset link was not valid"); return RedirectToAction("Index", "Home"); } }
public IList<TeamModel> GetAllTeams() { using (var db = new BonoboGitServerContext()) { var dbTeams = db.Teams.Select(team => new { Name = team.Name, Description = team.Description, Members = team.Users.Select(i => i.Username), Repositories = team.Repositories.Select(m => m.Name), }).ToList(); return dbTeams.Select(item => new TeamModel { Name = item.Name, Description = item.Description, Members = item.Members.ToArray(), }).ToList(); } }
public ActionResult ResetPassword(string digest) { string username = CheckForPasswordResetUsername(digest); if (username != null ) { using (var db = new BonoboGitServerContext()) { var user = db.Users.FirstOrDefault(x => x.Username.Equals(username, StringComparison.OrdinalIgnoreCase)); if (user == null) { throw new UnauthorizedAccessException("Unknown user " + username); } return View(new ResetPasswordModel { Username = username, Digest = digest}); } } else { ModelState.AddModelError("", "Password reset link was not valid"); return RedirectToAction("Index", "Home"); } }
public ActionResult ResetPassword(ResetPasswordModel model) { if (ModelState.IsValid) { using (var db = new BonoboGitServerContext()) { var user = db.Users.FirstOrDefault(x => x.Username.Equals(model.Username, StringComparison.OrdinalIgnoreCase)); if (user == null) { TempData["ResetSuccess"] = false; Response.AppendToLog("FAILURE"); } else { MembershipService.UpdateUser(model.Username, user.Name, user.Surname, user.Email, model.Password); TempData["ResetSuccess"] = true; } } } return View(model); }
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { using (var database = new BonoboGitServerContext()) { var role = database.Roles.FirstOrDefault(i => i.Name == roleName); if (role != null) { if (throwOnPopulatedRole) { if (role.Users.Count > 0) { throw new InvalidOperationException("Can't delete role with members."); } } database.Roles.Remove(role); database.SaveChanges(); return true; } return false; } }
private void AddMembers(IEnumerable <string> users, IEnumerable <string> admins, IEnumerable <string> teams, Repository repo, BonoboGitServerContext database) { if (admins != null) { var administrators = database.Users.Where(i => admins.Contains(i.Username)); foreach (var item in administrators) { repo.Administrators.Add(item); } } if (users != null) { var permittedUsers = database.Users.Where(i => users.Contains(i.Username)); foreach (var item in permittedUsers) { repo.Users.Add(item); } } if (teams != null) { var permittedTeams = database.Teams.Where(i => teams.Contains(i.Name)); foreach (var item in permittedTeams) { repo.Teams.Add(item); } } }