Plugin Directory

Changeset 160056


Ignore:
Timestamp:
10/02/2009 10:40:47 PM (16 years ago)
Author:
wnorris
Message:

allow individual user fields to be marked as 'managed'

Location:
shibboleth/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • shibboleth/trunk/options-admin.php

    r159838 r160056  
    148148            </p>
    149149
    150             <table class="form-table optiontable editform" cellspacing="2" cellpadding="5" width="100%">
     150            <table class="form-table optiontable editform" cellspacing="2" cellpadding="5">
    151151                <tr valign="top">
    152152                    <th scope="row"><label for="username"><?php _e('Username') ?></label</th>
    153                     <td><input type="text" id="username" name="headers[username]" value="<?php echo $shib_headers['username'] ?>" /></td>
     153                    <td><input type="text" id="username" name="headers[username][name]" value="<?php echo
     154                        $shib_headers['username']['name'] ?>" /></td>
     155                    <td width="60%"></td>
    154156                </tr>
    155157                <tr valign="top">
    156158                    <th scope="row"><label for="first_name"><?php _e('First name') ?></label</th>
    157                     <td><input type="text" id="first_name" name="headers[first_name]" value="<?php echo $shib_headers['first_name'] ?>" /></td>
     159                    <td><input type="text" id="first_name" name="headers[first_name][name]" value="<?php echo
     160                        $shib_headers['first_name']['name'] ?>" /></td>
     161                    <td><input type="checkbox" id="first_name_managed" name="headers[first_name][managed]" <?php
     162                        checked($shib_headers['first_name']['managed']) ?> /> <?php _e('Managed', 'shibboleth') ?></td>
    158163                </tr>
    159164                <tr valign="top">
    160165                    <th scope="row"><label for="last_name"><?php _e('Last name') ?></label</th>
    161                     <td><input type="text" id="last_name" name="headers[last_name]" value="<?php echo $shib_headers['last_name'] ?>" /></td>
     166                    <td><input type="text" id="last_name" name="headers[last_name][name]" value="<?php echo
     167                        $shib_headers['last_name']['name'] ?>" /></td>
     168                    <td><input type="checkbox" id="last_name_managed" name="headers[last_name][managed]" <?php
     169                        checked($shib_headers['last_name']['managed']) ?> /> <?php _e('Managed', 'shibboleth') ?></td>
    162170                </tr>
    163171                <tr valign="top">
    164172                    <th scope="row"><label for="nickname"><?php _e('Nickname') ?></label</th>
    165                     <td><input type="text" id="nickname" name="headers[nickname]" value="<?php echo $shib_headers['nickname'] ?>" /></td>
    166                 </tr>
    167                 <tr valign="top">
    168                     <th scope="row"><label for="display_name"><?php _e('Display name') ?></label</th>
    169                     <td><input type="text" id="display_name" name="headers[display_name]" value="<?php echo $shib_headers['display_name'] ?>" /></td>
    170                 </tr>
    171                 <tr valign="top">
    172                     <th scope="row"><label for="email"><?php _e('Email Address') ?></label</th>
    173                     <td><input type="text" id="email" name="headers[email]" value="<?php echo $shib_headers['email'] ?>" /></td>
    174                 </tr>
    175                 <tr valign="top">
    176                     <th scope="row"><label for="update_users"><?php _e('Update User Data', 'shibboleth') ?></label</th>
    177                     <td>
    178                         <input type="checkbox" id="update_users" name="update_users" <?php echo shibboleth_get_option('shibboleth_update_users') ? ' checked="checked"' : '' ?> />
    179                         <label for="update_users"><?php _e('Use Shibboleth data to update user profile data each time the user logs in.', 'shibboleth'); ?></label>
    180 
    181                         <p><?php _e('This will prevent users from being able to manually update these'
    182                             . ' fields.  Note that Shibboleth data is always used to populate the user'
    183                             . ' profile during account creation.', 'shibboleth'); ?></p>
    184 
    185                     </td>
     173                    <td><input type="text" id="nickname" name="headers[nickname][name]" value="<?php echo
     174                        $shib_headers['nickname']['name'] ?>" /></td>
     175                    <td><input type="checkbox" id="nickname_managed" name="headers[nickname][managed]" <?php
     176                        checked($shib_headers['nickname']['managed']) ?> /> <?php _e('Managed', 'shibboleth') ?></td>
     177                </tr>
     178                <tr valign="top">
     179                    <th scope="row"><label for="_display_name"><?php _e('Display name', 'shibboleth') ?></label</th>
     180                    <td><input type="text" id="_display_name" name="headers[display_name][name]" value="<?php echo
     181                        $shib_headers['display_name']['name'] ?>" /></td>
     182                    <td><input type="checkbox" id="display_name_managed" name="headers[display_name][managed]" <?php
     183                        checked($shib_headers['display_name']['managed']) ?> /> <?php _e('Managed', 'shibboleth') ?></td>
     184                </tr>
     185                <tr valign="top">
     186                    <th scope="row"><label for="email"><?php _e('Email Address', 'shibboleth') ?></label</th>
     187                    <td><input type="text" id="email" name="headers[email][name]" value="<?php echo
     188                        $shib_headers['email']['name'] ?>" /></td>
     189                    <td><input type="checkbox" id="email_managed" name="headers[email][managed]" <?php
     190                        checked($shib_headers['email']['managed']) ?> /> <?php _e('Managed', 'shibboleth') ?></td>
    186191                </tr>
    187192            </table>
     193
     194            <p><?php _e('<em>Managed</em> profile fields are updated each time the user logs in using the current'
     195                . ' data provided by Shibboleth.  Additionally, users will be prevented from manually updating these'
     196                . ' fields from within WordPress.  Note that Shibboleth data is always used to populate the user'
     197                . ' profile during initial account creation.', 'shibboleth'); ?></p>
    188198
    189199            <br class="clear" />
  • shibboleth/trunk/options-user.php

    r159838 r160056  
    55add_action('personal_options_update', 'shibboleth_personal_options_update');
    66add_action('show_user_profile', 'shibboleth_show_user_profile');
    7 add_action('edit_user_profile', 'shibboleth_edit_user_profile');
     7add_action('admin_footer-user-edit.php', 'shibboleth_admin_footer_edit_user');
    88
    99
     
    1717        add_filter('show_password_fields', create_function('$v', 'return false;'));
    1818
    19         if (shibboleth_get_option('shibboleth_update_users')) {
    20             echo '
    21             <script type="text/javascript">
    22                 jQuery(function() {
    23                     jQuery("#first_name,#last_name,#nickname,#display_name,#email").attr("disabled", true);
    24                     jQuery("h3:contains(\'Name\')").after("<div class=\"updated fade\"><p>'
    25                         . __('These fields cannot be changed from WordPress.', 'shibboleth') . '<p></div>");
    26                     jQuery("form#your-profile").submit(function() {
    27                         jQuery("#first_name,#last_name,#nickname,#display_name,#email").attr("disabled", false);
    28                     });
     19        add_action('admin_footer-profile.php', 'shibboleth_admin_footer_profile');
     20    }
     21}
     22
     23function shibboleth_admin_footer_profile() {
     24    $managed_fields = shibboleth_get_managed_user_fields();
     25
     26    if ( !empty($managed_fields) ) {
     27        $selectors = join(',', array_map(create_function('$a', 'return "#$a";'), $managed_fields));
     28
     29        echo '
     30        <script type="text/javascript">
     31            jQuery(function() {
     32                jQuery("' . $selectors . '").attr("disabled", true);
     33                jQuery("#first_name").parents(".form-table").before("<div class=\"updated fade\"><p>'
     34                    . __('Some profile fields cannot be changed from WordPress.', 'shibboleth') . '</p></div>");
     35                jQuery("form#your-profile").submit(function() {
     36                    jQuery("' . $selectors . '").attr("disabled", false);
    2937                });
    30             </script>';
    31         }
     38            });
     39        </script>';
    3240    }
    3341}
     
    3846 * Shibboleth managed attributes.
    3947 */
    40 function shibboleth_edit_user_profile() {
     48function shibboleth_admin_footer_edit_user() {
    4149    global $user_id;
    4250
     
    4452        $shibboleth_fields = array();
    4553
    46         if (shibboleth_get_option('shibboleth_update_users')) {
    47             $shibboleth_fields = array_merge($shibboleth_fields,
    48                 array('user_login', 'first_name', 'last_name', 'nickname', 'display_name', 'email'));
    49         }
     54        $shibboleth_fields = array_merge($shibboleth_fields, shibboleth_get_managed_user_fields());
    5055
    5156        if (shibboleth_get_option('shibboleth_update_roles')) {
     
    6469                jQuery(function() {
    6570                    jQuery("' . implode(',', $selectors) . '").before("<span style=\"color: #F00; font-weight: bold;\">*</span> ");
    66                     jQuery("h3:contains(\'Name\')")
    67                         .after("<div class=\"updated fade\"><p><span style=\"color: #F00; font-weight: bold;\">*</span> '
    68                             . __('Starred fields are managed by Shibboleth and should not be changed from WordPress.', 'shibboleth') . '</p></div>");
     71                    jQuery("#first_name").parents(".form-table")
     72                        .before("<div class=\"updated fade\"><p><span style=\"color: #F00; font-weight: bold;\">*</span> '
     73                        . __('Starred fields are managed by Shibboleth and should not be changed from WordPress.', 'shibboleth') . '</p></div>");
    6974                });
    7075            </script>';
     
    7984function shibboleth_show_user_profile() {
    8085    $user = wp_get_current_user();
    81     if (get_usermeta($user->ID, 'shibboleth_account')) {
    82         if (shibboleth_get_option('shibboleth_password_change_url')) {
     86    $password_change_url = shibboleth_get_option('shibboleth_password_change_url');
     87    if (get_usermeta($user->ID, 'shibboleth_account') && !empty($password_change_url) ) {
    8388?>
    8489    <table class="form-table">
    8590        <tr>
    86             <th>Change Password</th>
    87             <td><a href="<?php echo shibboleth_get_option('shibboleth_password_change_url');
    88                 ?>" target="_blank"><?php _e('Change your password', 'shibboleth'); ?></a></td>
     91            <th><?php _e('Change Password') ?></th>
     92            <td><a href="<?php echo esc_url($password_change_url); ?>" target="_blank"><?php
     93                _e('Change your password', 'shibboleth'); ?></a></td>
    8994        </tr>
    9095    </table>
    9196<?php
    92         }
    9397    }
    9498}
     
    96100
    97101/**
    98  * Ensure profile data isn't updated by the user.  This only applies to
    99  * accounts that were provisioned through Shibboleth, and only if the option
    100  * to manage user attributes exclusively from Shibboleth is enabled.
     102 * Ensure profile data isn't updated by the user.  This only applies to accounts that were
     103 * provisioned through Shibboleth, and only for those user fields marked as 'managed'.
    101104 */
    102105function shibboleth_personal_options_update() {
    103106    $user = wp_get_current_user();
    104107
    105     if (get_usermeta($user->ID, 'shibboleth_account') && shibboleth_get_option('shibboleth_update_users')) {
    106         add_filter('pre_user_first_name',
    107             create_function('$n', 'return $GLOBALS["current_user"]->first_name;'));
     108    if ( get_usermeta($user->ID, 'shibboleth_account') ) {
     109        $managed = shibboleth_get_managed_user_fields();
    108110
    109         add_filter('pre_user_last_name',
    110             create_function('$n', 'return $GLOBALS["current_user"]->last_name;'));
     111        if ( in_array('first_name', $managed) ) {
     112            add_filter('pre_user_first_name', create_function('$n', 'return $GLOBALS["current_user"]->first_name;'));
     113        }
    111114
    112         add_filter('pre_user_nickname',
    113             create_function('$n', 'return $GLOBALS["current_user"]->nickname;'));
     115        if ( in_array('last_name', $managed) ) {
     116            add_filter('pre_user_last_name', create_function('$n', 'return $GLOBALS["current_user"]->last_name;'));
     117        }
    114118
    115         add_filter('pre_user_display_name',
    116             create_function('$n', 'return $GLOBALS["current_user"]->display_name;'));
     119        if ( in_array('nickname', $managed) ) {
     120            add_filter('pre_user_nickname', create_function('$n', 'return $GLOBALS["current_user"]->nickname;'));
     121        }
    117122
    118         add_filter('pre_user_email',
    119             create_function('$e', 'return $GLOBALS["current_user"]->user_email;'));
     123        if ( in_array('display_name', $managed) ) {
     124            add_filter('pre_user_display_name', create_function('$n', 'return $GLOBALS["current_user"]->display_name;'));
     125        }
     126
     127        if ( in_array('email', $managed) ) {
     128            add_filter('pre_user_email', create_function('$e', 'return $GLOBALS["current_user"]->user_email;'));
     129        }
    120130    }
    121131}
  • shibboleth/trunk/shibboleth.php

    r160015 r160056  
    3434
    3535    $headers = array(
    36         'username' => 'eppn',
    37         'first_name' => 'givenName',
    38         'last_name' => 'sn',
    39         'nickname' => 'eppn',
    40         'display_name' => 'displayName',
    41         'email' => 'mail',
     36        'username' => array( 'name' => 'eppn', 'managed' => false),
     37        'first_name' => array( 'name' => 'givenName', 'managed' => true),
     38        'last_name' => array( 'name' => 'sn', 'managed' => true),
     39        'nickname' => array( 'name' => 'eppn', 'managed' => true),
     40        'display_name' => array( 'name' => 'displayName', 'managed' => true),
     41        'email' => array( 'name' => 'mail', 'managed' => true),
    4242    );
    4343    shibboleth_add_option('shibboleth_headers', $headers);
     
    5252            'value' => 'faculty',
    5353        ),
     54        // TODO: this could likely do strange things if WordPress has an actual role named 'default'
    5455        'default' => 'subscriber',
    5556    );
    5657    shibboleth_add_option('shibboleth_roles', $roles);
    5758
    58     shibboleth_add_option('shibboleth_update_users', true);
    5959    shibboleth_add_option('shibboleth_update_roles', true);
    6060
    6161    shibboleth_insert_htaccess();
     62
     63    shibboleth_migrate_old_data();
    6264
    6365    shibboleth_update_option('shibboleth_plugin_revision', SHIBBOLETH_PLUGIN_REVISION);
     
    7678register_deactivation_hook('shibboleth/shibboleth.php', 'shibboleth_deactivate_plugin');
    7779
     80
     81/**
     82 * Migrate old data to newer formats.
     83 */
     84function shibboleth_migrate_old_data() {
     85
     86    // new header format, allowing each header to be marked as 'managed' individually
     87    $managed = shibboleth_get_option('shibboleth_update_users');
     88    $headers = shibboleth_get_option('shibboleth_headers');
     89    $updated = false;
     90
     91    foreach ($headers as $key => $value) {
     92        if ( is_string($value) ) {
     93            $headers[$key] = array(
     94                'name' => $value,
     95                'managed' => $managed,
     96            );
     97            $updated = true;
     98        }
     99    }
     100
     101    if ( $updated ) {
     102        shibboleth_update_option('shibboleth_headers', $headers);
     103    }
     104    shibboleth_remove_option('shibboleth_update_users');
     105
     106}
    78107
    79108/**
     
    241270    }
    242271
    243     $username = $_SERVER[$shib_headers['username']];
     272    $username = $_SERVER[$shib_headers['username']['name']];
    244273    $user = new WP_User($username);
    245274
     
    266295    // update user data
    267296    update_usermeta($user->ID, 'shibboleth_account', true);
    268     if ( shibboleth_get_option('shibboleth_update_users') ) shibboleth_update_user_data($user->ID);
     297    shibboleth_update_user_data($user->ID);
    269298    if ( shibboleth_get_option('shibboleth_update_roles') ) $user->set_role($user_role);
    270299
     
    289318
    290319    // always update user data and role on account creation
    291     shibboleth_update_user_data($user->ID);
     320    shibboleth_update_user_data($user->ID, true);
    292321    $user_role = shibboleth_get_user_role();
    293322    $user->set_role($user_role);
     
    333362
    334363/**
    335  * Update the user data for the specified user based on the current Shibboleth headers.
     364 * Get the user fields that are managed by Shibboleth.
     365 *
     366 * @return Array user fields managed by Shibboleth
     367 */
     368function shibboleth_get_managed_user_fields() {
     369    $headers = shibboleth_get_option('shibboleth_headers');
     370    $managed = array();
     371
     372    foreach ($headers as $name => $value) {
     373        if ( $value['managed'] ) {
     374            $managed[] = $name;
     375        }
     376    }
     377
     378    return $managed;
     379}
     380
     381
     382/**
     383 * Update the user data for the specified user based on the current Shibboleth headers.  Unless
     384 * the 'force_update' parameter is true, only the user fields marked as 'managed' fields will be
     385 * updated.
    336386 *
    337387 * @param int $user_id ID of the user to update
     388 * @param boolean $force_update force update of user data, regardless of 'managed' flag on fields
    338389 * @uses apply_filters() Calls 'shibboleth_user_*' before setting user attributes,
    339  *       where '*' is one of: login, nicename, first_name, last_name, nickname,
    340  *       display_name, email
    341  */
    342 function shibboleth_update_user_data($user_id) {
     390 *       where '*' is one of: login, nicename, first_name, last_name,
     391 *       nickname, display_name, email
     392 */
     393function shibboleth_update_user_data($user_id, $force_update = false) {
    343394    require_once( ABSPATH . WPINC . '/registration.php' );
    344395
    345396    $shib_headers = shibboleth_get_option('shibboleth_headers');
     397
     398    $user_fields = array(
     399        'user_login' => 'username',
     400        'user_nicename' => 'username',
     401        'first_name' => 'first_name',
     402        'last_name' => 'last_name',
     403        'nickname' => 'nickname',
     404        'display_name' => 'display_name',
     405        'user_email' => 'email'
     406    );
    346407
    347408    $user_data = array(
    348409        'ID' => $user_id,
    349         'user_login' => apply_filters('shibboleth_user_login', $_SERVER[$shib_headers['username']]),
    350         'user_nicename' => apply_filters('shibboleth_user_nicename', $_SERVER[$shib_headers['username']]),
    351         'first_name' => apply_filters('shibboleth_user_first_name', $_SERVER[$shib_headers['first_name']]),
    352         'last_name' => apply_filters('shibboleth_user_last_name', $_SERVER[$shib_headers['last_name']]),
    353         'nickname' => apply_filters('shibboleth_user_nickname', $_SERVER[$shib_headers['nickname']]),
    354         'display_name' => apply_filters('shibboleth_user_display_name', $_SERVER[$shib_headers['display_name']]),
    355         'user_email' => apply_filters('shibboleth_user_email', $_SERVER[$shib_headers['email']]),
    356410    );
     411   
     412    foreach ($user_fields as $field => $header) {
     413        if ( $force_update || $shib_headers[$header]['managed'] ) {
     414            $filter = 'shibboleth_' . ( strpos($field, 'user_') === 0 ? '' : 'user_' ) . $field;
     415            $user_data[$field] = apply_filters($filter, $_SERVER[$shib_headers[$header]['name']]);
     416        }
     417    }
    357418
    358419    wp_update_user($user_data);
Note: See TracChangeset for help on using the changeset viewer.