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
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $entityId; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $endpointPrefix = self::ENDPOINT_PREFIX; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This setting will destroy sessions when the Name Id matches a user with existing sessions. |
37
|
|
|
* A current user session doesn't have to exist, ie, `\Craft::$app->user->isGuest === true`. |
38
|
|
|
* |
39
|
|
|
* This can be useful if the LogoutRequest is sent over AJAX. |
40
|
|
|
* |
41
|
|
|
* Warning: this will delete all current sessions for the user |
42
|
|
|
* |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
public $sloDestroySpecifiedSessions = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function attributes() |
51
|
|
|
{ |
52
|
|
|
return array_merge( |
53
|
|
|
parent::attributes(), |
54
|
|
|
[ |
55
|
|
|
'entityId', |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/******************************************* |
61
|
|
|
* ENTITY ID |
62
|
|
|
*******************************************/ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $entityId |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function setEntityId($entityId) |
69
|
|
|
{ |
70
|
|
|
$this->entityId = $entityId; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
* @throws \craft\errors\SiteNotFoundException |
78
|
|
|
*/ |
79
|
|
|
public function getEntityId() |
80
|
|
|
{ |
81
|
|
|
if (! $this->entityId) { |
82
|
|
|
$this->entityId = UrlHelper::baseUrl(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return \Craft::parseEnv($this->entityId); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
* @throws \craft\errors\SiteNotFoundException |
91
|
|
|
*/ |
92
|
|
|
public function getEntityIdRaw() |
93
|
|
|
{ |
94
|
|
|
if (! $this->entityId) { |
95
|
|
|
$this->entityId = UrlHelper::baseUrl(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->entityId; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getEndpointPrefix() |
102
|
|
|
{ |
103
|
|
|
return \Craft::parseEnv($this->endpointPrefix); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
//@todo get rid of my type here. Should be back on the plugin |
107
|
|
|
/** |
108
|
|
|
* @param $myType |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function setMyType($myType) |
112
|
|
|
{ |
113
|
|
|
$this->myType = $myType; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getMyType() |
121
|
|
|
{ |
122
|
|
|
return $this->myType; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getRemoteType() |
129
|
|
|
{ |
130
|
|
|
return $this->getMyType() === self::IDP ? self::SP : self::IDP; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function isIDP() |
137
|
|
|
{ |
138
|
|
|
return $this->getMyType() === self::IDP; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function isSP() |
145
|
|
|
{ |
146
|
|
|
return $this->getMyType() === self::SP; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.