Mono Support Programmicaly add rules ? 

Viewed 14790 time(s), 6 post(s), 5/2/2012 4:53:03 PM - by shawndg
5/2/2012 4:53:03 PM
1871 Reputation 252 Total posts

Hi Guys,

I want to add rules during registration and so far I did try setting it in the web config but that did not work.. maybe because my rule is custom..

The following code, does not work but I think it is close.

//add default isband rule to band profiles..

IEnumerable<MonoSoftware.MonoX.API.Role> Rules = new IEnumerable<MonoSoftware.MonoX.API.Role>();
Rules = SecurityUtility.UserRoleIds(userId);

MonoSoftware.MonoX.API.RoleAdapter ruleadb = MonoSoftware.MonoX.API.RoleAdapter.GetInstance();
ruleadb.ChangeUserRoles(userId, Rules);

any help ?

thanks

1
5/3/2012 6:50:26 AM
629 Reputation 83 Total posts

Hi shawndg,

If you're trying to add new roles to the user, you should try this

var userRoles = MonoSoftware.MonoX.API.RoleAdapter.GetInstance().GetUserRoles(userId).ToList();
 
userRoles.Add(MonoSoftware.MonoX.API.RoleAdapter.GetInstance().First(role => role.Name == "YourRole"));
 
MonoSoftware.MonoX.API.RoleAdapter.GetInstance().ChangeUserRoles(userId, userRoles);

2
5/3/2012 5:14:05 PM
1871 Reputation 252 Total posts

The code does not work.. =/

Error 19 'MonoSoftware.MonoX.API.RoleAdapter' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'MonoSoftware.MonoX.API.RoleAdapter' could be found (are you missing a using directive or an assembly reference?) C:\#PittsburghSceneProd\CustomParts\BandRegister\MembershipEditor.ascx.cs 119 76 Portal
Error 18 'System.Linq.IQueryable<MonoSoftware.MonoX.API.Role>' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Linq.IQueryable<MonoSoftware.MonoX.API.Role>' could be found (are you missing a using directive or an assembly reference?) C:\#PittsburghSceneProd\CustomParts\BandRegister\MembershipEditor.ascx.cs 118 99 Portal


Note im using .net 4.0 and the latest MonoX

3
5/3/2012 5:16:27 PM
1871 Reputation 252 Total posts

Missing..

using System.Linq;

my bad..

4
5/3/2012 5:45:41 PM
1871 Reputation 252 Total posts

still no dice.. =/

//add default isband rule to band profiles..
var userRoles = MonoSoftware.MonoX.API.RoleAdapter.GetInstance().GetUserRoles(userId).ToList();
userRoles.Add(MonoSoftware.MonoX.API.RoleAdapter.GetInstance().First(role => role.Name == "isband"));
MonoSoftware.MonoX.API.RoleAdapter.GetInstance().ChangeUserRoles(userId, userRoles);

{"Object reference not set to an instance of an object."}
happens on line ..
userRoles.Add(MonoSoftware.MonoX.API.RoleAdapter.GetInstance().First(role => role.Name == "isband"));


5
5/10/2012 12:05:32 PM
3016 Reputation 428 Total posts

Hi shawdg,

Did you try with DefaultUserRoles property in web.config? By setting that property newly registered user should be added in role(s) defined by that property.

Have you checked that your role exist in database?

Regards.

6
This is a demo site for MonoX. Please visit Mono Software for more info.