From what you are describing, it looks like you need some form of user access control, not role-based permissions. If so, then this should be implemented in your business logic. Your script sounds like you can implement it at your service level.
Basically, you need to implement all the functions in the ProductRepository from the point of view of the current user, and the products will be marked with permissions for this user.
Sounds harder than it really is. First, you need a token user interface that contains information about the uid user and the list of roles (if you want to use roles). You can use IPrincipal or create your own line by line
public interface IUserToken { public int Uid { get; } public bool IsInRole(string role); }
Then in your controller, you parse the user token into your repository constructor.
IProductRepository ProductRepository = new ProductRepository(User);
If you are using FormsAuthentication and a custom IUserToken, then you can create a Wrapper around IPrincipal so that your ProductRepository is created as follows:
IProductRepository ProductRepository = new ProductRepository(new IUserTokenWrapper(User));
Now all your IProductRepository functions should be able to access the user's token in order to check permissions. For example:
public Product GetProductById(productId) { Product product = InternalGetProductById(UserToken.uid, productId); if (product == null) { throw new NotAuthorizedException(); } product.CanEdit = ( UserToken.IsInRole("admin") ||
If you are interested in knowing a list of all products, in the data access code you can request a request based on permission. In your case, the left join is to see if the many-to-many table contains UserToken.Uid and productId. If the right side of the connection is present, you know that the user has permission for this product, and then you can install your Product.CanEdit boolean.
Using this method, you can use the following, if you want, in your view (where Model is your product).
<% if(Model.CanEdit) { %> <a href="/Products/1/Edit">Edit</a> <% } %>
or in your controller
public ActionResult Get(int id) { Product p = ProductRepository.GetProductById(id); if (p.CanEdit) { return View("EditProduct"); } else { return View("Product"); } }
The advantage of this method is that security is built into your service level (ProductRepository), so it is not processed by your controllers and cannot be bypassed by your controller.
The main thing is that security is placed in your business logic, and not in your controller.