PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/role-scoper/admin/groups-support.php

https://bitbucket.org/broderboy/nycendurance-wordpress
PHP | 350 lines | 207 code | 82 blank | 61 comment | 50 complexity | 7f826a085110d31792ef715f3e8a050f MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. if( basename(__FILE__) == basename($_SERVER['SCRIPT_FILENAME']) )
  3. die('This page cannot be called directly.');
  4. /* this file adapted from:
  5. Group Restriction plugin
  6. http://code.google.com/p/wp-group-restriction/
  7. Tiago Pocinho, Siemens Networks, S.A.
  8. some group-related functions also moved to ScoperAdminLib with slight adaptation
  9. */
  10. class UserGroups_tp {
  11. function getUsersWithGroup($group_id) {
  12. return ScoperAdminLib::get_group_members($group_id);
  13. }
  14. function addGroupMembers ($group_id, $user_ids){
  15. ScoperAdminLib::add_group_user($group_id, $user_ids);
  16. }
  17. function deleteGroupMembers ($group_id, $user_ids) {
  18. ScoperAdminLib::remove_group_user($group_id, $user_ids);
  19. }
  20. function GetGroup($group_id) {
  21. return ScoperAdminLib::get_group($group_id);
  22. }
  23. function getGroupByName($name) {
  24. return ScoperAdminLib::get_group_by_name($name);
  25. }
  26. /**
  27. * Creates a new Group
  28. *
  29. * @param string $name - Name of the group
  30. * @param string $description - Group description (optional)
  31. * @return group ID on successful creation
  32. **/
  33. function createGroup ($name, $description = ''){
  34. global $wpdb;
  35. if( ! UserGroups_tp::isValidName($name) )
  36. return false;
  37. $insert = "INSERT INTO $wpdb->groups_rs ($wpdb->groups_name_col, $wpdb->groups_descript_col) VALUES ('$name','$description')";
  38. scoper_query( $insert );
  39. wpp_cache_flush_group('all_usergroups');
  40. wpp_cache_flush_group('group_members' );
  41. wpp_cache_flush_group('usergroups_for_user');
  42. wpp_cache_flush_group('usergroups_for_groups');
  43. wpp_cache_flush_group('usergroups_for_ug');
  44. do_action('created_group_rs', (int) $wpdb->insert_id);
  45. return (int) $wpdb->insert_id;
  46. }
  47. /**
  48. * Removes a given group
  49. *
  50. * @param int $id - Identifier of the group to delete
  51. * @param boolean True if the deletion is successful
  52. **/
  53. function deleteGroup ($group_id){
  54. global $wpdb;
  55. if( ! $group_id || ! UserGroups_tp::getGroup($group_id) )
  56. return false;
  57. do_action('delete_group_rs', $group_id);
  58. wpp_cache_flush_group( 'all_usergroups' );
  59. wpp_cache_flush_group( 'group_members' );
  60. wpp_cache_flush_group( 'usergroups_for_user' );
  61. wpp_cache_flush_group( 'usergroups_for_groups' );
  62. wpp_cache_flush_group( 'usergroups_for_ug' );
  63. // first delete all cache entries related to this group
  64. if ( $group_members = ScoperAdminLib::get_group_members( $group_id, COL_ID_RS ) ) {
  65. $id_in = "'" . implode("', '", $group_members) . "'";
  66. $any_user_roles = scoper_get_var("SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE role_type = 'rs' AND user_id IN ($id_in) LIMIT 1");
  67. foreach ($group_members as $user_id )
  68. wpp_cache_delete( $user_id, 'group_membership_for_user' );
  69. }
  70. //if ( $got_blogrole = scoper_get_var("SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE scope = 'blog' AND role_type = 'rs' AND group_id = '$group_id' LIMIT 1") ) {
  71. scoper_query("DELETE FROM $wpdb->user2role2object_rs WHERE scope = 'blog' AND role_type = 'rs' AND group_id = '$group_id'");
  72. scoper_flush_roles_cache( BLOG_SCOPE_RS, ROLE_BASIS_GROUPS );
  73. if ( $any_user_roles )
  74. scoper_flush_roles_cache( BLOG_SCOPE_RS, ROLE_BASIS_USER_AND_GROUPS, $group_members );
  75. //}
  76. //if ( $got_taxonomyrole = scoper_get_var("SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE scope = 'term' AND role_type = 'rs' AND group_id = '$group_id' LIMIT 1") ) {
  77. scoper_query("DELETE FROM $wpdb->user2role2object_rs WHERE scope = 'term' AND role_type = 'rs' AND group_id = '$group_id'");
  78. scoper_flush_roles_cache( TERM_SCOPE_RS, ROLE_BASIS_GROUPS );
  79. if ( $any_user_roles )
  80. scoper_flush_roles_cache( TERM_SCOPE_RS, ROLE_BASIS_USER_AND_GROUPS, $group_members );
  81. //}
  82. //if ( $got_objectrole = scoper_get_var("SELECT assignment_id FROM $wpdb->user2role2object_rs WHERE scope = 'object' AND role_type = 'rs' AND group_id = '$group_id' LIMIT 1") ) {
  83. scoper_query("DELETE FROM $wpdb->user2role2object_rs WHERE scope = 'object' AND role_type = 'rs' AND group_id = '$group_id'");
  84. scoper_flush_roles_cache( OBJECT_SCOPE_RS, ROLE_BASIS_GROUPS );
  85. if ( $any_user_roles )
  86. scoper_flush_roles_cache( OBJECT_SCOPE_RS, ROLE_BASIS_USER_AND_GROUPS, $group_members );
  87. //}
  88. //if ( $got_blogrole || $got_taxonomyrole || $got_objectrole ) {
  89. scoper_flush_results_cache( ROLE_BASIS_GROUPS );
  90. if ( $any_user_roles )
  91. scoper_flush_results_cache( ROLE_BASIS_USER_AND_GROUPS, $group_members );
  92. //}
  93. $delete = "DELETE FROM $wpdb->groups_rs WHERE $wpdb->groups_id_col='$group_id'";
  94. scoper_query( $delete );
  95. $delete = "DELETE FROM $wpdb->user2group_rs WHERE $wpdb->user2group_gid_col='$group_id'";
  96. scoper_query( $delete );
  97. return true;
  98. }
  99. /**
  100. * Checks if a group with a given name exists
  101. *
  102. * @param string $name - Name of the group to test
  103. * @return boolean True if the group exists, false otherwise.
  104. **/
  105. function groupExists($name) {
  106. global $wpdb;
  107. $query = "SELECT COUNT(*) FROM $wpdb->groups_rs WHERE $wpdb->groups_name_col = '$name'";
  108. $results = scoper_get_var( $query );
  109. return $results != 0;
  110. }
  111. /**
  112. * Verifies if a group name is valid (for a new group)
  113. *
  114. * @param string $string - Name of the group
  115. * @return boolean True if the name is valid, false otherwise.
  116. **/
  117. function isValidName($string){
  118. if($string == "" || UserGroups_tp::groupExists($string)){
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * Updates an existing Group
  125. *
  126. * @param int $groupID - Group identifier
  127. * @param string $name - Name of the group
  128. * @param string $description - Group description (optional)
  129. * @return boolean True on successful update
  130. **/
  131. function updateGroup ($group_id, $name, $description = ''){
  132. global $wpdb;
  133. $description = strip_tags($description);
  134. if ( $prev = scoper_get_row("SELECT * FROM $wpdb->groups_rs WHERE $wpdb->groups_id_col='$group_id';") ) {
  135. if( ($prev->{$wpdb->groups_name_col} != $name) && ! UserGroups_tp::isValidName($name))
  136. return false;
  137. // don't allow updating of metagroup name / descript
  138. if( ! empty($prev->meta_id) )
  139. return false;
  140. }
  141. do_action('update_group_rs', $group_id);
  142. $query = "UPDATE $wpdb->groups_rs SET $wpdb->groups_name_col = '$name', $wpdb->groups_descript_col='$description' WHERE $wpdb->groups_id_col='$group_id';";
  143. scoper_query( $query );
  144. wpp_cache_flush_group('all_usergroups');
  145. wpp_cache_flush_group('group_members' );
  146. wpp_cache_flush_group('usergroups_for_user');
  147. wpp_cache_flush_group('usergroups_for_groups');
  148. wpp_cache_flush_group('usergroups_for_ug');
  149. return true;
  150. }
  151. function update_group_members_multi_status( $group_id, $current_members ) {
  152. $posted_members = array();
  153. $is_administrator = is_user_administrator_rs();
  154. $can_manage = $is_administrator || current_user_can( 'manage_groups' );
  155. $can_moderate = $can_manage || current_user_can( 'recommend_group_membership' );
  156. if ( ! $can_moderate && ! current_user_can( 'request_group_membership' ) )
  157. return;
  158. if ( $can_manage )
  159. $posted_members['active'] = explode( ',', trim($_POST['current_agents_rs_csv'], ',') );
  160. else
  161. $current_members = array_diff_key( $current_members, array( 'active' => true ) );
  162. if ( $can_moderate ) {
  163. $current_members['recommended'] = ScoperAdminLib::get_group_members($group_id, COL_ID_RS, false, array( 'status' => 'recommended' ) );
  164. if ( ! empty($_POST['recommended_agents_rs_csv']) )
  165. $posted_members['recommended'] = explode( ',', trim($_POST['recommended_agents_rs_csv'], ',') );
  166. }
  167. $current_members['requested'] = ScoperAdminLib::get_group_members($group_id, COL_ID_RS, false, array( 'status' => 'requested' ) );
  168. if ( ! empty($_POST['requested_agents_rs_csv']) )
  169. $posted_members['requested'] = explode( ',', trim($_POST['requested_agents_rs_csv'], ',') );
  170. $all_current_members = agp_array_flatten ( $current_members );
  171. $all_posted_members = agp_array_flatten ( $posted_members );
  172. foreach ( $current_members as $status => $stored ) {
  173. // remove group memberships which were not posted for any status
  174. foreach ( $stored as $user_id ) {
  175. if ( $user_id )
  176. if ( ! in_array( $user_id, $all_posted_members ) )
  177. ScoperAdminLib::remove_group_user($group_id, $user_id);
  178. }
  179. }
  180. foreach ( $posted_members as $status => $posted ) {
  181. // insert or update group memberships as specified
  182. foreach ( $posted as $user_id ) {
  183. if ( $user_id )
  184. if ( ! in_array( $user_id, $all_current_members ) )
  185. ScoperAdminLib::add_group_user($group_id, $user_id, $status);
  186. elseif ( ! in_array( $user_id, $current_members[$status] ) )
  187. ScoperAdminLib::update_group_user($group_id, $user_id, $status);
  188. }
  189. }
  190. }
  191. // Called once each for members checklist, managers checklist in admin UI.
  192. // In either case, current (checked) members are at the top of the list.
  193. function group_members_checklist( $group_id, $user_class = 'member', $all_users = '' ) {
  194. global $scoper;
  195. if ( ! $all_users )
  196. $all_users = $scoper->users_who_can('', COLS_ID_NAME_RS);
  197. if ( $group_id )
  198. $group = ScoperAdminLib::get_group($group_id);
  199. if ( 'member' == $user_class ) {
  200. $current_ids = ($group_id) ? array_flip(ScoperAdminLib::get_group_members($group_id, COL_ID_RS)) : array();
  201. if ( ! empty($group) && in_array( $group->meta_id, array( 'rv_pending_rev_notice_ed_nr_', 'rv_scheduled_rev_notice_ed_nr_' ) ) ) {
  202. $args = array( 'any_object' => true );
  203. $eligible_ids = array();
  204. foreach( get_post_types( array( 'public' => true ), 'object' ) as $_type => $_type_obj ) {
  205. $args['object_type'] = $_type;
  206. $type_eligible_ids = $scoper->users_who_can( array( $_type_obj->cap->edit_published_posts, $_type_obj->cap->edit_others_posts ), COL_ID_RS, 'post', 0, $args );
  207. $eligible_ids = array_merge( $eligible_ids, $type_eligible_ids );
  208. }
  209. $eligible_ids = array_unique( $eligible_ids );
  210. } else {
  211. // force_all_users arg is a temporary measure to ensure that any user can be viewed / added to a sitewide MU group regardless of what blog backend it's edited through
  212. $_args = ( IS_MU_RS && scoper_get_option( 'mu_sitewide_groups', true ) ) ? array( 'force_all_users' => true ) : array();
  213. $eligible_ids = $scoper->users_who_can( '', COL_ID_RS, '', '', $_args );
  214. }
  215. $admin_ids = array();
  216. } else {
  217. $group_role_defs = ( 'moderator' == $user_class ) ? array( 'rs_group_moderator' ) : array( 'rs_group_manager' );
  218. if ( $group_id ) {
  219. require_once( dirname(__FILE__).'/role_assignment_lib_rs.php');
  220. $current_roles = ScoperRoleAssignments::organize_assigned_roles(OBJECT_SCOPE_RS, 'group', $group_id, $group_role_defs, ROLE_BASIS_USER);
  221. $current_roles = agp_array_flatten($current_roles, false);
  222. $current_ids = ( isset($current_roles['assigned']) ) ? $current_roles['assigned'] : array();
  223. } else
  224. $current_ids = array();
  225. $cap_name = ( defined( 'SCOPER_USER_ADMIN_CAP' ) ) ? constant( 'SCOPER_USER_ADMIN_CAP' ) : 'edit_users';
  226. $admin_ids = $scoper->users_who_can( $cap_name, COL_ID_RS );
  227. // optionally, limit available group managers according to role_admin_blogwide_editor_only option
  228. if ( 'manager' == $user_class ) {
  229. $require_blogwide_editor = false;
  230. if ( ! empty($group) ) {
  231. if ( ! strpos( $group->meta_id, '_nr_' ) ) { // don't limit manager selection for groups that don't have role assignments
  232. $require_blogwide_editor = scoper_get_option('role_admin_blogwide_editor_only');
  233. }
  234. }
  235. if ( 'admin' == $require_blogwide_editor ) {
  236. $eligible_ids = $admin_ids;
  237. } elseif ( 'admin_content' == $require_blogwide_editor ) {
  238. $cap_name = ( defined( 'SCOPER_CONTENT_ADMIN_CAP' ) ) ? constant( 'SCOPER_CONTENT_ADMIN_CAP' ) : 'activate_plugins';
  239. $eligible_ids = array_unique( array_merge( $admin_ids, $scoper->users_who_can( $cap_name, COL_ID_RS ) ) );
  240. } elseif ( $require_blogwide_editor ) {
  241. $post_editors = $scoper->users_who_can('edit_others_posts', COL_ID_RS);
  242. $page_editors = $scoper->users_who_can('edit_others_pages', COL_ID_RS);
  243. $eligible_ids = array_unique( array_merge($post_editors, $page_editors, $admin_ids) );
  244. } else
  245. $eligible_ids = '';
  246. } else
  247. $eligible_ids = '';
  248. } // endif user class is not "member"
  249. $css_id = $user_class;
  250. $args = array( 'eligible_ids' => $eligible_ids, 'via_other_scope_ids' => $admin_ids, 'suppress_extra_prefix' => true );
  251. require_once( dirname(__FILE__).'/agents_checklist_rs.php');
  252. ScoperAgentsChecklist::agents_checklist( ROLE_BASIS_USER, $all_users, $css_id, $current_ids, $args);
  253. }
  254. /**
  255. * Writes the success/error messages
  256. * @param string $string - message to be displayed
  257. * @param boolean $success - boolean that defines if is a success(true) or error(false) message
  258. **/
  259. function write($string, $success=true, $id="message"){
  260. if($success){
  261. echo '<div id="'.$id.'" class="updated fade"><p>'.$string.'</p></div>';
  262. }else{
  263. echo '<div id="'.$id.'" class="error fade"><p>'.$string.'</p></div>';
  264. }
  265. }
  266. }
  267. ?>