Skip to content

Commit fb22e2e

Browse files
committed
Extend login policy with setters and test them
1 parent bf0b71e commit fb22e2e

File tree

3 files changed

+289
-17
lines changed

3 files changed

+289
-17
lines changed

src/LoginPolicy/LoginPolicy.php

+120-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace ActiveCollab\Authentication\LoginPolicy;
1010

11+
use InvalidArgumentException;
12+
1113
/**
1214
* @package ActiveCollab\Authentication\LoginPolicy
1315
*/
@@ -56,25 +58,27 @@ class LoginPolicy implements LoginPolicyInterface
5658
/**
5759
* LoginPolicy constructor.
5860
*
59-
* @param string $username_format
60-
* @param bool $remember_extends_session
61-
* @param bool $password_change_enabled
62-
* @param bool $password_recovery_enabled
63-
* @param string $external_login_url
64-
* @param string $external_logout_url
65-
* @param string $external_change_password_url
66-
* @param string $external_update_profile_url
61+
* @param string $username_format
62+
* @param bool $remember_extends_session
63+
* @param bool $password_change_enabled
64+
* @param bool $password_recovery_enabled
65+
* @param string|null $external_login_url
66+
* @param string|null $external_logout_url
67+
* @param string|null $external_change_password_url
68+
* @param string|null $external_update_profile_url
6769
*/
68-
public function __construct($username_format, $remember_extends_session, $password_change_enabled, $password_recovery_enabled, $external_login_url, $external_logout_url, $external_change_password_url, $external_update_profile_url)
70+
public function __construct($username_format = self::USERNAME_FORMAT_TEXT, $remember_extends_session = true, $password_change_enabled = true, $password_recovery_enabled = true, $external_login_url = null, $external_logout_url = null, $external_change_password_url = null, $external_update_profile_url = null)
6971
{
70-
$this->username_format = $username_format;
71-
$this->remember_extends_session = (bool) $remember_extends_session;
72-
$this->password_change_enabled = (bool) $password_change_enabled;
73-
$this->password_recovery_enabled = (bool) $password_recovery_enabled;
74-
$this->external_login_url = $external_login_url;
75-
$this->external_logout_url = $external_logout_url;
76-
$this->external_change_password_url = $external_change_password_url;
77-
$this->external_update_profile_url = $external_update_profile_url;
72+
$this->setUsernameFormat($username_format);
73+
74+
$this->setRememberExtendsSession($remember_extends_session);
75+
$this->setIsPasswordChangeEnabled($password_change_enabled);
76+
$this->setIsPasswordRecoveryEnabled($password_recovery_enabled);
77+
78+
$this->setExternalLoginUrl($external_login_url);
79+
$this->setExternalLogoutUrl($external_logout_url);
80+
$this->setExternalChangePasswordUrl($external_change_password_url);
81+
$this->setExternalUpdateProfileUrl($external_update_profile_url);
7882
}
7983

8084
/**
@@ -85,6 +89,20 @@ public function getUsernameFormat()
8589
return $this->username_format;
8690
}
8791

92+
/**
93+
* {@inheritdoc}
94+
*/
95+
public function &setUsernameFormat($value)
96+
{
97+
if (in_array($value, self::VALID_USERNAME_FORMATS)) {
98+
$this->username_format = $value;
99+
} else {
100+
throw new InvalidArgumentException('Username format is not valid');
101+
}
102+
103+
return $this;
104+
}
105+
88106
/**
89107
* {@inheritdoc}
90108
*/
@@ -93,6 +111,16 @@ public function rememberExtendsSession()
93111
return $this->remember_extends_session;
94112
}
95113

114+
/**
115+
* {@inheritdoc}
116+
*/
117+
public function &setRememberExtendsSession($value)
118+
{
119+
$this->remember_extends_session = (bool) $value;
120+
121+
return $this;
122+
}
123+
96124
/**
97125
* {@inheritdoc}
98126
*/
@@ -101,6 +129,16 @@ public function isPasswordChangeEnabled()
101129
return $this->password_change_enabled;
102130
}
103131

132+
/**
133+
* {@inheritdoc}
134+
*/
135+
public function &setIsPasswordChangeEnabled($value)
136+
{
137+
$this->password_change_enabled = (bool) $value;
138+
139+
return $this;
140+
}
141+
104142
/**
105143
* {@inheritdoc}
106144
*/
@@ -109,6 +147,16 @@ public function isPasswordRecoveryEnabled()
109147
return $this->password_recovery_enabled;
110148
}
111149

150+
/**
151+
* {@inheritdoc}
152+
*/
153+
public function &setIsPasswordRecoveryEnabled($value)
154+
{
155+
$this->password_recovery_enabled = (bool) $value;
156+
157+
return $this;
158+
}
159+
112160
/**
113161
* {@inheritdoc}
114162
*/
@@ -117,6 +165,16 @@ public function getExternalLoginUrl()
117165
return $this->external_login_url;
118166
}
119167

168+
/**
169+
* {@inheritdoc}
170+
*/
171+
public function &setExternalLoginUrl($value)
172+
{
173+
$this->external_login_url = $this->getValidExternalUrlValue($value);
174+
175+
return $this;
176+
}
177+
120178
/**
121179
* {@inheritdoc}
122180
*/
@@ -125,6 +183,16 @@ public function getExternalLogoutUrl()
125183
return $this->external_logout_url;
126184
}
127185

