Completed
Push — master ( fe270b...d89563 )
by leo
02:58
created

SecurityController::authenticated()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.1647

Importance

Changes 0
Metric Value
cc 5
eloc 17
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 26
ccs 13
cts 16
cp 0.8125
crap 5.1647
rs 8.439
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 1
                return $this->loginInteraction->redirectToHome($errors);
69
            }
70
71
            //must not be transparent
72 1
            if ($request->get('warn') === 'true' && !empty($service)) {
73 1
                $query = $request->query->all();
74 1
                unset($query['warn']);
75 1
                $url = cas_route('login_page', $query);
76
77 1
                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
        if ($user === null) {
95
            //unreachable code
96
            throw new CasException(
97
                CasException::INTERNAL_ERROR,
98
                'should call authenticated only after getCurrentUser return not null'
99
            );
100
        }
101 1
        event(new CasUserLoginEvent($request, $user));
102 1
        $serviceUrl = $request->get('service', '');
103 1
        if (!empty($serviceUrl)) {
104 1
            $query = parse_url($serviceUrl, PHP_URL_QUERY);
105
            try {
106 1
                $ticket = $this->ticketRepository->applyTicket($user, $serviceUrl);
107 1
            } catch (CasException $e) {
108 1
                return $this->loginInteraction->redirectToHome([$e->getCasMsg()]);
109
            }
110 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...
111
112 1
            return redirect($finalUrl);
113
        }
114
115 1
        return $this->loginInteraction->redirectToHome();
116
    }
117
118 1
    public function logout(Request $request)
119
    {
120 1
        return $this->loginInteraction->logout(
121 1
            $request,
122 1
            function (Request $request) {
123 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...
124 1
            }
125 1
        );
126
    }
127
}
128