|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: dsmrt |
|
5
|
|
|
* Date: 1/23/18 |
|
6
|
|
|
* Time: 9:26 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\saml\core\models; |
|
10
|
|
|
|
|
11
|
|
|
use craft\base\Model; |
|
12
|
|
|
use craft\config\GeneralConfig; |
|
13
|
|
|
use craft\helpers\UrlHelper; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractSettings extends Model implements SettingsInterface |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
const ENDPOINT_PREFIX = 'sso'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $myType; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This is the system's entity id. Environmental Variables are allowed, ie, '$ENTITY_ID' (as a string) |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $entityId; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
public $endpointPrefix = self::ENDPOINT_PREFIX; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* This setting will destroy sessions when the Name Id matches a user with existing sessions. |
|
38
|
|
|
* A current user session doesn't have to exist, ie, `\Craft::$app->user->isGuest === true`. |
|
39
|
|
|
* |
|
40
|
|
|
* This can be useful if the LogoutRequest is sent over AJAX. |
|
41
|
|
|
* |
|
42
|
|
|
* Warning: this will delete all current sessions for the user |
|
43
|
|
|
* |
|
44
|
|
|
* @var bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public $sloDestroySpecifiedSessions = false; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
51
|
|
|
public function attributes() |
|
52
|
|
|
{ |
|
53
|
|
|
return array_merge( |
|
54
|
|
|
parent::attributes(), |
|
55
|
|
|
[ |
|
56
|
|
|
'entityId', |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/******************************************* |
|
62
|
|
|
* ENTITY ID |
|
63
|
|
|
*******************************************/ |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $entityId |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setEntityId($entityId) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->entityId = $entityId; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getEntityId() |
|
81
|
|
|
{ |
|
82
|
|
|
if (! $this->entityId) { |
|
83
|
|
|
$this->entityId = UrlHelper::baseUrl(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return \Craft::parseEnv($this->entityId); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return string |
|
91
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getEntityIdRaw() |
|
94
|
|
|
{ |
|
95
|
|
|
if (! $this->entityId) { |
|
96
|
|
|
$this->entityId = UrlHelper::baseUrl(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->entityId; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getEndpointPrefix() |
|
103
|
|
|
{ |
|
104
|
|
|
return \Craft::parseEnv($this->endpointPrefix); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
//@todo get rid of my type here. Should be back on the plugin |
|
108
|
|
|
/** |
|
109
|
|
|
* @param $myType |
|
110
|
|
|
* @return $this |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setMyType($myType) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->myType = $myType; |
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getMyType() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->myType; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getRemoteType() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->getMyType() === self::IDP ? self::SP : self::IDP; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function isIDP() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->getMyType() === self::IDP; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public function isSP() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->getMyType() === self::SP; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.