Skip to content

Commit a3056b9

Browse files
committed
Add DecisionType-type
1 parent b58825e commit a3056b9

4 files changed

Lines changed: 129 additions & 11 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML11\Type;
6+
7+
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML11\XML\saml\DecisionTypeEnum;
9+
use SimpleSAML\XML\Exception\SchemaViolationException;
10+
use SimpleSAML\XML\Type\StringValue;
11+
12+
use function array_column;
13+
14+
/**
15+
* @package simplesamlphp/saml11
16+
*/
17+
class DecisionTypeValue extends StringValue
18+
{
19+
/** @var string */
20+
public const SCHEMA_TYPE = 'decisionType';
21+
22+
23+
/**
24+
* Validate the value.
25+
*
26+
* @param string $value The value
27+
* @throws \Exception on failure
28+
* @return void
29+
*/
30+
protected function validateValue(string $value): void
31+
{
32+
Assert::oneOf(
33+
$this->sanitizeValue($value),
34+
array_column(DecisionTypeEnum::cases(), 'value'),
35+
SchemaViolationException::class,
36+
);
37+
}
38+
39+
40+
/**
41+
* @param \SimpleSAML\XSD\XML\xsd\DecisionTypeEnum $value
42+
* @return static
43+
*/
44+
public static function fromEnum(DecisionTypeEnum $value): static
45+
{
46+
return new static($value->value);
47+
}
48+
49+
50+
/**
51+
* @return \SimpleSAML\XSD\XML\xsd\DecisionTypeEnum $value
52+
*/
53+
public function toEnum(): DecisionTypeEnum
54+
{
55+
return DecisionTypeEnum::from($this->getValue());
56+
}
57+
}

