Skip to content

Instantly share code, notes, and snippets.

@happygrizzly
Last active August 29, 2015 14:13
Show Gist options
  • Save happygrizzly/4bf2e4b0bee3ee1b23b1 to your computer and use it in GitHub Desktop.
Save happygrizzly/4bf2e4b0bee3ee1b23b1 to your computer and use it in GitHub Desktop.
protected override void Seed(AppDbContext context) {
// define roles
var role = new Role("SimpleUser");
var roleAdvanced = new Role("AdvancedUser");
// define activities
var readActivity = new Activity("Read");
var updateActivity = new Activity("Update");
var deleteActivity = new Activity("Delete");
var manageActivity = new Activity("Manage");
// define resources
var blogsResource = new Resource("Blogs");
var talksResource = new Resource("Talks");
var eventsResource = new Resource("Events");
// allow simple user to read blogs
var blogsReadActivity = new RoleResourceActivity { Role = role, Resource = blogsResource, Activity = readActivity };
context.RoleResourceActivities.Add(blogsReadActivity);
// allow advanced user to update blogs
var blogsUpdateActivity = new RoleResourceActivity { Role = roleAdvanced, Resource = blogsResource, Activity = updateActivity };
context.RoleResourceActivities.Add(blogsUpdateActivity);
var blogsDeleteActivity = new RoleResourceActivity { Role = roleAdvanced, Resource = blogsResource, Activity = deleteActivity };
context.RoleResourceActivities.Add(blogsDeleteActivity);
// allow simple user to read talks
var talksReadActivity = new RoleResourceActivity { Role = role, Resource = talksResource, Activity = readActivity };
context.RoleResourceActivities.Add(talksReadActivity);
// allow advanced user to update talks
var talksUpdateActivity = new RoleResourceActivity { Role = roleAdvanced, Resource = talksResource, Activity = updateActivity };
context.RoleResourceActivities.Add(talksUpdateActivity);
var talksDeleteActivity = new RoleResourceActivity { Role = roleAdvanced, Resource = talksResource, Activity = deleteActivity };
context.RoleResourceActivities.Add(talksDeleteActivity);
// create one user
var user = new User("User", PasswordHash.CreateHash("User"));
var user2 = new User("User2", PasswordHash.CreateHash("User2"));
// add both "SimpleUser", "AdvancedUser" roles
user.Roles.Add(role);
user.Roles.Add(roleAdvanced);
user2.Roles.Add(role);
// commit changes
context.Users.Add(user);
context.Users.Add(user2);
context.SaveChanges();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment