@@ -445,32 +445,38 @@ public function hasRole($role, $subject_id = null)
445
445
}
446
446
447
447
/**
448
- *
449
- * @param string|array $role
448
+ * Retrieves an array of subject IDs associated with a given user role.
449
+ *
450
+ * @param string $role The role code for which to retrieve subjects.
451
+ * @return array An array of unique subject IDs associated with the specified role.
450
452
*/
451
453
public function getRoleSubjectsArray ($ role )
452
454
{
455
+ // Fetch roles and associated subject IDs from the database
453
456
$ roles = UserRoles::find ()
454
457
->select (['user_roles.subject_id ' , 'roles.function_to_get_all_subjects ' ])
455
458
->leftJoin ('roles ' , 'user_roles.role_id = roles.id ' )
456
- ->where (['user_roles.user_id ' => $ this ->id ])
457
- ->andWhere (['roles.code ' => $ role ])
459
+ ->where (['user_roles.user_id ' => $ this ->id ]) // Filter by user ID
460
+ ->andWhere (['roles.code ' => $ role ]) // Filter by role code
458
461
->asArray ()
459
462
->all ();
460
463
461
- // list of subjects
464
+ // Initialize an empty list for subjects
462
465
$ subjects = [];
463
466
464
467
foreach ($ roles as $ role ) {
468
+ // Check if a subject ID is set and add it to the subjects array
465
469
if (isset ($ role ['subject_id ' ])) {
466
470
$ subjects = array_unique (array_merge ([$ role ['subject_id ' ]], $ subjects ));
471
+ // If a function to get all subjects is defined and callable
467
472
} else if (isset ($ role ['function_to_get_all_subjects ' ]) && is_callable ($ role ['function_to_get_all_subjects ' ])) {
468
-
473
+ // Call the function to get subjects and merge the results
469
474
$ result = $ role ['function_to_get_all_subjects ' ]();
470
475
$ subjects = array_unique (array_merge (array_keys ($ result ), $ subjects ));
471
476
}
472
477
}
473
478
479
+ // Return the array of unique subject IDs
474
480
return $ subjects ;
475
481
}
476
482
0 commit comments