1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: leo108 |
5
|
|
|
* Date: 2016/10/25 |
6
|
|
|
* Time: 16:34 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Leo108\CAS\Repositories; |
10
|
|
|
|
11
|
|
|
use Carbon\Carbon; |
12
|
|
|
use Leo108\CAS\Contracts\Models\UserModel; |
13
|
|
|
use Leo108\CAS\Exceptions\CAS\CasException; |
14
|
|
|
use Leo108\CAS\Models\PGTicket; |
15
|
|
|
use Leo108\CAS\Services\TicketGenerator; |
16
|
|
|
|
17
|
|
|
class PGTicketRepository |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var PGTicket |
21
|
|
|
*/ |
22
|
|
|
protected $pgTicket; |
23
|
|
|
/** |
24
|
|
|
* @var ServiceRepository |
25
|
|
|
*/ |
26
|
|
|
protected $serviceRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TicketGenerator |
30
|
|
|
*/ |
31
|
|
|
protected $ticketGenerator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* PGTicketRepository constructor. |
35
|
|
|
* @param PGTicket $pgTicket |
36
|
|
|
* @param ServiceRepository $serviceRepository |
37
|
|
|
* @param TicketGenerator $ticketGenerator |
38
|
|
|
*/ |
39
|
30 |
|
public function __construct( |
40
|
|
|
PGTicket $pgTicket, |
41
|
|
|
ServiceRepository $serviceRepository, |
42
|
|
|
TicketGenerator $ticketGenerator |
43
|
|
|
) { |
44
|
30 |
|
$this->pgTicket = $pgTicket; |
45
|
30 |
|
$this->serviceRepository = $serviceRepository; |
46
|
30 |
|
$this->ticketGenerator = $ticketGenerator; |
47
|
30 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $ticket |
51
|
|
|
* @param bool $checkExpired |
52
|
|
|
* @return null|PGTicket |
53
|
|
|
*/ |
54
|
1 |
View Code Duplication |
public function getByTicket($ticket, $checkExpired = true) |
|
|
|
|
55
|
|
|
{ |
56
|
1 |
|
$record = $this->pgTicket->where('ticket', $ticket)->first(); |
57
|
1 |
|
if (!$record) { |
58
|
1 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return ($checkExpired && $record->isExpired()) ? null : $record; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param UserModel $user |
66
|
|
|
*/ |
67
|
1 |
|
public function invalidTicketByUser(UserModel $user) |
68
|
|
|
{ |
69
|
1 |
|
$this->pgTicket->where('user_id', $user->getEloquentModel()->getKey())->delete(); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param UserModel $user |
74
|
|
|
* @param string $pgtUrl |
75
|
|
|
* @param array $proxies |
76
|
|
|
* @return PGTicket |
77
|
|
|
* @throws CasException |
78
|
|
|
*/ |
79
|
4 |
View Code Duplication |
public function applyTicket(UserModel $user, $pgtUrl, $proxies = []) |
|
|
|
|
80
|
|
|
{ |
81
|
4 |
|
$service = $this->serviceRepository->getServiceByUrl($pgtUrl); |
82
|
4 |
|
if (!$service || !$service->allow_proxy) { |
83
|
2 |
|
throw new CasException(CasException::UNAUTHORIZED_SERVICE_PROXY); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
$ticket = $this->getAvailableTicket(config('cas.pg_ticket_len', 64)); |
87
|
2 |
|
if ($ticket === false) { |
88
|
1 |
|
throw new CasException(CasException::INTERNAL_ERROR, 'apply proxy-granting ticket failed'); |
89
|
|
|
} |
90
|
1 |
|
$record = $this->pgTicket->newInstance( |
91
|
|
|
[ |
92
|
1 |
|
'ticket' => $ticket, |
93
|
1 |
|
'expire_at' => new Carbon(sprintf('+%dsec', config('cas.pg_ticket_expire', 7200))), |
94
|
1 |
|
'created_at' => new Carbon(), |
95
|
1 |
|
'pgt_url' => $pgtUrl, |
96
|
1 |
|
'proxies' => $proxies, |
97
|
|
|
] |
98
|
|
|
); |
99
|
1 |
|
$record->user()->associate($user->getEloquentModel()); |
100
|
1 |
|
$record->service()->associate($service); |
101
|
1 |
|
$record->save(); |
102
|
|
|
|
103
|
1 |
|
return $record; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $totalLength |
108
|
|
|
* @return string|false |
109
|
|
|
*/ |
110
|
1 |
View Code Duplication |
protected function getAvailableTicket($totalLength) |
|
|
|
|
111
|
|
|
{ |
112
|
1 |
|
return $this->ticketGenerator->generate( |
113
|
1 |
|
$totalLength, |
114
|
1 |
|
'PGT-', |
115
|
|
|
function ($ticket) { |
116
|
1 |
|
return is_null($this->getByTicket($ticket, false)); |
117
|
1 |
|
}, |
118
|
1 |
|
10 |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.