PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/buddypress/bp-forums/bbpress/bb-includes/functions.bb-capabilities.php

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