Skip to:
Content

bbPress.org


Ignore:
Timestamp:
08/21/2012 06:40:18 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Capabilities:

  • Remove roles and move everything to mapped capabilities.
  • First pass at admin-side forum capability manager.
  • Deprecate moderator, anonymous, and participant roles and associated functions.
  • Deprecate add/remove role/cap functions.
  • More cap mapping needed for users that are not explicitly blocked.
  • Adds cap check to forms for topic-tag adding/editing.
  • See #1939
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-admin/bbp-users.php

    r4053 r4164  
    2121class BBP_Users_Admin {
    2222
    23     /** Variables *************************************************************/
    24 
    25     /** Functions *************************************************************/
    26 
    2723    /**
    2824     * The bbPress users admin loader
     
    3329     * @uses BBP_Users_Admin::setup_actions() Setup the hooks and actions
    3430     */
    35     function __construct() {
    36         $this->setup_globals();
     31    public function __construct() {
    3732        $this->setup_actions();
    3833    }
     
    4742     */
    4843    function setup_actions() {
     44
     45        // Admin styles
     46        add_action( 'admin_head',               array( $this, 'admin_head'          ) );
    4947
    5048        // User profile edit/display actions
     
    5654        add_action( 'edit_user_profile_update', array( $this, 'user_profile_update' ) );
    5755    }
    58 
    59     /**
    60      * Admin globals
    61      *
    62      * @since bbPress (r2646)
    63      * @access private
    64      */
    65     function setup_globals() { }
    6656
    6757    /**
     
    7565     * @uses sanitize_html_class() To sanitize the classes
    7666     */
    77     function admin_head() { }
     67    public function admin_head() {
     68        ?>
     69
     70        <style type="text/css" media="screen">
     71        /*<![CDATA[*/
     72            div.bbp-user-capabilities {
     73                float: left;
     74                margin: 0 20px 0 0;
     75            }
     76            body.rtl div.bbp-user-capabilities {
     77                float: right;
     78                margin: 0 0 0 20px;
     79            }
     80           
     81            div.bbp-user-capabilities h4 {
     82                margin: 0 0 10px;
     83            }
     84           
     85            p.bbp-default-caps-wrapper {
     86                clear: both;
     87                margin: 80px -10px 0;
     88            }
     89        /*]]>*/
     90        </style>
     91
     92        <?php
     93    }
    7894
    7995    /**
     
    86102     * @return bool Always false
    87103     */
    88     function user_profile_update( $user_id ) { }
     104    public function user_profile_update( $user_id ) {
     105
     106        // Bail if no user
     107        if ( empty( $user_id ) )
     108            return;
     109
     110        // Load up the user
     111        $user      = new WP_User( $user_id );
     112        $user_role = bbp_get_user_role( $user_id );
     113
     114        // Either reset caps for role
     115        if ( ! empty( $_POST['bbp-default-caps'] ) ) {
     116
     117            // Remove all caps
     118            foreach ( bbp_get_capability_groups() as $group ) {
     119                foreach ( bbp_get_capabilities_for_group( $group ) as $capability ) {
     120                    $user->remove_cap( $capability );
     121                }
     122            }
     123
     124            // Maybe use users new role
     125            if ( ! empty( $_POST['role'] ) ) {
     126                $new_role = get_role( $_POST['role'] );
     127                $new_role = isset( $new_role->name ) ? $new_role->name : '';
     128
     129                if ( $new_role != $user_role ) {
     130                    $user_role = $new_role;
     131                }
     132            }
     133
     134            // Add back caps for current role
     135            if ( !empty( $user_role ) ) {
     136                foreach ( bbp_get_caps_for_role( $user_role ) as $capability ) {
     137                    $user->add_cap( $capability );
     138                }
     139            }
     140
     141        // Or set caps individually
     142        } else {
     143
     144            // Loop through capability groups
     145            foreach ( bbp_get_capability_groups() as $group ) {
     146
     147                // Loop through capabilities
     148                foreach ( bbp_get_capabilities_for_group( $group ) as $capability ) {
     149
     150                    // Maybe add cap
     151                    if ( ! empty( $_POST['_bbp_' . $capability] ) && ! $user->has_cap( $capability ) ) {
     152                        $user->add_cap( $capability );
     153
     154                    // Maybe remove cap
     155                    } elseif ( empty( $_POST['_bbp_' . $capability] ) && $user->has_cap( $capability ) ) {
     156                        $user->remove_cap( $capability );
     157                    }
     158                }
     159            }
     160        }
     161    }
    89162
    90163    /**
     
    97170     * @return bool Always false
    98171     */
    99     function user_profile_forums( $profileuser ) { }
     172    public function user_profile_forums( $profileuser ) {
     173
     174        // Bail if current user cannot edit users
     175        if ( ! current_user_can( 'edit_user', $profileuser->ID ) )
     176            return;
     177
     178        // Noop WordPress additional caps output area
     179        add_filter( 'additional_capabilities_display', '__return_false' ); ?>
     180
     181        <h3><?php _e( 'Forum Capabilities', 'bbpress' ); ?></h3>
     182
     183        <table class="form-table">
     184            <tbody>
     185                <tr>
     186                    <th><?php _e( 'This user can:', 'bbpress' ); ?></th>
     187
     188                    <td>
     189                        <fieldset>
     190                            <legend class="screen-reader-text"><span><?php _e( 'Additional Capabilities', 'bbpress' ); ?></span></legend>
     191
     192                            <?php foreach ( bbp_get_capability_groups() as $group ) : ?>
     193
     194                                <div class="bbp-user-capabilities">
     195                                    <h4><?php bbp_capability_group_title( $group ); ?></h4>
     196
     197                                    <?php foreach ( bbp_get_capabilities_for_group( $group ) as $capability ) : ?>
     198
     199                                        <label for="_bbp_<?php echo $capability; ?>">
     200                                            <input id="_bbp_<?php echo $capability; ?>" name="_bbp_<?php echo $capability; ?>" type="checkbox" id="_bbp_<?php echo $capability; ?>" value="1" <?php checked( user_can( $profileuser->ID, $capability ) ); ?> />
     201                                            <?php bbp_capability_title( $capability ); ?>
     202                                        </label>
     203                                        <br />
     204
     205                                    <?php endforeach; ?>
     206
     207                                </div>
     208
     209                            <?php endforeach; ?>
     210
     211                            <p class="bbp-default-caps-wrapper">
     212                                <input type="submit" name="bbp-default-caps" class="button" value="<?php _e( 'Reset to Default', 'bbpress' ); ?>"/>
     213                            </p>
     214
     215                        </fieldset>
     216                    </td>
     217                </tr>
     218
     219            </tbody>
     220        </table>
     221
     222        <?php
     223    }
    100224}
     225new BBP_Users_Admin();
    101226endif; // class exists
Note: See TracChangeset for help on using the changeset viewer.