• Resolved sontru

    (@sontru)


    Is it possible to have users local to each subdomain?

    What I want to do is to set up a multisite with global (admin of subdomain) users created and authenticated via Shibboleth and allow users (non-admins) created locally for any subdomain that need users. Has anyone set up anything similar to this and how?

    P.S. Has anyone gotten the Shibboleth plugin to work with this current MS (3.4.2) version?

Viewing 15 replies - 1 through 15 (of 25 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    By default users belong to the network.

    They have ‘subscriber’ access to all sites (read-only and comment).

    If you need more roles, they get provided ad hoc.

    So … that’s kind of similar?

    Thread Starter sontru

    (@sontru)

    Thanks for the reply, but not really.

    I wonder if it is possible add a prefix or suffix to the user/login name when a user is created with a subdomain..? Does anyone know of such a plugin?

    No response from anyone using Shibboleth sign-on? (Do I need to make another post asking..?)

    Thread Starter sontru

    (@sontru)

    Right, I’ve managed to get the Shibboleth plugin working! Yay!

    Could anyone give me a pointer to a user creation plugin that can put a prefix or suffix (of that subdomain) on the username when a user is created?

    Thread Starter sontru

    (@sontru)

    Right, I’ve managed to break Shibboleth login! 🙁

    Looks like the WP Email Login plugin is breaking Shib – anyone know why and how to fix this?

    The idea is that (if I can get prefix/suffix added to a created user, then they can login using their email instead of an odd login name. But if the email login breaks Shib, then it won’t work.

    Has anyone got any suggestions?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    I wonder if it is possible add a prefix or suffix to the user/login name when a user is created with a subdomain..? Does anyone know of such a plugin?

    Probably. You’d want to extend something like this:

    <?php
    global $blog_id;
    if ( $blog_id == 2 ) {
    
      function sontru_activate_user( $user_id, $password, $meta )
      {
     // Something to change the user name...
    }
      add_action( 'wpmu_activate_user', 'sontru_activate_user', 10, 3 );
    }
    ?>

    This would say ‘Anyone from blog 2 gets something changed’

    Thread Starter sontru

    (@sontru)

    Thanks for the reply and suggestion (was beginning to think I was just talking to myself!) I’ve tried various pieces of code and have not made any success 🙁

    I have found that if I insert the following code:

    $hostAddress = explode('.',$_SERVER['HTTP_HOST']);
            $user .= '_' . $hostAddress[0];

    directly into the function wpmu_signup_user in the file wp-includes/ms-functions, it does what I want, as:

    function wpmu_signup_user($user, $user_email, $meta = '') {
            global $wpdb;
    
            $hostAddress = explode('.',$_SERVER['HTTP_HOST']);
            $user .= '_' . $hostAddress[0];
    
            // Format data
            $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
            $user_email = sanitize_email( $user_email );
            $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
            $meta = serialize($meta);
    
            $wpdb->insert( $wpdb->signups, array(
                    'domain' => '',
                    'path' => '',
                    'title' => '',
                    'user_login' => $user,
                    'user_email' => $user_email,
                    'registered' => current_time('mysql', true),
                    'activation_key' => $key,
                    'meta' => $meta
            ) );
    
            wpmu_signup_user_notification($user, $user_email, $key, $meta);
    }

    Unfortunately this is not a plugin, as I have struggled to find the appropriate hook and code to do this job. Any ideas what the hook and the plugin code would be to do the same thing?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    You hook like this:

    add_action( 'wpmu_signup_user', 'sontru_signup_user', 10, 3 );

    And then make

    function sontru_activate_user( $user, $user_email, $meta = '' ) {
    
            $hostAddress = explode('.',$_SERVER['HTTP_HOST']);
            $user .= '_' . $hostAddress[0];
    }

    You may need a return in there, but that’s the basic idea.

    Thread Starter sontru

    (@sontru)

    Thanks for that. I thought I had already tried that and it did work. So I tried it again and… nope does not work for some reason. My code is in a plugin file which fixes the ‘login name must be a minimum of 4 characters’ error, and an email login code which works. (Included here in case anyone want these problems fixed too…)

    <?php
    /*
    Plugin Name: Email Login, Remove Min 4 Chars Login Name, Add Subdomain Suffix
    Plugin URI:
    Description: Does all of the above!
    Author: sontru
    Version: 1.3.0
    Author URI:
    */
    
    function login_with_email_address($username) {
            $user = get_user_by_email($username);
            if(!empty($user->user_login))
                    $username = $user->user_login;
            return $username;
    }
    add_action('wp_authenticate','login_with_email_address');
    
    function remove_username_char_limit($result) {
      if ( is_wp_error( $result[ 'errors' ] ) && !empty( $result[ 'errors' ]->errors ) ) {
    
        // Get all the error messages from $result
        $messages = $result['errors']->get_error_messages();
        $i = 0;
        foreach ( $messages as $message ) {
    
          // Check if any message is the char limit message
          if ( 0 == strcasecmp("Username must be at least 4 characters.", $message)) {
            // Unset whole 'user_name' error array if only 1 message exists
            // and that message is the char limit error
            if ( 1 == count($messages) ) {
              unset( $result['errors']->errors['user_name'] );
            } else {
              // Otherwise just unset the char limit message
              unset( $result['errors']->errors['user_name'][$i] );
            }
          }
    
          $i++;
        }
      }
    
      return $result;
    }
    add_action('wpmu_validate_user_signup', 'remove_username_char_limit');
    
    function add_suffix_to_login($user, $user_email, $meta = '')
    {
            $hostAddress = explode('.',$_SERVER['HTTP_HOST']);
            $user .= '_' . $hostAddress[0];
            return $user;
    }
    add_action('wpmu_signup_user', 'add_suffix_to_login',10,3);
    
    ?>

    <strike>Is there a different between a calling add_action before defining the function and after it?</strike> Just tried this and no difference – still does not have the desired effect.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Okay… Let’s step back 🙂

    try making your add_suffix code just add a letter or the underscore. See if that works.

    Thread Starter sontru

    (@sontru)

    Nope. The code being:

    function add_suffix_to_login($user, $user_email, $meta = '')
    {
    //        $hostAddress = explode('.',$_SERVER['HTTP_HOST']);
            $user .= '_'; // . $hostAddress[0];
            return $user;
    }
    add_action('wpmu_signup_user', 'add_suffix_to_login',10,3);

    and no underscore is added. In fact, this code does not even work:

    function add_suffix_to_login($user, $user_email, $meta = '')
    {
    //        $hostAddress = explode('.',$_SERVER['HTTP_HOST']);
    //        $user .= '-' . $hostAddress[0];
            $user = 'testusersontru';
            return $user;
    }
    add_action('wpmu_signup_user', 'add_suffix_to_login',10,3);

    which means the function is not being called. I’m a newbie at this but is there a hook for this function ‘wpmu_signup_user’ for the action to be added?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Reading… Cause I’m an idiot. “This function is used when user registration is open but new site registration is not.”

    *facepalm*

    Thread Starter sontru

    (@sontru)

    Is that a no? What can I do instead? Is hacking ms-functions.php my only choice?

    Is hacking ms-functions.php my only choice?

    Hacking core is a really, really bad idea (kittens will actually be murdered). You’re opening yourself up to major headaches in the future.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    No it’s just that wpmu_signup_user is wrong.

    I’m not 100% sure what right is. Maybe wpmu_create_user?

    http://codex.wordpress.org/WPMU_Functions/wpmu_create_user

    Thread Starter sontru

    (@sontru)

    The reason I am not using wpmu_activate_user (initially suggested) is because of the validation/activation part.

    There is a wp_signups table that maintains and reserves unique login names. If this table does not get updated with the login_subdomain then there will be problems with subdomains creating users, such as billy_domain1 and billy_domain2. These login named will be treated as the same if they don’t get suffixed before it goes into the wp_signups table.

    I will verify this if I get the plugin to work with wpmu_create_user.

    EDIT: I don’t think this would work. I used to have my code inserted directly into ‘function wpmu_activate_signup($key)’ at about //HERE!:

    $user_id = username_exists($user_login);
    
            if ( ! $user_id )
            {
                    //HERE!
                    $hostAddress = explode('.',$_SERVER['HTTP_HOST']);
                    $user_login .= '_' . $hostAddress[0];
                    $user_id = wpmu_create_user($user_login, $password, $user_email);
            }
            else
                    $user_already_exists = true;
    
            if ( ! $user_id )
                    return new WP_Error('create_user', __('Could not create user'), $signup);

    i.e. before wpmu_create_user is called. Although this added the suffix to the $user_login, this username does not appear in the wp_signups table.

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Create local users in each subdomain’ is closed to new replies.