LLMS_User_Permissions
Filters and actions related to user permissions
Contents
Source Source
File: includes/class.llms.user.permissions.php
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | class LLMS_User_Permissions { /** * Constructor * * @since 3.13.0 * @since 3.34.0 Always add the `editable_roles` filter. * * @return void */ public function __construct() { add_filter( 'user_has_cap' , array ( $this , 'handle_caps' ), 10, 3 ); add_filter( 'editable_roles' , array ( $this , 'editable_roles' ) ); } /** * Determines what other user roles can be managed by a user role * * Allows LMS Managers to create instructors and other managers. * Allows instructors to create & manage assistants. * * @since 3.13.0 * @since 3.34.0 Moved the `llms_editable_roles` filter to the class method get_editable_roles(). * @since 3.37.14 Use strict comparison. * @since 4.10.0 Better handling of users with multiple roles. * * * @param array $all_roles All roles array. * @return array */ public function editable_roles( $all_roles ) { /** * Prevent issues when other plugins call get_editable_roles() before `init`. * */ if ( ! function_exists( 'wp_get_current_user' ) ) { return $all_roles ; } if ( is_multisite() && is_super_admin() ) { return $all_roles ; } $user = wp_get_current_user(); $user_roles = $user ->roles; if ( in_array( 'administrator' , $user_roles , true ) ) { return $all_roles ; } $editable_roles = self::get_editable_roles(); if ( empty ( array_intersect ( $user_roles , array_keys ( $editable_roles ) ) ) ) { return $all_roles ; } $roles = array (); foreach ( $user_roles as $user_role ) { if ( isset( $editable_roles [ $user_role ] ) ) { $roles = array_merge ( $roles , $editable_roles [ $user_role ] ); } } $roles = array_unique ( $roles ); foreach ( array_keys ( $all_roles ) as $role ) { if ( ! in_array( $role , $roles , true ) ) { unset( $all_roles [ $role ] ); } } return $all_roles ; } /** * Handle capabilities checks for lms content to allow *editing* content based on course instructor * * @since 3.13.0 * * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name and boolean values * represent whether the user has that capability. * @param string[] $cap Required primitive capabilities for the requested capability. * @param array $args { * Arguments that accompany the requested capability check. * * @type string $0 Requested capability. * @type int $1 Concerned user ID. * @type mixed ...$2 Optional second and further parameters, typically object ID. * } * @return array */ public function edit_others_lms_content( $allcaps , $cap , $args ) { /** * this might be a problem * this happens when in wp-admin/includes/post.php * when actually creating/updating a course * and no post_id is passed in $args[2]. */ if ( empty ( $args [2] ) ) { $allcaps [ $cap [0] ] = true; return $allcaps ; } $instructor = llms_get_instructor( $args [1] ); if ( $instructor && $instructor ->is_instructor( $args [2] ) ) { $allcaps [ $cap [0] ] = true; } return $allcaps ; } /** * Get a map of roles that can be managed by LifterLMS User Roles * * @since 3.34.0 * * @return array */ public static function get_editable_roles() { /** * Get a map of roles that can be managed by LifterLMS User Roles * * @since 3.13.0 * * @param array $roles Array of user roles. The array key is the role and the value is an array of roles that can be managed by that role. */ $roles = apply_filters( 'llms_editable_roles' , array ( 'lms_manager' => array ( 'instructor' , 'instructors_assistant' , 'lms_manager' , 'student' ), 'instructor' => array ( 'instructors_assistant' ), ) ); return $roles ; } /** * Modify a users ability to `view_grades` * * Users can view the grades (quiz results) if one of the following conditions is met: * + Users can view their own grades. * + Admins and LMS Managers can view anyone's grade. * + Any user who has been explicitly granted the `view_grades` cap can view anyone's grade (via custom code). * + Any instructor/assistant who can `edit_post` for the course the quiz belongs to can view grades of the students within that course. * * @since 4.21.2 * * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name and boolean values * represent whether the user has that capability. * @param array $args { * Arguments that accompany the requested capability check. * * @type string $0 Requested capability: 'view_grades'. * @type int $1 Current User ID. * @type int $2 Requested User ID. * @type int $3 WP_Post ID of the quiz. * } * @return array */ private function handle_cap_view_grades( $allcaps , $args ) { // Logged out user or missing required args. if ( empty ( $args [1] ) || empty ( $args [2] ) || empty ( $args [3] ) ) { return $allcaps ; } list( $requested_cap , $current_user_id , $requested_user_id , $post_id ) = $args ; // Administrators and LMS managers explicitly have the cap so we don't need to perform any further checks. if ( ! empty ( $allcaps [ $requested_cap ] ) ) { return $allcaps ; } // Users can view their own grades. if ( $current_user_id === $requested_user_id ) { $allcaps [ $requested_cap ] = true; } elseif ( current_user_can( 'edit_post' , $post_id ) ) { $instructor = llms_get_instructor( $current_user_id ); if ( $instructor && $instructor ->has_student( $requested_user_id ) ) { $allcaps [ $requested_cap ] = true; } } return $allcaps ; } /** * Custom capability checks for LifterLMS things * * @since 3.13.0 * @since 3.34.0 Add logic for `edit_users` and `delete_users` capabilities with regards to LifterLMS user roles. * Add logic for `view_students`, `edit_students`, and `delete_students` capabilities. * @since 3.36.5 Add `llms_user_caps_edit_others_posts_post_types` filter. * @since 3.37.14 Use strict comparison. * @since 4.21.2 Add logic to handle the `view_grades` capability. * * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name and boolean values * represent whether the user has that capability. * @param string[] $cap Required primitive capabilities for the requested capability. * @param array $args { * Arguments that accompany the requested capability check. * * @type string $0 Requested capability. * @type int $1 Concerned user ID. * @type mixed ...$2 Optional second and further parameters, typically object ID. * } * @return array */ public function handle_caps( $allcaps , $cap , $args ) { /** * Modify the list of post types that users may not own but can still edit based on instructor permissions on the course * * @since 3.36.5 * * @param string[] $post_types Array of unprefixed post type names. */ $post_types = apply_filters( 'llms_user_caps_edit_others_posts_post_types' , array ( 'courses' , 'lessons' , 'sections' , 'quizzes' , 'questions' , 'memberships' ) ); foreach ( $post_types as $cpt ) { // Allow any instructor to edit courses they're attached to. if ( in_array( sprintf( 'edit_others_%s' , $cpt ), $cap , true ) ) { $allcaps = $this ->edit_others_lms_content( $allcaps , $cap , $args ); } } $required_cap = ! empty ( $cap [0] ) ? $cap [0] : false; if ( 'view_grades' === $required_cap ) { return $this ->handle_cap_view_grades( $allcaps , $args ); } // We don't have a cap or the user doesn't have the requested cap. if ( ! $required_cap || empty ( $allcaps [ $required_cap ] ) ) { return $allcaps ; } $user_id = ! empty ( $args [1] ) ? $args [1] : false; $object_id = ! empty ( $args [2] ) ? $args [2] : false; if ( in_array( $required_cap , array ( 'edit_users' , 'delete_users' ), true ) ) { if ( $user_id && $object_id && false === $this ->user_can_manage_user( $user_id , $object_id ) ) { unset( $allcaps [ $required_cap ] ); } } if ( in_array( $required_cap , array ( 'view_students' , 'edit_students' , 'delete_students' ), true ) ) { $others_cap = str_replace ( '_' , '_others_' , $required_cap ); if ( $user_id && $object_id && ! user_can( $user_id , $others_cap ) ) { $instructor = llms_get_instructor( $user_id ); if ( ! $instructor || ! $instructor ->has_student( $object_id ) ) { unset( $allcaps [ $required_cap ] ); } } } return $allcaps ; } /** * Determines if the current user is an instructor. * * @since 3.34.0 * * @return bool */ public static function is_current_user_instructor() { return ( current_user_can( 'lifterlms_instructor' ) && current_user_can( 'list_users' ) && ! current_user_can( 'manage_lifterlms' ) ); } /** * Determine if a user can manage another user. * * Run on `user_has_cap` filters for the `edit_users` and `delete_users` capabilities. * * @since 3.34.0 * @since 3.41.0 Better handling of users with multiple roles. * * @param int $user_id WP User ID of the user requesting to perform the action. * @param int $edit_id WP User ID of the user the action will be performed on. * @return bool|null Returns true if the user performs the action, false if it can't, and null for core user roles which are skipped. */ protected function user_can_manage_user( $user_id , $edit_id ) { $user = get_user_by( 'id' , $user_id ); /** * Filter the list of "ignored" user roles * * If a user has one of the roles specified in this list, LifterLMS * will not attempt to determine if the user can manage other users * and will instead allow the WordPress core (or another plugin) * to determine if they have the required permissions. * * @since 3.41.0 * * @param string[] $ignored Array of user roles. */ $ignored = apply_filters( 'llms_user_can_manage_user_ignored_roles' , array ( 'administrator' ) ); $lms_roles = array_keys ( LLMS_Roles::get_roles() ); $user_roles = array_intersect ( $user ->roles, $lms_roles ); $user_ignored_roles = array_intersect ( $user ->roles, $ignored ); /** * Skip the user because: * * + User has no LMS roles, eg: Administrator, Editor, or Subscriber. * + User has an LMS role and a "protected" role, eg: Administrator and student. * * In both scenarios we will return `null` which signals that the WordPress core (or another plugin) * should take care of determining if the user can manage the user. */ if ( ! $user_roles || ! empty ( $user_ignored_roles ) ) { return null; } $edit_id = absint( $edit_id ); $user_id = absint( $user_id ); // Users can edit themselves. if ( $user_id === $edit_id ) { return true; } $edit_user = get_user_by( 'id' , $edit_id ); $edit_roles = array_intersect ( $edit_user ->roles, $lms_roles ); $editable_roles = self::get_editable_roles(); foreach ( $user_roles as $role ) { if ( 'instructor' === $role && in_array( 'instructors_assistant' , $edit_roles , true ) ) { $instructor = llms_get_instructor( $user ); if ( in_array( $edit_id , array_map ( 'absint' , $instructor ->get_assistants() ), true ) ) { return true; } } elseif ( ! empty ( $editable_roles [ $role ] ) && array_intersect ( $edit_roles , $editable_roles [ $role ] ) ) { return true; } } return false; } } |
Expand full source code Collapse full source code View on GitHub
Methods Methods
- __construct — Constructor
- edit_others_lms_content — Handle capabilities checks for lms content to allow *editing* content based on course instructor
- editable_roles — Determines what other user roles can be managed by a user role
- get_editable_roles — Get a map of roles that can be managed by LifterLMS User Roles
- handle_cap_view_grades — Modify a users ability to `view_grades`
- handle_caps — Custom capability checks for LifterLMS things
- is_current_user_instructor — Determines if the current user is an instructor.
- user_can_manage_user — Determine if a user can manage another user.
Changelog Changelog
Version | Description |
---|---|
3.41.0 | Improve user management of other users when the managing user has multiple roles. |
3.37.14 | Use strict comparisons where needed. |
3.36.5 | Add llms_user_caps_edit_others_posts_post_types filter to allow 3rd parties to utilize core methods for modifying other users posts. |
3.34.0 | Added methods and logic for managing user management of other users. Add logic for view_students , edit_students , and delete_students capabilities. |
3.13.0 | Introduced. |