[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/bb-includes/ -> functions.bb-capabilities.php (source)

   1  <?php
   2  /**
   3   * bbPress Roles and Capabilities Wrapping Functions.
   4   *
   5   * @package bbPress
   6   * @subpackage User
   7   */
   8  
   9  
  10  
  11  /**
  12   * Whether current user has capability or role.
  13   *
  14   * @since 0.7.2
  15   * @uses $bb_current_user Current User Object
  16   *
  17   * @param string $capability Capability or role name.
  18   * @return bool
  19   */
  20  function bb_current_user_can($capability) {
  21      global $bb_current_user;
  22  
  23      $args = array_slice(func_get_args(), 1);
  24      $args = array_merge(array($capability), $args);
  25  
  26      if ( empty($bb_current_user) ) {
  27          $retvalue = false;
  28          if ( ( $capability == 'write_topic' || $capability == 'write_topics' ) && !bb_is_login_required() )
  29              $retvalue = true;
  30      } else {
  31          $retvalue = call_user_func_array(array(&$bb_current_user, 'has_cap'), $args);
  32      }
  33      
  34      // Use bb_user_has_cap whenever possible!  This will not work everywhere.
  35      return apply_filters('bb_current_user_can', $retvalue, $capability, $args);
  36  }
  37  
  38  /**
  39   * Give a user the default role
  40   *
  41   * @since 0.7.2
  42   *
  43   * @param BP_User $user User object to give default role to
  44   */
  45  function bb_give_user_default_role( $user ) {
  46      if ( !( is_object($user) && is_a($user, 'BP_User') ) )
  47          return;
  48      $user->set_role('member');
  49  }
  50  
  51  /**
  52   * Setup all default roles and associate them with capabilities
  53   *
  54   * @since 0.7.2
  55   *
  56   * @param BP_Roles $roles Roles object to add default roles to
  57   */
  58  function bb_init_roles( &$roles ) {
  59      $roles->add_role( 'keymaster', __('Key Master'), array(
  60          'use_keys' => true,                // Verb forms of roles - keymaster
  61          'administrate' => true,            // administrator
  62          'moderate' => true,             // moderator
  63          'participate' => true,            // member
  64  
  65          'keep_gate' => true,            // Make new Key Masters        //+
  66          'import_export' => true,        // Import and export data    //+
  67          'recount' => true,                // bb-do-counts.php            //+
  68          'manage_options' => true,        // backend                    //+
  69          'manage_themes' => true,        // Themes                    //+
  70          'manage_plugins' => true,        // Plugins                    //+
  71          'manage_options' => true,        // Options                    //+
  72          'edit_users' => true,
  73          'manage_tags' => true,            // Rename, Merge, Destroy
  74          'edit_others_favorites' => true,
  75          'manage_forums' => true,        // Add/Rename forum
  76          'delete_forums' => true,        // Delete forum
  77          'delete_topics' => true,
  78          'close_topics' => true,
  79          'stick_topics' => true,
  80          'move_topics' => true,
  81          'view_by_ip' => true,            // view-ip.php
  82          'edit_closed' => true,            // Edit closed topics
  83          'edit_deleted' => true,            // Edit deleted topics/posts
  84          'browse_deleted' => true,        // Use 'deleted' view
  85          'edit_others_tags' => true,
  86          'edit_others_topics' => true,
  87          'delete_posts' => true,
  88          'throttle' => true,                // Post back to back arbitrarily quickly
  89          'ignore_edit_lock' => true,
  90          'edit_others_posts' => true,
  91          'edit_favorites' => true,
  92          'edit_tags' => true,
  93          'edit_topics' => true,            // Edit title, resolution status
  94          'edit_posts' => true,
  95          'edit_profile' => true,
  96          'write_topics' => true,
  97          'write_posts' => true,
  98          'change_password' => true,
  99          'read' => true
 100      ) );
 101  
 102      $roles->add_role( 'administrator', __('Administrator'), array(
 103          'administrate' => true,
 104          'moderate' => true,
 105          'participate' => true,
 106  
 107          'edit_users' => true,                //+
 108          'edit_others_favorites' => true,    //+
 109          'manage_forums' => true,            //+
 110          'delete_forums' => true,            //+
 111          'manage_tags' => true,
 112          'delete_topics' => true,
 113          'close_topics' => true,
 114          'stick_topics' => true,
 115          'move_topics' => true,
 116          'view_by_ip' => true,
 117          'edit_closed' => true,
 118          'edit_deleted' => true,
 119          'browse_deleted' => true,
 120          'edit_others_tags' => true,
 121          'edit_others_topics' => true,
 122          'delete_posts' => true,
 123          'throttle' => true,
 124          'ignore_edit_lock' => true,
 125          'edit_others_posts' => true,
 126          'edit_favorites' => true,
 127          'edit_tags' => true,
 128          'edit_topics' => true,
 129          'edit_posts' => true,
 130          'edit_profile' => true,
 131          'write_topics' => true,
 132          'write_posts' => true,
 133          'change_password' => true,
 134          'read' => true
 135      ) );
 136  
 137      $roles->add_role( 'moderator', __('Moderator'), array(
 138          'moderate' => true,
 139          'participate' => true,
 140  
 141          'manage_tags' => true,            //+
 142          'delete_topics' => true,        //+
 143          'close_topics' => true,            //+
 144          'stick_topics' => true,            //+
 145          'move_topics' => true,            //+
 146          'view_by_ip' => true,            //+
 147          'edit_closed' => true,            //+
 148          'edit_deleted' => true,            //+
 149          'browse_deleted' => true,        //+
 150          'edit_others_tags' => true,        //+
 151          'edit_others_topics' => true,    //+
 152          'delete_posts' => true,            //+
 153          'throttle' => true,                //+
 154          'ignore_edit_lock' => true,        //+
 155          'edit_others_posts' => true,    //+
 156          'edit_favorites' => true,
 157          'edit_tags' => true,
 158          'edit_topics' => true,
 159          'edit_posts' => true,
 160          'edit_profile' => true,
 161          'write_topics' => true,
 162          'write_posts' => true,
 163          'change_password' => true,
 164          'read' => true
 165      ) );
 166  
 167  
 168      $roles->add_role( 'member', __('Member'), array(
 169          'participate' => true,
 170  
 171          'edit_favorites' => true,
 172          'edit_tags' => true,
 173          'edit_topics' => true,
 174          'edit_posts' => true,
 175          'edit_profile' => true,
 176          'write_topics' => true,
 177          'write_posts' => true,
 178          'change_password' => true,
 179          'read' => true
 180      ) );
 181  
 182      $roles->add_role( 'inactive', __('Inactive'), array(
 183          'change_password' => true,
 184          'read' => true
 185      ) );
 186  
 187      $roles->add_role( 'blocked', __('Blocked'), array(
 188          'not_play_nice' => true // Madness - a negative capability.  Don't try this at home.
 189      ) );
 190  }
 191  
 192  /**
 193   * Map meta capabilities to primitive capabilities.
 194   *
 195   * This does not actually compare whether the user ID has the actual capability,
 196   * just what the capability or capabilities are. Meta capability list value can
 197   * be 'delete_user', 'edit_user', 'delete_post', 'delete_page', 'edit_post',
 198   * 'edit_page', 'read_post', or 'read_page'.
 199   *
 200   * @since 0.7.2
 201   *
 202   * @param array $caps Previously existing capabilities
 203   * @param string $cap Capability name.
 204   * @param int $user_id User ID.
 205   * @return array Actual capabilities for meta capability.
 206   */
 207  function bb_map_meta_cap( $caps, $cap, $user_id, $args ) {
 208      // Unset the meta cap
 209      if ( false !== $cap_pos = array_search( $cap, $caps ) )
 210          unset( $caps[$cap_pos] );
 211  
 212      switch ( $cap ) {
 213          case 'write_post':
 214              $caps[] = 'write_posts';
 215              break;
 216          case 'edit_post':
 217              // edit_posts, edit_others_posts, edit_deleted, edit_closed, ignore_edit_lock
 218              if ( !$bb_post = bb_get_post( $args[0] ) ) {
 219                  $caps[] = 'magically_provide_data_given_bad_input';
 220                  return $caps;
 221              }
 222              if ( $user_id == $bb_post->poster_id )
 223                  $caps[] = 'edit_posts';
 224              else
 225                  $caps[] = 'edit_others_posts';
 226              if ( $bb_post->post_status == '1' )
 227                  $caps[] = 'edit_deleted';
 228              if ( !topic_is_open( $bb_post->topic_id ) )
 229                  $caps[] = 'edit_closed';
 230              $post_time = bb_gmtstrtotime( $bb_post->post_time );
 231              $curr_time = time() + 1;
 232              $edit_lock = bb_get_option( 'edit_lock' );
 233              if ( $edit_lock >= 0 && $curr_time - $post_time > $edit_lock * 60 )
 234                  $caps[] = 'ignore_edit_lock';
 235              break;
 236          case 'delete_post' :
 237              // edit_deleted, delete_posts
 238              if ( !$bb_post = bb_get_post( $args[0] ) ) {
 239                  $caps[] = 'magically_provide_data_given_bad_input';
 240                  return $caps;
 241              }
 242              if ( 0 != $bb_post->post_status )
 243                  $caps[] = 'edit_deleted';
 244              // NO BREAK
 245          case 'manage_posts' : // back compat
 246              $caps[] = 'delete_posts';
 247              break;
 248          case 'write_topic':
 249              $caps[] = 'write_topics';
 250              break;
 251          case 'edit_topic':
 252              // edit_closed, edit_deleted, edit_topics, edit_others_topics
 253              if ( !$topic = get_topic( $args[0] ) ) {
 254                  $caps[] = 'magically_provide_data_given_bad_input';
 255                  return $caps;
 256              }
 257              if ( !topic_is_open( $args[0]) )
 258                  $caps[] = 'edit_closed';
 259              if ( '1' == $topic->topic_status )
 260                  $caps[] = 'edit_deleted';
 261              if ( $user_id == $topic->topic_poster )
 262                  $caps[] = 'edit_topics';
 263              else
 264                  $caps[] = 'edit_others_topics';
 265              break;
 266          case 'move_topic' :
 267              $caps[] = 'move_topics';
 268              break;
 269          case 'stick_topic' :
 270              $caps[] = 'stick_topics';
 271              break;
 272          case 'close_topic' :
 273              $caps[] = 'close_topics';
 274              break;
 275          case 'delete_topic' :
 276              $caps[] = 'delete_topics';
 277              add_filter( 'get_topic_where', 'bb_no_where', 9999 );
 278              if ( !$topic = get_topic( $args[0] ) ) {
 279                  $caps[] = 'magically_provide_data_given_bad_input';
 280                  return $caps;
 281              }
 282              if ( 0 != $topic->topic_status )
 283                  $caps[] = 'edit_deleted';
 284              remove_filter( 'get_topic_where', 'bb_no_where', 9999 );
 285              break;
 286          case 'manage_topics' :
 287              // back compat
 288              $caps[] = 'move_topics';
 289              $caps[] = 'stick_topics';
 290              $caps[] = 'close_topics';
 291              $caps[] = 'delete_topics';
 292              break;
 293          case 'add_tag_to':
 294              // edit_closed, edit_deleted, edit_tags;
 295              if ( !$topic = get_topic( $args[0] ) ) {
 296                  $caps[] = 'magically_provide_data_given_bad_input';
 297                  return $caps;
 298              }
 299              if ( !topic_is_open( $topic->topic_id ) )
 300                  $caps[] = 'edit_closed';
 301              if ( '1' == $topic->topic_status )
 302                  $caps[] = 'edit_deleted';
 303              $caps[] = 'edit_tags';
 304              break;
 305          case 'edit_tag_by_on':
 306              // edit_closed, edit_deleted, edit_tags, edit_others_tags
 307              if ( !$topic = get_topic( $args[1] ) ) {
 308                  $caps[] = 'magically_provide_data_given_bad_input';
 309                  return $caps;
 310              }
 311              if ( !topic_is_open( $topic->topic_id ) )
 312                  $caps[] = 'edit_closed';
 313              if ( '1' == $topic->topic_status )
 314                  $caps[] = 'edit_deleted';
 315              if ( $user_id == $args[0] )
 316                  $caps[] = 'edit_tags';
 317              else
 318                  $caps[] = 'edit_others_tags';
 319              break;
 320          case 'edit_user':
 321              // edit_profile, edit_users;
 322              if ( $user_id == $args[0] )
 323                  $caps[] = 'edit_profile';
 324              else
 325                  $caps[] = 'edit_users';
 326              break;
 327          case 'edit_favorites_of':
 328              // edit_favorites, edit_others_favorites;
 329              if ( $user_id == $args[0] )
 330                  $caps[] = 'edit_favorites';
 331              else
 332                  $caps[] = 'edit_others_favorites';
 333              break;
 334          case 'delete_forum':
 335              $caps[] = 'delete_forums';
 336              break;
 337          case 'change_user_password':
 338              // change_password, edit_users
 339              $caps[] = 'change_password';
 340              if ( $user_id != $args[0] )
 341                  $caps[] = 'edit_users';
 342              break;
 343          default:
 344              // If no meta caps match, return the original cap.
 345              $caps[] = $cap;
 346      }
 347      return $caps;
 348  }


Generated: Thu Dec 7 01:01:35 2017 Cross-referenced by PHPXref 0.7.1