populating dbo.Orchard_Roles_PermissionRecord table

Topics: Administration, Announcements, Core, Customizing Orchard, General, Troubleshooting, Writing modules, Writing themes
Jun 18, 2012 at 3:12 PM
Edited Jun 18, 2012 at 5:29 PM

How/where is dbo.Orchard_Roles_PermissionRecord table populated?

Jun 18, 2012 at 3:20 PM

In the AdminController of the Orchard.Roles module (look for the EditSavePOST method).

Jun 18, 2012 at 3:53 PM
Edited Jun 19, 2012 at 6:56 AM


I fail to see the piece of code that populates the dbo.Orchard_Roles_PermissionRecord table. For your convenience I include the method. Could you point me to the part that populates the table?

public ActionResult EditSavePOST(int id)
  if ( !Services.Authorizer.Authorize(
        , T("Not authorized to manage roles")) )
    return new HttpUnauthorizedResult();

  var viewModel = new RoleEditViewModel();

  if ( String.IsNullOrEmpty(viewModel.Name) )
    ModelState.AddModelError("Name", T("Role name can't be empty"));

  var role = _roleService.GetRoleByName(viewModel.Name);
  if ( role != null && role.Id != id )
    ModelState.AddModelError("Name", T("Role with same name already exists"));

  if ( !ModelState.IsValid )
    return Edit(id);

  // Save
  List rolePermissions = new List();
  foreach ( string key in Request.Form.Keys )
    if ( key.StartsWith("Checkbox.") && Request.Form[key] == "true" )
      string permissionName = key.Substring("Checkbox.".Length);
  _roleService.UpdateRole(viewModel.Id, viewModel.Name, rolePermissions);

  Services.Notifier.Information(T("Your Role has been saved."));
  return RedirectToAction("Edit", new { id });

Jun 18, 2012 at 5:26 PM

Sure, it's happening right on this line:

_roleService.UpdateRole(viewModel.Id, viewModel.Name, rolePermissions);

To be more specific, it actually happens somewhere inside that method:

public void UpdateRole(int id, string roleName, IEnumerable<string> rolePermissions) {
            RoleRecord roleRecord = GetRole(id);
            roleRecord.Name = roleName;
            foreach (var rolePermission in rolePermissions) {
                string permission = rolePermission;
                if (_permissionRepository.Get(x => x.Name == permission) == null) {
                    _permissionRepository.Create(new PermissionRecord {
                        Description = GetPermissionDescription(permission),
                        Name = permission,
                        FeatureName = GetFeatureName(permission)
                PermissionRecord permissionRecord = _permissionRepository.Get(x => x.Name == permission);
                roleRecord.RolesPermissions.Add(new RolesPermissionsRecord { Permission = permissionRecord, Role = roleRecord });

Or, even more specifically, it happens right inside of the Create method of the _permissionRepository.
And of course, the repository class uses NHibernate, which in turn will generate the required SQL statements.

If you drill down deep enough using the debugger, you'll be able to see this.


Jun 19, 2012 at 6:35 AM


I understand what you say. However, I have a difficulty believing this is how the dbo.Orchard_Roles_PermissionRecord table gets populated. By populating I mean pre-populating. Let me explain.

If/when I trace the execution flow, following your avenue, i.e.
I see a page on the screen that I've never seen before, BUT the table is already pre-populated.

Your avenue writes a new record to the dbo.Orchard_Roles_PermissionRecord table, if/when a user edits roles. I have never edited any roles. Therefore, I suspect there is another mechanism that populates the dbo.Orchard_Roles_PermissionRecord table.

Can anybody shed light on this?






Jun 19, 2012 at 5:04 PM

When the Roles module is enabled, it goes through all IPermissionProvider implementations and populates the permissions for each described role. This is done using an implementation of IFeatureEventHandler. It is in Orchard.Roles.DefaultRoleUpdater.