New modules and permission roles

Topics: Customizing Orchard, Writing modules
Jul 1, 2011 at 12:54 PM

Hi, I am starting out with Orchard, trying to write my first module.

I would like to have a new role with a few permissions that I can access in the standard users and roles admin interface.

How do I go about getting them created?

I have implemented a class with IPermissionProvider interface using the Orchard.Comments permissions.cs file as a starting point. However, when I run the application the roles/permissions do not show up. I guess I am missing some sort of start up routine that creates them, but don't know what of where.

Can anyone help point me in the right direction please??

Thanks,

Rob.

Jul 1, 2011 at 2:54 PM

OK, after digging around some more I have found that the Permissions have been created, but the role has not.

It looks like the new role should be created in the DefaultRoleUpdater.AddDefaultRolesForFeature process but I don't understand how/when this will be called.

Any ideas??

thanks,

Rob.

Jul 1, 2011 at 2:57 PM

Heres my code if that helps:

 

using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Environment.Extensions.Models;
using Orchard.Security.Permissions;

namespace HelloWorld
{
    public class HelloWorldPermissions : IPermissionProvider
    {
        public static readonly Permission FunctionOne = new Permission { Name = "FunctionOne", Description = "Function One" };
        public static readonly Permission FunctionTwo = new Permission { Name = "FunctionTwo", Description = "Function Two" };
        public static readonly Permission FunctionThree = new Permission { Name = "FunctionThree", Description = "Function Three" };

        public virtual Feature Feature { get; set; }

        public IEnumerable<Permission> GetPermissions()
        {
            return new[] {
                FunctionOne,
                FunctionTwo,
                FunctionThree
            };
        }

        public IEnumerable<PermissionStereotype> GetDefaultStereotypes()
        {
            return new[] {
                new PermissionStereotype {
                    Name = "FunctionController",
                    Permissions = new[] {FunctionOne,FunctionThree}
                }
            };
        }

    }
}

Coordinator
Jul 1, 2011 at 11:20 PM

Why do you want to create a role? The permissions and default stereotypes should be all you need in most situations. Roles should be created by users, not modules.

Jul 2, 2011 at 8:48 AM

Hi,

I am trying to integrate an existing booking system that has a few user roles. These roles are not related to publishing, like the default Orchard roles, but are more specific to the application. I would like to use Orchard to host the app as it is a great framework providing I good deal of the other requirements of the application. So, in this case I want my module to drive the role existence if possible and to setup the basic permissions against those roles by default.

Coordinator
Jul 2, 2011 at 11:42 PM

Then you should inject a IRolesService and call CreateRole on it.

Jul 3, 2011 at 10:13 AM

OK, I'll give it a go. Thanks for your help.