Hey People,
this is my first foray into WordPress and PHP so paint me with a bullseye and call me flamebait...
My end-goal is to map the Windows Azure Active Directory groups through to the WordPress Groups Module groups.
So yes, I'm running WordPress on Azure (standard create website install) and have a Windows Azure AD tenant that I'm getting users and groups from.
I'm using the great Auth0.com plugin to authenticate the WAAD users and the Groups plugin from itthinx.
I've done some playing around with some code in the footer.php to get things going and it's working fine. But realistically I don't want the group mapping being done on every page, I just needs to be on login.
So I tried adding some code to functions.php with an add_action('wp_login', ...) but as far as I can tell, it's not firing.
I'd appreciate any help, and chances are I've missed something obvious....
Here's what I've added to functions.php:
if(!function_exists('_log')){
function _log( $message ) {
if( WP_DEBUG === true ){
if( is_array( $message ) || is_object( $message ) ){
error_log( print_r( $message, true ) );
} else {
error_log( $message );
}
}
}
}
function craig_Login_Hook() {
_log ('firing craig_Login_Hook()');
require_once(ABSPATH . 'wp-includes/pluggable.php' );
global $currentauth0_user;
$user_id = get_current_user_id();
if ($user_id > 0) {
get_currentauth0userinfo();
/* grab the Auth0 groups for the current user */
$auth0Groups = $currentauth0_user->groups;
/* grab all the WordPress Groups */
$groups = Groups_Group::get_groups(array ( 'order_by'=>'name') );
/*
* Iterate through the Auth0 based groups
* If the group starts with our prefix
* If a matching one exists in WordPress's Groups module,
* if current user is a member
* do nothing
* else
* add user to group
* else
* ignore, we're only concerned with groups that exist in WordPress Groups
* else
* ignore, we're only concerned with groups that start with out prefix (helps prevent accidental conflicts)
*/
foreach ($auth0Groups as $auth0Group) {
if (substr($auth0Group, 0, 2) == 'CT') {
if ( $group = Groups_Group::read_by_name( $auth0Group ) ) {
if (Groups_User_Group::read( $user_id , $group->group_id )) {
/* user already in group */
_log('user in group '.$group->name);
} else {
/* add user to group */
_log('add user to group '.$group->name);
Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) );
}
} else {
/* no matching group */
_log('no matching group '.$group->name);
}
} else {
/* not a prefixed group */
_log('not a prefix group '.$group->name);
}
}
/* Removal from WordPress Groups
* Iterate through the WordPress Groups
* If the group starts with our prefix
* Is the current user a member of this group
* Is this group in the list of Auth0 groups
* Do nothing, they're meant to be in that group
* else
* Remove the current user from the WordPress group
* else
* Do nothing, they're already not in the group
* else
* Do nothing, we don't want to mess with WordPress's "standard" groups
*/
foreach ($groups as $groupG) {
if (substr($groupG->name, 0, 2) == 'CT') {
if (Groups_User_Group::read( $user_id , $groupG->group_id )) {
if (in_array($groupG->name, $auth0Groups)) {
/* user already in group */
} else {
/* user should not be in group */
Groups_User_Group::delete( $user_id, $groupG->group_id );
}
} else {
/* user isn't in group */ }
} else {
/* not a prefixed group */
}
}
}
}
add_action( 'wp_login', 'craig_Login_Hook');
Thanks
Craig