|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\attribute_check; |
| 6 | + |
| 7 | +class AttributeCheck |
| 8 | +{ |
| 9 | + public const MANDATORY_ONE = 'MANDATORY_AT_LEAST_ONE'; |
| 10 | + |
| 11 | + public const MANDATORY_ALL = 'MANDATORY_ALL'; |
| 12 | + |
| 13 | + public const OPTIONAL = 'OPTIONAL'; |
| 14 | + |
| 15 | + public const OK = 'OK'; |
| 16 | + |
| 17 | + public const NOT_OK = 'NOT_OK'; |
| 18 | + |
| 19 | + public static function handleAttributesGroup($t, $conf, $attributes): string |
| 20 | + { |
| 21 | + $translates = $conf['translates']; |
| 22 | + $type = $conf['type']; |
| 23 | + |
| 24 | + if (! in_array($type, [self::MANDATORY_ONE, self::MANDATORY_ALL, self::OPTIONAL], true)) { |
| 25 | + throw new \SimpleSAML\Error\Exception('AttributeSP: Bad type!'); |
| 26 | + } |
| 27 | + |
| 28 | + $title = self::translate($t, $translates, 'title'); |
| 29 | + $description = self::translate($t, $translates, 'description'); |
| 30 | + |
| 31 | + $group_attributes = $conf['attribute_list']; |
| 32 | + if (! is_array($group_attributes)) { |
| 33 | + throw new \SimpleSAML\Error\Exception('Attributes must be an array!'); |
| 34 | + } |
| 35 | + |
| 36 | + $result = self::getGroupResult($type, $group_attributes, $attributes); |
| 37 | + |
| 38 | + $resultTitle = self::translate($t, $translates[$result], 'title'); |
| 39 | + $resultDescription = self::translate($t, $translates[$result], 'description'); |
| 40 | + |
| 41 | + return "<div class='alert " . self::getAlertClass( |
| 42 | + $type, |
| 43 | + $result |
| 44 | + ) . "'><strong>" . $title . '</strong> - ' . $resultTitle . '<br/><br/>' . $resultDescription . '</div>'; |
| 45 | + } |
| 46 | + |
| 47 | + private static function translate($t, $translates, $key): string |
| 48 | + { |
| 49 | + if ($translates === null) { |
| 50 | + throw new \SimpleSAML\Error\Exception('Translation configuration cannot be null!'); |
| 51 | + } |
| 52 | + |
| 53 | + if (isset($translates[$key])) { |
| 54 | + return $t->getTranslation($translates[$key]); |
| 55 | + } |
| 56 | + |
| 57 | + return ''; |
| 58 | + } |
| 59 | + |
| 60 | + private static function getGroupResult($type, array $group_attributes, $attributes): string |
| 61 | + { |
| 62 | + if (in_array($type, [self::MANDATORY_ONE, self::OPTIONAL], true)) { |
| 63 | + foreach ($group_attributes as $item) { |
| 64 | + if (is_string($item)) { |
| 65 | + if (array_key_exists($item, $attributes)) { |
| 66 | + return self::OK; |
| 67 | + } |
| 68 | + } elseif (is_array($item)) { |
| 69 | + foreach ($item as $listItem) { |
| 70 | + if (! array_key_exists($item, $attributes)) { |
| 71 | + return self::NOT_OK; |
| 72 | + } |
| 73 | + } |
| 74 | + return self::OK; |
| 75 | + } |
| 76 | + } |
| 77 | + return self::NOT_OK; |
| 78 | + } |
| 79 | + foreach ($group_attributes as $item) { |
| 80 | + if (is_string($item)) { |
| 81 | + if (! array_key_exists($item, $attributes)) { |
| 82 | + return self::NOT_OK; |
| 83 | + } |
| 84 | + } elseif (is_array($item)) { |
| 85 | + foreach ($item as $listItem) { |
| 86 | + if (! array_key_exists($item, $attributes)) { |
| 87 | + return self::NOT_OK; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return self::OK; |
| 93 | + } |
| 94 | + |
| 95 | + private static function getAlertClass($type, $result): string |
| 96 | + { |
| 97 | + if (in_array($type, [self::MANDATORY_ONE, self::MANDATORY_ALL], true)) { |
| 98 | + if ($result === self::OK) { |
| 99 | + return 'alert-success'; |
| 100 | + } |
| 101 | + return 'alert-danger'; |
| 102 | + } |
| 103 | + if ($result === self::OK) { |
| 104 | + return 'alert-success'; |
| 105 | + } |
| 106 | + return 'alert-warning'; |
| 107 | + } |
| 108 | +} |
0 commit comments