186+
/**
187+
* {@inheritdoc}
188+
*/
189+
public function &setExternalLogoutUrl($value)
190+
{
191+
$this->external_logout_url = $this->getValidExternalUrlValue($value);
192+
193+
return $this;
194+
}
195+
128196
/**
129197
* {@inheritdoc}
130198
*/
@@ -133,6 +201,16 @@ public function getExternalChangePasswordUrl()
133201
return $this->external_change_password_url;
134202
}
135203

204+
/**
205+
* {@inheritdoc}
206+
*/
207+
public function &setExternalChangePasswordUrl($value)
208+
{
209+
$this->external_change_password_url = $this->getValidExternalUrlValue($value);
210+
211+
return $this;
212+
}
213+
136214
/**
137215
* {@inheritdoc}
138216
*/
@@ -141,6 +219,31 @@ public function getExternalUpdateProfileUrl()
141219
return $this->external_update_profile_url;
142220
}
143221

222+
/**
223+
* {@inheritdoc}
224+
*/
225+
public function &setExternalUpdateProfileUrl($value)
226+
{
227+
$this->external_update_profile_url = $this->getValidExternalUrlValue($value);
228+
229+
return $this;
230+
}
231+
232+
/**
233+
* Validate and return acceptable URL value.
234+
*
235+
* @param string|null $url
236+
* @return string|null
237+
*/
238+
private function getValidExternalUrlValue($url)
239+
{
240+
if ($url === null || (is_string($url) && filter_var($url, FILTER_VALIDATE_URL))) {
241+
return $url;
242+
}
243+
244+
throw new InvalidArgumentException('URL is not valid');
245+
}
246+
144247
/**
145248
* {@inheritdoc}
146249
*/

src/LoginPolicy/LoginPolicyInterface.php

+66
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,125 @@ interface LoginPolicyInterface extends JsonSerializable
1818
const USERNAME_FORMAT_TEXT = 'text';
1919
const USERNAME_FORMAT_EMAIL = 'email';
2020

21+
const VALID_USERNAME_FORMATS = [self::USERNAME_FORMAT_TEXT, self::USERNAME_FORMAT_EMAIL];
22+
2123
/**
2224
* Return username format.
2325
*
2426
* @return string
2527
*/
2628
public function getUsernameFormat();
2729

30+
/**
31+
* Set username format.
32+
*
33+
* @param string $value
34+
* @return $this
35+
*/
36+
public function &setUsernameFormat($value);
37+
2838
/**
2939
* Enable Remember Me feature to support extended sessions.
3040
*
3141
* @return bool
3242
*/
3343
public function rememberExtendsSession();
3444

45+
/**
46+
* Set value of Remember Me flag.
47+
*
48+
* @param bool $value
49+
* @return $this
50+
*/
51+
public function &setRememberExtendsSession($value);
52+
3553
/**
3654
* Password change is supported by this authentication adapter.
3755
*
3856
* @return bool
3957
*/
4058
public function isPasswordChangeEnabled();
4159

60+
/**
61+
* Set value of is password enabled flag.
62+
*
63+
* @param bool $value
64+
* @return $this
65+
*/
66+
public function &setIsPasswordChangeEnabled($value);
67+
4268
/**
4369
* Return true if this authentication adapter supports password recovery.
4470
*
4571
* @return bool
4672
*/
4773
public function isPasswordRecoveryEnabled();
4874

75+
/**
76+
* Set value of is password recovery enabled flag.
77+
*
78+
* @param bool $value
79+
* @return $this
80+
*/
81+
public function &setIsPasswordRecoveryEnabled($value);
82+
4983
/**
5084
* Get login URL.
5185
*
5286
* @return string|null
5387
*/
5488
public function getExternalLoginUrl();
5589

90+
/**
91+
* Set external login URL.
92+
*
93+
* @param string $value
94+
* @return $this
95+
*/
96+
public function &setExternalLoginUrl($value);
97+
5698
/**
5799
* Get logout URL.
58100
*
59101
* @return string
60102
*/
61103
public function getExternalLogoutUrl();
62104

105+
/**
106+
* Set external log out URL.
107+
*
108+
* @param string $value
109+
* @return $this
110+
*/
111+
public function &setExternalLogoutUrl($value);
112+
63113
/**
64114
* Get change password URL.
65115
*
66116
* @return string|null
67117
*/
68118
public function getExternalChangePasswordUrl();
69119

120+
/**
121+
* Set external change password URL.
122+
*
123+
* @param string $value
124+
* @return $this
125+
*/
126+
public function &setExternalChangePasswordUrl($value);
127+
70128
/**
71129
* Get update profile URL.
72130
*
73131
* @return string|null
74132
*/
75133
public function getExternalUpdateProfileUrl();
134+
135+
/**
136+
* Set external update profile URL.
137+
*
138+
* @param string $value
139+
* @return $this
140+
*/
141+
public function &setExternalUpdateProfileUrl($value);
76142
}

0 commit comments

Comments
 (0)