public ActionResult Edit(RepositoryDetailModel model)
 {
     if (ModelState.IsValid)
     {
         var currentUserIsInAdminList = model.PostedSelectedAdministrators != null && model.PostedSelectedAdministrators.Contains(User.Id());
         if (currentUserIsInAdminList || User.IsInRole(Definitions.Roles.Administrator))
         {
             var existingRepo = RepositoryRepository.GetRepository(model.Id);
             var repoModel = ConvertRepositoryDetailModel(model);
             MoveRepo(existingRepo, repoModel);
             try
             {
                 RepositoryRepository.Update(repoModel);
             }
             catch (System.Data.Entity.Infrastructure.DbUpdateException)
             {
                 MoveRepo(repoModel, existingRepo);
             }
             ViewBag.UpdateSuccess = true;
         }
         else
         {
             ModelState.AddModelError("Administrators", Resources.Repository_Edit_CantRemoveYourself);
         }
     }
     PopulateCheckboxListData(ref model);
     return View(model);
 }
        public ActionResult Add(RepositoryDetailModel model)
        {
            if (!User.IsInRole(Definitions.Roles.Administrator) && !UserConfigurationManager.AllowUserRepositoryCreation)
            {
                return RedirectToAction("Unauthorized", "Home");
            }

            if (model != null && !String.IsNullOrEmpty(model.Name))
            {
                model.Name = Regex.Replace(model.Name, @"\s", "");
            }

            if (String.IsNullOrEmpty(model.Name))
            {
                ModelState.AddModelError("Name", Resources.Repository_Add_NameFailure);
            }
            else if (ModelState.IsValid)
            {
                if (RepositoryRepository.Create(ConvertRepositoryDetailModel(model)))
                {
                    string path = Path.Combine(UserConfigurationManager.Repositories, model.Name);
                    if (Directory.Exists(path))
                    {
                        using (GitSharp.Repository repository = new GitSharp.Repository(path))
                        {
                            if (repository.Branches.Count > 0)
                            {
                                TempData["AddSuccess"] = true;
                                return RedirectToAction("Index");
                            }
                            else
                            {
                                RepositoryRepository.Delete(model.Name);
                                ModelState.AddModelError("", Resources.Repository_Add_NoBranches);
                            }
                        }
                    }
                    else
                    {
                        RepositoryRepository.Delete(model.Name);
                        ModelState.AddModelError("", Resources.Repository_Add_DirectoryNotExisting);
                    }
                }
                else
                {
                    ModelState.AddModelError("", Resources.Repository_Create_Failure);
                }
            }
            PopulateEditData();
            return View(model);
        }
        public ActionResult Add()
        {
            if (!User.IsInRole(Definitions.Roles.Administrator) && !UserConfigurationManager.AllowUserRepositoryCreation)
            {
                return RedirectToAction("Unauthorized", "Home");
            }

            var model = new RepositoryDetailModel
            {
                Administrators = new string[] { User.Identity.Name },
            };
            PopulateEditData();
            return View(model);
        }
        public ActionResult Clone(string id)
        {
            if (!User.IsInRole(Definitions.Roles.Administrator) && !UserConfiguration.Current.AllowUserRepositoryCreation)
            {
                return RedirectToAction("Unauthorized", "Home");
            }

            var model = new RepositoryDetailModel
            {
                Administrators = new[] { User.Identity.Name.ToLowerInvariant() },
            };
            ViewBag.ID = id;
            PopulateEditData();
            return View(model);
        }
 public ActionResult Edit(RepositoryDetailModel model)
 {
     if (ModelState.IsValid)
     {
         if (model.Administrators.Contains(User.Id(), StringComparer.OrdinalIgnoreCase))
         {
             RepositoryRepository.Update(ConvertRepositoryDetailModel(model));
             ViewBag.UpdateSuccess = true;
         }
         else
         {
             ModelState.AddModelError("Administrators", Resources.Repository_Edit_CantRemoveYourself);
         }
     }
     PopulateEditData();
     return View(model);
 }
 public ActionResult Edit(RepositoryDetailModel model)
 {
     if (ModelState.IsValid)
     {
         if (model.Administrators.Contains(User.Identity.Name.ToLowerInvariant()))
         {
             RepositoryRepository.Update(ConvertRepositoryDetailModel(model));
             ViewBag.UpdateSuccess = true;
         }
         else
         {
             ModelState.AddModelError("Administrators", Resources.Repository_Edit_CantRemoveYourself);
         }
     }
     PopulateEditData();
     return View(model);
 }
        public ActionResult Create(RepositoryDetailModel model)
        {
            if (!User.IsInRole(Definitions.Roles.Administrator) && !UserConfiguration.Current.AllowUserRepositoryCreation)
            {
                return RedirectToAction("Unauthorized", "Home");
            }

            if (model != null && !String.IsNullOrEmpty(model.Name))
            {
                model.Name = Regex.Replace(model.Name, @"\s", "");
            }

            if (String.IsNullOrEmpty(model.Name))
            {
                ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure);
            }
            else if (ModelState.IsValid)
            {
                if (RepositoryRepository.Create(ConvertRepositoryDetailModel(model)))
                {
                    string path = Path.Combine(UserConfiguration.Current.Repositories, model.Name);
                    if (!Directory.Exists(path))
                    {
                        LibGit2Sharp.Repository.Init(path, true);
                        TempData["CreateSuccess"] = true;
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        RepositoryRepository.Delete(model.Name);
                        ModelState.AddModelError("", Resources.Repository_Create_DirectoryExists);
                    }
                }
                else
                {
                    ModelState.AddModelError("", Resources.Repository_Create_Failure);
                }
            }
            PopulateEditData();
            return View(model);
        }
        public ActionResult Clone(string id, RepositoryDetailModel model)
        {
            if (!User.IsInRole(Definitions.Roles.Administrator) && !UserConfiguration.Current.AllowUserRepositoryCreation)
            {
                return RedirectToAction("Unauthorized", "Home");
            }

            if (model != null && !String.IsNullOrEmpty(model.Name))
            {
                model.Name = Regex.Replace(model.Name, @"\s", "");
            }

            if (String.IsNullOrEmpty(model.Name))
            {
                ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure);
            }
            else if (ModelState.IsValid)
            {
                if (RepositoryRepository.Create(ConvertRepositoryDetailModel(model)))
                {
                    string targetRepositoryPath = Path.Combine(UserConfiguration.Current.Repositories, model.Name);
                    if (!Directory.Exists(targetRepositoryPath))
                    {
                        string sourceRepositoryPath = Path.Combine(UserConfiguration.Current.Repositories, id);

                        LibGit2Sharp.CloneOptions options = new LibGit2Sharp.CloneOptions()
                            {
                                IsBare = true,
                                Checkout = false
                            };

                        LibGit2Sharp.Repository.Clone(sourceRepositoryPath, targetRepositoryPath, options);
                        TempData["CloneSuccess"] = true;
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        RepositoryRepository.Delete(model.Name);
                        ModelState.AddModelError("", Resources.Repository_Create_DirectoryExists);
                    }
                }
                else
                {
                    ModelState.AddModelError("", Resources.Repository_Create_Failure);
                }
            }

            ViewBag.ID = id;
            PopulateEditData();
            return View(model);
        }
 private RepositoryModel ConvertRepositoryDetailModel(RepositoryDetailModel model)
 {
     return model == null ? null : new RepositoryModel
     {
         Name = model.Name,
         Group = model.Group,
         Description = model.Description,
         Users = model.Users,
         Administrators = model.Administrators,
         Teams = model.Teams,
         AnonymousAccess = model.AllowAnonymous,
         AuditPushUser = model.AuditPushUser,
         Logo = model.Logo != null ? model.Logo.BinaryData : null,
         RemoveLogo = model.Logo != null && model.Logo.RemoveLogo
     };
 }
 private void PopulateCheckboxListData(ref RepositoryDetailModel model)
 {
     model = model.Id != Guid.Empty ? ConvertRepositoryModel(RepositoryRepository.GetRepository(model.Id), User) : model;
     model.AllAdministrators = MembershipService.GetAllUsers().ToArray();
     model.AllUsers = MembershipService.GetAllUsers().ToArray();
     model.AllTeams = TeamRepository.GetAllTeams().ToArray();
     if (model.PostedSelectedUsers != null && model.PostedSelectedUsers.Any())
     {
         model.Users = model.PostedSelectedUsers.Select(x => MembershipService.GetUserModel(x)).ToArray();
     }
     if (model.PostedSelectedTeams != null && model.PostedSelectedTeams.Any())
     {
         model.Teams = model.PostedSelectedTeams.Select(x => TeamRepository.GetTeam(x)).ToArray();
     }
     if (model.PostedSelectedAdministrators != null && model.PostedSelectedAdministrators.Any())
     {
         model.Administrators = model.PostedSelectedAdministrators.Select(x => MembershipService.GetUserModel(x)).ToArray();
     }
     model.PostedSelectedAdministrators =  new Guid[0];
     model.PostedSelectedUsers = new Guid[0];
     model.PostedSelectedTeams = new Guid[0];
 }
 private RepositoryModel ConvertRepositoryDetailModel(RepositoryDetailModel model)
 {
     return model == null ? null : new RepositoryModel
     {
         Id = model.Id,
         Name = model.Name,
         Group = model.Group,
         Description = model.Description,
         Users = model.PostedSelectedUsers != null ? model.PostedSelectedUsers.Select(x => MembershipService.GetUserModel(x)).ToArray() : new UserModel[0],
         Administrators = model.PostedSelectedAdministrators != null ? model.PostedSelectedAdministrators.Select(x => MembershipService.GetUserModel(x)).ToArray() : new UserModel[0],
         Teams = model.PostedSelectedTeams != null ? model.PostedSelectedTeams.Select(x => TeamRepository.GetTeam(x)).ToArray() : new TeamModel[0],
         AnonymousAccess = model.AllowAnonymous,
         AuditPushUser = model.AuditPushUser,
         Logo = model.Logo != null ? model.Logo.BinaryData : null,
         AllowAnonymousPush = model.AllowAnonymousPush,
         RemoveLogo = model.Logo != null && model.Logo.RemoveLogo,
         LinksUseGlobal = model.LinksUseGlobal,
         LinksRegex = model.LinksRegex ?? "",
         LinksUrl = model.LinksUrl ?? ""
     };
 }
        public ActionResult Create(RepositoryDetailModel model)
        {
            if (!RepositoryPermissionService.HasCreatePermission(User.Id()))
            {
                return RedirectToAction("Unauthorized", "Home");
            }

            if (model != null && !String.IsNullOrEmpty(model.Name))
            {
                model.Name = Regex.Replace(model.Name, @"\s", "");
            }

            if (model != null && String.IsNullOrEmpty(model.Name))
            {
                ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure);
            }
            else if (ModelState.IsValid)
            {

                var repo_model = ConvertRepositoryDetailModel(model);
                if (RepositoryRepository.Create(repo_model))
                {
                    string path = Path.Combine(UserConfiguration.Current.Repositories, model.Name);
                    if (!Directory.Exists(path))
                    {
                        LibGit2Sharp.Repository.Init(path, true);
                        TempData["CreateSuccess"] = true;
                        TempData["SuccessfullyCreatedRepositoryName"] = model.Name;
                        TempData["SuccessfullyCreatedRepositoryId"] = repo_model.Id;
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        RepositoryRepository.Delete(model.Id);
                        ModelState.AddModelError("", Resources.Repository_Create_DirectoryExists);
                    }
                }
                else
                {
                    ModelState.AddModelError("", Resources.Repository_Create_Failure);
                }
            }
            PopulateCheckboxListData(ref model);
            return View(model);
        }
        public ActionResult Create()
        {
            if (!RepositoryPermissionService.HasCreatePermission(User.Id()))
            {
                return RedirectToAction("Unauthorized", "Home");
            }

            var model = new RepositoryDetailModel
            {
                Administrators = new UserModel[] { MembershipService.GetUserModel(User.Id()) },
            };
            PopulateCheckboxListData(ref model);
            return View(model);
        }
        public ActionResult Clone(Guid id, RepositoryDetailModel model)
        {
            if (!RepositoryPermissionService.HasCreatePermission(User.Id()))
            {
                return RedirectToAction("Unauthorized", "Home");
            }

            if (model != null && !String.IsNullOrEmpty(model.Name))
            {
                model.Name = Regex.Replace(model.Name, @"\s", "");
            }

            if (model != null && String.IsNullOrEmpty(model.Name))
            {
                ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure);
            }
            else if (ModelState.IsValid)
            {
                var repo_model = ConvertRepositoryDetailModel(model);
                if (RepositoryRepository.Create(repo_model))
                {
                    string targetRepositoryPath = Path.Combine(UserConfiguration.Current.Repositories, model.Name);
                    if (!Directory.Exists(targetRepositoryPath))
                    {
                        var source_repo = RepositoryRepository.GetRepository(id);
                        string sourceRepositoryPath = Path.Combine(UserConfiguration.Current.Repositories, source_repo.Name);

                        LibGit2Sharp.CloneOptions options = new LibGit2Sharp.CloneOptions()
                            {
                                IsBare = true,
                                Checkout = false
                            };

                        LibGit2Sharp.Repository.Clone(sourceRepositoryPath, targetRepositoryPath, options);

                        using (var repo = new LibGit2Sharp.Repository(targetRepositoryPath))
                        {
                            if (repo.Network.Remotes.Any(r => r.Name == "origin"))
                            {
                                repo.Network.Remotes.Remove("origin");
                            }
                        }

                        TempData["CloneSuccess"] = true;
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        RepositoryRepository.Delete(model.Id);
                        ModelState.AddModelError("", Resources.Repository_Create_DirectoryExists);
                    }
                }
                else
                {
                    ModelState.AddModelError("", Resources.Repository_Create_Failure);
                }
            }

            ViewBag.ID = id;
            PopulateCheckboxListData(ref model);
            return View(model);
        }
 public ActionResult Delete(RepositoryDetailModel model)
 {
     if (model != null && !String.IsNullOrEmpty(model.Name))
     {
         string path = Path.Combine(UserConfiguration.Current.Repositories, model.Name);
         if (Directory.Exists(path))
         {
             DeleteFileSystemInfo(new DirectoryInfo(path));
         }
         RepositoryRepository.Delete(model.Name);
         TempData["DeleteSuccess"] = true;
     }
     return RedirectToAction("Index");
 }
 private RepositoryModel ConvertRepositoryDetailModel(RepositoryDetailModel model)
 {
     return model == null ? null : new RepositoryModel
     {
         Name = model.Name,
         Group = model.Group,
         Description = model.Description,
         Users = model.Users,
         Administrators = model.Administrators,
         Teams = model.Teams,
         AnonymousAccess = model.AllowAnonymous,
         AuditPushUser = model.AuditPushUser,
     };
 }
        /// <summary>
        /// Construct the URLs for the repository
        /// (This code extracted from the view)
        /// </summary>
        void SetGitUrls(RepositoryDetailModel model)
        {
            string serverAddress = System.Configuration.ConfigurationManager.AppSettings["GitServerPath"]
                                   ?? string.Format("{0}://{1}{2}{3}/",
                                       Request.Url.Scheme,
                                       Request.Url.Host,
                                       (Request.Url.IsDefaultPort ? "" : (":" + Request.Url.Port)),
                                       Request.ApplicationPath == "/" ? "" : Request.ApplicationPath
                                       );

            model.GitUrl = String.Concat(serverAddress, model.Name, ".git");
            if (User.Identity.IsAuthenticated)
            {
                model.PersonalGitUrl =
                    String.Concat(serverAddress.Replace("://", "://" + Uri.EscapeDataString(User.Username()) + "@"), model.Name, ".git");
            }
        }