Search code examples
permissionsorchardcms

Permission defaults ignored in Orchard


I try to set permissions for my new module. Otherwise they seem to work, but the defaults are ignored, nothing is checked for role-permission pairs I've set in the code. My code (Permissions.cs) seems OK:

using System.Collections.Generic;
using Orchard.Environment.Extensions.Models;
using Orchard.Security.Permissions;
using Orchard.Environment.Extensions;
using My.Module.Utils;

namespace My.Module
{
    public class Permissions : IPermissionProvider {
        public static readonly Permission AccessMyModule = new Permission { 
            Description = Constants.AccessAddon, Name = "AccessMyModule" 
        };

        public virtual Feature Feature { get; set; }

        public IEnumerable<Permission> GetPermissions() {
            return new[] {
                AccessMyModule
            };
        }

        public IEnumerable<PermissionStereotype> GetDefaultStereotypes() {
            return new[] {
                new PermissionStereotype {
                    Name = Constants.MyModuleAdministratorRole,
                    Permissions = new[] { AccessMyModule }
                }
            };
        }
    }
}

I double checked that all the constants stored in Constants and the references for them are valid. The code snippet here is simplified, in fact I have more permissions and more roles in my project, but I confirmed commenting out everything but one permission and one role doesn't fix the problem. The defaults for the other modules in the same solution work fine, though there's no bug reported by IntelliSense and everything else in the module seems to work. So where else could be the root of the problem?

EDIT: I followed @mdameer's comment and confirmed that GetDefaultStereotypes() really runs only after reinstall. However, an error occurred while enabling the module after reinstall, so the defaults were not loaded. I know that the supposed way is to check the role - permission name in the dashboard, but I would like to find another workaround, because

  • I would like to solve the error that occurred and not to risk it happenning on the production server as well, and I can't delete and reinstall this rather complex module several times just to debug this. It was likely caused by the reinstall process, nowhere in the permission initialization, but I can't know without running the code.
  • there are tens of roles affected, so relying on someone to click the permissions by hand each time means that there will likely be errors due to human factor.

The GetDefaultStereotypes() method is being called from class DefaultRoleUpdater in Orchard.Roles. It is called automatically from somewhere deep in the Orchard core, so simply mimicking the call and running it on startup isn't that easy. I also tried to mimic the whole function and placed it into my permissions class (or into a custom service), but now I got lost on how to run it. It is not static, but it either is part of or refers to my Permissions class, which doesn't allow for ordinary referencing by default (it has no proper constructor) and I don't want to mess it even more by changing the class to something it is not and shouldn't be.


Solution

  • Just set the default permissions you need in a migration instead of using GetDefaultStereotypes(). Here is a short example:

    public class MyMigration: Orchard.Data.Migration.DataMigrationImpl
    {
      // public
        public MyMigration(Orchard.Roles.Services.IRoleService aRoleService)
        {
          mRoleService = aRoleService;
        }
    
        public int Create()
        {
          //mRoleService.CreateRole("MyRoleName");
          //mRoleService.UpdateRole("MyRoleName", MyPermissions)
    
          return 1;
        }
    
      // private
        Orchard.Roles.Services.IRoleService aRoleService mRoleService;
    }