private void CheckForNewRepositories()
        {
            if (!Directory.Exists(UserConfiguration.Current.Repositories))
            {
                // We don't want an exception if the repo dir no longer exists, 
                // as this would make it impossible to start the server
                return;
            }
            IEnumerable<string> directories = Directory.EnumerateDirectories(UserConfiguration.Current.Repositories);
            foreach (string directory in directories)
            {
                string name = Path.GetFileName(directory);

                RepositoryModel repository = _repositoryRepository.GetRepository(name);
                if (repository == null)
                {
                    if (LibGit2Sharp.Repository.IsValid(directory))
                    {
                        repository = new RepositoryModel();
                        repository.Id = Guid.NewGuid();
                        repository.Description = "Discovered in file system.";
                        repository.Name = name;
                        repository.AnonymousAccess = false;
                        repository.Users = new UserModel[0];
                        repository.Teams = new TeamModel[0];
                        repository.Administrators = new UserModel[0];
                        if (repository.NameIsValid)
                        {
                            _repositoryRepository.Create(repository);
                        }
                    }
                }
            }
        }
        public override bool Create(RepositoryModel model)
        {
            if (model == null) throw new ArgumentException("model");
            if (model.Name == null) throw new ArgumentException("name");

            using (var database = CreateContext())
            {
                model.Id = Guid.NewGuid();
                var repository = new Repository
                {
                    Id = model.Id,
                    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.Select(x => x.Id), model.Administrators.Select(x => x.Id), model.Teams.Select(x => x.Id), repository, database);
                try
                {
                    database.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    return false;
                }
                catch (UpdateException)
                {
                    return false;
                }
                return true;
            }
        }
        public bool Create(RepositoryModel model)
        {
            if (model == null) throw new ArgumentException("model");
            if (model.Name == null) throw new ArgumentException("name");

            using (var database = new DataEntities())
            {
                var repository = new Repository
                {
                    Name = model.Name,
                    Description = model.Description,
                    Anonymous = model.AnonymousAccess,
                };
                database.AddToRepository(repository);
                AddMembers(model.Users, model.Administrators, model.Teams, repository, database);
                try
                {
                    database.SaveChanges();
                }
                catch (UpdateException)
                {
                    return false;
                }
            }

            return true;
        }
        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 bool Create(RepositoryModel repository)
        {
            // Make sure we don't already have a repo with this name
            if (GetRepository(repository.Name) != null)
            {
                return false;
            }

            repository.Id = Guid.NewGuid();
            return ADBackend.Instance.Repositories.Add(SanitizeModel(repository));
        }
 public void Update(RepositoryModel repository)
 {
     if (repository.RemoveLogo)
     {
         repository.Logo = null;
     }
     else if (repository.Logo == null)
     {
         // If we're given a null logo, then we need to preserve the existing one
         repository.Logo = GetRepository(repository.Id).Logo;
     }
     ADBackend.Instance.Repositories.Update(SanitizeModel(repository));
 }
        private void CheckForNewRepositories()
        {
            IEnumerable<string> directories = Directory.EnumerateDirectories(UserConfiguration.Current.Repositories);
            foreach (string directory in directories)
            {
                string name = Path.GetFileName(directory);

                RepositoryModel repository = _repositoryRepository.GetRepository(name);
                if (repository == null)
                {
                    repository = new RepositoryModel();
                    repository.Description = "Discovered in file system.";
                    repository.Name = name;
                    repository.AnonymousAccess = false;
                    _repositoryRepository.Create(repository);
                }
            }
        }
        private RepositoryModel SanitizeModel(RepositoryModel model)
        {
            if (model.Administrators == null)
            {
                model.Administrators = new string[0];
            }

            if (model.Users == null)
            {
                model.Users = new string[0];
            }

            if (model.Teams == null)
            {
                model.Teams = new string[0];
            }

            return model;
        }
        private bool CheckAnonymousPermission(RepositoryModel repository, RepositoryAccessLevel requiredLevel)
        {
            if (!repository.AnonymousAccess)
            {
                // There's no anon access at all to this repo
                return false;
            }

            switch (requiredLevel)
            {
                case RepositoryAccessLevel.Pull:
                    return true;
                case RepositoryAccessLevel.Push:
                    return repository.AllowAnonymousPush == RepositoryPushMode.Yes || (repository.AllowAnonymousPush == RepositoryPushMode.Global && UserConfiguration.Current.AllowAnonymousPush);
                case RepositoryAccessLevel.Administer:
                    // No anonymous administrators ever
                    return false;
                default:
                    throw new ArgumentOutOfRangeException("requiredLevel", requiredLevel, null);
            }
        }
        private bool CheckNamedUserPermission(Guid userId, RepositoryModel repository, RepositoryAccessLevel requiredLevel)
        {
            if (userId == Guid.Empty) { throw new ArgumentException("Do not pass anonymous user id", "userId"); }

            bool userIsAnAdministrator = IsAnAdminstrator(userId, repository);
            var userIsATeamMember =
                TeamRepository.GetTeams(userId)
                    .Any(x => repository.Teams.Select(y => y.Name).Contains(x.Name, StringComparer.OrdinalIgnoreCase));
            var userIsARepoUser = repository.Users.Any(x => x.Id == userId);

            switch (requiredLevel)
            {
                case RepositoryAccessLevel.Push:
                case RepositoryAccessLevel.Pull:
                    return userIsARepoUser || userIsATeamMember || userIsAnAdministrator;

                case RepositoryAccessLevel.Administer:
                    return userIsAnAdministrator;
                default:
                    throw new ArgumentOutOfRangeException("requiredLevel", requiredLevel, null);
            }
        }
 public static RepositoryDetailModel ConvertRepositoryModel(RepositoryModel model, IPrincipal User)
 {
     return model == null ? null : new RepositoryDetailModel
     {
         Id = model.Id,
         Name = model.Name,
         Group = model.Group,
         Description = model.Description,
         Users = model.Users,
         Administrators = model.Administrators,
         Teams = model.Teams,
         IsCurrentUserAdministrator = model.Administrators.Select(x => x.Id).Contains(User.Id()),
         AllowAnonymous = model.AnonymousAccess,
         AllowAnonymousPush = model.AllowAnonymousPush,
         Status = GetRepositoryStatus(model),
         AuditPushUser = model.AuditPushUser,
         Logo = new RepositoryLogoDetailModel(model.Logo),
         LinksUseGlobal = model.LinksUseGlobal,
         LinksRegex = model.LinksRegex,
         LinksUrl = model.LinksUrl,
     };
 }
        public bool Create(RepositoryModel model)
        {
            if (model == null) throw new ArgumentException("model");
            if (model.Name == null) throw new ArgumentException("name");

            using (var database = CreateContext())
            {
                model.EnsureCollectionsAreValid();
                model.Id = Guid.NewGuid();
                var repository = new Repository
                {
                    Id = model.Id,
                    Name = model.Name,
                    Logo = model.Logo,
                    Group = model.Group,
                    Description = model.Description,
                    Anonymous = model.AnonymousAccess,
                    AllowAnonymousPush = model.AllowAnonymousPush,
                    AuditPushUser = model.AuditPushUser,
                    LinksUseGlobal = model.LinksUseGlobal,
                    LinksUrl = model.LinksUrl,
                    LinksRegex = model.LinksRegex 
                };
                database.Repositories.Add(repository);
                AddMembers(model.Users.Select(x => x.Id), model.Administrators.Select(x => x.Id), model.Teams.Select(x => x.Id), repository, database);
                try
                {
                    database.SaveChanges();
                }
                catch (DbUpdateException ex)
                {
                    Trace.TraceWarning("Failed to create repo {0} - {1}", model.Name, ex);
                    return false;
                }
                catch (UpdateException)
                {
                    return false;
                }
                return true;
            }
        }
 public void Update(RepositoryModel repository)
 {
     ADBackend.Instance.Repositories.Update(SanitizeModel(repository));
 }
 public bool Create(RepositoryModel repository)
 {            
     return ADBackend.Instance.Repositories.Add(SanitizeModel(repository));
 }
        private Guid AddRepo(string name)
        {
            var newRepo = new RepositoryModel();
            newRepo.Name = name;
            newRepo.Users = new UserModel[0];
            newRepo.Administrators = new UserModel[0];
            newRepo.Teams = new TeamModel[0];

            EFRepositoryRepository repoRepo = new EFRepositoryRepository { CreateContext = GetContext };
            Assert.IsTrue(repoRepo.Create(newRepo));
            return newRepo.Id;
        }
 private static RepositoryModel MakeRepo(string name)
 {
     var newRepo = new RepositoryModel();
     newRepo.Name = name;
     return newRepo;
 }
        public void RepoLogoIsPreservedWhenNullAtUpdate()
        {
            var logoBytes = Encoding.UTF8.GetBytes("Hello");
            var repo = MakeRepo("Repo1");
            repo.Logo = logoBytes;
            _repo.Create(repo);

            var updateRepo = new RepositoryModel();
            updateRepo.Id = repo.Id;
            updateRepo.Name = repo.Name;
            updateRepo.Logo = null;
            updateRepo.Users = new UserModel[0];
            updateRepo.Administrators = new UserModel[0];
            updateRepo.Teams = new TeamModel[0];
            _repo.Update(updateRepo);

            CollectionAssert.AreEqual(logoBytes, _repo.GetRepository("Repo1").Logo);
        }
        private bool CheckNamedUserPermission(Guid userId, IList<TeamModel> userTeams, bool userIsSystemAdministrator, RepositoryModel repository, RepositoryAccessLevel requiredLevel)
        {
            if (userId == Guid.Empty) { throw new ArgumentException("Do not pass anonymous user id", "userId"); }

            bool userIsAnAdministrator = userIsSystemAdministrator || repository.Administrators.Any(x => x.Id == userId);

            switch (requiredLevel)
            {
                case RepositoryAccessLevel.Push:
                case RepositoryAccessLevel.Pull:
                    return userIsAnAdministrator ||
                        UserIsARepoUser(userId, repository) ||
                        UserIsATeamMember(userTeams, repository);

                case RepositoryAccessLevel.Administer:
                    return userIsAnAdministrator;
                default:
                    throw new ArgumentOutOfRangeException("requiredLevel", requiredLevel, null);
            }
        }
        public void Update(RepositoryModel model)
        {
            if (model == null) throw new ArgumentException("model");
            if (model.Name == null) throw new ArgumentException("name");

            using (var db = new BonoboGitServerContext())
            {
                var repo = db.Repositories.FirstOrDefault(i => i.Name == model.Name);
                if (repo != null)
                {
                    repo.Group = model.Group;
                    repo.Description = model.Description;
                    repo.Anonymous = model.AnonymousAccess;
                    repo.AuditPushUser = model.AuditPushUser;

                    if (model.Logo != null)
                        repo.Logo = model.Logo;

                    if (model.RemoveLogo)
                        repo.Logo = null;

                    repo.Users.Clear();
                    repo.Teams.Clear();
                    repo.Administrators.Clear();

                    AddMembers(model.Users, model.Administrators, model.Teams, repo, db);

                    db.SaveChanges();
                }
            }
        }
 private static RepositoryModel SanitizeModel(RepositoryModel model)
 {
     model.EnsureCollectionsAreValid();
     return model;
 }
        private bool TryCreateOnPush(string repositoryName)
        {
            DirectoryInfo directory = GetDirectoryInfo(repositoryName);
            if (directory.Exists)
            {
                // We can't create a new repo - there's already a directory with that name
                return false;
            }
            RepositoryModel repository = new RepositoryModel();
            repository.Name = repositoryName;
            if (!repository.NameIsValid)
            {
                // We don't like this name
                return false;
            }
            var user = MembershipService.GetUserModel(User.Id());
            repository.Description = "Auto-created by push for " + user.DisplayName;
            repository.AnonymousAccess = false;
            repository.Administrators = new[] {user};
            if (!RepositoryRepository.Create(repository))
            {
                // We can't add this to the repo store
                return false;
            }

            Repository.Init(Path.Combine(UserConfiguration.Current.Repositories, repository.Name), true);
            return true;
        }
 public abstract bool Create(RepositoryModel repository);
 private RepositoryDetailModel ConvertRepositoryModel(RepositoryModel model)
 {
     return model == null ? null : new RepositoryDetailModel
     {
         Name = model.Name,
         Group = model.Group,
         Description = model.Description,
         Users = model.Users,
         Administrators = model.Administrators,
         Teams = model.Teams,
         IsCurrentUserAdministrator = model.Administrators.Contains(User.Identity.Name.ToLowerInvariant()),
         AllowAnonymous = model.AnonymousAccess,
         Status = GetRepositoryStatus(model),
         AuditPushUser = model.AuditPushUser,
     };
 }
 private RepositoryDetailModel ConvertRepositoryModel(RepositoryModel model)
 {
     return model == null ? null : new RepositoryDetailModel
     {
         Name = model.Name,
         Description = model.Description,
         Users = model.Users,
         Administrators = model.Administrators,
         Teams = model.Teams,
         IsCurrentUserAdministrator = model.Administrators.Contains(User.Identity.Name),
         AllowAnonymous = model.AnonymousAccess
     };
 }
 private void MoveRepo(RepositoryModel oldRepo, RepositoryModel newRepo)
 {
     if (oldRepo.Name != newRepo.Name)
     {
         string old_path = Path.Combine(UserConfiguration.Current.Repositories, oldRepo.Name);
         string new_path = Path.Combine(UserConfiguration.Current.Repositories, newRepo.Name);
         try
         {
             Directory.Move(old_path, new_path);
         }
         catch (IOException exc)
         {
             ModelState.AddModelError("Name", exc.Message);
         }
     }
 }
 public abstract void Update(RepositoryModel repository);
 private RepositoryDetailModel ConvertRepositoryModel(RepositoryModel model)
 {
     return model == null ? null : new RepositoryDetailModel
     {
         Name = model.Name,
         Group = model.Group,
         Description = model.Description,
         Users = model.Users,
         Administrators = model.Administrators,
         Teams = model.Teams,
         IsCurrentUserAdministrator = model.Administrators.Contains(User.Id(), StringComparer.OrdinalIgnoreCase),
         AllowAnonymous = model.AnonymousAccess,
         Status = GetRepositoryStatus(model),
         AuditPushUser = model.AuditPushUser,
         Logo = new RepositoryLogoDetailModel(model.Logo)
     };
 }
        public void Update(RepositoryModel model)
        {
            if (model == null) throw new ArgumentException("model");
            if (model.Name == null) throw new ArgumentException("name");

            using (var db = CreateContext())
            {
                var repo = db.Repositories.FirstOrDefault(i => i.Id == model.Id);
                if (repo != null)
                {
                    model.EnsureCollectionsAreValid();

                    repo.Name = model.Name;
                    repo.Group = model.Group;
                    repo.Description = model.Description;
                    repo.Anonymous = model.AnonymousAccess;
                    repo.AuditPushUser = model.AuditPushUser;
                    repo.AllowAnonymousPush = model.AllowAnonymousPush;
                    repo.LinksRegex = model.LinksRegex;
                    repo.LinksUrl = model.LinksUrl;
                    repo.LinksUseGlobal = model.LinksUseGlobal;

                    if (model.Logo != null)
                        repo.Logo = model.Logo;

                    if (model.RemoveLogo)
                        repo.Logo = null;

                    repo.Users.Clear();
                    repo.Teams.Clear();
                    repo.Administrators.Clear();

                    AddMembers(model.Users.Select(x => x.Id), model.Administrators.Select(x => x.Id), model.Teams.Select(x => x.Id), repo, db);

                    db.SaveChanges();
                }
            }
        }
 private RepositoryDetailStatus GetRepositoryStatus(RepositoryModel model)
 {
     string path = Path.Combine(UserConfiguration.Current.Repositories, model.Name);
     if (!Directory.Exists(path))
         return RepositoryDetailStatus.Missing;
     else
         return RepositoryDetailStatus.Valid;
 }
        public void Update(RepositoryModel model)
        {
            if (model == null) throw new ArgumentException("model");
            if (model.Name == null) throw new ArgumentException("name");

            using (var db = new DataEntities())
            {
                var repo = db.Repository.FirstOrDefault(i => i.Name == model.Name);
                if (repo != null)
                {
                    repo.Description = model.Description;
                    repo.Anonymous = model.AnonymousAccess;

                    repo.Users.Clear();
                    repo.Teams.Clear();
                    repo.Administrators.Clear();

                    AddMembers(model.Users, model.Administrators, model.Teams, repo, db);

                    db.SaveChanges();
                }
            }
        }