| Conditions | 7 |
| Paths | 7 |
| Total Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 32 | public static function hasRoles($user, $roles, $or = true) { |
||
| 33 | |||
| 34 | // Check the user. |
||
| 35 | if (null === $user || false === ($user instanceof UserInterface)) { |
||
| 36 | return false; |
||
| 37 | } |
||
| 38 | |||
| 39 | // Check the roles. |
||
| 40 | if (false === is_array($roles)) { |
||
| 41 | $roles = [$roles]; |
||
| 42 | } |
||
| 43 | |||
| 44 | // Initialize the result. |
||
| 45 | $result = 1 <= count($roles); |
||
| 46 | |||
| 47 | // Handle each role. |
||
| 48 | foreach ($roles as $role) { |
||
| 49 | $buffer = in_array($role, $user->getRoles()); |
||
| 50 | if (true === $buffer && true === $or) { |
||
| 51 | $result = $buffer; |
||
| 52 | break; |
||
| 53 | } |
||
| 54 | $result &= $buffer; |
||
| 55 | } |
||
| 56 | |||
| 57 | // Return the result. |
||
| 58 | return boolval($result); |
||
| 59 | } |
||
| 60 | |||
| 62 |