Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 1469 save button doesnt always appear to work #1488

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions src/NuGetGallery/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,9 @@ public virtual ActionResult LogOff(string returnUrl)
}

[NonAction]
public virtual ActionResult SafeRedirect(string returnUrl)
protected virtual ActionResult SafeRedirect(string returnUrl)
{
if (!String.IsNullOrWhiteSpace(returnUrl)
&& Url.IsLocalUrl(returnUrl)
&& returnUrl.Length > 1
&& returnUrl.StartsWith("/", StringComparison.Ordinal)
&& !returnUrl.StartsWith("//", StringComparison.Ordinal)
&& !returnUrl.StartsWith("/\\", StringComparison.Ordinal))
{
return Redirect(returnUrl);
}

return Redirect(Url.Home());
return Redirect(RedirectHelper.SafeRedirectUrl(Url, returnUrl));
}
}
}
19 changes: 15 additions & 4 deletions src/NuGetGallery/Controllers/PackagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ public virtual ActionResult DisplayPackage(string id, string version)

if (package.IsOwner(HttpContext.User))
{
// Tell logged-in package owners not to cache the package page, so they won't be confused about the state of pending edits.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Response.Cache.SetMaxAge(TimeSpan.Zero);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

var pendingMetadata = _editPackageService.GetPendingMetadata(package);
if (pendingMetadata != null)
{
Expand Down Expand Up @@ -585,28 +591,33 @@ public virtual ActionResult Edit(string id, string version)
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Edit(string id, string version, EditPackageRequest formData)
public virtual ActionResult Edit(string id, string version, EditPackageRequest formData, string returnUrl)
{
var package = _packageService.FindPackageByIdAndVersion(id, version);
if (package == null)
{
return HttpNotFound();
}

if (!package.IsOwner(HttpContext.User))
var user = _userService.FindByUsername(HttpContext.User.Identity.Name);
if (user == null || !package.IsOwner(HttpContext.User))
{
return new HttpStatusCodeResult(403, "Forbidden");
}

var user = _userService.FindByUsername(HttpContext.User.Identity.Name);
if (!ModelState.IsValid)
{
return View();
}

// Add the edit request to a queue where it will be processed in the background.
if (formData.Edit != null)
{
_editPackageService.StartEditPackageRequest(package, formData.Edit, user);
_entitiesContext.SaveChanges();
}
return Redirect(Url.Package(id, version));

return Redirect(RedirectHelper.SafeRedirectUrl(Url, returnUrl ?? Url.Package(id, version)));
}

[Authorize]
Expand Down
23 changes: 23 additions & 0 deletions src/NuGetGallery/Helpers/RedirectHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Web.Mvc;

namespace NuGetGallery
{
public static class RedirectHelper
{
public static string SafeRedirectUrl(UrlHelper url, string returnUrl)
{
if (!String.IsNullOrWhiteSpace(returnUrl)
&& url.IsLocalUrl(returnUrl)
&& returnUrl.Length > 1
&& returnUrl.StartsWith("/", StringComparison.Ordinal)
&& !returnUrl.StartsWith("//", StringComparison.Ordinal)
&& !returnUrl.StartsWith("/\\", StringComparison.Ordinal))
{
return returnUrl;
}

return url.Home();
}
}
}
1 change: 1 addition & 0 deletions src/NuGetGallery/NuGetGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@
<Compile Include="Helpers\HtmlExtensions.cs" />
<Compile Include="Helpers\PackageHelper.cs" />
<Compile Include="Helpers\QuietLog.cs" />
<Compile Include="Helpers\RedirectHelper.cs" />
<Compile Include="Helpers\StringHelper.cs" />
<Compile Include="Helpers\StatisticsHelper.cs" />
<Compile Include="Helpers\TreeView.cs" />
Expand Down
3 changes: 2 additions & 1 deletion src/NuGetGallery/PackagesController.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,12 @@ public class T4MVC_PackagesController: NuGetGallery.PackagesController {
return callInfo;
}

public override System.Web.Mvc.ActionResult Edit(string id, string version, NuGetGallery.EditPackageRequest formData) {
public override System.Web.Mvc.ActionResult Edit(string id, string version, NuGetGallery.EditPackageRequest formData, string returnUrl) {
var callInfo = new T4MVC_ActionResult(Area, Name, ActionNames.Edit);
callInfo.RouteValueDictionary.Add("id", id);
callInfo.RouteValueDictionary.Add("version", version);
callInfo.RouteValueDictionary.Add("formData", formData);
callInfo.RouteValueDictionary.Add("returnUrl", returnUrl);
return callInfo;
}

Expand Down
16 changes: 10 additions & 6 deletions src/NuGetGallery/Views/Packages/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,15 @@
modal: true,
buttons: [{
text: "Save", click: function () {
$("#navigate-away-dialog").dialog("close");
var returnUrl = href.substr(window.location.origin.length);
$(document.forms.EditForm).append($('<input type="hidden" name="returnUrl" value="' + returnUrl + '">'));
document.forms.EditForm.submit();
window.location.href = href;
}
},
{
text: "Don't Save", click: function () {
$("#navigate-away-dialog").dialog("close");
window.location.href = href;
}
},
Expand Down Expand Up @@ -160,11 +163,6 @@
}

@Styles.Render("~/Content/themes/custom/jquery-ui-1.10.3.custom.css")
<div id="navigate-away-dialog" title="Save Changes?">
<p>
You are about to view a different page but your changes are not yet saved. Do you wish to save your changes?
</p>
</div>

@section SideColumn {
@{
Expand Down Expand Up @@ -282,3 +280,9 @@
</p>
</fieldset>
}

<div id="navigate-away-dialog" title="Save Changes?" style="display: none">
<p>
You are about to view a different page but your changes are not yet saved. Do you wish to save your changes?
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public TestableAuthenticationController()
Users = (MockUsers = new Mock<IUserService>()).Object;
}

public override ActionResult SafeRedirect(string returnUrl)
protected override ActionResult SafeRedirect(string returnUrl)
{
return new RedirectResult("aSafeRedirectUrl");
}
Expand Down