src/SAML11/XML/saml/AbstractAuthorizationDecisionStatementType.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
9-
use SimpleSAML\SAML11\Type\SAMLStringValue;
9+
use SimpleSAML\SAML11\Type\{DecisionTypeValue, SAMLStringValue};
1010
use SimpleSAML\XML\Exception\{
1111
InvalidDOMElementException,
1212
MissingElementException,
@@ -28,7 +28,7 @@ abstract class AbstractAuthorizationDecisionStatementType extends AbstractSubjec
2828
* Initialize a saml:AuthorizationDecisionStatementType from scratch
2929
*
3030
* @param \SimpleSAML\XML\Type\AnyURIValue $resource
31-
* @param \SimpleSAML\SAML11\XML\saml\DecisionTypeEnum $decision
31+
* @param \SimpleSAML\SAML11\Type\DecisionTypeValue $decision
3232
* @param \SimpleSAML\SAML11\XML\saml\Subject $subject
3333
* @param array<\SimpleSAML\SAML11\XML\saml\Action> $action
3434
* @param \SimpleSAML\SAML11\XML\saml\Evidence|null $evidence
@@ -37,7 +37,7 @@ final public function __construct(
3737
Subject $subject,
3838
// Uses the base AnyURIValue because the SAML specification allows this attribute to be an empty string
3939
protected AnyURIValue $resource,
40-
protected DecisionTypeEnum $decision,
40+
protected DecisionTypeValue $decision,
4141
protected array $action = [],
4242
protected ?Evidence $evidence = null,
4343
) {
@@ -62,9 +62,9 @@ public function getResource(): AnyURIValue
6262
/**
6363
* Collect the value of the decision-property
6464
*
65-
* @return \SimpleSAML\SAML11\XML\saml\DecisionTypeEnum
65+
* @return \SimpleSAML\SAML11\Type\DecisionTypeValue
6666
*/
67-
public function getDecision(): DecisionTypeEnum
67+
public function getDecision(): DecisionTypeValue
6868
{
6969
return $this->decision;
7070
}
@@ -116,9 +116,7 @@ public static function fromXML(DOMElement $xml): static
116116
return new static(
117117
array_pop($subject),
118118
self::getAttribute($xml, 'Resource', AnyURIValue::class),
119-
DecisionTypeEnum::from(
120-
strval(self::getAttribute($xml, 'Decision', SAMLStringValue::class)),
121-
),
119+
self::getAttribute($xml, 'Decision', DecisionTypeValue::class),
122120
Action::getChildrenOfClass($xml),
123121
array_pop($evidence),
124122
);
@@ -137,7 +135,7 @@ public function toXML(?DOMElement $parent = null): DOMElement
137135
$e = parent::toXML($parent);
138136

139137
$e->setAttribute('Resource', strval($this->getResource()));
140-
$e->setAttribute('Decision', $this->getDecision()->value);
138+
$e->setAttribute('Decision', strval($this->getDecision()));
141139

142140
foreach ($this->getAction() as $action) {
143141
$action->toXML($e);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\SAML11\Type;
6+
7+
use PHPUnit\Framework\Attributes\{CoversClass, DataProvider, DependsOnClass};
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\SAML11\Type\DecisionTypeValue;
10+
use SimpleSAML\SAML11\XML\saml\DecisionTypeEnum;
11+
use SimpleSAML\XML\Exception\SchemaViolationException;
12+
13+
/**
14+
* Class \SimpleSAML\Test\SAML11\Type\DecisionTypeValueTest
15+
*
16+
* @package simplesamlphp/saml11
17+
*/
18+
#[CoversClass(DecisionTypeValue::class)]
19+
final class DecisionTypeValueTest extends TestCase
20+
{
21+
/**
22+
* @param string $decisionType
23+
* @param bool $expected
24+
*/
25+
#[DataProvider('provideDecisionType')]
26+
public function testDecisionTypeValue(string $decisionType, bool $shouldPass): void
27+
{
28+
try {
29+
DecisionTypeValue::fromString($decisionType);
30+
$this->assertTrue($shouldPass);
31+
} catch (SchemaViolationException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* Test helpers
39+
*/
40+
public function testHelpers(): void
41+
{
42+
$x = DecisionTypeValue::fromEnum(DecisionTypeEnum::Deny);
43+
$this->assertEquals(DecisionTypeEnum::Deny, $x->toEnum());
44+
45+
$y = DecisionTypeValue::fromString('Deny');
46+
$this->assertEquals(DecisionTypeEnum::Deny, $y->toEnum());
47+
}
48+
49+
50+
/**
51+
* @return array<string, array{0: string, 1: string}>
52+
*/
53+
public static function provideDecisionType(): array
54+
{
55+
return [
56+
'deny' => ['Deny', true],
57+
'indeterminate' => ['Indeterminate', true],
58+
'permit' => ['Permit', true],
59+
'undefined' => ['undefined', false],
60+
'empty' => ['', false],
61+
];
62+
}
63+
}

tests/src/SAML11/XML/saml/AuthorizationDecisionStatementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PHPUnit\Framework\Attributes\{CoversClass, Group};
99
use PHPUnit\Framework\TestCase;
1010
use SimpleSAML\SAML11\Compat\{AbstractContainer, ContainerSingleton};
11-
use SimpleSAML\SAML11\Type\SAMLAnyURIValue;
11+
use SimpleSAML\SAML11\Type\{DecisionTypeValue, SAMLAnyURIValue};
1212
use SimpleSAML\SAML11\XML\saml\{
1313
AbstractAuthorizationDecisionStatementType,
1414
AbstractSamlElement,
@@ -116,7 +116,7 @@ public function testMarshalling(): void
116116
$authzDecisionStatement = new AuthorizationDecisionStatement(
117117
$subject,
118118
SAMLAnyURIValue::fromString('urn:x-simplesamlphp:resource'),
119-
DecisionTypeEnum::Permit,
119+
DecisionTypeValue::fromEnum(DecisionTypeEnum::Permit),
120120
[$action],
121121
$evidence,
122122
);

0 commit comments

Comments
 (0)