/
RealMeFederatedIdentity.php
209 lines (181 loc) · 8.66 KB
/
RealMeFederatedIdentity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Class RealMeFederatedIdentity
*
* Contains data to describe an identity, verified by RealMe. Provides simpler access to identity information, rather
* than having to parse XML via {@link DOMDocument} or similar.
*
* All public methods return individual elements from the federated identity.
*
* Standard usage:
* Injector::inst()->get('RealMeService')->enforceLogin(); // Enforce login and ensure auth data exists
* $identity = Injector::inst()->get('RealMeService')->getAuthData()->getIdentity();
*
* Notes:
* - We can't store the original DOMDocument as it's not possible to properly serialize and unserialize this such that
* it can be stored in session. Therefore, during object instantiation, we parse the XML, and store individual details
* directly against properties.
*
* - See this object's constructor for the XML / DOMDocument object expected to be passed during instantiation.
*/
class RealMeFederatedIdentity extends ViewableData
{
/**
* @var string The FIT (Federated Identity Tag) for this identity. This is the unique string that identifies an
* individual, and should generally be mapped one-to-one with a {@link Member} object
*/
private $nameId;
/**
* @var string The given first name(s) of the federated identity returned by RealMe.
*/
public $FirstName;
/**
* @var string The given middle name(s) of the federated identity returned by RealMe.
*/
public $MiddleName;
/**
* @var string The given last name of the federated identity returned by RealMe.
*/
public $LastName;
/**
* @var string The gender of the federated identity returned by RealMe. Will be one of 'M', 'F', possibly 'U' or 'O'
* (messaging specs are unclear).
*/
public $Gender;
/**
* @var DOMNodeList Undocumented in RealMe messaging spec, generally describes the quality of birth info based
* presumably on the source.
*/
public $BirthInfoQuality;
/**
* @var string The birth year of the federated identity returned by RealMe, e.g. 1993, 1954, 2015.
* Probably better to use {@link getDateOfBirth()} which will return an {@link SS_Datetime} object.
*/
public $BirthYear;
/**
* @var string The birth month of the federated identity returned by RealMe, e.g. 05 (May).
* Probably better to use {@link getDateOfBirth()} which will return an {@link SS_Datetime} object.
*/
public $BirthMonth;
/**
* @var string The birth day of the federated identity returned by RealMe, e.g. 05 (5th day of the month).
* Probably better to use {@link getDateOfBirth()} which will return an {@link SS_Datetime} object.
*/
public $BirthDay;
/**
* @var string Undocumented in RealMe messaging spec, generally describes the quality of birthplace info based
* presumably on the source.
*/
public $BirthPlaceQuality;
/**
* @var string The country of birth for the given federated identity returned by RealMe.
*/
public $BirthPlaceCountry;
/**
* @var string The birthplace 'locality' of the federated identity returned by RealMe, e.g. 'Wellington', 'Unknown'
*/
public $BirthPlaceLocality;
/**
* Constructor that sets the expected federated identity details based on a provided DOMDocument. The expected XML
* structure for the DOMDocument is the following:
*
* <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
* <ns1:Party xmlns:ns1="urn:oasis:names:tc:ciq:xpil:3" xmlns:ns2="urn:oasis:names:tc:ciq:ct:3" xmlns:ns3="urn:oasis:names:tc:ciq:xnl:3" xmlns:ns4="http://www.w3.org/1999/xlink" xmlns:ns5="urn:oasis:names:tc:ciq:xal:3">
* <ns1:PartyName>
* <ns3:PersonName>
* <ns3:NameElement ns3:ElementType="FirstName">Edmund</ns3:NameElement>
* <ns3:NameElement ns3:ElementType="MiddleName">Percival</ns3:NameElement>
* <ns3:NameElement ns3:ElementType="LastName">Hillary</ns3:NameElement>
* </ns3:PersonName>
* </ns1:PartyName>
* <ns1:PersonInfo ns1:Gender="M"/>
* <ns1:BirthInfo ns2:DataQualityType="Valid">
* <ns1:BirthInfoElement ns1:Type="BirthYear">1919</ns1:BirthInfoElement>
* <ns1:BirthInfoElement ns1:Type="BirthMonth">07</ns1:BirthInfoElement>
* <ns1:BirthInfoElement ns1:Type="BirthDay">20</ns1:BirthInfoElement>
* <ns1:BirthPlaceDetails ns2:DataQualityType="Valid">
* <ns5:Country>
* <ns5:NameElement ns5:NameType="Name">New Zealand</ns5:NameElement>
* </ns5:Country>
* <ns5:Locality>
* <ns5:NameElement ns5:NameType="Name">Auckland</ns5:NameElement>
* </ns5:Locality>
* </ns1:BirthPlaceDetails>
* </ns1:BirthInfo>
* </ns1:Party>
*
* @param DOMDocument $identity
* @param string $nameId
*/
public function __construct(DOMDocument $identity, $nameId)
{
parent::__construct();
$this->nameId = $nameId;
$xpath = new DOMXPath($identity);
$xpath->registerNamespace('p', 'urn:oasis:names:tc:ciq:xpil:3');
$xpath->registerNamespace('dataQuality', 'urn:oasis:names:tc:ciq:ct:3');
$xpath->registerNamespace('n', 'urn:oasis:names:tc:ciq:xnl:3');
$xpath->registerNamespace('xlink', 'http://www.w3.org/1999/xlink');
$xpath->registerNamespace('addr', 'urn:oasis:names:tc:ciq:xal:3');
// Name elements
$this->FirstName = $this->getNodeValue($xpath, "/p:Party/p:PartyName/n:PersonName/n:NameElement[@n:ElementType='FirstName']");
$this->MiddleName = $this->getNodeValue($xpath, "/p:Party/p:PartyName/n:PersonName/n:NameElement[@n:ElementType='MiddleName']");
$this->LastName = $this->getNodeValue($xpath, "/p:Party/p:PartyName/n:PersonName/n:NameElement[@n:ElementType='LastName']");
// Gender
$this->Gender = $this->getNamedItemNodeValue($xpath, '/p:Party/p:PersonInfo[@p:Gender]', 'Gender');
// Birth info
$this->BirthInfoQuality = $xpath->query("/p:Party/p:BirthInfo[@dataQuality:DataQualityType]");
// Birth date
$this->BirthYear = $this->getNodeValue($xpath, "/p:Party/p:BirthInfo/p:BirthInfoElement[@p:Type='BirthYear']");
$this->BirthMonth = $this->getNodeValue($xpath, "/p:Party/p:BirthInfo/p:BirthInfoElement[@p:Type='BirthMonth']");
$this->BirthDay = $this->getNodeValue($xpath, "/p:Party/p:BirthInfo/p:BirthInfoElement[@p:Type='BirthDay']");
// Birth place
$this->BirthPlaceQuality = $this->getNamedItemNodeValue($xpath, '/p:Party/p:BirthInfo/p:BirthPlaceDetails[@dataQuality:DataQualityType]', 'DataQualityType');
$this->BirthPlaceCountry = $this->getNodeValue($xpath, "/p:Party/p:BirthInfo/p:BirthPlaceDetails/addr:Country/addr:NameElement[@addr:NameType='Name']");
$this->BirthPlaceLocality = $this->getNodeValue($xpath, "/p:Party/p:BirthInfo/p:BirthPlaceDetails/addr:Locality/addr:NameElement[@addr:NameType='Name']");
}
public function isValid()
{
return true;
}
public function getDateOfBirth()
{
if($this->BirthYear && $this->BirthMonth && $this->BirthDay) {
$value = sprintf('%d-%d-%d', $this->BirthYear, $this->BirthMonth, $this->BirthDay);
return DBField::create_field('SS_DateTime', $value);
} else {
return null;
}
}
/**
* @param DOMXPath $xpath The DOMXPath object to carry out the query on
* @param string $query The XPath query to find the relevant node
* @param string $namedAttr The named attribute to retrieve from the XPath query
* @return string|null Either the value from the named item, or null if no item exists
*/
private function getNamedItemNodeValue(DOMXPath $xpath, $query, $namedAttr)
{
$query = $xpath->query($query);
$value = null;
if($query->length > 0) {
$item = $query->item(0);
if($item->hasAttributes()) {
$value = $item->attributes->getNamedItem($namedAttr);
if(strlen($value->nodeValue) > 0) {
$value = $value->nodeValue;
}
}
}
return $value;
}
/**
* @param DOMXPath $xpath The DOMXPath object to carry out the query on
* @param string $query The XPath query to find the relevant node
* @return string|null Either the first matching node's value (there should only ever be one), or null if none found
*/
private function getNodeValue(DOMXPath $xpath, $query)
{
$query = $xpath->query($query);
return ($query->length > 0 ? $query->item(0)->nodeValue : null);
}
}