Permalink
Browse files

Remove instances of Auth facade

  • Loading branch information...
REBELinBLUE committed Mar 4, 2017
1 parent a95b675 commit c55076f8717f50d1c7e8ac40437fbf9b38c04f41
@@ -2,13 +2,13 @@
namespace REBELinBLUE\Deployer\Http\Controllers;
+use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
-use Illuminate\Support\Facades\Auth;
use Intervention\Image\ImageManager;
use MicheleAngioni\MultiLanguage\LanguageManager;
use PragmaRX\Google2FA\Contracts\Google2FA as Google2FA;
@@ -48,6 +48,11 @@ class ProfileController extends Controller
*/
private $redirect;
+ /**
+ * @var Guard
+ */
+ private $auth;
+
/**
* ProfileController constructor.
*
@@ -56,19 +61,22 @@ class ProfileController extends Controller
* @param ViewFactory $view
* @param Translator $translator
* @param Redirector $redirector
+ * @param Guard $auth
*/
public function __construct(
UserRepositoryInterface $repository,
Google2FA $google2fa,
ViewFactory $view,
Translator $translator,
- Redirector $redirector
+ Redirector $redirector,
+ Guard $auth
) {
$this->repository = $repository;
$this->google2fa = $google2fa;
$this->view = $view;
$this->translator = $translator;
$this->redirect = $redirector;
+ $this->auth = $auth;
}
/**
@@ -82,7 +90,7 @@ public function __construct(
*/
public function index(Request $request, LanguageManager $languageManager, Settings $settings)
{
- $user = Auth::user();
+ $user = $this->auth->user();
$code = $this->google2fa->generateSecretKey();
if ($user->has_two_factor_authentication || $request->old('google_code')) {
@@ -112,7 +120,7 @@ public function update(StoreProfileRequest $request)
$this->repository->updateById($request->only(
'name',
'password'
- ), Auth::user()->id);
+ ), $this->auth->id());
return $this->redirect->to('/');
}
@@ -130,7 +138,7 @@ public function settings(StoreSettingsRequest $request)
'skin',
'scheme',
'language'
- ), Auth::user()->id);
+ ), $this->auth->id());
return $this->redirect->to('/');
}
@@ -143,7 +151,7 @@ public function settings(StoreSettingsRequest $request)
*/
public function requestEmail(Dispatcher $dispatcher)
{
- $dispatcher->dispatch(new EmailChangeRequested(Auth::user()));
+ $dispatcher->dispatch(new EmailChangeRequested($this->auth->user()));
return 'success';
}
@@ -222,7 +230,7 @@ public function upload(Request $request, UrlGenerator $url)
*/
public function gravatar()
{
- $user = Auth::user();
+ $user = $this->auth->user();
$user->avatar = null;
$user->save();
@@ -261,7 +269,7 @@ public function avatar(Request $request, UrlGenerator $url, ImageManager $image)
$image->save(public_path() . $path);
- $user = Auth::user();
+ $user = $this->auth->user();
$user->avatar = $path;
$user->save();
@@ -293,7 +301,7 @@ public function twoFactor(Request $request)
}
}
- $user = Auth::user();
+ $user = $this->auth->user();
$user->google2fa_secret = $secret;
$user->save();
@@ -3,9 +3,9 @@
namespace REBELinBLUE\Deployer\Http\Middleware;
use Closure;
+use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Routing\Redirector;
-use Illuminate\Support\Facades\Auth;
/**
* Authentication middleware.
@@ -22,14 +22,21 @@ class Authenticate
*/
private $response;
+ /**
+ * @var AuthFactory
+ */
+ private $auth;
+
/**
* @param Redirector $redirector
* @param ResponseFactory $response
+ * @param AuthFactory $auth
*/
- public function __construct(Redirector $redirector, ResponseFactory $response)
+ public function __construct(Redirector $redirector, ResponseFactory $response, AuthFactory $auth)
{
$this->redirector = $redirector;
$this->response = $response;
+ $this->auth = $auth;
}
/**
@@ -43,7 +50,7 @@ public function __construct(Redirector $redirector, ResponseFactory $response)
*/
public function handle($request, Closure $next, $guard = null)
{
- if (Auth::guard($guard)->guest()) {
+ if ($this->auth->guard($guard)->guest()) {
if ($request->ajax()) {
return $this->response->make('Unauthorized.', 401);
}
@@ -3,7 +3,7 @@
namespace REBELinBLUE\Deployer\Http\Middleware;
use Closure;
-use Illuminate\Support\Facades\Auth;
+use Illuminate\Contracts\Auth\Factory as AuthFactory;
use MicheleAngioni\MultiLanguage\LanguageManager;
/**
@@ -16,26 +16,35 @@ class Locale
*/
private $languageManager;
+ /**
+ * @var AuthFactory
+ */
+ private $auth;
+
/**
* Locale constructor.
+ *
* @param LanguageManager $languageManager
+ * @param AuthFactory $auth
*/
- public function __construct(LanguageManager $languageManager)
+ public function __construct(LanguageManager $languageManager, AuthFactory $auth)
{
$this->languageManager = $languageManager;
+ $this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
+ * @param string|null $guard
*
* @return \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
- public function handle($request, Closure $next)
+ public function handle($request, Closure $next, $guard = null)
{
- $user = Auth::user();
+ $user = $this->auth->guard($guard)->user();
if ($user && $user->language) {
$this->languageManager->setLocale($user->language);
}
@@ -3,8 +3,8 @@
namespace REBELinBLUE\Deployer\Http\Middleware;
use Closure;
+use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Routing\Redirector;
-use Illuminate\Support\Facades\Auth;
/**
* Middleware to prevent access to pages when already authenticated.
@@ -17,11 +17,18 @@ class RedirectIfAuthenticated
private $redirector;
/**
- * @param Redirector $redirector
+ * @var Factory
*/
- public function __construct(Redirector $redirector)
+ private $auth;
+
+ /**
+ * @param Redirector $redirector
+ * @param AuthFactory $auth
+ */
+ public function __construct(Redirector $redirector, AuthFactory $auth)
{
$this->redirector = $redirector;
+ $this->auth = $auth;
}
/**
@@ -35,7 +42,7 @@ public function __construct(Redirector $redirector)
*/
public function handle($request, Closure $next, $guard = null)
{
- if (Auth::guard($guard)->check()) {
+ if ($this->auth->guard($guard)->check()) {
return $this->redirector->to('/');
}
@@ -3,10 +3,10 @@
namespace REBELinBLUE\Deployer\Http\Middleware;
use Closure;
+use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Routing\Redirector;
-use Illuminate\Support\Facades\Auth;
use REBELinBLUE\Deployer\Events\JsonWebTokenExpired;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
@@ -18,7 +18,7 @@
class RefreshJsonWebToken
{
/**
- * @var JWTAuth
+ * @var AuthFactory
*/
protected $auth;
@@ -38,21 +38,29 @@ class RefreshJsonWebToken
private $response;
/**
- * @param JWTAuth $auth
+ * @var JWTAuth
+ */
+ private $jwt;
+
+ /**
+ * @param JWTAuth $jwt
* @param Dispatcher $dispatcher
* @param Redirector $redirector
* @param ResponseFactory $response
+ * @param AuthFactory $auth
*/
public function __construct(
- JWTAuth $auth,
+ JWTAuth $jwt,
Dispatcher $dispatcher,
Redirector $redirector,
- ResponseFactory $response
+ ResponseFactory $response,
+ AuthFactory $auth
) {
$this->auth = $auth;
$this->dispatcher = $dispatcher;
$this->redirector = $redirector;
$this->response = $response;
+ $this->jwt = $jwt;
}
/**
@@ -66,7 +74,7 @@ public function __construct(
*/
public function handle($request, Closure $next, $guard = null)
{
- $authenticated_user = Auth::guard($guard)->user();
+ $authenticated_user = $this->auth->guard($guard)->user();
$has_valid_token = false;
@@ -75,7 +83,7 @@ public function handle($request, Closure $next, $guard = null)
$token = $request->session()->get('jwt');
try {
- $token_user = $this->auth->authenticate($token);
+ $token_user = $this->jwt->authenticate($token);
if ($token_user->id !== $authenticated_user->id) {
throw new JWTException('Token does not belong to the authenticated user');
@@ -2,22 +2,35 @@
namespace REBELinBLUE\Deployer\Http\Requests;
+use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\Http\FormRequest;
-use Illuminate\Support\Facades\Auth;
/**
* Generic Request class.
* @SuppressWarnings(PHPMD.NumberOfChildren)
*/
abstract class Request extends FormRequest
{
+ /**
+ * @var Guard
+ */
+ protected $auth;
+
+ /**
+ * @param Guard $auth
+ */
+ public function __construct(Guard $auth)
+ {
+ $this->auth = $auth;
+ }
+
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
- return Auth::check();
+ return $this->auth->check();
}
}
@@ -2,8 +2,8 @@
namespace REBELinBLUE\Deployer\Jobs;
+use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\Bus\DispatchesJobs;
-use Illuminate\Support\Facades\Auth;
use REBELinBLUE\Deployer\Deployment;
use REBELinBLUE\Deployer\Jobs\QueueDeployment\GroupedCommandListTransformer;
use REBELinBLUE\Deployer\Jobs\QueueDeployment\StepsBuilder;
@@ -50,10 +50,11 @@ public function __construct(Project $project, Deployment $deployment, array $opt
*
* @param GroupedCommandListTransformer $transformer
* @param StepsBuilder $steps
+ * @param Guard $auth
*/
- public function handle(GroupedCommandListTransformer $transformer, StepsBuilder $steps)
+ public function handle(GroupedCommandListTransformer $transformer, StepsBuilder $steps, Guard $auth)
{
- $this->setDeploymentStatus();
+ $this->setDeploymentStatus($auth);
$steps->build(
$transformer->groupCommandsByDeployStep($this->project),
@@ -67,15 +68,17 @@ public function handle(GroupedCommandListTransformer $transformer, StepsBuilder
/**
* Sets the deployment to pending.
+ *
+ * @param Guard $auth
*/
- private function setDeploymentStatus()
+ private function setDeploymentStatus(Guard $auth)
{
$this->deployment->status = Deployment::PENDING;
$this->deployment->started_at = $this->deployment->freshTimestamp();
$this->deployment->project_id = $this->project->id;
- if (Auth::check()) {
- $this->deployment->user_id = Auth::user()->id;
+ if ($auth->check()) {
+ $this->deployment->user_id = $auth->id();
} else {
$this->deployment->is_webhook = true;
}
Oops, something went wrong.

0 comments on commit c55076f

Please sign in to comment.