Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID Sign In with Twitter
Support for Vanilla Forums Cloud product

In this Discussion

Follow Us


automatic assigning roles

jeroengjeroeng New
edited March 2012 in Questions
This discussion is related to the Vanilla jsConnect addon.

Hi!

Is it possible when using jsconnect and php to assign users to a certain role? In the example the name, email etc. is passed. But there is no documentation about what else could be passed.

Thanks,

Jeroen

Tagged:

Best Answers

  • KezzBKezzB New
    Answer ✓

    I just spent the whole day trying to figure this out and finally got it going.

    As mentioned above, you need 2.1 branch.

    After that it's actually super easy once you find it.

    You just add to your config.php this setting:

    $Configuration['Garden']['SSO']['SynchRoles'] = TRUE;
    

    Then your array of roles will carry through perfectly.

    I'm using WP Vanilla Connect plugin on WordPress, so I just changed it from this:

        $user = array(
                'uniqueid'  => $current_user->ID,
                'name'      => $current_user->display_name,
                'email'     => $current_user->user_email
            );
    

    To this:

         $user = array(
                'uniqueid'  => $current_user->ID,
                'name'      => $current_user->display_name,
                'email'     => $current_user->user_email,
                'roles'     => "Member,Moderator"
            );
    

    Now it's working great.

    I'm also going to have it check on my user's levels on WP first, as I'm using the s2Member plugin, so then I can create a tiered access system in the forum.

    Perfect!

    HalfCatUnderDog
  • KezzBKezzB New
    Answer ✓

    Thought I'd also share the code I used to synch my WP s2Member roles with my forum in case anyone else finds it useful:

    //get current user role
            if(current_user_can("access_s2member_level4")){
                $thisrole = "Level 4";
            } else if(current_user_can("access_s2member_level3")){
                $thisrole = "Level 3";
            } else if(current_user_can("access_s2member_level2")){
                $thisrole = "Level 2";
            } else if(current_user_can("access_s2member_level1")){
                $thisrole = "Level 1";
            } else if($current_user->role=="subscriber"){
                $thisrole = "Free Member";
            } else {
                $thisrole = "";
            }
    
            // Map the current user values for jsConnect
            $user = array(
                'uniqueid'  => $current_user->ID,
                'name'      => $current_user->display_name,
                'email'     => $current_user->user_email,
                'roles'     => $thisrole
            );
    
    HalfCat
«1

