| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-user.php |
| 4 |
* |
| 5 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author Karim Rahimpur |
| 18 |
* @package groups |
| 19 |
* @since groups 1.0.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
require_once( GROUPS_CORE_LIB . "/interface-i-capable.php" ); |
| 23 |
require_once( GROUPS_CORE_LIB . "/class-groups-capability.php" ); |
| 24 |
|
| 25 |
class Groups_User implements I_Capable { |
| 26 |
|
| 27 |
/** |
| 28 |
* User object. |
| 29 |
* |
| 30 |
* @var WP_User |
| 31 |
*/ |
| 32 |
var $user = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Create, if $user_id = 0 an anonymous user is assumed. |
| 36 |
* |
| 37 |
* @param int $user_id |
| 38 |
*/ |
| 39 |
public function __construct( $user_id ) { |
| 40 |
if ( Groups_Utility::id( $user_id ) ) { |
| 41 |
$this->user = get_user_by( "id", $user_id ); |
| 42 |
} else { |
| 43 |
$this->user = new WP_User( 0 ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Retrieve a user property. |
| 49 |
* Must be "capabilities" or a property of the WP_User class. |
| 50 |
* @param string $name property's name |
| 51 |
*/ |
| 52 |
public function __get( $name ) { |
| 53 |
|
| 54 |
global $wpdb; |
| 55 |
|
| 56 |
$result = null; |
| 57 |
|
| 58 |
if ( $this->user !== null ) { |
| 59 |
switch ( $name ) { |
| 60 |
case "capabilities" : |
| 61 |
$user_capability_table = _groups_get_tablename( "user_capability" ); |
| 62 |
$rows = $wpdb->get_results( $wpdb->prepare( |
| 63 |
"SELECT capability_id FROM $user_capability_table WHERE user_id = %d", |
| 64 |
Groups_Utility::id( $this->user->ID ) |
| 65 |
) ); |
| 66 |
if ( $rows ) { |
| 67 |
$result = array(); |
| 68 |
foreach ( $rows as $row ) { |
| 69 |
$result[] = new Groups_Capability( $row->capability_id ); |
| 70 |
} |
| 71 |
} |
| 72 |
break; |
| 73 |
case "groups" : |
| 74 |
$user_group_table = _groups_get_tablename( "user_group" ); |
| 75 |
$rows = $wpdb->get_results( $wpdb->prepare( |
| 76 |
"SELECT group_id FROM $user_group_table WHERE user_id = %d", |
| 77 |
Groups_Utility::id( $this->user->ID ) |
| 78 |
) ); |
| 79 |
if ( $rows ) { |
| 80 |
$result = array(); |
| 81 |
foreach( $rows as $row ) { |
| 82 |
$result[] = new Groups_Group( $row->group_id ); |
| 83 |
} |
| 84 |
} |
| 85 |
break; |
| 86 |
default: |
| 87 |
$result = $this->user->$name; |
| 88 |
} |
| 89 |
} |
| 90 |
return $result; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* (non-PHPdoc) |
| 95 |
* @see I_Capable::can() |
| 96 |
*/ |
| 97 |
public function can( $capability ) { |
| 98 |
|
| 99 |
global $wpdb; |
| 100 |
$result = false; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if ( $this->user !== null ) { |
| 105 |
|
| 106 |
// if administrators can override access, let them |
| 107 |
if ( get_option( GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE, GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE_DEFAULT ) ) { |
| 108 |
if ( user_can( $this->user->ID, 'administrator' ) ) { // just using $this->user would raise a warning on 3.2.1 |
| 109 |
return true; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$group_table = _groups_get_tablename( "group" ); |
| 114 |
$capability_table = _groups_get_tablename( "capability" ); |
| 115 |
$group_capability_table = _groups_get_tablename( "group_capability" ); |
| 116 |
$user_group_table = _groups_get_tablename( "user_group" ); |
| 117 |
|
| 118 |
// determine capability id |
| 119 |
$capability_id = null; |
| 120 |
if ( is_numeric( $capability ) ) { |
| 121 |
$capability_id = Groups_Utility::id( $capability ); |
| 122 |
} else if ( is_string( $capability ) ) { |
| 123 |
$capability_id = $wpdb->get_var( $wpdb->prepare( |
| 124 |
"SELECT capability_id FROM $capability_table WHERE capability = %s", |
| 125 |
$capability |
| 126 |
) ); |
| 127 |
} |
| 128 |
|
| 129 |
if ( $capability_id !== null ) { |
| 130 |
// either the user can ... |
| 131 |
$result = ( Groups_User_Capability::read( $this->user->ID, $capability_id ) !== false ); |
| 132 |
// ... or the user can because a group the user belongs to can |
| 133 |
if ( !$result ) { |
| 134 |
// Important: before making any changes check |
| 135 |
// INDEX usage on modified query! |
| 136 |
$rows = $wpdb->get_results( $wpdb->prepare( |
| 137 |
"SELECT capability_id FROM $group_capability_table WHERE capability_id = %d AND group_id IN (SELECT group_id FROM $user_group_table WHERE user_id = %d)", |
| 138 |
Groups_Utility::id( $capability_id ), |
| 139 |
Groups_Utility::id( $this->user->ID ) |
| 140 |
) ); |
| 141 |
if ( count( $rows ) > 0 ) { |
| 142 |
$result = true; |
| 143 |
} |
| 144 |
} |
| 145 |
// ... or because any of the parent groups can |
| 146 |
if ( !$result ) { |
| 147 |
// search in parent groups |
| 148 |
$limit = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $group_table" ) ); |
| 149 |
if ( $limit !== null ) { |
| 150 |
|
| 151 |
// note that limits by blog_id for multisite are |
| 152 |
// enforced when a user is added to a blog |
| 153 |
$user_groups = $wpdb->get_results( $wpdb->prepare( |
| 154 |
"SELECT group_id FROM $user_group_table WHERE user_id = %d", |
| 155 |
Groups_Utility::id( $this->user->ID ) |
| 156 |
) ); |
| 157 |
|
| 158 |
if ( $user_groups ) { |
| 159 |
$group_ids = array(); |
| 160 |
foreach( $user_groups as $user_group ) { |
| 161 |
$group_ids[] = Groups_Utility::id( $user_group->group_id ); |
| 162 |
} |
| 163 |
if ( count( $group_ids ) > 0 ) { |
| 164 |
$iterations = 0; |
| 165 |
$old_group_ids_count = 0; |
| 166 |
while( ( $iterations < $limit ) && ( count( $group_ids ) !== $old_group_ids_count ) ) { |
| 167 |
$iterations++; |
| 168 |
$old_group_ids_count = count( $group_ids ); |
| 169 |
$id_list = implode( ",", $group_ids ); |
| 170 |
$parent_group_ids = $wpdb->get_results( |
| 171 |
"SELECT parent_id FROM $group_table WHERE parent_id IS NOT NULL AND group_id IN ($id_list)" |
| 172 |
); |
| 173 |
if ( $parent_group_ids ) { |
| 174 |
foreach( $parent_group_ids as $parent_group_id ) { |
| 175 |
$parent_group_id = Groups_Utility::id( $parent_group_id->parent_id ); |
| 176 |
if ( !in_array( $parent_group_id, $group_ids ) ) { |
| 177 |
$group_ids[] = $parent_group_id; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
$id_list = implode( ",", $group_ids ); |
| 183 |
$rows = $wpdb->get_results( $wpdb->prepare( |
| 184 |
"SELECT capability_id FROM $group_capability_table WHERE capability_id = %d AND group_id IN ($id_list)", |
| 185 |
Groups_Utility::id( $capability_id ) |
| 186 |
) ); |
| 187 |
if ( count( $rows ) > 0 ) { |
| 188 |
$result = true; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
$result = apply_filters_ref_array( "groups_user_can", array( $result, &$this, $capability ) ); |
| 197 |
return $result; |
| 198 |
} |
| 199 |
} |