Reference Module Controller From Theme View?

Topics: Administration, General, Troubleshooting, Writing modules
Nov 8, 2012 at 2:41 PM

Is there a way to reference a module's controller from a theme View?

I am trying to allow the user to remove themselves from the system.  So far I have tried 

@Html.ActionLink("Close Account", "MethodName", "Controller", new{ id = WorkContext.CurrentUser.Id }, null)

However, this is not working for me. I have also tried @Html.Action() and this throws errors.

Has anyone tried this before? Allow the user to remove themselves from the orchard security?

Thanks in advance!

Nov 8, 2012 at 3:03 PM

You need to supply an area too (the module name) @ the new { } bit.

Nov 8, 2012 at 3:13 PM

Is this what you are saying?

@Html.ActionLink("Close Account", "MethodName", "Controller", new{ id = WorkContext.CurrentUser.Id, Module = Orchard.Users }, null)

 

Sorry can you be a little more specific?

Nov 8, 2012 at 3:14 PM

@Html.ActionLink("Close Account", "MethodName", "Controller", new{ id = WorkContext.CurrentUser.Id, area = "Orchard.Users" }, null)

Nov 8, 2012 at 3:19 PM

I am getting this error:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Admin/Users/Delete_SingleUser


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

Nov 8, 2012 at 3:20 PM

I am wanting to run the method, then redirect the user to another page.  For example, Run Method, then redirect to www.google.com

 

Is that possible with @Html.ActionLink?

Nov 8, 2012 at 3:30 PM

You could try specifying a ReturnUrl (if the target supports it),  otherwise : no

Nov 8, 2012 at 3:31 PM

In your opinion what would be the best means of referencing a method withing a module controller?

Again, I want to allow a user to "Remove" themselves from the system.  Is that possible?

Dec 6, 2012 at 3:01 PM

The following code snippet from Orchard.Users\AdminController.cs says it pretty much all in a few lines of code:

[HttpPost]
public ActionResult Delete(int id) {
    if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage users")))
        return new HttpUnauthorizedResult();

    var user = Services.ContentManager.Get<IUser>(id);

    if (user != null) {
        if (String.Equals(Services.WorkContext.CurrentSite.SuperUser, user.UserName, StringComparison.Ordinal)) {
            Services.Notifier.Error(T("The Super user can't be removed. Please disable this account or specify another Super user account"));
        }
        else if (String.Equals(Services.WorkContext.CurrentUser.UserName, user.UserName, StringComparison.Ordinal)) {
            Services.Notifier.Error(T("You can't remove your own account. Please log in with another account"));
        }
        else{
            Services.ContentManager.Remove(user.ContentItem);
            Services.Notifier.Information(T("User {0} deleted", user.UserName));
        }
    }

    return RedirectToAction("Index");
}

More precisely:

  • Line 1: You need to send a Http POST request to properly execute the delete method - @Html.ActionLink() renders just a simple hyperlink, clicking on which will always result in a GET request. You need a <form> that will post to the desired action and a <button> to submit that form.
  • Line 3: You need to have the SiteOwner permission to delete an account.
  • Line 10: You can't remove your own account.

If you want to circumvent the last 2 restrictions, write your own controller and do what is being done in above method, deleting those safety nets first, of course. Do not, however, change the first restrictions - every change to the running application (state) should be requested using the appropriate Http method, POST in this case. (In a REST world, you would use DELETE, but we don't support that here.)