Skip to content

Commit

Permalink
Revert "refactor: use ::class keyword in config()"
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jul 5, 2023
1 parent 7f4503b commit e53c7c9
Show file tree
Hide file tree
Showing 23 changed files with 47 additions and 95 deletions.
7 changes: 3 additions & 4 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use CodeIgniter\Shield\Authentication\Authentication;
use CodeIgniter\Shield\Authentication\AuthenticationException;
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
use CodeIgniter\Shield\Config\Auth as AuthConfig;
use CodeIgniter\Shield\Config\AuthRoutes;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Models\UserModel;

Expand Down Expand Up @@ -108,7 +106,7 @@ public function authenticate(array $credentials): Result
*/
public function routes(RouteCollection &$routes, array $config = []): void
{
$authRoutes = config(AuthRoutes::class)->routes;
$authRoutes = config('AuthRoutes')->routes;

$routes->group('/', ['namespace' => 'CodeIgniter\Shield\Controllers'], static function (RouteCollection $routes) use ($authRoutes, $config): void {
foreach ($authRoutes as $name => $row) {
Expand All @@ -135,7 +133,8 @@ public function getProvider(): UserModel
return $this->userProvider;
}

$config = config(AuthConfig::class);
/** @var \CodeIgniter\Shield\Config\Auth $config */
$config = config('Auth');

if (! property_exists($config, 'userProvider')) {
throw AuthenticationException::forUnknownUserProvider();
Expand Down
3 changes: 1 addition & 2 deletions src/Authentication/Actions/Email2FA.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Entities\UserIdentity;
use CodeIgniter\Shield\Exceptions\RuntimeException;
Expand Down Expand Up @@ -121,7 +120,7 @@ public function verify(IncomingRequest $request)
}

// Get our login redirect url
return redirect()->to(config(Auth::class)->loginRedirect());
return redirect()->to(config('Auth')->loginRedirect());
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Authentication/Actions/EmailActivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use CodeIgniter\HTTP\Response;
use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Entities\UserIdentity;
use CodeIgniter\Shield\Exceptions\LogicException;
Expand Down Expand Up @@ -115,7 +114,7 @@ public function verify(IncomingRequest $request)
$user->activate();

// Success!
return redirect()->to(config(Auth::class)->registerRedirect())
return redirect()->to(config('Auth')->registerRedirect())
->with('message', lang('Auth.registerSuccess'));
}

Expand Down
9 changes: 4 additions & 5 deletions src/Authentication/Authenticators/AccessTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authentication\AuthenticationException;
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
use CodeIgniter\Shield\Models\TokenLoginModel;
Expand Down Expand Up @@ -105,7 +104,7 @@ public function check(array $credentials): Result
if (! array_key_exists('token', $credentials) || empty($credentials['token'])) {
return new Result([
'success' => false,
'reason' => lang('Auth.noToken', [config(Auth::class)->authenticatorHeader['tokens']]),
'reason' => lang('Auth.noToken', [config('Auth')->authenticatorHeader['tokens']]),
]);
}

Expand All @@ -130,7 +129,7 @@ public function check(array $credentials): Result
// Hasn't been used in a long time
if (
$token->last_used_at
&& $token->last_used_at->isBefore(Time::now()->subSeconds(config(Auth::class)->unusedTokenLifetime))
&& $token->last_used_at->isBefore(Time::now()->subSeconds(config('Auth')->unusedTokenLifetime))
) {
return new Result([
'success' => false,
Expand Down Expand Up @@ -169,7 +168,7 @@ public function loggedIn(): bool
$request = service('request');

return $this->attempt([
'token' => $request->getHeaderLine(config(Auth::class)->authenticatorHeader['tokens']),
'token' => $request->getHeaderLine(config('Auth')->authenticatorHeader['tokens']),
])->isOK();
}

Expand Down Expand Up @@ -227,7 +226,7 @@ public function getBearerToken(): ?string
/** @var IncomingRequest $request */
$request = service('request');

$header = $request->getHeaderLine(config(Auth::class)->authenticatorHeader['tokens']);
$header = $request->getHeaderLine(config('Auth')->authenticatorHeader['tokens']);

if (empty($header)) {
return null;
Expand Down
8 changes: 4 additions & 4 deletions src/Authentication/Authenticators/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use CodeIgniter\Shield\Authentication\AuthenticationException;
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
use CodeIgniter\Shield\Authentication\Passwords;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Entities\UserIdentity;
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
Expand Down Expand Up @@ -90,7 +89,8 @@ public function __construct(UserModel $provider)
*/
private function checkSecurityConfig(): void
{
$securityConfig = config(Security::class);
/** @var Security $securityConfig */
$securityConfig = config('Security');

if ($securityConfig->csrfProtection === 'cookie') {
throw new SecurityException(
Expand Down Expand Up @@ -275,8 +275,8 @@ private function recordLoginAttempt(
): void {
// Determine the type of ID we're using.
// Standard fields would be email, username,
// but any column within config(Auth::class)->validFields can be used.
$field = array_intersect(config(Auth::class)->validFields ?? [], array_keys($credentials));
// but any column within config('Auth')->validFields can be used.
$field = array_intersect(config('Auth')->validFields ?? [], array_keys($credentials));

if (count($field) !== 1) {
throw new InvalidArgumentException('Invalid credentials passed to recordLoginAttempt.');
Expand Down
2 changes: 1 addition & 1 deletion src/Authentication/Passwords.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function check(string $password, ?User $user = null): Result
*/
public static function getMaxLengthRule(): string
{
if (config(Auth::class)->hashAlgorithm === PASSWORD_BCRYPT) {
if (config('Auth')->hashAlgorithm === PASSWORD_BCRYPT) {
return 'max_byte[72]';
}

Expand Down
3 changes: 1 addition & 2 deletions src/Authentication/Passwords/ValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\Shield\Authentication\Passwords;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Entities\User;

/**
Expand Down Expand Up @@ -103,7 +102,7 @@ protected function buildUserFromData(array $data = []): User
*/
protected function prepareValidFields(): array
{
$config = config(Auth::class);
$config = config('Auth');
$fields = array_merge($config->validFields, $config->personalFields);
$fields[] = 'password';

Expand Down
7 changes: 3 additions & 4 deletions src/Authorization/Traits/Authorizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authorization\AuthorizationException;
use CodeIgniter\Shield\Config\AuthGroups;
use CodeIgniter\Shield\Exceptions\LogicException;
use CodeIgniter\Shield\Models\GroupModel;
use CodeIgniter\Shield\Models\PermissionModel;
Expand Down Expand Up @@ -258,7 +257,7 @@ public function can(string $permission): bool

$matrix = function_exists('setting')
? setting('AuthGroups.matrix')
: config(AuthGroups::class)->matrix;
: config('AuthGroups')->matrix;

foreach ($this->groupCache as $group) {
// Check exact match
Expand Down Expand Up @@ -394,7 +393,7 @@ private function getConfigGroups(): array
{
return function_exists('setting')
? array_keys(setting('AuthGroups.groups'))
: array_keys(config(AuthGroups::class)->groups);
: array_keys(config('AuthGroups')->groups);
}

/**
Expand All @@ -404,6 +403,6 @@ private function getConfigPermissions(): array
{
return function_exists('setting')
? array_keys(setting('AuthGroups.permissions'))
: array_keys(config(AuthGroups::class)->permissions);
: array_keys(config('AuthGroups')->permissions);
}
}
5 changes: 2 additions & 3 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use CodeIgniter\Shield\Authentication\Authentication;
use CodeIgniter\Shield\Authentication\JWTManager;
use CodeIgniter\Shield\Authentication\Passwords;
use CodeIgniter\Shield\Config\Auth as AuthConfig;
use Config\Services as BaseService;

class Services extends BaseService
Expand All @@ -22,7 +21,7 @@ public static function auth(bool $getShared = true): Auth
return self::getSharedInstance('auth');
}

$config = config(AuthConfig::class);
$config = config('Auth');

return new Auth(new Authentication($config));
}
Expand All @@ -36,7 +35,7 @@ public static function passwords(bool $getShared = true): Passwords
return self::getSharedInstance('passwords');
}

return new Passwords(config(AuthConfig::class));
return new Passwords(config('Auth'));
}

/**
Expand Down
12 changes: 5 additions & 7 deletions src/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Authentication\Passwords;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Config\AuthSession;
use CodeIgniter\Shield\Traits\Viewable;

class LoginController extends BaseController
Expand All @@ -26,7 +24,7 @@ class LoginController extends BaseController
public function loginView()
{
if (auth()->loggedIn()) {
return redirect()->to(config(Auth::class)->loginRedirect());
return redirect()->to(config('Auth')->loginRedirect());
}

/** @var Session $authenticator */
Expand Down Expand Up @@ -73,7 +71,7 @@ public function loginAction(): RedirectResponse
return redirect()->route('auth-action-show')->withCookies();
}

return redirect()->to(config(Auth::class)->loginRedirect())->withCookies();
return redirect()->to(config('Auth')->loginRedirect())->withCookies();
}

/**
Expand All @@ -87,11 +85,11 @@ protected function getValidationRules(): array
return setting('Validation.login') ?? [
// 'username' => [
// 'label' => 'Auth.username',
// 'rules' => config(AuthSession::class)->usernameValidationRules,
// 'rules' => config('AuthSession')->usernameValidationRules,
// ],
'email' => [
'label' => 'Auth.email',
'rules' => config(AuthSession::class)->emailValidationRules,
'rules' => config('AuthSession')->emailValidationRules,
],
'password' => [
'label' => 'Auth.password',
Expand All @@ -110,7 +108,7 @@ public function logoutAction(): RedirectResponse
{
// Capture logout redirect URL before auth logout,
// otherwise you cannot check the user in `logoutRedirect()`.
$url = config(Auth::class)->logoutRedirect();
$url = config('Auth')->logoutRedirect();

auth()->logout();

Expand Down
8 changes: 3 additions & 5 deletions src/Controllers/MagicLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Config\AuthSession;
use CodeIgniter\Shield\Models\LoginModel;
use CodeIgniter\Shield\Models\UserIdentityModel;
use CodeIgniter\Shield\Models\UserModel;
Expand Down Expand Up @@ -53,7 +51,7 @@ public function __construct()
public function loginView()
{
if (auth()->loggedIn()) {
return redirect()->to(config(Auth::class)->loginRedirect());
return redirect()->to(config('Auth')->loginRedirect());
}

return $this->view(setting('Auth.views')['magic-link-login']);
Expand Down Expand Up @@ -191,7 +189,7 @@ public function verify(): RedirectResponse
Events::trigger('magicLogin');

// Get our login redirect url
return redirect()->to(config(Auth::class)->loginRedirect());
return redirect()->to(config('Auth')->loginRedirect());
}

/**
Expand Down Expand Up @@ -226,7 +224,7 @@ protected function getValidationRules(): array
return [
'email' => [
'label' => 'Auth.email',
'rules' => config(AuthSession::class)->emailValidationRules,
'rules' => config('AuthSession')->emailValidationRules,
],
];
}
Expand Down
14 changes: 7 additions & 7 deletions src/Controllers/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Authentication\Passwords;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Config\AuthSession;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Exceptions\ValidationException;
use CodeIgniter\Shield\Models\UserModel;
Expand Down Expand Up @@ -47,7 +46,8 @@ public function initController(
$logger
);

$authConfig = config(Auth::class);
/** @var Auth $authConfig */
$authConfig = config('Auth');
$this->tables = $authConfig->tables;
}

Expand All @@ -59,7 +59,7 @@ public function initController(
public function registerView()
{
if (auth()->loggedIn()) {
return redirect()->to(config(Auth::class)->registerRedirect());
return redirect()->to(config('Auth')->registerRedirect());
}

// Check if registration is allowed
Expand All @@ -85,7 +85,7 @@ public function registerView()
public function registerAction(): RedirectResponse
{
if (auth()->loggedIn()) {
return redirect()->to(config(Auth::class)->registerRedirect());
return redirect()->to(config('Auth')->registerRedirect());
}

// Check if registration is allowed
Expand Down Expand Up @@ -145,7 +145,7 @@ public function registerAction(): RedirectResponse
$authenticator->completeLogin($user);

// Success!
return redirect()->to(config(Auth::class)->registerRedirect())
return redirect()->to(config('Auth')->registerRedirect())
->with('message', lang('Auth.registerSuccess'));
}

Expand Down Expand Up @@ -178,11 +178,11 @@ protected function getUserEntity(): User
protected function getValidationRules(): array
{
$registrationUsernameRules = array_merge(
config(AuthSession::class)->usernameValidationRules,
config('AuthSession')->usernameValidationRules,
[sprintf('is_unique[%s.username]', $this->tables['users'])]
);
$registrationEmailRules = array_merge(
config(AuthSession::class)->emailValidationRules,
config('AuthSession')->emailValidationRules,
[sprintf('is_unique[%s.secret]', $this->tables['identities'])]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class CreateAuthTables extends Migration

public function __construct(?Forge $forge = null)
{
$authConfig = config(Auth::class);
/** @var Auth $authConfig */
$authConfig = config('Auth');

if ($authConfig->DBGroup !== null) {
$this->DBGroup = $authConfig->DBGroup;
Expand Down
3 changes: 1 addition & 2 deletions src/Filters/ChainAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Shield\Config\Auth;

/**
* Chain Authentication Filter.
Expand Down Expand Up @@ -42,7 +41,7 @@ public function before(RequestInterface $request, $arguments = null)

helper('settings');

$chain = config(Auth::class)->authenticationChain;
$chain = config('Auth')->authenticationChain;

foreach ($chain as $alias) {
if (auth($alias)->loggedIn()) {
Expand Down
Loading

0 comments on commit e53c7c9

Please sign in to comment.