|
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; |
|
|
|
|
|
|
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))); |
|
|
|
|
|
|
124
|
1 |
|
} |
|
125
|
1 |
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
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@propertyannotation to your class or interface to document the existence of this variable.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.