|
35 | 35 |
|
36 | 36 | function doesPayloadSatisfyRoleRequirement($payload) {
|
37 | 37 |
|
38 |
| - $roleField = getenv('MOODLE_JWT_ROLE_FIELD'); |
39 |
| - $requiredRole = getenv('MOODLE_JWT_REQUIRED_ROLE'); |
40 |
| - |
41 |
| - if (empty($roleField) || empty($requiredRole)) { |
42 |
| - return true; |
43 |
| - } |
| 38 | + $checkForRole = getenv('MOODLE_JWT_USE_ROLE_CHECK'); |
| 39 | + $roleField = getenv('MOODLE_JWT_ROLE_FIELD'); |
| 40 | + $requiredRole = getenv('MOODLE_JWT_REQUIRED_ROLE'); |
44 | 41 |
|
45 |
| - if (isset($payload->$roleField) && is_array($payload->$roleField)) { |
46 |
| - return in_array($requiredRole, $payload->$roleField); |
47 |
| - } |
| 42 | + if (empty($checkForRole) || $checkForRole != "true") { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + if (empty($roleField) || empty($requiredRole)) { |
| 47 | + return false; |
| 48 | + } |
48 | 49 |
|
49 |
| - // Return false if the field is not present or the role is not found |
50 |
| - return false; |
| 50 | + if (isset($payload->$roleField) && is_array($payload->$roleField)) { |
| 51 | + return in_array($requiredRole, $payload->$roleField); |
| 52 | + } |
| 53 | + |
| 54 | + // Return false if the field is not present or the role is not found |
| 55 | + return false; |
51 | 56 | }
|
52 | 57 |
|
53 | 58 | $payloadWithRole = (object)[
|
54 | 59 | 'roles' => ['admin', 'editor', 'user']
|
55 | 60 | ];
|
56 | 61 |
|
| 62 | +putenv("MOODLE_JWT_USE_ROLE_CHECK="); |
57 | 63 | echo "EXPECTING SUCCESS ...";
|
58 | 64 | echo true == doesPayloadSatisfyRoleRequirement($payloadWithRole);
|
59 | 65 | echo "\n";
|
60 | 66 |
|
| 67 | +putenv("MOODLE_JWT_USE_ROLE_CHECK=true"); |
| 68 | +echo "EXPECTING FAILURE ..."; |
| 69 | +echo false == doesPayloadSatisfyRoleRequirement($payloadWithRole); |
| 70 | +echo "\n"; |
| 71 | + |
61 | 72 | echo "EXPECTING FAILURE ...";
|
62 | 73 | putenv("MOODLE_JWT_ROLE_FIELD=non-existent-property");
|
63 | 74 | putenv("MOODLE_JWT_REQUIRED_ROLE=admin");
|
|
0 commit comments