Example #1
0
function wind_addNewUsers()
{
    require_once "wind_functions.php";
    if ($_POST['wind_new_users'] && $_POST['wind_new_role'] && $_POST['wind_new_blog']) {
        print "<div id='message' class='updated fade'><p>Adding new users...</p>";
        $users_to_add = array();
        $users_to_add = explode("\n", $_POST['wind_new_users']);
        $users_to_add = array_filter(array_map('trim', $users_to_add));
        $users_to_add = array_map('strtolower', $users_to_add);
        foreach ($users_to_add as $user) {
            // does this look like a valid uni?
            if (preg_match('/^[a-z]{2,}[0-9]+$/', $user)) {
                // yes, go ahead
                // Check to see if user already exists; if so, subscribe them
                if ($existing_user = get_userdatabylogin($user)) {
                    // user already exists; add to blog
                    print "<br>{$user} already exists, adding to blog";
                    add_user_to_blog($_POST['wind_new_blog'], $existing_user->ID, $_POST['wind_new_role']);
                } else {
                    // user doesn't exist
                    //look up their ldap info... make warning if user can't be found in ldap
                    print "<br>{$user} does not exist yet... ";
                    $user_ldap_info = get_ldap_information($user);
                    if ($user_ldap_info['first_name']) {
                        print "found {$user_ldap_info['first_name']} {$user_ldap_info['last_name']} in LDAP. Adding...";
                    } else {
                        print "No match in LDAP. Bad uni or FERPA-protected student? Adding user regardless...";
                    }
                    // done warning admin about user LDAP status
                    // add user to WP; pass in empty array of wind affiliations
                    $wind_affiliations = array();
                    wind_create_wp_user($user, $wind_affiliations);
                    // now get their user ID and add them to this blog
                    $existing_user = get_userdatabylogin($user);
                    add_user_to_blog($_POST['wind_new_blog'], $existing_user->ID, $_POST['wind_new_role']);
                }
                // done checking whether user exists in WP
            } else {
                // no, it does not ... abort
                print "<br>'{$user}' does not appear to be a valid uni; skipping...";
            }
            // done checking for valid uni
        }
        // done iterating through the list of users
        print "</div>";
    } else {
        // some field was missing
        print "<div id='message' class='updated fade'><p>A list of unis, the role, and a blog are all required. Please make a selection for each.</p></div>";
    }
}
Example #2
0
/**
 * provisions new user account - does not add to any particular blog
 * @param $user_name
 * @return nothing
 */
function wind_create_wp_user($user_name, $wind_affiliations)
{
    # now all the site options are available as variables
    extract(wind_getSiteOptions());
    require_once "wind_defaults.php";
    global $wpdb;
    $debug = false;
    // get_ldap_info returns
    // array(first_name => $firstName, last_name => $lastName, email => $email, uni => $uni);
    error_log("getting ldap info for {$user_name}\n", 3, $wind_log_file);
    $ldap_user_data = get_ldap_information($user_name);
    $user_email = $ldap_user_data['email'];
    $random_password = substr(md5(uniqid(microtime())), 0, 20);
    // create user
    $user_id = wpmu_create_user($user_name, $random_password, $user_email);
    /*	 for reference - other options
    				$user_data = array(
    					'ID' => $user_id,
    					'user_login' => x,
    					'user_nicename' => x,
    					'first_name' => x,
    					'last_name' => x,
    					'nickname' => x,
    					'display_name' => x,
    					'user_email' => x,
    					);
    				*/
    update_usermeta($user_id, 'first_name', $ldap_user_data['first_name']);
    update_usermeta($user_id, 'last_name', $ldap_user_data['last_name']);
    $superadmins = explode(" ", $wind_super_admins);
    if (in_array($user_name, $superadmins)) {
        error_log("{$user_name} is a super admin\n", 3, $wind_log_file);
        require_once WIND_WP_PATH . "wp-admin/includes/ms.php";
        grant_super_admin($user_id);
    }
    $display_name = $ldap_user_data['display_name'] ? $ldap_user_data['display_name'] : $ldap_user_data['nickname'];
    if (empty($display_name) & !empty($ldap_user_data['first_name'])) {
        $display_name = $ldap_user_data['first_name'] . " " . $ldap_user_data['last_name'];
    }
    if (!empty($display_name)) {
        $wpdb->update($wpdb->users, compact('display_name'), array('ID' => $user_id));
    }
    //This is for plugin events
    do_action('wpmu_activate_user', $user_id, $random_password, false);
    error_log("In create user - wind check course affils is {$wind_check_course_affils} \n", 3, $wind_log_file);
    if ($wind_check_course_affils) {
        error_log("yes check course affils for {$result->user_login}\n", 3, $wind_log_file);
        wind_add_to_blogs($result, $wind_affiliations, $debug);
    }
}