Support » Plugin: External Login » Role field on a different table

  • Resolved intlabnz

    (@intlabnz)


    Hi Tom.

    First of all, thank you for your awesome plugin. I really appreciated it!

    Today, I have a question regarding the Role filed on a different table.

    My role filed is not on the user table(it is on ‘rx_member’), but it is on a different table(rx_group).

    So, I can’t set the role filed name at the Database Table Mapping on your plugin.

    Can you please let me know how to connect with the different table?

    Hope you have a great day!

    • This topic was modified 3 months, 2 weeks ago by intlabnz.

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author tbenyon

    (@tbenyon)

    Hey @intlabnz,

    Unfortunately the answer to your question is, the plugin does not currently support this.

    I am working on a pro version of the plugin and support for this is on my list of ideas but that is a long way off.

    I have two suggested solutions for now:

    Add a hook
    I could add a hook into the plugin allowing you to override the logic for fetching the role.

    Then you could write the logic to query your database.

    I would provide the connections details into the hook so you could use those settings to make the query. I can send an example with an example query.

    This would still require me to schedule in the time to add the hook. My day job is busy at the moment so could be a few weeks πŸ™

    Hack the plugin
    Basically either yourself or pay for a developer to modify the code for the plugin to do what you want. Work on this could start immediately however everytime the plugin gets updated you’d need to re-integrate these changes.

    This is not a very good solution but just wanted to give you options.

    Let me know your thoughts.

    Thanks,

    Tom

    Thread Starter intlabnz

    (@intlabnz)

    Thank you for your kind answer. If there was a pro version, I would definitely have bought it.

    As you adviced, I wish to add a query on the hook, and I would grateful if you let me know the details of connection even though you are very busy.

    Thank you again, and hope you have a great day. πŸ™‚

    Plugin Author tbenyon

    (@tbenyon)

    Hey @intlabnz,

    I will start work on adding the hook for you in the next couple of weeks πŸ™‚

    Feel free to come back and check up on me if you haven’t heard any more in two weeks time but I will try and update you also πŸ™‚

    Thanks,

    Tom

    Plugin Author tbenyon

    (@tbenyon)

    Hey @intlabnz,

    I have completed this work for you.

    There is now a hook that allows you to do what you want to do, and all you’ll need to do is write your own query based on the structure of your personal database.

    The hook provides you with a range of different information:
    $roles – the array of roles already mapped using the built in logic based on data set it the admin panel
    $username – the username that was typed in the login screen
    $userData – the data that was originally queried for the user

    It is expected that you will return an array of roles.

    The below example assumes that you want to use the roles derived from the built in logic and add to it.
    For this reason new roles are added onto the passed array and that is returned.
    If you want to ignore what the logic currently provides you could simply pass a new array.

    The example uses some built in functionality to the plugin to help you build your query. You may prefer to write a more basic query just using text.

    
    function myExlogRolesMapper($roles, $username, $userData) {
        // Uses the data provided to the plugin to create the database object and data required for a query
        $db_data = exlog_get_external_db_instance_and_fields();
    
        // Start building a query to fetch the user
        // This is the first bit you will want to modify to select data from your additional table where the role is being stored
        $query_string =
            'SELECT *' .
            // This is specifying the table specified in the settings panel, you can hard code these if you rather
            ' FROM ' . esc_sql($db_data["dbstructure_table"]) .
            // This finds the correct user based on the username field set in the settings and the username that they typed in
            ' WHERE (' . esc_sql($db_data["dbstructure_username"]) . '="' . esc_sql($username) . '"';
    
        if ($db_data["dbstructure_email"]) {
            // Because the username they type in can be an e-mail, if you have set an e-mail field in the settings panel we will also try and find the user by e-mail
            $query_string .= ' OR ' . esc_sql($db_data["dbstructure_email"]) . '="' . esc_sql($username) . '")';
        } else {
            $query_string .= ')';
        }
    
        // Use the above computed query actually fetch the data
        $rows = $db_data["db_instance"]->get_results($query_string, ARRAY_A);
    
        // Checking if a user was found
        if ($rows && count($rows) > 0) {
            $foundData = $rows[0];
            // If the custom field in your database called 'myCustomRoleField' has 'editingKing' stored in it
            if ($foundData['myCustomRoleField'] == 'editingKing') {
                // Add the wordpress role 'editor' to the user
                array_push($roles, "editor");
            }
        }
    
        // return the array of roles as WordPress supports multiple roles in the backend even though their settings pane only shows one
        return $roles;
    }
    add_filter('exlog_hook_filter_assign_roles', 'myExlogRolesMapper', 10, 3);
    

    I’m going to mark this as resolved as I’ve now provided this hook as discussed, but if you have any more questions, please don’t hesitate to get back in contact πŸ™‚

    Let me know how you get on!

    Thanks,

    Tom πŸ™‚

    Thread Starter intlabnz

    (@intlabnz)

    Great! Finally it works!
    Thank you for your support. Once the pro version came out, I will buy it without any hesitation. πŸ™‚

    Plugin Author tbenyon

    (@tbenyon)

    Hey @intlabnz,

    Thanks for getting back to me and I’m really glad you’ve got it working.

    Nice one 😊

    Thread Starter intlabnz

    (@intlabnz)

    I have one more question regarding mapping a website address.

    My external login has a field of “homepage” URL address, and I wish it to map at “website” URL field on Users at WordPress.

    I have tried to add it many times, but have failed.

    I would be grateful if you let me the query.

    Thank you for your kind support. πŸ™‚

    Kind regards.
    Jin

    • This reply was modified 2 months ago by intlabnz.
    Plugin Author tbenyon

    (@tbenyon)

    To update the website your will need another hook. I’ve not tested this but it should be something like this.

    More details on the plugin hooks in the FAQ.

    function exlog_add_additional_user_data($wp_user, $exlog_user_data, $rawResponse) {
        $user_data = wp_update_user( array( 
            'ID' => $wp_user->ID,
            'user_url' => $rawResponse['homepage']
        ));
    }
    
    add_action('exlog_hook_action_authenticated', 'exlog_add_additional_user_data', 10, 3);
    

    Let me know how you get on Jin. πŸ™‚

    Thread Starter intlabnz

    (@intlabnz)

    Thank you for your time! Finally, it works very well!

    I have just sent a beer to you! πŸ™‚

    Plugin Author tbenyon

    (@tbenyon)

    @intlabnz that’s great news and thanks for the beer 😊

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.