Completed
Push — master ( 59d827...fe270b )
by leo
05:52
created

SecurityController::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: chenyihong
5
 * Date: 16/8/1
6
 * Time: 14:50
7
 */
8
9
namespace Leo108\CAS\Http\Controllers;
10
11
use Leo108\CAS\Contracts\Interactions\UserLogin;
12
use Leo108\CAS\Events\CasUserLoginEvent;
13
use Leo108\CAS\Events\CasUserLogoutEvent;
14
use Leo108\CAS\Exceptions\CAS\CasException;
15
use Illuminate\Http\Request;
16
use Leo108\CAS\Repositories\ServiceRepository;
17
use Leo108\CAS\Repositories\TicketRepository;
18
19
class SecurityController extends Controller
20
{
21
    /**
22
     * @var ServiceRepository
23
     */
24
    protected $serviceRepository;
25
26
    /**
27
     * @var TicketRepository
28
     */
29
    protected $ticketRepository;
30
31
    /**
32
     * @var UserLogin
33
     */
34
    protected $loginInteraction;
35
36
    /**
37
     * SecurityController constructor.
38
     * @param ServiceRepository $serviceRepository
39
     * @param TicketRepository  $ticketRepository
40
     * @param UserLogin         $loginInteraction
41
     */
42 4
    public function __construct(
43
        ServiceRepository $serviceRepository,
44
        TicketRepository $ticketRepository,
45
        UserLogin $loginInteraction
46
    ) {
47 4
        $this->serviceRepository = $serviceRepository;
48 4
        $this->ticketRepository  = $ticketRepository;
49 4
        $this->loginInteraction  = $loginInteraction;
50 4
    }
51
52 1
    public function showLogin(Request $request)
53
    {
54 1
        $service = $request->get('service', '');
55 1
        $errors  = [];
56 1
        if (!empty($service)) {
57
            //service not found in white list
58 1
            if (!$this->serviceRepository->isUrlValid($service)) {
59 1
                $errors[] = (new CasException(CasException::INVALID_SERVICE))->getCasMsg();
60 1
            }
61 1
        }
62
63 1
        $user = $this->loginInteraction->getCurrentUser($request);
64
        //user already has sso session
65 1
        if ($user) {
66
            //has errors, should not be redirected to target url
67 1
            if (!empty($errors)) {
68
                return $this->loginInteraction->redirectToHome($errors);
69
            }
70
71
            //must not be transparent
72 1
            if ($request->get('warn') === 'true' && !empty($service)) {
73
                $query = $request->query->all();
74
                unset($query['warn']);
75
                $url = cas_route('login_page', $query);
76
77
                return $this->loginInteraction->showLoginWarnPage($request, $url, $service);
78
            }
79
80 1
            return $this->authenticated($request);
81
        }
82
83 1
        return $this->loginInteraction->showLoginPage($request, $errors);
84
    }
85
86 1
    public function login(Request $request)
87
    {
88 1
        return $this->loginInteraction->login($request, array($this, 'authenticated'));
89
    }
90
91 1
    public function authenticated(Request $request)
92
    {
93 1
        $user = $this->loginInteraction->getCurrentUser($request);
94 1
        event(new CasUserLoginEvent($request, $user));
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->loginInteraction->getCurrentUser($request) on line 93 can be null; however, Leo108\CAS\Events\CasUserLoginEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
95 1
        $serviceUrl = $request->get('service', '');
96 1
        if (!empty($serviceUrl)) {
97 1
            $query = parse_url($serviceUrl, PHP_URL_QUERY);
98
            try {
99 1
                $ticket = $this->ticketRepository->applyTicket($user, $serviceUrl);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->loginInteraction->getCurrentUser($request) on line 93 can be null; however, Leo108\CAS\Repositories\...pository::applyTicket() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
100 1
            } catch (CasException $e) {
101 1
                return $this->loginInteraction->redirectToHome([$e->getCasMsg()]);
102
            }
103 1
            $finalUrl = $serviceUrl.($query ? '&' : '?').'ticket='.$ticket->ticket;
0 ignored issues
show
Documentation introduced by
The property ticket does not exist on object<Leo108\CAS\Models\Ticket>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
105 1
            return redirect($finalUrl);
106
        }
107
108 1
        return $this->loginInteraction->redirectToHome();
109
    }
110
111 1
    public function logout(Request $request)
112
    {
113 1
        return $this->loginInteraction->logout(
114 1
            $request,
115 1
            function (Request $request) {
116 1
                event(new CasUserLogoutEvent($request, $this->loginInteraction->getCurrentUser($request)));
0 ignored issues
show
Bug introduced by
It seems like $this->loginInteraction->getCurrentUser($request) can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
117 1
            }
118 1
        );
119
    }
120
}
121