|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace flipbox\saml\core\helpers; |
|
5
|
|
|
|
|
6
|
|
|
use craft\helpers\UrlHelper as CraftUrlHelper; |
|
7
|
|
|
use craft\records\Site; |
|
8
|
|
|
use flipbox\saml\core\models\AbstractSettings; |
|
9
|
|
|
use flipbox\saml\core\records\AbstractProvider; |
|
10
|
|
|
|
|
11
|
|
|
class UrlHelper extends CraftUrlHelper |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this. |
|
15
|
|
|
* @see GeneralConfig::$loginPath |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
const LOGIN_ENDPOINT = 'login'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this. |
|
23
|
|
|
* @see GeneralConfig::$loginPath |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
const LOGOUT_ENDPOINT = 'logout'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this. |
|
31
|
|
|
* @see GeneralConfig::$loginPath |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
const LOGIN_REQUEST_ENDPOINT = 'login/request'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* This is the endpoint used to initiate logout. In this case, `logoutPath` cannot be used. |
|
39
|
|
|
* Point your logout button to this endpoint. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
const LOGOUT_REQUEST_ENDPOINT = 'logout/request'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param AbstractSettings $settings |
|
47
|
|
|
* @param string $endpoint |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function buildEndpointPath(AbstractSettings $settings, string $endpoint) |
|
51
|
|
|
{ |
|
52
|
|
|
return implode('/', [$settings->getEndpointPrefix(), $endpoint,]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $baseUrl |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected static function providerBaseUrl(string $baseUrl) |
|
60
|
|
|
{ |
|
61
|
|
|
$url = ''; |
|
62
|
|
|
// alias |
|
63
|
|
|
if (strpos($baseUrl, '@') === 0) { |
|
64
|
|
|
$url = \Craft::getAlias($baseUrl); |
|
65
|
|
|
} |
|
66
|
|
|
// env var |
|
67
|
|
|
if (strpos($baseUrl, '$') === 0) { |
|
68
|
|
|
$url = \Craft::parseEnv($baseUrl); |
|
69
|
|
|
} |
|
70
|
|
|
// url |
|
71
|
|
|
if (strpos($baseUrl, 'http') === 0) { |
|
72
|
|
|
$url = $baseUrl; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Trim last / |
|
76
|
|
|
return preg_replace('#/$#', '', $url); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param AbstractSettings $settings |
|
81
|
|
|
* @param string $endpoint |
|
82
|
|
|
* @param AbstractProvider $provider |
|
83
|
|
|
* @param bool $fullUrl |
|
84
|
|
|
* @return string |
|
85
|
|
|
* @throws \yii\base\Exception |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function buildEndpointUrl(AbstractSettings $settings, string $endpoint, AbstractProvider $provider, $fullUrl = true) |
|
88
|
|
|
{ |
|
89
|
|
|
$uri = implode('/', [$settings->getEndpointPrefix(), $endpoint, $provider->uid]); |
|
90
|
|
|
/** @var Site $site */ |
|
91
|
|
|
$site = $provider->site; |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
$endpointUrl = $fullUrl ? implode('/', [ |
|
96
|
|
|
static::providerBaseUrl( |
|
97
|
|
|
$site ? (string)$site->baseUrl : UrlHelper::baseUrl() |
|
98
|
|
|
), |
|
99
|
|
|
$uri |
|
100
|
|
|
]) : "/" . $uri; |
|
101
|
|
|
|
|
102
|
|
|
return $endpointUrl; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.