Add a Role

Topics: Writing modules
Feb 4, 2013 at 6:08 PM
I am currently creating a commerce module (based on Nwazet). My ProductPart will have a UnitPrice and a WholesaleUnitPrice. I'd like to be able to display only the WholesaleUnitPrice in the view to wholesale users. Standard (Anonymous and Authenticated) users will see the standard Unit price.

The administrator will create an account for a wholesale user and give them the role of a Wholesale user.

I could certainly create a role via the admin page, but I'd rather create it programmatically. My question is what is the best way to accomplish this? In poking around via the API, I see that one can implement Orchard.Environment.IFeatureEventHandler. Within that I suppose one could use the default RoleService and add roles. Is that the best way to do it? I suppose it might since one would want to delete the role if the feature is disabled.

Thoughts?
Feb 6, 2013 at 12:38 AM
bump
Coordinator
Feb 10, 2013 at 6:47 AM
Through the content manager, you can create content items, configure parts and so on. Users are just content items, and roles are added as a part.
Feb 11, 2013 at 8:59 PM
Hi Bertrand,

Or using the RoleService, I can get Permissions and update/create a Role. Right?
Coordinator
Feb 12, 2013 at 12:57 AM
Not following. How would the role service help with permissions?
Feb 26, 2013 at 2:47 AM
Now I haven't tried this yet, but it seems to me that one can use the RoleService to create a Role and then the permissions for it. Correct?

If so, in a module, where would I create that code? In the OnFeatureActivated handler? Where might that be located?
Coordinator
Feb 26, 2013 at 6:18 AM
Yes. Why not do that in a migration?
Feb 26, 2013 at 2:14 PM
That's a really good idea. Thanks! That way, it's still there if the feature is deactivated and reactivated.
Feb 26, 2013 at 2:52 PM
I've been Googling on adding rows via a migration. I see lots of examples of creating tables and adding/altering columns/fields. But I haven't found anything on adding rows. I know I need to add a row to Orchard_OrchardRoles_RoleRecord and to Orchard_OrchardRoles_RolesPermissionRecord, but I don't see how.
Feb 26, 2013 at 3:14 PM
Create a content item through the UI, and then use the Import/Export feature to export the data for that content item. The export gives you an .xml that in orchard is called a "Recipe". If you run that recipe from the command line via Orchard.exe it will import the data for the content items in the recipe. Same thing if you import the same file through the dashboard Import/Export feature. This handles creating the Records associated with the content parts that make up the content items in the recipe.

When I need to import data for "Records" as you put it, i create a recipe and run it after I deploy the updated code. I tried once to have recipe execution as part of my migration but it didn't work for some reason. I'm not sure if I was doing something wrong or if some changes are needed to support that use case.

so just do, replacing myRecipeName with the name you declare in the recipe.xml file:
c:\projects\Orchard\src\Orchard.Web>bin\orchard.exe recipes execute myRecipeName
Feb 26, 2013 at 6:05 PM
Monarch,

What didn't work exactly?
Feb 26, 2013 at 6:17 PM
samuelshirley2008 wrote:
Monarch,

What didn't work exactly?
Running a recipe as part of my migration, in my Migrations.cs class. That was something I specifically wanted to do, but you don't have to do it that way. It works perfectly fine if you run the recipe from the command line with the command I pasted above.
Feb 26, 2013 at 6:19 PM
TheMonarch wrote:
samuelshirley2008 wrote:
Monarch,

What didn't work exactly?
Running a recipe as part of my migration, in my Migrations.cs class. That was something I specifically wanted to do, but you don't have to do it that way. It works perfectly fine if you run the recipe from the command line with the command I pasted above.
Right but do you have an example of your unsuccessful code? Maybe Bertrand can help us both with that?
Coordinator
Feb 26, 2013 at 10:39 PM
I'm merely making suggestions. Sorry if I led you in a direction that doesn't work.
Feb 27, 2013 at 4:43 AM
All good, man. I'm happy for the help. :-)

How would you insert a couple of rows?
Coordinator
Feb 27, 2013 at 4:48 AM
Same as anywhere else, with ContentManager or a repo if not a part record.
Mar 7, 2013 at 5:07 PM
So at that point, it's better to not do it in a migration then. Is there somewhere where I can write code to perform an action (i.e. write these couple of rows to a table) upon feature activation? Also then delete them upon feature deactivation?
Coordinator
Mar 7, 2013 at 5:16 PM
A recipe?
Mar 7, 2013 at 5:28 PM
I'd prefer to do it from code rather than a recipe. I know I've seen some code somewhere in some forum that describes using an event "OnFeatureAvtivating" but I can't seem to find anything on that right now.
Coordinator
Mar 7, 2013 at 7:53 PM
Creating a role seems like a perfect job for a recipe. Not sure why you prefer not to do it that way.
Mar 7, 2013 at 8:02 PM
Maybe I don't understand recipes then. As far as I understand them, they're XML files that describe what components are needed in an action (i.e. installing a new site). You can't add rows to an individual table, can you?

I also see recipes as an extra step that needs to be run from the command line. I'd like these rows to be added upon feature activation.

Again, maybe I don't understand recipes well enough.
Coordinator
Mar 7, 2013 at 8:09 PM
You can do lots of things in recipes: run commands, create items, enable features, or pretty much anything as they are fully extensible. The extra step enables the user to say no, which is nice, especially if you are going to create a role. I'd want to be able to refuse that.
Mar 7, 2013 at 8:21 PM
Ok. I understand that. My first thought in reading that was "Well, if I am coding to need a certain role type, then it better be there. And if I'm writing the software for my client, then they better accept it".

But I suppose that's not a good view and you need to write your software to be more fault tolerant.

I'll look into recipes then. Now by creating items, do you mean a role type? Can you give me an example?
Coordinator
Mar 7, 2013 at 8:32 PM
Your code should definitely not depend on that role being there. The administrator can always delete roles.

Look at existing setup recipes for examples.