-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
4 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Javaabu\Helpers\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class AllowOnlySpecificUserType | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @param string|null $authenticable | ||
* @return mixed | ||
* @throws AuthorizationException | ||
*/ | ||
public function handle($request, Closure $next, $authenticable = 'user') | ||
{ | ||
$authenticable_class = Model::getActualClassNameForMorph($authenticable); | ||
|
||
if (auth()->user() instanceof $authenticable_class) { | ||
return $next($request); | ||
} | ||
|
||
if (expects_json($request)) { | ||
throw new AuthorizationException('Disallowed user type'); | ||
} else { | ||
$url = with(new $authenticable_class)->loginUrl(); | ||
return redirect()->to($url); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters