ValidateResponse::setTicket()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 32
rs 8.5806
cc 4
eloc 13
nc 6
nop 3
1
<?php
2
/**
3
 * CAS ticket validation response class.
4
 *
5
 * @version 1.2.0
6
 * @since 1.2.0
7
 */
8
9
namespace Cassava\CAS\Response;
10
11
use Cassava\CAS;
12
use Cassava\Options;
13
use Cassava\Plugin;
14
15
/**
16
 * Implements the CAS response for validation requests.
17
 *
18
 * @version 1.2.0
19
 */
20
class ValidateResponse extends BaseResponse {
21
22
	/**
23
	 * XML success response to a CAS 2.0 validation request.
24
	 *
25
	 * @param  \Cassava\CAS\Ticket $ticket              Validated ticket.
26
	 * @param  string              $proxyGrantingTicket Generated Proxy-Granting Ticket (PGT) to return.
27
	 * @param  array               $proxies             List of proxy URIs.
28
	 *
29
	 * @todo Throw exception on bad or no ticket.
30
	 */
31
	public function setTicket( CAS\Ticket $ticket, $proxyGrantingTicket = '', $proxies = array() ) {
32
33
		$this->response = $this->createElement( 'authenticationSuccess' );
34
35
		// Include login name:
36
37
		$this->response->appendChild( $this->createElement( 'user', $ticket->user->user_login ) );
38
39
		// Include user attributes:
40
41
		$this->setUserAttributes( $ticket );
42
43
		// Include Proxy-Granting Ticket in successful `/proxyValidate` responses:
44
45
		if ( $proxyGrantingTicket ) {
46
			$this->response->appendChild( $this->createElement(
47
				'proxyGrantingTicket', $proxyGrantingTicket ) );
48
		}
49
50
		// Include proxies in successful `/proxyValidate` responses:
51
52
		if ( ! empty( $proxies ) ) {
53
			$xmlProxies = $this->createElement( 'proxies' );
54
55
			foreach ($proxies as $proxy) {
56
				$xmlProxies->appendChild( $this->createElement(
57
					'proxy', $proxy ) );
58
			}
59
60
			$this->response->appendChild( $xmlProxies );
61
		}
62
	}
63
64
	/**
65
	 * Add user attributes to the response.
66
	 *
67
	 * @param CAS\Ticket $ticket Validated ticket.
68
	 *
69
	 * @uses \apply_filters()
70
	 */
71
	protected function setUserAttributes( CAS\Ticket $ticket ) {
72
		$attributeKeys = Options::get( 'attributes' );
73
		$attributes    = array();
74
75
		foreach ( $attributeKeys as $key ) {
76
			$attributes[ $key ] = implode( ',', (array) $ticket->user->get( $key ) );
77
		}
78
79
		/**
80
		 * Allows developers to change the list of (key, value) pairs before they're included
81
		 * in a `/serviceValidate` response.
82
		 *
83
		 * @param  array   $attributes List of attributes to output.
84
		 * @param  WP_User $user       Authenticated user.
85
		 */
86
		$attributes = \apply_filters( 'cas_server_validation_user_attributes', $attributes, $ticket->user );
87
88
		if ( ! is_array( $attributes ) || empty( $attributes ) ) {
89
			return;
90
		}
91
92
		$xmlAttributes = $this->createElement( 'attributes' );
93
94
		foreach ($attributes as $key => $value) {
95
			$xmlAttribute = $this->createElement( $key, $value );
96
			$xmlAttributes->appendChild( $xmlAttribute );
97
		}
98
99
		$this->response->appendChild( $xmlAttributes );
100
	}
101
}
102