Skip to content

Commit

Permalink
All tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ttimot24 committed Dec 7, 2024
1 parent 19e5d63 commit b61bd05
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
8 changes: 4 additions & 4 deletions app/Http/Middleware/AdminMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class AdminMiddleware {
* @return mixed
*/
public function handle($request, Closure $next){
if(!$request->user()->isAdmin() || !$request->user()->isActive()){

\Auth::logout();
return redirect()->back();
if($request->user()->isAdmin() && $request->user()->isActive()){
return $next($request);
}

return $next($request);
\Auth::logout();
return redirect()->back();
}
}
5 changes: 3 additions & 2 deletions app/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function blogposts(){
*/
public function role(){

if(\App\Model\UserRole::find($this->role_id)==NULL){
if(\App\Model\UserRole::find($this->role_id)==null){
$this->role_id = 1;
}

Expand All @@ -98,7 +98,8 @@ public function comments(){
}


public function hasPermission($permission){
public function hasPermission(string $permission): bool {

return empty($this->role->rights)? false : in_array($permission, $this->role->rights);
}

Expand Down
7 changes: 4 additions & 3 deletions resources/tests/unit/Middleware/AdminMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,20 @@ public function testAdminsAreNotRedirected()
public function testNonActiveAdminsAreRedirected()
{

$user = ModelFactory::createUser(true);
$user = ModelFactory::createUser(false);

$request = Request::create('/admin', 'GET');

$request->setUserResolver(function () use ($user) {

$role = new \App\Model\UserRole(['name' => 'admin', 'rights' => ['admin_area' => 1]]);
$role = new \App\Model\UserRole(['name' => 'admin']);
$role->setRightsAttribute(['admin_area']);
$user->role = $role;

return $user;
});

$middleware = new \App\Http\Middleware\AdminMiddleware;
$middleware = new \App\Http\Middleware\AdminMiddleware();

$response = $middleware->handle($request, function () {});

Expand Down

0 comments on commit b61bd05

Please sign in to comment.