• Greetings,

    I have used your code example at: https://wp-oauth.com/docs/how-to/extending-endpoints/

    I have unset all the unwanted output and extra sensitive/unwanted output that generates.

    By adding

    array_walk($me_data[$wpdb->prefix.'capabilities'], function (&$value, $key) {
       $value="role";
    });

    AFTER

    foreach ($infometa as $metarow) {
     $key = $metarow->meta_key;
     if( is_serialized( $metarow->meta_value ) ){
     $me_data[$key] = unserialize( $metarow->meta_value );
     }else{
     $me_data[$key] = $metarow->meta_value;
     }
    }

    I was able to get the output of focus to look like this

    "wpos_capabilities":{"administrator":"role","bbp_keymaster":"role"}

    The trick is, I am now trying to flip the key and value. I.E.

    "wpos_capabilities":{"role":"administrator","role":"bbp_keymaster"}

    I have tried

    array_walk($me_data[$wpdb->prefix.'capabilities'], function(&$key) { $key = array_flip($key); });

    However, that sets this output

    "wpos_capabilities":{"administrator":"null","bbp_keymaster":"null"}

    Any ideas of what I may be doing wrong here?

    I am going to keep working on this and see if I can come up with a solution, but I would really appreciate some suggestions, and hopefully this can help others looking to do this.

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Justin Greer

    (@justingreerbbi)

    Hi @odinwynd,
    Thanks for reaching out. Without actually seeing your code, I would suggest simply supplying the capabilities in an array and not using the key of “role” to avoid conflicts with key => values.

    Would it be sufficient enough to just supply all the capabilities in a simple array and not a key pair?

    Thread Starter odinwynd

    (@odinwynd)

    I am uncertain honestly.

    The main thing I am trying to do, is get my WordPress roles into Rocket.Chat for group and role mapping.

    I have yet to produce a successful result. I think I am mostly lacking the understanding of how to pass the data of a Role Claim and Group Claim in a manner where the role would be picked up and usable from the endpoint.

    // Get the user object.
    $user = get_userdata( $user_id );
    
    // Get all the user roles as an array.
    $user_roles2 = $user->roles;
    
    // Check if the role subscriber is present in the array.
    if ( in_array( 'subscriber', $user_roles2, true ) ) {
    //    // Do something.
    $subRole = array(
          
        // sub_role_1 will act as key
        "sub_role_1" => array(
              
            "role" => "user",
    //        "DCO" => 85,
    //        "FOL" => 74,
        ),
              
        // sub_role_2 will act as key
        "sub_role_2" => array(
              
            "role" => "subscriber",
        ),
    );
    //    echo 'YES, User is a subscriber';
    }else{
     $subRole = array();
    }
    
    $result1 = $result + $subRole;

    at the bottom…

     $response = new WPOAuth2\Response($result2);  //$me_data //$result //$result4
     $response->send();

    The “added” output…

    "adm_role_1":{"role":"admin"},"adm_role_2":{"role":"administrator"}

    So I have drastically changed my approach when I learned that a duplicate key is always dropped along with it’s value. So my solution was to make a multidimensional array with that data for each role as well as target roles in Rocket.Chat.

    I just do not know what other key(s) may be intended to accompany in order for it to function, like a guid or description, as I feel I am missing a parameter and it is being skipped.

    However, if I can get this to spit out an output that is up to the “standards” of OAuth’s Group/Role claim it may work. Problem is, I am not finding much definitive information.

    • This reply was modified 3 years, 11 months ago by odinwynd. Reason: code quoting
    Thread Starter odinwynd

    (@odinwynd)

    I think I may have found the issue, but it is only a hunch until tested, but I am unsure I can present the data in this manner….

          "realmRoles": ["user", "offline_access"],
          "clientRoles": {
            "test-app": [ "customer-user" ],
            "account": [ "view-profile", "manage-account" ]
          }
    Thread Starter odinwynd

    (@odinwynd)

    This may be beyond the scope of merely extending the endpoint…

    I have tried to encode it in the needed format, and it just crashes WordPress with a critical error.

    I tried with json_encode()

    That would spit out my array as I believe it is needed to be picked up by Rocket.Chat.

    However, the array does not like having a portion that is not a forced object it seems.

    I am no expert on OAuth, and I was simply trying to think outside the box to work-around an issue. There is Roles, Groups, and Mapping I would all somehow need to pass through the /me endpoint to make this work.

    So I think this is beyond my current understanding, and would require some modifications to the plugin beyond extending an endpoint, especially since I can not get the output into the syntax needed to pick it up as a hunch work-around.

    Otherwise, this is an absolutely amazing plugin and I intend to continue using it, I just wish I could solve this.

    • This reply was modified 3 years, 11 months ago by odinwynd.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Extending Endpoint to Return User Role’ is closed to new replies.