Fix AppFramework services

* We can't just register an alias as the services need the appId to be
  injected. if we just register an alias this blows up since the main
  container doesn't have the appId.
* Moved the Authtokens over to show the PoC works

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2020-07-14 09:18:39 +02:00 committed by Morris Jobke
parent 5dca062c97
commit fa9dfd83c9
No known key found for this signature in database
GPG Key ID: FE03C3A163FEDE68
3 changed files with 30 additions and 20 deletions

View File

@ -26,6 +26,7 @@ declare(strict_types=1);
namespace OCA\Settings\Settings\Personal\Security; namespace OCA\Settings\Settings\Personal\Security;
use OCP\AppFramework\Services\IInitialState;
use OCP\IUserSession; use OCP\IUserSession;
use function array_map; use function array_map;
use OC\Authentication\Exceptions\InvalidTokenException; use OC\Authentication\Exceptions\InvalidTokenException;
@ -33,7 +34,6 @@ use OC\Authentication\Token\INamedToken;
use OC\Authentication\Token\IProvider as IAuthTokenProvider; use OC\Authentication\Token\IProvider as IAuthTokenProvider;
use OC\Authentication\Token\IToken; use OC\Authentication\Token\IToken;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\IInitialStateService;
use OCP\ISession; use OCP\ISession;
use OCP\Session\Exceptions\SessionNotAvailableException; use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\Settings\ISettings; use OCP\Settings\ISettings;
@ -46,8 +46,8 @@ class Authtokens implements ISettings {
/** @var ISession */ /** @var ISession */
private $session; private $session;
/** @var IInitialStateService */ /** @var IInitialState */
private $initialStateService; private $initialState;
/** @var string|null */ /** @var string|null */
private $uid; private $uid;
@ -58,24 +58,22 @@ class Authtokens implements ISettings {
public function __construct(IAuthTokenProvider $tokenProvider, public function __construct(IAuthTokenProvider $tokenProvider,
ISession $session, ISession $session,
IUserSession $userSession, IUserSession $userSession,
IInitialStateService $initialStateService, IInitialState $initialState,
?string $UserId) { ?string $UserId) {
$this->tokenProvider = $tokenProvider; $this->tokenProvider = $tokenProvider;
$this->session = $session; $this->session = $session;
$this->initialStateService = $initialStateService; $this->initialState = $initialState;
$this->uid = $UserId; $this->uid = $UserId;
$this->userSession = $userSession; $this->userSession = $userSession;
} }
public function getForm(): TemplateResponse { public function getForm(): TemplateResponse {
$this->initialStateService->provideInitialState( $this->initialState->provideInitialState(
'settings',
'app_tokens', 'app_tokens',
$this->getAppTokens() $this->getAppTokens()
); );
$this->initialStateService->provideInitialState( $this->initialState->provideInitialState(
'settings',
'can_create_app_token', 'can_create_app_token',
$this->userSession->getImpersonatingUserID() === null $this->userSession->getImpersonatingUserID() === null
); );

View File

@ -30,7 +30,7 @@ use OC\Authentication\Token\DefaultToken;
use OC\Authentication\Token\IProvider as IAuthTokenProvider; use OC\Authentication\Token\IProvider as IAuthTokenProvider;
use OCA\Settings\Settings\Personal\Security\Authtokens; use OCA\Settings\Settings\Personal\Security\Authtokens;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\IInitialStateService; use OCP\AppFramework\Services\IInitialState;
use OCP\ISession; use OCP\ISession;
use OCP\IUserSession; use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
@ -47,8 +47,8 @@ class AuthtokensTest extends TestCase {
/** @var IUserSession|MockObject */ /** @var IUserSession|MockObject */
private $userSession; private $userSession;
/** @var IInitialStateService|MockObject */ /** @var IInitialState|MockObject */
private $initialStateService; private $initialState;
/** @var string */ /** @var string */
private $uid; private $uid;
@ -62,14 +62,14 @@ class AuthtokensTest extends TestCase {
$this->authTokenProvider = $this->createMock(IAuthTokenProvider::class); $this->authTokenProvider = $this->createMock(IAuthTokenProvider::class);
$this->session = $this->createMock(ISession::class); $this->session = $this->createMock(ISession::class);
$this->userSession = $this->createMock(IUserSession::class); $this->userSession = $this->createMock(IUserSession::class);
$this->initialStateService = $this->createMock(IInitialStateService::class); $this->initialState = $this->createMock(IInitialState::class);
$this->uid = 'test123'; $this->uid = 'test123';
$this->section = new Authtokens( $this->section = new Authtokens(
$this->authTokenProvider, $this->authTokenProvider,
$this->session, $this->session,
$this->userSession, $this->userSession,
$this->initialStateService, $this->initialState,
$this->uid $this->uid
); );
} }
@ -97,9 +97,9 @@ class AuthtokensTest extends TestCase {
->method('getToken') ->method('getToken')
->with('session123') ->with('session123')
->willReturn($sessionToken); ->willReturn($sessionToken);
$this->initialStateService->expects($this->at(0)) $this->initialState->expects($this->at(0))
->method('provideInitialState') ->method('provideInitialState')
->with('settings', 'app_tokens', [ ->with('app_tokens', [
[ [
'id' => 100, 'id' => 100,
'name' => null, 'name' => null,
@ -121,9 +121,9 @@ class AuthtokensTest extends TestCase {
], ],
]); ]);
$this->initialStateService->expects($this->at(1)) $this->initialState->expects($this->at(1))
->method('provideInitialState') ->method('provideInitialState')
->with('settings', 'can_create_app_token', true); ->with('can_create_app_token', true);
$form = $this->section->getForm(); $form = $this->section->getForm();

View File

@ -59,6 +59,8 @@ use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\Folder; use OCP\Files\Folder;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Group\ISubAdmin; use OCP\Group\ISubAdmin;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
use OCP\INavigationManager; use OCP\INavigationManager;
@ -295,8 +297,18 @@ class DIContainer extends SimpleContainer implements IAppContainer {
return $dispatcher; return $dispatcher;
}); });
$this->registerAlias(IAppConfig::class, OC\AppFramework\Services\AppConfig::class); $this->registerService(IAppConfig::class, function (SimpleContainer $c) {
$this->registerAlias(IInitialState::class, OC\AppFramework\Services\InitialState::class); return new OC\AppFramework\Services\AppConfig(
$c->query(IConfig::class),
$c->query('AppName')
);
});
$this->registerService(IInitialState::class, function (SimpleContainer $c) {
return new OC\AppFramework\Services\InitialState(
$c->query(IInitialStateService::class),
$c->query('AppName')
);
});
} }
/** /**