Updating a Module - Add new Permissions and default Role Permissions

Topics: Writing modules
Jan 16, 2012 at 10:39 PM
Edited Jan 16, 2012 at 10:41 PM

Hi, there's probably a simple answer to this, but I'm struggling to see where I would be able to "Update" a module and add default permissions for a given role.

I noticed that the call to "GetDefaultStereotypes" only occurs on "Install" of a module in  DefaultRoleUpdater : IFeatureEventHandler.Installing()

I thought I could maybe manually add the permission setting to the role in my Migrations file, but not sure that's the right way to do it either....

For example

MyNiceModule Version 1 has the Permission = "Can Read Stuff" which is enabled for the "Authenticated" role via GetDefaultStereotypes 

Now I am on MyNiceModule Version 2 (using UpdateFrom1)  and I have added the Permission "Can Write Stuff" to the Permissions.cs file and I also want to enable it for the "Authenticated" role by default...

Adding the permission to GetDefaultStereotypes does nothing as it's not being Installed. I tried Enabling and Disabling the module, but that didn't do anything either.... 

Any suggestions? 

Gracias

Coordinator
Jan 16, 2012 at 11:42 PM

What happens when you just add the new permission definition to the others you have already defined? It's just code, if you update a file, the module will be recompiled.

Jan 17, 2012 at 4:55 AM

That of course was the first things I tried. I don't get my default permission set...

Then I tried using the "Upgrade" feature by using UpdateFrom1() to indicate to Orchard that a newer version of the module existed. I had thought that perhaps it would fire the DefaultRoleUpdater : IFeatureEventHandler.Installing() method but it didn't.

Is is supposed to? 

Coordinator
Jan 17, 2012 at 5:25 AM

OK, so it's done only at feature activation. That looks like a bug to me. Please file it in the issue tracker.

Jan 17, 2012 at 5:35 AM

Ok Will do. Any suggestions for a workaround?

Coordinator
Jan 17, 2012 at 5:42 AM

Well, the only one I can think of would be to reproduce what DefaultRoleUpdater.AddDefaultRolesForFeature is doing.

Jan 17, 2012 at 9:26 AM
Edited Jan 17, 2012 at 9:27 AM

An Update:

I ended up adding a Command to make it easy to re-execute the AddDefaultRolesForFeature method. In theory, I should be able to use the same approach in my Migrations class which I'll try as well at some point, but this works for the time being

 

 

	public SRBAuthorizationCommands(IFeatureManager featureManager, 
                                        IExtensionManager extensionManager,
                                        IEnumerable<IPermissionProvider> permissionProviders)
        {
            _featureManager = featureManager;
            _extensionManager = extensionManager;
            _permissionProviders = permissionProviders;
	    //....Some code and parameters were removed from
//the constructor as it had nothing to do with this post
        }

 

 

        /// <summary>
        /// UpdateDefaultPermissionsForFeature
        /// </summary>
        /// <param name="name"></param>
       
        [CommandName("feature update-default-permissions")]
        [CommandHelp("feature update-default-permissions <featureName> \r\n\t" + "Reinitializes the default permissions for a feature.")]
        public void UpdateDefaultPermissionsForFeature(string featureName)
        {
            var featureDesc = _featureManager.GetEnabledFeatures().Where(f=>f.Name==featureName).FirstOrDefault();

            if (featureDesc == null)
                throw new OrchardException(T("Invalid feature: {0}",featureName));

            var feature = _extensionManager.LoadFeatures(new List<FeatureDescriptor>() { featureDesc }).FirstOrDefault();

            if (feature == null)
                throw new OrchardException(T("Could not load the feature from the Extension Manager: {0}", featureName));

            DefaultRoleUpdater roleUpdater = new DefaultRoleUpdater(_roleService, _permissionProviders);

            roleUpdater.AddDefaultRolesForFeature(feature);

            Context.Output.WriteLine(T("Done.").Text);
            
        }