Timsen
Timsen

Reputation: 4126

How to implement plugins/addons in Asp.net MVC?

I cant really get my head around one of the features i have to implement. Customers can have a bunch of addons which they pay for (list of addons is inside the db table). I have to implement functionality which only show plugins which customer had payed for.

I know that i can create a bunch of if statements inside the views, but its feels like it would be a bit "hacky" solution which would be a pain to work on.

Whats the right way to implement such functionality?

Upvotes: 1

Views: 907

Answers (1)

NightOwl888
NightOwl888

Reputation: 56909

Since you are talking about features rather than plug-ins, I suggest you look at using AuthorizeAttribute with roles to control access to each feature and/or feature set.

public class FeatureController : Controller
{
    [Authorize(Roles = "Feature1")]
    public ActionResult Feature1()
    {
        // Implement feature here...

        return View();
    }

    [Authorize(Roles = "Feature1")]
    [HttpPost]
    public ActionResult Feature1(Model model)
    {
        // Implement feature here...

        return View();
    }

    [Authorize(Roles = "Feature2")]
    public ActionResult Feature2()
    {
        // Implement feature here...

        return View();
    }

    // Other features...
}

Then any user who is in the Feature1 role will have access to Feature1, the Feature2 role will have access to Feature2, etc.

You could combine AuthorizeAttribute with MvcSiteMapProvider's security trimming feature to make menu items that are only visible when the user is in the appropriate role.

Full Disclosure: I am a major contributor of MvcSiteMapProvider.

Upvotes: 1

Related Questions