none
permissions based on dotnetnuke roles RRS feed

  • Question

  • Hi

    I've built an app that uses a dotnetuke database for authentication. I based this on Michael W's great post:

    Lightswitch in dotnetnuke

    This works very well and I am almost done. However, I just need to based my edit permissions on the users role in dotnetnuke.

    All I need to do is check if the user is a supervisor in the UserRoles table.  I have added users, roles and useroles to my project from the dnn database.  And I can get the user's ID with this code:

    string dnnUserName = this.Application.User.Name;
    
                // first get the user record and find the userID
                var items = from dataitem in DataWorkspace.DotnetnukeData.Users
                            where dataitem.Username == dnnUserName
                            select dataitem;
                int UserID;
                foreach (User item in items)
                {
                   UserID = item.UserID;
                }
    

    Now I just want to see if the user ID (UserID) exists for the roleID of 13 (Area Supervisor) in the UserRoles table.

    Normally I would just call some SQL but of course this is different. Can anyone point me in the right direction?

    The userID field is in the UserRoles table and it appears a userRoles collection exists for the users table.  But I am a bit lost

    as to how to work with it.  I am sure this is simple when you know how, maybe one or two lines of code?  Perhaps I don't even have

    to look up the user ID first?

    TIA

    Gus

    Monday, July 2, 2012 2:16 PM

All replies

  • Hmm, from what I can see, your code's not getting just the first record for that you need to add ".FirstOrDefault". Doing it this way, you don't need the loop.

    string dnnUserName = this.Application.User.Name;
    
    // first get the user record and find the userID
    var item = (from dataitem in DataWorkspace.DotnetnukeData.Users
        where dataitem.Username == dnnUserName
        select dataitem).FirstOrDefault();
    
    if (item != null)
    {
        var UserID = item.UserID;
    }
    Does that help?

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Monday, July 2, 2012 3:08 PM
    Moderator
  • thanks Yann, yes, that was very helpful. I had been wondering how to handle just one row.

    I managed to get where I wanted to but am not sure if it's the most efficient method. After getting the userID I now

    look up the roles to see if there is a record that matches the userId and the roleID which is 13 - Area Supervisors.

     // now get the first record that matches userID and roleID=13 (Area Supervisors)
                var RoleItem = (from dataitem in DataWorkspace.contrib2011Data.UserRoles
                            where dataitem.User.UserID == UserID
                            && dataitem.Role1.RoleID == 13
                            select dataitem).FirstOrDefault();
    
                string strRoleName;
    
                // get the role name if there is one. 
                if (RoleItem != null)
                {
                    strRoleName = RoleItem.Role1.RoleName;
                }

    This does the job for me but I am not sure if it's the most efficient method. I am wondering if I shouldn't just add a reference to the dotnetuke DLL and just call the API to get the user info and roles? I have no idea if that would work but I might try later if I have time. That would be many lines less code.

    Gus

    Monday, July 2, 2012 3:42 PM
  • The code looks basically OK. But I think you're falling into the trap of trying to have Role knowledge in the application code.

    LS expects you to create Permissions, & test for those permissions only. Users & Roles get created/defned after the application has been published & installed by the Administrative User (who adds permissions to groups, then users to roles. On the development side, you have knowledge of what Roles/Users have been create, so you defieitely can depend on there being a Role with ID of 13

    It's a common trap for those who are new to LS, who expect to have programmattic acess to roles & users.

    Does any of that make any sense?


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Monday, July 2, 2012 3:53 PM
    Moderator
  • yes, I see

    however, I am not using the LS app to manage users or roles. These already exist in the dotnetnuke database.

    I am using forms authentication to enable my users to log into the LS app with their dotnetnuke credentials.

    Dotnetnuke uses the .net authentication stuff, however, it uses its own roles tables. Am not sure why but I guess the developers wanted more flexibility or something??

    So, when my LS app loads the user gets a login box and can login with their usual credentials. But in practice the module will load inside an iFrame

    in dotnetnuke so that they will already be authenticated and will not have to log in again.

    So, I need to bypass the way LS thinks about roles and permissions and find out if the current dotnetnuke user has a role assigned to them and use that to show hide my add/edit and delete buttons.

    Does that make sense now?

    Monday, July 2, 2012 4:26 PM
  • OK, sorry, I missed that. So the RoleID is coming from a DNN table?

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Tuesday, July 3, 2012 1:39 AM
    Moderator