AzureAD / active-directory-b2c-wordpress-plugin-openidconnect

A plugin for WordPress that allows users to authenticate with Azure AD B2C using OpenID Connect.
MIT License
31 stars 28 forks source link

Allows users to save custom fields created in Azure to users in WP #20

Open peterspliid opened 6 years ago

peterspliid commented 6 years ago

For example if I want a field called company, I first create the field in Azure, then I add the following code to my Wordpress plugin/functions.php file:

function custom_ms_fields($userID, $payload) {
    if (isset($payload['extension_Company']))
        update_user_meta($userID, 'company', sanitize_text_field($payload['extension_Company']));
}
add_action('b2c_new_userdata', 'custom_ms_fields', 10, 2);
add_action('b2c_update_userdata', 'custom_ms_fields', 10, 2);

This will save the information the user entered in the company field to the user meta field 'company' in Wordpress

msftclas commented 6 years ago

CLA assistant check
All CLA requirements met.

peterspliid commented 6 years ago

The new commit is needed if I have a Wordpress installation in different languages, and want the user to view the authentication site in the same language as Wordpress. With the committed changes, I have added the following to my functions file:

function npa_azure_lang($authorization_endpoint) {
    return $authorization_endpoint . '&lang=' . $_COOKIE['npa_lang'];
}
add_filter('b2c_authorization_endpoint', 'npa_azure_lang');

where $_COOKIE['npa_lang'] is the language identifier.

ArthurDumas commented 5 years ago

@peterspliid, I just started using this AD B2C Wordpress plugin and I like your changes for updating user meta fields from the AD B2C custom attributes. May I ask why you require the use of an action hook rather than just calling update_user_meta() in b2c_verify_token() after the calls to wp_insert_user() and wp_update_user(). Is it so you can more easily control which AD B2C custom attributes are added to Wordpress? Thanks for the work on this plugin!

peterspliid commented 5 years ago

@ArthurDumas Sorry for the late response. Yes you are correct. You might want to map fields from AD B2C to your custom wordpress fields, or process or verify the data before inserting it. It is generally good practice to use actions or filters when it comes to custom data