I am using Entity Framework code first, and I would like to implement some user permissions system on top of my entities.
For example I have some entities:
public partial class Course
{
public virtual string Name { get; set; }
public virtual bool Visible { get; set; }
}
public partial class Student
{
public virtual string Name { get; set; }
public virtual bool Visible { get; set; }
}
and so on.
Then I would like to have some entities like: User and Permission.
The Permission entity would have some bool columns : Create, Read, Update, Delete which specify the user permissions, UserId column, and one column that specifies the table for which this permission is added.
What would be the best method to implement such behaviour in code first?
If you are using MVC you could leverage the existing authorization framework and create a custom AuthorizeAttribute where you would handle your custom permissions.
You could introduce a new
Permissionstable in your database and map specific permissions to your users that way, or alternatively you could just add aPermissionsfield into theStudenttable.