PluginProbe
Groups – Memberships and Access Control / 1.1.5
Groups – Memberships and Access Control v1.1.5
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / core / class-groups-user.php

class-groups-user.php in Groups – Memberships and Access Control 1.1.5, at lib/core/class-groups-user.php

202 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
26 * User OPM.
27 */
28 class Groups_User implements I_Capable {
29
30 /**
31 * User object.
32 *
33 * @var WP_User
34 */
35 var $user = null;
36
37 /**
38 * Create, if $user_id = 0 an anonymous user is assumed.
39 *
40 * @param int $user_id
41 */
42 public function __construct( $user_id ) {
43 if ( Groups_Utility::id( $user_id ) ) {
44 $this->user = get_user_by( "id", $user_id );
45 } else {
46 $this->user = new WP_User( 0 );
47 }
48 }
49
50 /**
51 * Retrieve a user property.
52 * Must be "capabilities" or a property of the WP_User class.
53 * @param string $name property's name
54 */
55 public function __get( $name ) {
56
57 global $wpdb;
58
59 $result = null;
60
61 if ( $this->user !== null ) {
62 switch ( $name ) {
63 case "capabilities" :
64 $user_capability_table = _groups_get_tablename( "user_capability" );
65 $rows = $wpdb->get_results( $wpdb->prepare(
66 "SELECT capability_id FROM $user_capability_table WHERE user_id = %d",
67 Groups_Utility::id( $this->user->ID )
68 ) );
69 if ( $rows ) {
70 $result = array();
71 foreach ( $rows as $row ) {
72 $result[] = new Groups_Capability( $row->capability_id );
73 }
74 }
75 break;
76 case "groups" :
77 $user_group_table = _groups_get_tablename( "user_group" );
78 $rows = $wpdb->get_results( $wpdb->prepare(
79 "SELECT group_id FROM $user_group_table WHERE user_id = %d",
80 Groups_Utility::id( $this->user->ID )
81 ) );
82 if ( $rows ) {
83 $result = array();
84 foreach( $rows as $row ) {
85 $result[] = new Groups_Group( $row->group_id );
86 }
87 }
88 break;
89 default:
90 $result = $this->user->$name;
91 }
92 }
93 return $result;
94 }
95
96 /**
97 * (non-PHPdoc)
98 * @see I_Capable::can()
99 */
100 public function can( $capability ) {
101
102 global $wpdb;
103 $result = false;
104
105
106
107 if ( $this->user !== null ) {
108
109 // if administrators can override access, let them
110 if ( get_option( GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE, GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE_DEFAULT ) ) {
111 if ( user_can( $this->user->ID, 'administrator' ) ) { // just using $this->user would raise a warning on 3.2.1
112 return true;
113 }
114 }
115
116 $group_table = _groups_get_tablename( "group" );
117 $capability_table = _groups_get_tablename( "capability" );
118 $group_capability_table = _groups_get_tablename( "group_capability" );
119 $user_group_table = _groups_get_tablename( "user_group" );
120
121 // determine capability id
122 $capability_id = null;
123 if ( is_numeric( $capability ) ) {
124 $capability_id = Groups_Utility::id( $capability );
125 } else if ( is_string( $capability ) ) {
126 $capability_id = $wpdb->get_var( $wpdb->prepare(
127 "SELECT capability_id FROM $capability_table WHERE capability = %s",
128 $capability
129 ) );
130 }
131
132 if ( $capability_id !== null ) {
133 // either the user can ...
134 $result = ( Groups_User_Capability::read( $this->user->ID, $capability_id ) !== false );
135 // ... or the user can because a group the user belongs to can
136 if ( !$result ) {
137 // Important: before making any changes check
138 // INDEX usage on modified query!
139 $rows = $wpdb->get_results( $wpdb->prepare(
140 "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)",
141 Groups_Utility::id( $capability_id ),
142 Groups_Utility::id( $this->user->ID )
143 ) );
144 if ( count( $rows ) > 0 ) {
145 $result = true;
146 }
147 }
148 // ... or because any of the parent groups can
149 if ( !$result ) {
150 // search in parent groups
151 $limit = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $group_table" ) );
152 if ( $limit !== null ) {
153
154 // note that limits by blog_id for multisite are
155 // enforced when a user is added to a blog
156 $user_groups = $wpdb->get_results( $wpdb->prepare(
157 "SELECT group_id FROM $user_group_table WHERE user_id = %d",
158 Groups_Utility::id( $this->user->ID )
159 ) );
160
161 if ( $user_groups ) {
162 $group_ids = array();
163 foreach( $user_groups as $user_group ) {
164 $group_ids[] = Groups_Utility::id( $user_group->group_id );
165 }
166 if ( count( $group_ids ) > 0 ) {
167 $iterations = 0;
168 $old_group_ids_count = 0;
169 while( ( $iterations < $limit ) && ( count( $group_ids ) !== $old_group_ids_count ) ) {
170 $iterations++;
171 $old_group_ids_count = count( $group_ids );
172 $id_list = implode( ",", $group_ids );
173 $parent_group_ids = $wpdb->get_results(
174 "SELECT parent_id FROM $group_table WHERE parent_id IS NOT NULL AND group_id IN ($id_list)"
175 );
176 if ( $parent_group_ids ) {
177 foreach( $parent_group_ids as $parent_group_id ) {
178 $parent_group_id = Groups_Utility::id( $parent_group_id->parent_id );
179 if ( !in_array( $parent_group_id, $group_ids ) ) {
180 $group_ids[] = $parent_group_id;
181 }
182 }
183 }
184 }
185 $id_list = implode( ",", $group_ids );
186 $rows = $wpdb->get_results( $wpdb->prepare(
187 "SELECT capability_id FROM $group_capability_table WHERE capability_id = %d AND group_id IN ($id_list)",
188 Groups_Utility::id( $capability_id )
189 ) );
190 if ( count( $rows ) > 0 ) {
191 $result = true;
192 }
193 }
194 }
195 }
196 }
197 }
198 }
199 $result = apply_filters_ref_array( "groups_user_can", array( $result, &$this, $capability ) );
200 return $result;
201 }
202 }