Answers

  • Did you ever figure this out?

  • RippRipp New
    edited May 2012

    So I played around with this a bit, adding:

    $Form->SetFormValue('Roles', GetValue('roles', $JsData, ''));

    line to class.jsconnect.plugin.php, and also making some other changes (including of course, specifying the roles string in my application.)

    However, it seems that jsConnect completely ignores this.

    Where is documentation for exactly how the class Gdn_Form works, and this function SetFormValue?

    What conceptually is it doing? Are we creating a temporary form?

    Does JsConnect initialize this "Roles" entry to "Member", and then use this temporary form to write the User model to the database?

    I kind of suspect that it ignores the Roles field of this Form which I am setting. If so, where exactly is Roles being set?

    Any insight would be greatly appreciated.

  • Unfortunately I didn't figure it out. But I will look at your ideas!

  • I can set up a github repository, perhaps we can figure it out between the two of us.

  • Github repo here: http://bit.ly/JaYmU3

    The readme file also contains a diff between my version of "class.jsconnect.plugin.php" and the default one.

  • Actually it looks like their repository on Github made the same change...

    https://github.com/vanillaforums/Addons/blob/master/plugins/jsconnect/class.jsconnect.plugin.php

    I tried their version and it doesn't work for me either. The user I add is always only just a Member, right than Member and Moderator like I intended.

    So now I'm wondering if I'm generating the string incorrectly..?

  • Here is the JSON string I'm generating, anonymity a bit:

    {"uniqueid":"xxx","name":"xxx","email":"xxx","photourl":"","roles":"Member,Moderator","clientid":"xxx","signature":"xxx"}

    This is the right way to set "roles", right? Comma separated string, no spaces?

    westhian
  • The Gdn_Form class : http://lincolnwebs.com/vanilla2doc/class_gdn___form.html

    I'll watch your repo. Maybe it's better if you commit the Vanilla file and then commit your adjustments. Then it is possible to use the Git Diff tool. That is better to read and understand :)

    Since the roles in JSON is an array (or isn't it?), maybe it should look like

    "roles":{"Member","Moderator"}, "clientid" etc.

  • Yeah I can go back and do that, but a bit lazy :)

    From my email conversation with the author of the module, it seems the development branch version works. I'll try it out when I get home (as well as your suggestion about roles.)

  • I didn't have any success with their development branch, nor with your suggestion for the stable branch. Constantly logs me out of the dashboard for development branch; stable branch your suggestion didn't make a difference.

    Let me know if you have better luck.

  • Can we send a mail or something like that to the author? It seems that he is not looking in this forums.

  • RippRipp New
    edited May 2012

    I send an email to him before, didn't seem like he was too interested. Perhaps we'll have better luck if you shoot him an email.

  • jeroengjeroeng New
    edited August 2012

    After some time, I didn't get a response, sadly.

    EDIT: maybe I did something wrong (I couldn't find the message) so I've send another one.

  • @Ripp @jeroeng Did one of you guys figure this out? I am also interested in this.

  • It's possible to do this in our 2.1 branch with a config setting, but not in 2.0.18.

    This was the reply I got.

  • @jeroeng said: This was the reply I got.

    Nice, thanks for the reply. Now we just need to figure out how to set those config settings.

  • Thank you for sharing @KezzB. This is amazing stuff.

  • KezzBKezzB New
    edited October 2012

    You're welcome!

    Thanks also to you guys above, @HalfCat @Ripp @jeroeng , for figuring out the first part so I could get the whole thing going.

    I tested everything out last night btw and it works great, so my whole s2Member user base is all set to easily sign into the forum now and instantly get access to exclusive sections depending on their level. Stoked!

  • Thanks a lot! One sidenote: with this solution, you can not simply change the name of roles, can you? Since the roles are hardcoded, it will blow up if I change 'Moderator' to 'Supervisor' in the Vanilla admin. So be aware of that.

  • hi, is there a way to pass over the user roles assigned in wordpress over to vanilla? i created the same user roles in wordpress and vanilla. if a new user registers in wordpress and automatically gets a new user role, i want to sync them with vanilla. since the username and password is send over to vanilla it must be possible with the user role as well, or?

  • I am using jsconnect PHP in my website for SSO Some users are administrators and some are moderators. I tried passing 'roles' => "Member,Moderator" But didn't get it working. I tried searching but didn't find any documentation on this. Please guide.

  • I can understand that this will work in branch 2.1 But how to make it work in other versions

  • wow. if that is gonna working out, then you will be the king!

  • As an additional to Webkadabra's solution if you are sending in rolenames as opposed to roleids you could use this at the beginning of InsertForBasic

    This handles initial synchronisation when an account is created.

    $syncRoles = Gdn::Config('Garden.SSO.SyncRoles'); if($syncRoles && isset($FormPostValues['Roles'])) { $Rolenames = array_map('trim', explode(',', $FormPostValues['Roles'])); $RoleIDs = $this->SQL ->Select('r.RoleID') ->From('Role r') ->WhereIn('r.Name', $Rolenames) ->Get()->ResultArray(); $RoleIDs = ConsolidateArrayValuesByKey($RoleIDs, 'RoleID'); } else { $RoleIDs = Gdn::Config('Garden.Registration.DefaultRoles'); }

    You can define the defaultroles in config.php

    $Configuration['Garden']['Registration']['DefaultRoles'] = 'a:1:{i:0;s:1:"8";}'; a:1 indicates the array contains 1 role In parentheses i is the role index (just increment for each role you add) "8" is my role id for member. e.g. 'a:2:{i:0;s:1:"8";i:1;s1:"9"}'

    If you already have the account created but want to sync roles for every login. In applications\dashboard\controllers\class.profilecontroller.php on around line 285 after the photo is set and before starting the session add or modify

    $UserModel->Save($Data, array('NoConfirmEmail' => TRUE, 'SaveRoles' => FALSE)); $UserModel->SaveRoles($UserID, $Data['Roles']); if ($Attributes = $this->Form->GetFormValue('Attributes')) { $UserModel->SaveAttribute($UserID, $Attributes); }

    I'm sure you could have it save the roles in the save call but as I had them as rolenames it was easier to do it this way

  • you can format above so it is more readable.

    I can also suggest when posting code - mark it (select it and press the C on the buttonbar). this will format it - so it is readable.

    conversely you could try three tildes above and below your code.

    ~~~
    
    your code
    
    ~~~
    

    your code

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

    UnderDog
  • i agree. the threat is also so long now that I would recommend writing a summary on how and where exactly to implement the code

  • great. but where do i have to place your code?

    @bubazineti said: As an additional to Webkadabra's solution if you are sending in rolenames as opposed to roleids you could use this at the beginning of InsertForBasic

    This handles initial synchronisation when an account is created.

    $syncRoles = Gdn::Config('Garden.SSO.SyncRoles'); if($syncRoles && isset($FormPostValues['Roles'])) { $Rolenames = array_map('trim', explode(',', $FormPostValues['Roles'])); $RoleIDs = $this->SQL ->Select('r.RoleID') ->From('Role r') ->WhereIn('r.Name', $Rolenames) ->Get()->ResultArray(); $RoleIDs = ConsolidateArrayValuesByKey($RoleIDs, 'RoleID'); } else { $RoleIDs = Gdn::Config('Garden.Registration.DefaultRoles'); }

    You can define the defaultroles in config.php

    $Configuration['Garden']['Registration']['DefaultRoles'] = 'a:1:{i:0;s:1:"8";}'; a:1 indicates the array contains 1 role In parentheses i is the role index (just increment for each role you add) "8" is my role id for member. e.g. 'a:2:{i:0;s:1:"8";i:1;s1:"9"}'

    If you already have the account created but want to sync roles for every login. In applications\dashboard\controllers\class.profilecontroller.php on around line 285 after the photo is set and before starting the session add or modify

    $UserModel->Save($Data, array('NoConfirmEmail' => TRUE, 'SaveRoles' => FALSE)); $UserModel->SaveRoles($UserID, $Data['Roles']); if ($Attributes = $this->Form->GetFormValue('Attributes')) { $UserModel->SaveAttribute($UserID, $Attributes); }

    I'm sure you could have it save the roles in the save call but as I had them as rolenames it was easier to do it this way

«1
Sign In or Register to comment.