Code Duplication    Length = 41-42 lines in 2 locations

src/Platfourm/Auth/Middleware/Permission.php 1 location

@@ 16-56 (lines=41) @@
13
use Closure;
14
use Longman\Platfourm\Contracts\Auth\AuthUserService as AuthUserServiceContract;
15
16
class Permission
17
{
18
    /**
19
     * The Guard implementation.
20
     *
21
     * @var Guard
22
     */
23
    protected $authService;
24
25
    /**
26
     * Create a new filter instance.
27
     *
28
     * @param  Guard $auth
29
     * @return void
30
     */
31
    public function __construct(AuthUserServiceContract $authService)
32
    {
33
        $this->authService = $authService;
34
    }
35
36
    /**
37
     * Handle an incoming request.
38
     *
39
     * @param  \Illuminate\Http\Request $request
40
     * @param  Closure                  $next
41
     * @param                           $permissions
42
     * @return mixed
43
     */
44
    public function handle($request, Closure $next, $permissions)
45
    {
46
        if ($this->authService->guest() || !$request->user()->can(explode('|', $permissions))) {
47
            if ($request->wantsJson()) {
48
                return response('Forbidden.', 403);
49
            } else {
50
                abort(403);
51
            }
52
        }
53
54
        return $next($request);
55
    }
56
}
57

src/Platfourm/Auth/Middleware/Role.php 1 location

@@ 16-57 (lines=42) @@
13
use Closure;
14
use Longman\Platfourm\Contracts\Auth\AuthUserService as AuthUserServiceContract;
15
16
class Role
17
{
18
    /**
19
     * The Guard implementation.
20
     *
21
     * @var Guard
22
     */
23
    protected $authService;
24
25
    /**
26
     * Create a new filter instance.
27
     *
28
     * @param  Guard $auth
29
     * @return void
30
     */
31
    public function __construct(AuthUserServiceContract $authService)
32
    {
33
        $this->authService = $authService;
34
    }
35
36
    /**
37
     * Handle an incoming request.
38
     *
39
     * @param  \Illuminate\Http\Request $request
40
     * @param  Closure                  $next
41
     * @param                           $roles
42
     * @return mixed
43
     */
44
    public function handle($request, Closure $next, $roles)
45
    {
46
47
        if ($this->authService->guest() || !$request->user()->hasRole(explode('|', $roles))) {
48
            if ($request->wantsJson()) {
49
                return response('Forbidden.', 403);
50
            } else {
51
                abort(403);
52
            }
53
        }
54
55
        return $next($request);
56
    }
57
}
58