|
24 | 24 | namespace OCA\Files_External\Lib; |
25 | 25 |
|
26 | 26 | use Icewind\SMB\KerberosTicket; |
| 27 | +use OCA\Files_External\Controller\UserGlobalStoragesController; |
| 28 | +use OCA\Files_External\Lib\Auth\SMB\KerberosSsoSession; |
| 29 | +use OCA\Files_External\Service\UserGlobalStoragesService; |
27 | 30 | use OCP\AppFramework\Http\Response; |
28 | 31 | use OCP\AppFramework\Middleware; |
29 | 32 | use OCP\ISession; |
| 33 | +use OCP\IUserSession; |
30 | 34 |
|
31 | 35 | class TicketSaveMiddleware extends Middleware { |
32 | 36 | private ISession $session; |
| 37 | + private IUserSession $userSession; |
| 38 | + private UserGlobalStoragesService $storagesService; |
33 | 39 |
|
34 | | - public function __construct(ISession $session) { |
| 40 | + public function __construct( |
| 41 | + ISession $session, |
| 42 | + IUserSession $userSession, |
| 43 | + UserGlobalStoragesService $storagesService |
| 44 | + ) { |
35 | 45 | $this->session = $session; |
| 46 | + $this->userSession = $userSession; |
| 47 | + $this->storagesService = $storagesService; |
36 | 48 | } |
37 | 49 |
|
38 | 50 | public function afterController($controller, $methodName, Response $response) { |
39 | 51 | $ticket = KerberosTicket::fromEnv(); |
40 | | - if ($ticket && $ticket->isValid()) { |
| 52 | + if ($ticket && $ticket->isValid() && $this->needToSaveTicket()) { |
41 | 53 | $this->session->set('kerberos_ticket', base64_encode($ticket->save())); |
42 | 54 | } |
43 | 55 | return $response; |
44 | 56 | } |
| 57 | + |
| 58 | + private function needToSaveTicket(): bool { |
| 59 | + $user = $this->userSession->getUser(); |
| 60 | + if (!$user) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + $storages = $this->storagesService->getAllStoragesForUser($user); |
| 64 | + foreach ($storages as $storage) { |
| 65 | + if ($storage->getAuthMechanism() instanceof KerberosSsoSession) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + } |
| 69 | + return false; |
| 70 | + } |
45 | 71 | } |
0 commit comments