Last active
August 29, 2015 14:10
-
-
Save itthinx/9f2763fb69116a31b572 to your computer and use it in GitHub Desktop.
Craig's Auth0 integration with Groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
_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 */ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment