Skip to content

Commit

Permalink
Merge pull request #748 from kenjis/refactor-use-class-in-config
Browse files Browse the repository at this point in the history
refactor: use ::class keyword in config()
  • Loading branch information
kenjis authored May 22, 2023
2 parents 3dc2154 + bee838a commit 736a88f
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 47 deletions.
7 changes: 4 additions & 3 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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 @@ -106,7 +108,7 @@ public function authenticate(array $credentials): Result
*/
public function routes(RouteCollection &$routes, array $config = []): void
{
$authRoutes = config('AuthRoutes')->routes;
$authRoutes = config(AuthRoutes::class)->routes;

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

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

if (! property_exists($config, 'userProvider')) {
throw AuthenticationException::forUnknownUserProvider();
Expand Down
3 changes: 2 additions & 1 deletion src/Authentication/Actions/Email2FA.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 @@ -120,7 +121,7 @@ public function verify(IncomingRequest $request)
}

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

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Authentication/Actions/EmailActivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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 @@ -114,7 +115,7 @@ public function verify(IncomingRequest $request)
$user->activate();

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

Expand Down
9 changes: 5 additions & 4 deletions src/Authentication/Authenticators/AccessTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 @@ -104,7 +105,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')->authenticatorHeader['tokens']]),
'reason' => lang('Auth.noToken', [config(Auth::class)->authenticatorHeader['tokens']]),
]);
}

Expand All @@ -129,7 +130,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')->unusedTokenLifetime))
&& $token->last_used_at->isBefore(Time::now()->subSeconds(config(Auth::class)->unusedTokenLifetime))
) {
return new Result([
'success' => false,
Expand Down Expand Up @@ -168,7 +169,7 @@ public function loggedIn(): bool
$request = service('request');

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

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

$header = $request->getHeaderLine(config('Auth')->authenticatorHeader['tokens']);
$header = $request->getHeaderLine(config(Auth::class)->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,6 +13,7 @@
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 @@ -89,8 +90,7 @@ public function __construct(UserModel $provider)
*/
private function checkSecurityConfig(): void
{
/** @var Security $securityConfig */
$securityConfig = config('Security');
$securityConfig = config(Security::class);

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')->validFields can be used.
$field = array_intersect(config('Auth')->validFields ?? [], array_keys($credentials));
// but any column within config(Auth::class)->validFields can be used.
$field = array_intersect(config(Auth::class)->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')->hashAlgorithm === PASSWORD_BCRYPT) {
if (config(Auth::class)->hashAlgorithm === PASSWORD_BCRYPT) {
return 'max_byte[72]';
}

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

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

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

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

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 @@ -257,7 +258,7 @@ public function can(string $permission): bool

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

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

/**
Expand All @@ -403,6 +404,6 @@ private function getConfigPermissions(): array
{
return function_exists('setting')
? array_keys(setting('AuthGroups.permissions'))
: array_keys(config('AuthGroups')->permissions);
: array_keys(config(AuthGroups::class)->permissions);
}
}
5 changes: 3 additions & 2 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 @@ -21,7 +22,7 @@ public static function auth(bool $getShared = true): Auth
return self::getSharedInstance('auth');
}

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

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

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

/**
Expand Down
12 changes: 7 additions & 5 deletions src/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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 @@ -24,7 +26,7 @@ class LoginController extends BaseController
public function loginView()
{
if (auth()->loggedIn()) {
return redirect()->to(config('Auth')->loginRedirect());
return redirect()->to(config(Auth::class)->loginRedirect());
}

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

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

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

auth()->logout();

Expand Down
8 changes: 5 additions & 3 deletions src/Controllers/MagicLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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 @@ -51,7 +53,7 @@ public function __construct()
public function loginView()
{
if (auth()->loggedIn()) {
return redirect()->to(config('Auth')->loginRedirect());
return redirect()->to(config(Auth::class)->loginRedirect());
}

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

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

/**
Expand Down Expand Up @@ -224,7 +226,7 @@ protected function getValidationRules(): array
return [
'email' => [
'label' => 'Auth.email',
'rules' => config('AuthSession')->emailValidationRules,
'rules' => config(AuthSession::class)->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,6 +12,7 @@
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 @@ -46,8 +47,7 @@ public function initController(
$logger
);

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

Expand All @@ -59,7 +59,7 @@ public function initController(
public function registerView()
{
if (auth()->loggedIn()) {
return redirect()->to(config('Auth')->registerRedirect());
return redirect()->to(config(Auth::class)->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')->registerRedirect());
return redirect()->to(config(Auth::class)->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')->registerRedirect())
return redirect()->to(config(Auth::class)->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')->usernameValidationRules,
config(AuthSession::class)->usernameValidationRules,
[sprintf('is_unique[%s.username]', $this->tables['users'])]
);
$registrationEmailRules = array_merge(
config('AuthSession')->emailValidationRules,
config(AuthSession::class)->emailValidationRules,
[sprintf('is_unique[%s.secret]', $this->tables['identities'])]
);

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

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

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

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

helper('settings');

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

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

0 comments on commit 736a88f

Please sign in to comment.