psignoret / aad-sso-wordpress

Single Sign-on with Azure Active Directory (for WordPress)
Other
266 stars 79 forks source link

Get and Store AD Attributes for users #109

Open zeechaudhry opened 7 years ago

zeechaudhry commented 7 years ago

I would like to build experiences for users based on their group membership and AD attributes. Since the plugin allows assigning wp-roles to groups, which by the way works great. But am struggling to find or understand how to get the AD attributes of the user and store then to users meta.

Is it even possible to get/store AD attributes? if so can you please tell how? Also do you plan to add this feature as part of the plugin, so that we can see the attributes list and set which ones to store in wp.

Thanks!

psignoret commented 7 years ago

It is possible to retrieve Azure AD attributes, yes. (Note that I put emphasis on the fact that we are talking about the cloud service Azure Active Directory, not about the on-premises Windows Server Active Directory.) This plugin obtains group membership details by querying the Azure AD Graph API for group membership details. When a new user is auto-provisioned (if the feature is enabled), then properties about the Azure AD user are used to create the new WordPress user.

So, it is certainly conceivable to enhance the plugin to retrieve additional details about the user, and map these properties to WordPress properties. The plugin would need to do this on every sign-in. It's a good idea: I'll add it officially to the backlog, though it will take some time to implement correctly. (Pull requests welcome!)

Another approach would be to use Azure AD's capabilities as a SCIM client, enabling automatic provisioning into the application: https://docs.microsoft.com/en-us/azure/active-directory/active-directory-scim-provisioning It is likely possible to enhance this plugin (or build a separate one) to act as a SCIM service provider.

Senilex commented 7 years ago

Thank you for providing this plugin. I would love for this plugin to be able to map the following azure ad fields to wordpress users user_meta: Job Title (title) Department (department) Manager (manager) Office (physicalDeliveryOfficeName) Phone (telephoneNumber) IP Phone (ipPhone) Email (mail)

Can it retrieve this information store in a variable and post it to the user_meta?

Right now I add this extra information by adding extra user fields (using the code below - putting it in my functions.php - posting for reference) to a user profile and then entering the information manually.

/* Extra User Profile Fields */

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra User Details", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label for="job_title"><?php _e("Job Title"); ?></label></th>
<td>
<input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( get_the_author_meta( 'job_title', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your job title."); ?></span>
</td>
</tr>

<tr>
<th><label for="dept"><?php _e("Department"); ?></label></th>
<td>
<input type="text" name="dept" id="dept" value="<?php echo esc_attr( get_the_author_meta( 'dept', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your department."); ?></span>
</td>
</tr>

<tr>
<th><label for="office"><?php _e("Office"); ?></label></th>
<td>
<input type="text" name="office" id="office" value="<?php echo esc_attr( get_the_author_meta( 'office', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your office location."); ?></span>
</td>
</tr>

<tr>
<th><label for="manager"><?php _e("Manager"); ?></label></th>
<td>
<input type="text" name="manager" id="manager" value="<?php echo esc_attr( get_the_author_meta( 'manager', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your manager."); ?></span>
</td>
</tr>

<tr>
<th><label for="phone"><?php _e("Phone"); ?></label></th>
<td>
<input type="text" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your phone number."); ?></span>
</td>
</tr>

</table>
<?php }

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

update_user_meta( $user_id, 'job_title', $_POST['job_title'] );
update_user_meta( $user_id, 'dept', $_POST['dept'] );
update_user_meta( $user_id, 'office', $_POST['office'] );
update_user_meta( $user_id, 'manager', $_POST['manager'] );
update_user_meta( $user_id, 'phone', $_POST['phone'] );
update_user_meta( $user_id, 'extension', $_POST['extension'] );
}
richardjgreen commented 7 years ago

Seconded. I would be interested in the ability to pull the job title through from Azure AD. I could then include this attribute on the users' bio summary to appear at the bottom of their posts.

paritosharya408 commented 3 years ago

Thank you for providing this plugin. I would love for this plugin to be able to map the following azure ad fields to wordpress users user_meta: Job Title (title) Department (department) Manager (manager) Office (physicalDeliveryOfficeName) Phone (telephoneNumber) IP Phone (ipPhone) Email (mail)

Can it retrieve this information store in a variable and post it to the user_meta?

Right now I add this extra information by adding extra user fields (using the code below - putting it in my functions.php - posting for reference) to a user profile and then entering the information manually.

/* Extra User Profile Fields */

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra User Details", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label for="job_title"><?php _e("Job Title"); ?></label></th>
<td>
<input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( get_the_author_meta( 'job_title', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your job title."); ?></span>
</td>
</tr>

<tr>
<th><label for="dept"><?php _e("Department"); ?></label></th>
<td>
<input type="text" name="dept" id="dept" value="<?php echo esc_attr( get_the_author_meta( 'dept', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your department."); ?></span>
</td>
</tr>

<tr>
<th><label for="office"><?php _e("Office"); ?></label></th>
<td>
<input type="text" name="office" id="office" value="<?php echo esc_attr( get_the_author_meta( 'office', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your office location."); ?></span>
</td>
</tr>

<tr>
<th><label for="manager"><?php _e("Manager"); ?></label></th>
<td>
<input type="text" name="manager" id="manager" value="<?php echo esc_attr( get_the_author_meta( 'manager', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your manager."); ?></span>
</td>
</tr>

<tr>
<th><label for="phone"><?php _e("Phone"); ?></label></th>
<td>
<input type="text" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your phone number."); ?></span>
</td>
</tr>

</table>
<?php }

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

update_user_meta( $user_id, 'job_title', $_POST['job_title'] );
update_user_meta( $user_id, 'dept', $_POST['dept'] );
update_user_meta( $user_id, 'office', $_POST['office'] );
update_user_meta( $user_id, 'manager', $_POST['manager'] );
update_user_meta( $user_id, 'phone', $_POST['phone'] );
update_user_meta( $user_id, 'extension', $_POST['extension'] );
}

@Senilex Did you ever get this working in some form? I have a similar requirement to map language attribute for user stored in Azure AD and use that in WP to set the default language for a visitor on a multi-lingual website.