Skip to content

Commit

Permalink
fix: Authorization bug
Browse files Browse the repository at this point in the history
  • Loading branch information
eddy8 committed Sep 11, 2021
1 parent ad00f19 commit 8b7c6ed
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
19 changes: 9 additions & 10 deletions app/Http/Middleware/Admin/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@ public function handle($request, Closure $next, $guard)
}

$routeParams = $route->parameters();
if (empty($routeParams)) {
return $next($request);
}
foreach ($routeParams as $k => $v) {
$val = "{$k}:{$v}";
break;
}
if (!empty($routeParams)) {
foreach ($routeParams as $k => $v) {
$val = "{$k}:{$v}";
break;
}

$permission = Menu::where('route', $routeName)->where('route_params', $val)->first();
if ($permission && $user->can($permission->name)) {
return $next($request);
$permission = Menu::where('route', $routeName)->where('route_params', $val)->first();
if ($permission && $user->can($permission->name)) {
return $next($request);
}
}

if ($request->expectsJson()) {
Expand Down
70 changes: 70 additions & 0 deletions tests/Feature/Admin/AuthorizationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Tests\Feature\Admin;

use App\Model\Admin\AdminUser;
use App\Model\Admin\Entity;
use App\Model\Admin\Menu;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class AuthorizationTest extends TestCase
{
use RefreshDatabase;

protected $user;
protected $superUser;

public function setUp(): void
{
parent::setUp();

$this->superUser = factory(AdminUser::class)->create(['id' => 1]);
$this->user = factory(AdminUser::class)->create(['id' => 2]);
}

public function testUserVistEntityListPage()
{
factory(Entity::class, 1)->create();
$testUrl = '/admin/entities';

$response = $this->actingAs($this->superUser, 'admin')->get($testUrl);
$response->assertStatus(200);

$response = $this->actingAs($this->user, 'admin')->get($testUrl);
$response->assertStatus(401);

// 授权后可访问
$response = $this->actingAs($this->superUser, 'admin')->post(
'/admin/menus',
[
'name' => '模型列表',
'route' => 'admin::entity.index',
'url' => '/admin/entities'
]
);
$response->assertStatus(200);
$response = $this->actingAs($this->superUser, 'admin')->post('/admin/roles', ['name' => 'entity']);
$response->assertStatus(200);
$response = $this->actingAs($this->superUser, 'admin')->put(
'/admin/roles/1/permission',
['permission' => [1 => '模型列表']]
);
$response->assertStatus(200);
$response = $this->actingAs($this->superUser, 'admin')->put(
'/admin/admin_user/' . $this->user->id . '/role',
['role' => [1 => 'entity']]
);
$response->assertStatus(200);
$response = $this->actingAs($this->user, 'admin')->get($testUrl);
$response->assertStatus(200);
}

public function tearDown(): void
{
parent::tearDown();
}
}

0 comments on commit 8b7c6ed

Please sign in to comment.