Skip to content

Commit e13f6d1

Browse files
brianhoggideadude
andauthored
Add additional check for Instructors enrolling users into courses or memberships (#2881)
* Add additional check for enrolling users into courses or memberships. * Filter to only return students for courses the instructor is an instructor for. * Changelog. * Only show bulk dropdown if they can manage lifterlms. --------- Co-authored-by: Jason Coleman <33220397+ideadude@users.noreply.github.com>
1 parent 818760e commit e13f6d1

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
significance: patch
2+
type: fixed
3+
entry: Adds additional verifications on permission for bulk enrolls, and REST
4+
API access for instructors.

includes/admin/class.llms.student.bulk.enroll.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function __construct() {
7373
*/
7474
public function display_product_selection_for_bulk_users( $which ) {
7575

76+
if ( ! current_user_can( 'manage_lifterlms' ) ) {
77+
return;
78+
}
79+
7680
// The attributes need to be different for top and bottom of the table.
7781
$id = 'bottom' === $which ? 'llms_bulk_enroll_product2' : 'llms_bulk_enroll_product';
7882
$submit = 'bottom' === $which ? 'llms_bulk_enroll2' : 'llms_bulk_enroll';
@@ -114,6 +118,12 @@ public function maybe_enroll_users_in_product() {
114118
return;
115119
}
116120

121+
if ( ! current_user_can( 'enroll', $this->product_id ) ) {
122+
$message = __( 'You do not have permission to enroll users into this course or membership.', 'lifterlms' );
123+
$this->generate_notice( 'error', $message );
124+
return;
125+
}
126+
117127
// Get the product title for notices.
118128
$this->product_title = get_the_title( $this->product_id );
119129

includes/class.llms.user.permissions.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct() {
3535

3636
add_filter( 'user_has_cap', array( $this, 'handle_caps' ), 10, 3 );
3737
add_filter( 'editable_roles', array( $this, 'editable_roles' ) );
38-
38+
add_filter( 'rest_user_query', array( $this, 'filter_rest_user_query' ), 10, 2 );
3939
}
4040

4141
/**
@@ -98,7 +98,45 @@ public function editable_roles( $all_roles ) {
9898
}
9999

100100
return $all_roles;
101+
}
102+
103+
/**
104+
* Filter the WP_User_Query args to ensure that instructors can only see their students
105+
*
106+
* @since [version]
107+
*
108+
* @param array $args WP_User_Query args.
109+
* @param WP_REST_Request $request Request object.
110+
* @return array
111+
*/
112+
public function filter_rest_user_query( $args, $request ) {
113+
114+
$user = wp_get_current_user();
115+
116+
if ( ! $user ) {
117+
return $args;
118+
}
119+
120+
if ( ! in_array( 'instructor', $user->roles, true ) ) {
121+
return $args;
122+
}
123+
124+
$instructor = llms_get_instructor( $user );
101125

126+
if ( ! $instructor ) {
127+
return $args;
128+
}
129+
130+
$student_query = $instructor->get_students( array( 'statuses' => array( 'enrolled' ) ) );
131+
$students = $student_query->get_results();
132+
133+
if ( empty( $students ) ) {
134+
$args['include'] = array( 0 );
135+
} else {
136+
$args['include'] = wp_list_pluck( $students, 'id' );
137+
}
138+
139+
return $args;
102140
}
103141

104142
/**
@@ -137,7 +175,6 @@ public function edit_others_lms_content( $allcaps, $cap, $args ) {
137175
}
138176

139177
return $allcaps;
140-
141178
}
142179

143180
/**
@@ -165,7 +202,6 @@ public static function get_editable_roles() {
165202
);
166203

167204
return $roles;
168-
169205
}
170206

171207
/**
@@ -198,10 +234,10 @@ private function handle_cap_view_grades( $allcaps, $args ) {
198234
return $allcaps;
199235
}
200236

201-
$requested_cap = $args[0];
202-
$current_user_id = intval( $args[1] );
237+
$requested_cap = $args[0];
238+
$current_user_id = intval( $args[1] );
203239
$requested_user_id = intval( $args[2] );
204-
$post_id = isset( $args[3] ) ? intval( $args[3] ) : false;
240+
$post_id = isset( $args[3] ) ? intval( $args[3] ) : false;
205241

206242
// Administrators and LMS managers explicitly have the cap so we don't need to perform any further checks.
207243
if ( ! empty( $allcaps[ $requested_cap ] ) ) {
@@ -222,7 +258,6 @@ private function handle_cap_view_grades( $allcaps, $args ) {
222258
}
223259

224260
return $allcaps;
225-
226261
}
227262

228263
/**
@@ -295,7 +330,6 @@ public function handle_caps( $allcaps, $cap, $args ) {
295330
}
296331

297332
return $allcaps;
298-
299333
}
300334

301335
/**
@@ -308,7 +342,6 @@ public function handle_caps( $allcaps, $cap, $args ) {
308342
public static function is_current_user_instructor() {
309343

310344
return ( current_user_can( 'lifterlms_instructor' ) && current_user_can( 'list_users' ) && ! current_user_can( 'manage_lifterlms' ) );
311-
312345
}
313346

314347
/**
@@ -384,7 +417,6 @@ protected function user_can_manage_user( $user_id, $edit_id ) {
384417
}
385418

386419
return false;
387-
388420
}
389421

390422
/**
@@ -396,14 +428,11 @@ protected function user_can_manage_user( $user_id, $edit_id ) {
396428
* @param int $requested_user_id WP User ID of the user the action will be performed on.
397429
* @return bool Returns true if the user has the student, false if it doesn't
398430
*/
399-
protected function instructor_has_student( $current_user_id, $requested_user_id )
400-
{
431+
protected function instructor_has_student( $current_user_id, $requested_user_id ) {
401432

402433
$instructor = llms_get_instructor( $current_user_id );
403434
return $instructor && $instructor->has_student( $requested_user_id );
404-
405435
}
406-
407436
}
408437

409438
return new LLMS_User_Permissions();

0 commit comments

Comments
 (0)