I want to use the following method whenever the GET or POST is called to create or edit an article page:
' userId = ID or username of the user logged in
' companyId = ID or name of the company for which the current blog is assigned
' blogId = ID or name of the blog for which the article is being written
' returnSuccessView = the view that will be returned if the user has access
' returnFailView = the view that will be returned if the user does not have access
return View(CheckUserAccess(userId, companyId, blogId, returnSuccessView, returnFailView))
Can someone show me what this function would look like? My structure is:
Companies -> Blogs -> Articles -> Comments
I want to create permissions so only users that belong to a certain company and belong to a certain blog and have certain permissions can perform the requested task.
For instance, my user model would have an ICollection of companies to which the user can be associated with, and they can have an ICollection of blogs they can be associated with. They can also have an ICollection of permissions, such as super-user, article writer, article editor, moderator, etc.
I would create a separate model for permissions so that they can be added and removed via a UI.
The function should check whether or not the requested company, blog and permissions match that which the user is associated with (has in their ICollection).
What’s the best way to go about something like this? Thank you.
I would recommend you handling this with a custom
[Authorize]attribute. Let’s take an example:Now you could decorate your controllers/actions with this attribute:
And of course to ensure that the user is not trying to cheat on you on the POST action you could also decorate it with this attribute: