@@ -20,134 +20,134 @@ |
||
20 | 20 | use Test\TestCase; |
21 | 21 | |
22 | 22 | class BackupCodesProviderTest extends TestCase { |
23 | - private string $appName; |
|
24 | - |
|
25 | - private BackupCodeStorage&MockObject $storage; |
|
26 | - private IL10N&MockObject $l10n; |
|
27 | - private AppManager&MockObject $appManager; |
|
28 | - private IInitialState&MockObject $initialState; |
|
29 | - |
|
30 | - private ITemplateManager $templateManager; |
|
31 | - private BackupCodesProvider $provider; |
|
32 | - |
|
33 | - protected function setUp(): void { |
|
34 | - parent::setUp(); |
|
35 | - |
|
36 | - $this->appName = 'twofactor_backupcodes'; |
|
37 | - $this->storage = $this->createMock(BackupCodeStorage::class); |
|
38 | - $this->l10n = $this->createMock(IL10N::class); |
|
39 | - $this->appManager = $this->createMock(AppManager::class); |
|
40 | - $this->initialState = $this->createMock(IInitialState::class); |
|
41 | - $this->templateManager = Server::get(ITemplateManager::class); |
|
42 | - |
|
43 | - $this->provider = new BackupCodesProvider( |
|
44 | - $this->appName, |
|
45 | - $this->storage, |
|
46 | - $this->l10n, |
|
47 | - $this->appManager, |
|
48 | - $this->initialState, |
|
49 | - $this->templateManager, |
|
50 | - ); |
|
51 | - } |
|
52 | - |
|
53 | - public function testGetId(): void { |
|
54 | - $this->assertEquals('backup_codes', $this->provider->getId()); |
|
55 | - } |
|
56 | - |
|
57 | - public function testGetDisplayName(): void { |
|
58 | - $this->l10n->expects($this->once()) |
|
59 | - ->method('t') |
|
60 | - ->with('Backup code') |
|
61 | - ->willReturn('l10n backup code'); |
|
62 | - $this->assertSame('l10n backup code', $this->provider->getDisplayName()); |
|
63 | - } |
|
64 | - |
|
65 | - public function testGetDescription(): void { |
|
66 | - $this->l10n->expects($this->once()) |
|
67 | - ->method('t') |
|
68 | - ->with('Use backup code') |
|
69 | - ->willReturn('l10n use backup code'); |
|
70 | - $this->assertSame('l10n use backup code', $this->provider->getDescription()); |
|
71 | - } |
|
72 | - |
|
73 | - public function testGetTempalte(): void { |
|
74 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
75 | - $expected = $this->templateManager->getTemplate('twofactor_backupcodes', 'challenge'); |
|
76 | - |
|
77 | - $this->assertEquals($expected, $this->provider->getTemplate($user)); |
|
78 | - } |
|
79 | - |
|
80 | - public function testVerfiyChallenge(): void { |
|
81 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
82 | - $challenge = 'xyz'; |
|
83 | - |
|
84 | - $this->storage->expects($this->once()) |
|
85 | - ->method('validateCode') |
|
86 | - ->with($user, $challenge) |
|
87 | - ->willReturn(false); |
|
88 | - |
|
89 | - $this->assertFalse($this->provider->verifyChallenge($user, $challenge)); |
|
90 | - } |
|
91 | - |
|
92 | - public function testIsTwoFactorEnabledForUser(): void { |
|
93 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
94 | - |
|
95 | - $this->storage->expects($this->once()) |
|
96 | - ->method('hasBackupCodes') |
|
97 | - ->with($user) |
|
98 | - ->willReturn(true); |
|
99 | - |
|
100 | - $this->assertTrue($this->provider->isTwoFactorAuthEnabledForUser($user)); |
|
101 | - } |
|
102 | - |
|
103 | - public function testIsActiveNoProviders(): void { |
|
104 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
105 | - |
|
106 | - $this->appManager->expects($this->once()) |
|
107 | - ->method('getEnabledAppsForUser') |
|
108 | - ->with($user) |
|
109 | - ->willReturn([ |
|
110 | - 'twofactor_backupcodes', |
|
111 | - 'mail', |
|
112 | - ]); |
|
113 | - $this->appManager->expects($this->once()) |
|
114 | - ->method('getAppInfo') |
|
115 | - ->with('mail') |
|
116 | - ->willReturn([ |
|
117 | - 'two-factor-providers' => [], |
|
118 | - ]); |
|
119 | - |
|
120 | - $this->assertFalse($this->provider->isActive($user)); |
|
121 | - } |
|
122 | - |
|
123 | - public function testIsActiveWithProviders(): void { |
|
124 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
125 | - |
|
126 | - $this->appManager->expects($this->once()) |
|
127 | - ->method('getEnabledAppsForUser') |
|
128 | - ->with($user) |
|
129 | - ->willReturn([ |
|
130 | - 'twofactor_backupcodes', |
|
131 | - 'twofactor_u2f', |
|
132 | - ]); |
|
133 | - $this->appManager->expects($this->once()) |
|
134 | - ->method('getAppInfo') |
|
135 | - ->with('twofactor_u2f') |
|
136 | - ->willReturn([ |
|
137 | - 'two-factor-providers' => [ |
|
138 | - 'OCA\TwoFactorU2F\Provider\U2FProvider', |
|
139 | - ], |
|
140 | - ]); |
|
141 | - |
|
142 | - $this->assertTrue($this->provider->isActive($user)); |
|
143 | - } |
|
144 | - |
|
145 | - public function testDisable(): void { |
|
146 | - $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
147 | - $this->storage->expects(self::once()) |
|
148 | - ->method('deleteCodes') |
|
149 | - ->with($user); |
|
150 | - |
|
151 | - $this->provider->disableFor($user); |
|
152 | - } |
|
23 | + private string $appName; |
|
24 | + |
|
25 | + private BackupCodeStorage&MockObject $storage; |
|
26 | + private IL10N&MockObject $l10n; |
|
27 | + private AppManager&MockObject $appManager; |
|
28 | + private IInitialState&MockObject $initialState; |
|
29 | + |
|
30 | + private ITemplateManager $templateManager; |
|
31 | + private BackupCodesProvider $provider; |
|
32 | + |
|
33 | + protected function setUp(): void { |
|
34 | + parent::setUp(); |
|
35 | + |
|
36 | + $this->appName = 'twofactor_backupcodes'; |
|
37 | + $this->storage = $this->createMock(BackupCodeStorage::class); |
|
38 | + $this->l10n = $this->createMock(IL10N::class); |
|
39 | + $this->appManager = $this->createMock(AppManager::class); |
|
40 | + $this->initialState = $this->createMock(IInitialState::class); |
|
41 | + $this->templateManager = Server::get(ITemplateManager::class); |
|
42 | + |
|
43 | + $this->provider = new BackupCodesProvider( |
|
44 | + $this->appName, |
|
45 | + $this->storage, |
|
46 | + $this->l10n, |
|
47 | + $this->appManager, |
|
48 | + $this->initialState, |
|
49 | + $this->templateManager, |
|
50 | + ); |
|
51 | + } |
|
52 | + |
|
53 | + public function testGetId(): void { |
|
54 | + $this->assertEquals('backup_codes', $this->provider->getId()); |
|
55 | + } |
|
56 | + |
|
57 | + public function testGetDisplayName(): void { |
|
58 | + $this->l10n->expects($this->once()) |
|
59 | + ->method('t') |
|
60 | + ->with('Backup code') |
|
61 | + ->willReturn('l10n backup code'); |
|
62 | + $this->assertSame('l10n backup code', $this->provider->getDisplayName()); |
|
63 | + } |
|
64 | + |
|
65 | + public function testGetDescription(): void { |
|
66 | + $this->l10n->expects($this->once()) |
|
67 | + ->method('t') |
|
68 | + ->with('Use backup code') |
|
69 | + ->willReturn('l10n use backup code'); |
|
70 | + $this->assertSame('l10n use backup code', $this->provider->getDescription()); |
|
71 | + } |
|
72 | + |
|
73 | + public function testGetTempalte(): void { |
|
74 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
75 | + $expected = $this->templateManager->getTemplate('twofactor_backupcodes', 'challenge'); |
|
76 | + |
|
77 | + $this->assertEquals($expected, $this->provider->getTemplate($user)); |
|
78 | + } |
|
79 | + |
|
80 | + public function testVerfiyChallenge(): void { |
|
81 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
82 | + $challenge = 'xyz'; |
|
83 | + |
|
84 | + $this->storage->expects($this->once()) |
|
85 | + ->method('validateCode') |
|
86 | + ->with($user, $challenge) |
|
87 | + ->willReturn(false); |
|
88 | + |
|
89 | + $this->assertFalse($this->provider->verifyChallenge($user, $challenge)); |
|
90 | + } |
|
91 | + |
|
92 | + public function testIsTwoFactorEnabledForUser(): void { |
|
93 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
94 | + |
|
95 | + $this->storage->expects($this->once()) |
|
96 | + ->method('hasBackupCodes') |
|
97 | + ->with($user) |
|
98 | + ->willReturn(true); |
|
99 | + |
|
100 | + $this->assertTrue($this->provider->isTwoFactorAuthEnabledForUser($user)); |
|
101 | + } |
|
102 | + |
|
103 | + public function testIsActiveNoProviders(): void { |
|
104 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
105 | + |
|
106 | + $this->appManager->expects($this->once()) |
|
107 | + ->method('getEnabledAppsForUser') |
|
108 | + ->with($user) |
|
109 | + ->willReturn([ |
|
110 | + 'twofactor_backupcodes', |
|
111 | + 'mail', |
|
112 | + ]); |
|
113 | + $this->appManager->expects($this->once()) |
|
114 | + ->method('getAppInfo') |
|
115 | + ->with('mail') |
|
116 | + ->willReturn([ |
|
117 | + 'two-factor-providers' => [], |
|
118 | + ]); |
|
119 | + |
|
120 | + $this->assertFalse($this->provider->isActive($user)); |
|
121 | + } |
|
122 | + |
|
123 | + public function testIsActiveWithProviders(): void { |
|
124 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
125 | + |
|
126 | + $this->appManager->expects($this->once()) |
|
127 | + ->method('getEnabledAppsForUser') |
|
128 | + ->with($user) |
|
129 | + ->willReturn([ |
|
130 | + 'twofactor_backupcodes', |
|
131 | + 'twofactor_u2f', |
|
132 | + ]); |
|
133 | + $this->appManager->expects($this->once()) |
|
134 | + ->method('getAppInfo') |
|
135 | + ->with('twofactor_u2f') |
|
136 | + ->willReturn([ |
|
137 | + 'two-factor-providers' => [ |
|
138 | + 'OCA\TwoFactorU2F\Provider\U2FProvider', |
|
139 | + ], |
|
140 | + ]); |
|
141 | + |
|
142 | + $this->assertTrue($this->provider->isActive($user)); |
|
143 | + } |
|
144 | + |
|
145 | + public function testDisable(): void { |
|
146 | + $user = $this->getMockBuilder(IUser::class)->getMock(); |
|
147 | + $this->storage->expects(self::once()) |
|
148 | + ->method('deleteCodes') |
|
149 | + ->with($user); |
|
150 | + |
|
151 | + $this->provider->disableFor($user); |
|
152 | + } |
|
153 | 153 | } |
@@ -21,110 +21,110 @@ |
||
21 | 21 | use OCP\Template\ITemplateManager; |
22 | 22 | |
23 | 23 | class BackupCodesProvider implements IDeactivatableByAdmin, IProvidesPersonalSettings { |
24 | - public function __construct( |
|
25 | - private string $appName, |
|
26 | - private BackupCodeStorage $storage, |
|
27 | - private IL10N $l10n, |
|
28 | - private AppManager $appManager, |
|
29 | - private IInitialState $initialState, |
|
30 | - private ITemplateManager $templateManager, |
|
31 | - ) { |
|
32 | - } |
|
24 | + public function __construct( |
|
25 | + private string $appName, |
|
26 | + private BackupCodeStorage $storage, |
|
27 | + private IL10N $l10n, |
|
28 | + private AppManager $appManager, |
|
29 | + private IInitialState $initialState, |
|
30 | + private ITemplateManager $templateManager, |
|
31 | + ) { |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get unique identifier of this 2FA provider |
|
36 | - * |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public function getId(): string { |
|
40 | - return 'backup_codes'; |
|
41 | - } |
|
34 | + /** |
|
35 | + * Get unique identifier of this 2FA provider |
|
36 | + * |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public function getId(): string { |
|
40 | + return 'backup_codes'; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get the display name for selecting the 2FA provider |
|
45 | - * |
|
46 | - * @return string |
|
47 | - */ |
|
48 | - public function getDisplayName(): string { |
|
49 | - return $this->l10n->t('Backup code'); |
|
50 | - } |
|
43 | + /** |
|
44 | + * Get the display name for selecting the 2FA provider |
|
45 | + * |
|
46 | + * @return string |
|
47 | + */ |
|
48 | + public function getDisplayName(): string { |
|
49 | + return $this->l10n->t('Backup code'); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Get the description for selecting the 2FA provider |
|
54 | - * |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public function getDescription(): string { |
|
58 | - return $this->l10n->t('Use backup code'); |
|
59 | - } |
|
52 | + /** |
|
53 | + * Get the description for selecting the 2FA provider |
|
54 | + * |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public function getDescription(): string { |
|
58 | + return $this->l10n->t('Use backup code'); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Get the template for rending the 2FA provider view |
|
63 | - * |
|
64 | - * @param IUser $user |
|
65 | - * @return ITemplate |
|
66 | - */ |
|
67 | - public function getTemplate(IUser $user): ITemplate { |
|
68 | - return $this->templateManager->getTemplate('twofactor_backupcodes', 'challenge'); |
|
69 | - } |
|
61 | + /** |
|
62 | + * Get the template for rending the 2FA provider view |
|
63 | + * |
|
64 | + * @param IUser $user |
|
65 | + * @return ITemplate |
|
66 | + */ |
|
67 | + public function getTemplate(IUser $user): ITemplate { |
|
68 | + return $this->templateManager->getTemplate('twofactor_backupcodes', 'challenge'); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Verify the given challenge |
|
73 | - * |
|
74 | - * @param IUser $user |
|
75 | - * @param string $challenge |
|
76 | - * @return bool |
|
77 | - */ |
|
78 | - public function verifyChallenge(IUser $user, string $challenge): bool { |
|
79 | - return $this->storage->validateCode($user, $challenge); |
|
80 | - } |
|
71 | + /** |
|
72 | + * Verify the given challenge |
|
73 | + * |
|
74 | + * @param IUser $user |
|
75 | + * @param string $challenge |
|
76 | + * @return bool |
|
77 | + */ |
|
78 | + public function verifyChallenge(IUser $user, string $challenge): bool { |
|
79 | + return $this->storage->validateCode($user, $challenge); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Decides whether 2FA is enabled for the given user |
|
84 | - * |
|
85 | - * @param IUser $user |
|
86 | - * @return boolean |
|
87 | - */ |
|
88 | - public function isTwoFactorAuthEnabledForUser(IUser $user): bool { |
|
89 | - return $this->storage->hasBackupCodes($user); |
|
90 | - } |
|
82 | + /** |
|
83 | + * Decides whether 2FA is enabled for the given user |
|
84 | + * |
|
85 | + * @param IUser $user |
|
86 | + * @return boolean |
|
87 | + */ |
|
88 | + public function isTwoFactorAuthEnabledForUser(IUser $user): bool { |
|
89 | + return $this->storage->hasBackupCodes($user); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Determine whether backup codes should be active or not |
|
94 | - * |
|
95 | - * Backup codes only make sense if at least one 2FA provider is active, |
|
96 | - * hence this method checks all enabled apps on whether they provide 2FA |
|
97 | - * functionality or not. If there's at least one app, backup codes are |
|
98 | - * enabled on the personal settings page. |
|
99 | - * |
|
100 | - * @param IUser $user |
|
101 | - * @return boolean |
|
102 | - */ |
|
103 | - public function isActive(IUser $user): bool { |
|
104 | - $appIds = array_filter($this->appManager->getEnabledAppsForUser($user), function ($appId) { |
|
105 | - return $appId !== $this->appName; |
|
106 | - }); |
|
107 | - foreach ($appIds as $appId) { |
|
108 | - $info = $this->appManager->getAppInfo($appId); |
|
109 | - if (isset($info['two-factor-providers']) && count($info['two-factor-providers']) > 0) { |
|
110 | - return true; |
|
111 | - } |
|
112 | - } |
|
113 | - return false; |
|
114 | - } |
|
92 | + /** |
|
93 | + * Determine whether backup codes should be active or not |
|
94 | + * |
|
95 | + * Backup codes only make sense if at least one 2FA provider is active, |
|
96 | + * hence this method checks all enabled apps on whether they provide 2FA |
|
97 | + * functionality or not. If there's at least one app, backup codes are |
|
98 | + * enabled on the personal settings page. |
|
99 | + * |
|
100 | + * @param IUser $user |
|
101 | + * @return boolean |
|
102 | + */ |
|
103 | + public function isActive(IUser $user): bool { |
|
104 | + $appIds = array_filter($this->appManager->getEnabledAppsForUser($user), function ($appId) { |
|
105 | + return $appId !== $this->appName; |
|
106 | + }); |
|
107 | + foreach ($appIds as $appId) { |
|
108 | + $info = $this->appManager->getAppInfo($appId); |
|
109 | + if (isset($info['two-factor-providers']) && count($info['two-factor-providers']) > 0) { |
|
110 | + return true; |
|
111 | + } |
|
112 | + } |
|
113 | + return false; |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * @param IUser $user |
|
118 | - * |
|
119 | - * @return IPersonalProviderSettings |
|
120 | - */ |
|
121 | - public function getPersonalSettings(IUser $user): IPersonalProviderSettings { |
|
122 | - $state = $this->storage->getBackupCodesState($user); |
|
123 | - $this->initialState->provideInitialState('state', $state); |
|
124 | - return new Personal(); |
|
125 | - } |
|
116 | + /** |
|
117 | + * @param IUser $user |
|
118 | + * |
|
119 | + * @return IPersonalProviderSettings |
|
120 | + */ |
|
121 | + public function getPersonalSettings(IUser $user): IPersonalProviderSettings { |
|
122 | + $state = $this->storage->getBackupCodesState($user); |
|
123 | + $this->initialState->provideInitialState('state', $state); |
|
124 | + return new Personal(); |
|
125 | + } |
|
126 | 126 | |
127 | - public function disableFor(IUser $user): void { |
|
128 | - $this->storage->deleteCodes($user); |
|
129 | - } |
|
127 | + public function disableFor(IUser $user): void { |
|
128 | + $this->storage->deleteCodes($user); |
|
129 | + } |
|
130 | 130 | } |
@@ -15,86 +15,86 @@ |
||
15 | 15 | use Psr\Log\LoggerInterface; |
16 | 16 | |
17 | 17 | class Manager implements IManager { |
18 | - /** @var ?class-string */ |
|
19 | - private ?string $providerClass = null; |
|
20 | - private ?IProvider $provider = null; |
|
18 | + /** @var ?class-string */ |
|
19 | + private ?string $providerClass = null; |
|
20 | + private ?IProvider $provider = null; |
|
21 | 21 | |
22 | - public function __construct( |
|
23 | - private ContainerInterface $container, |
|
24 | - private LoggerInterface $logger, |
|
25 | - ) { |
|
26 | - } |
|
22 | + public function __construct( |
|
23 | + private ContainerInterface $container, |
|
24 | + private LoggerInterface $logger, |
|
25 | + ) { |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * @inheritDoc |
|
30 | - */ |
|
31 | - public function getUserStatuses(array $userIds): array { |
|
32 | - $this->setupProvider(); |
|
33 | - if (!$this->provider) { |
|
34 | - return []; |
|
35 | - } |
|
28 | + /** |
|
29 | + * @inheritDoc |
|
30 | + */ |
|
31 | + public function getUserStatuses(array $userIds): array { |
|
32 | + $this->setupProvider(); |
|
33 | + if (!$this->provider) { |
|
34 | + return []; |
|
35 | + } |
|
36 | 36 | |
37 | - return $this->provider->getUserStatuses($userIds); |
|
38 | - } |
|
37 | + return $this->provider->getUserStatuses($userIds); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @param string $class |
|
42 | - * @since 20.0.0 |
|
43 | - * @internal |
|
44 | - */ |
|
45 | - public function registerProvider(string $class): void { |
|
46 | - $this->providerClass = $class; |
|
47 | - $this->provider = null; |
|
48 | - } |
|
40 | + /** |
|
41 | + * @param string $class |
|
42 | + * @since 20.0.0 |
|
43 | + * @internal |
|
44 | + */ |
|
45 | + public function registerProvider(string $class): void { |
|
46 | + $this->providerClass = $class; |
|
47 | + $this->provider = null; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Lazily set up provider |
|
52 | - */ |
|
53 | - private function setupProvider(): void { |
|
54 | - if ($this->provider !== null) { |
|
55 | - return; |
|
56 | - } |
|
57 | - if ($this->providerClass === null) { |
|
58 | - return; |
|
59 | - } |
|
50 | + /** |
|
51 | + * Lazily set up provider |
|
52 | + */ |
|
53 | + private function setupProvider(): void { |
|
54 | + if ($this->provider !== null) { |
|
55 | + return; |
|
56 | + } |
|
57 | + if ($this->providerClass === null) { |
|
58 | + return; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @psalm-suppress InvalidCatch |
|
63 | - */ |
|
64 | - try { |
|
65 | - $provider = $this->container->get($this->providerClass); |
|
66 | - } catch (ContainerExceptionInterface $e) { |
|
67 | - $this->logger->error('Could not load user-status "' . $this->providerClass . '" provider dynamically: ' . $e->getMessage(), [ |
|
68 | - 'exception' => $e, |
|
69 | - ]); |
|
70 | - return; |
|
71 | - } |
|
61 | + /** |
|
62 | + * @psalm-suppress InvalidCatch |
|
63 | + */ |
|
64 | + try { |
|
65 | + $provider = $this->container->get($this->providerClass); |
|
66 | + } catch (ContainerExceptionInterface $e) { |
|
67 | + $this->logger->error('Could not load user-status "' . $this->providerClass . '" provider dynamically: ' . $e->getMessage(), [ |
|
68 | + 'exception' => $e, |
|
69 | + ]); |
|
70 | + return; |
|
71 | + } |
|
72 | 72 | |
73 | - $this->provider = $provider; |
|
74 | - } |
|
73 | + $this->provider = $provider; |
|
74 | + } |
|
75 | 75 | |
76 | - public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false, ?string $customMessage = null): void { |
|
77 | - $this->setupProvider(); |
|
78 | - if (!$this->provider instanceof ISettableProvider) { |
|
79 | - return; |
|
80 | - } |
|
76 | + public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false, ?string $customMessage = null): void { |
|
77 | + $this->setupProvider(); |
|
78 | + if (!$this->provider instanceof ISettableProvider) { |
|
79 | + return; |
|
80 | + } |
|
81 | 81 | |
82 | - $this->provider->setUserStatus($userId, $messageId, $status, $createBackup, $customMessage); |
|
83 | - } |
|
82 | + $this->provider->setUserStatus($userId, $messageId, $status, $createBackup, $customMessage); |
|
83 | + } |
|
84 | 84 | |
85 | - public function revertUserStatus(string $userId, string $messageId, string $status): void { |
|
86 | - $this->setupProvider(); |
|
87 | - if (!$this->provider instanceof ISettableProvider) { |
|
88 | - return; |
|
89 | - } |
|
90 | - $this->provider->revertUserStatus($userId, $messageId, $status); |
|
91 | - } |
|
85 | + public function revertUserStatus(string $userId, string $messageId, string $status): void { |
|
86 | + $this->setupProvider(); |
|
87 | + if (!$this->provider instanceof ISettableProvider) { |
|
88 | + return; |
|
89 | + } |
|
90 | + $this->provider->revertUserStatus($userId, $messageId, $status); |
|
91 | + } |
|
92 | 92 | |
93 | - public function revertMultipleUserStatus(array $userIds, string $messageId, string $status): void { |
|
94 | - $this->setupProvider(); |
|
95 | - if (!$this->provider instanceof ISettableProvider) { |
|
96 | - return; |
|
97 | - } |
|
98 | - $this->provider->revertMultipleUserStatus($userIds, $messageId, $status); |
|
99 | - } |
|
93 | + public function revertMultipleUserStatus(array $userIds, string $messageId, string $status): void { |
|
94 | + $this->setupProvider(); |
|
95 | + if (!$this->provider instanceof ISettableProvider) { |
|
96 | + return; |
|
97 | + } |
|
98 | + $this->provider->revertMultipleUserStatus($userIds, $messageId, $status); |
|
99 | + } |
|
100 | 100 | } |
@@ -18,111 +18,111 @@ |
||
18 | 18 | |
19 | 19 | class InitialStateService implements IInitialStateService { |
20 | 20 | |
21 | - /** @var string[][] */ |
|
22 | - private array $states = []; |
|
23 | - |
|
24 | - /** @var Closure[][] */ |
|
25 | - private array $lazyStates = []; |
|
26 | - |
|
27 | - |
|
28 | - public function __construct( |
|
29 | - private LoggerInterface $logger, |
|
30 | - private Coordinator $bootstrapCoordinator, |
|
31 | - private ContainerInterface $container, |
|
32 | - ) { |
|
33 | - } |
|
34 | - |
|
35 | - public function provideInitialState(string $appName, string $key, $data): void { |
|
36 | - // Scalars and JsonSerializable are fine |
|
37 | - if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) { |
|
38 | - if (!isset($this->states[$appName])) { |
|
39 | - $this->states[$appName] = []; |
|
40 | - } |
|
41 | - try { |
|
42 | - $this->states[$appName][$key] = json_encode($data, JSON_THROW_ON_ERROR); |
|
43 | - } catch (\JsonException $e) { |
|
44 | - $this->logger->error('Invalid ' . $key . ' data provided to provideInitialState by ' . $appName, ['exception' => $e]); |
|
45 | - } |
|
46 | - return; |
|
47 | - } |
|
48 | - |
|
49 | - $this->logger->warning('Invalid ' . $key . ' data provided to provideInitialState by ' . $appName); |
|
50 | - } |
|
51 | - |
|
52 | - public function provideLazyInitialState(string $appName, string $key, Closure $closure): void { |
|
53 | - if (!isset($this->lazyStates[$appName])) { |
|
54 | - $this->lazyStates[$appName] = []; |
|
55 | - } |
|
56 | - $this->lazyStates[$appName][$key] = $closure; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Invoke all callbacks to populate the `states` property |
|
61 | - */ |
|
62 | - private function invokeLazyStateCallbacks(): void { |
|
63 | - foreach ($this->lazyStates as $app => $lazyStates) { |
|
64 | - foreach ($lazyStates as $key => $lazyState) { |
|
65 | - $startTime = microtime(true); |
|
66 | - $this->provideInitialState($app, $key, $lazyState()); |
|
67 | - $endTime = microtime(true); |
|
68 | - $duration = $endTime - $startTime; |
|
69 | - if ($duration > 1) { |
|
70 | - $this->logger->warning('Lazy initial state provider for {key} took {duration} seconds.', [ |
|
71 | - 'app' => $app, |
|
72 | - 'key' => $key, |
|
73 | - 'duration' => round($duration, 2), |
|
74 | - ]); |
|
75 | - } |
|
76 | - } |
|
77 | - } |
|
78 | - $this->lazyStates = []; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Load the lazy states via the IBootstrap mechanism |
|
83 | - */ |
|
84 | - private function loadLazyStates(): void { |
|
85 | - $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
86 | - |
|
87 | - if ($context === null) { |
|
88 | - // To early, nothing to do yet |
|
89 | - return; |
|
90 | - } |
|
91 | - |
|
92 | - $initialStates = $context->getInitialStates(); |
|
93 | - foreach ($initialStates as $initialState) { |
|
94 | - try { |
|
95 | - $provider = $this->container->query($initialState->getService()); |
|
96 | - } catch (QueryException $e) { |
|
97 | - // Log an continue. We can be fault tolerant here. |
|
98 | - $this->logger->error('Could not load initial state provider dynamically: ' . $e->getMessage(), [ |
|
99 | - 'exception' => $e, |
|
100 | - 'app' => $initialState->getAppId(), |
|
101 | - ]); |
|
102 | - continue; |
|
103 | - } |
|
104 | - |
|
105 | - if (!($provider instanceof InitialStateProvider)) { |
|
106 | - // Log an continue. We can be fault tolerant here. |
|
107 | - $this->logger->error('Initial state provider is not an InitialStateProvider instance: ' . $initialState->getService(), [ |
|
108 | - 'app' => $initialState->getAppId(), |
|
109 | - ]); |
|
110 | - } |
|
111 | - |
|
112 | - $this->provideInitialState($initialState->getAppId(), $provider->getKey(), $provider); |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - public function getInitialStates(): array { |
|
117 | - $this->invokeLazyStateCallbacks(); |
|
118 | - $this->loadLazyStates(); |
|
119 | - |
|
120 | - $appStates = []; |
|
121 | - foreach ($this->states as $app => $states) { |
|
122 | - foreach ($states as $key => $value) { |
|
123 | - $appStates["$app-$key"] = $value; |
|
124 | - } |
|
125 | - } |
|
126 | - return $appStates; |
|
127 | - } |
|
21 | + /** @var string[][] */ |
|
22 | + private array $states = []; |
|
23 | + |
|
24 | + /** @var Closure[][] */ |
|
25 | + private array $lazyStates = []; |
|
26 | + |
|
27 | + |
|
28 | + public function __construct( |
|
29 | + private LoggerInterface $logger, |
|
30 | + private Coordinator $bootstrapCoordinator, |
|
31 | + private ContainerInterface $container, |
|
32 | + ) { |
|
33 | + } |
|
34 | + |
|
35 | + public function provideInitialState(string $appName, string $key, $data): void { |
|
36 | + // Scalars and JsonSerializable are fine |
|
37 | + if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) { |
|
38 | + if (!isset($this->states[$appName])) { |
|
39 | + $this->states[$appName] = []; |
|
40 | + } |
|
41 | + try { |
|
42 | + $this->states[$appName][$key] = json_encode($data, JSON_THROW_ON_ERROR); |
|
43 | + } catch (\JsonException $e) { |
|
44 | + $this->logger->error('Invalid ' . $key . ' data provided to provideInitialState by ' . $appName, ['exception' => $e]); |
|
45 | + } |
|
46 | + return; |
|
47 | + } |
|
48 | + |
|
49 | + $this->logger->warning('Invalid ' . $key . ' data provided to provideInitialState by ' . $appName); |
|
50 | + } |
|
51 | + |
|
52 | + public function provideLazyInitialState(string $appName, string $key, Closure $closure): void { |
|
53 | + if (!isset($this->lazyStates[$appName])) { |
|
54 | + $this->lazyStates[$appName] = []; |
|
55 | + } |
|
56 | + $this->lazyStates[$appName][$key] = $closure; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Invoke all callbacks to populate the `states` property |
|
61 | + */ |
|
62 | + private function invokeLazyStateCallbacks(): void { |
|
63 | + foreach ($this->lazyStates as $app => $lazyStates) { |
|
64 | + foreach ($lazyStates as $key => $lazyState) { |
|
65 | + $startTime = microtime(true); |
|
66 | + $this->provideInitialState($app, $key, $lazyState()); |
|
67 | + $endTime = microtime(true); |
|
68 | + $duration = $endTime - $startTime; |
|
69 | + if ($duration > 1) { |
|
70 | + $this->logger->warning('Lazy initial state provider for {key} took {duration} seconds.', [ |
|
71 | + 'app' => $app, |
|
72 | + 'key' => $key, |
|
73 | + 'duration' => round($duration, 2), |
|
74 | + ]); |
|
75 | + } |
|
76 | + } |
|
77 | + } |
|
78 | + $this->lazyStates = []; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Load the lazy states via the IBootstrap mechanism |
|
83 | + */ |
|
84 | + private function loadLazyStates(): void { |
|
85 | + $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
86 | + |
|
87 | + if ($context === null) { |
|
88 | + // To early, nothing to do yet |
|
89 | + return; |
|
90 | + } |
|
91 | + |
|
92 | + $initialStates = $context->getInitialStates(); |
|
93 | + foreach ($initialStates as $initialState) { |
|
94 | + try { |
|
95 | + $provider = $this->container->query($initialState->getService()); |
|
96 | + } catch (QueryException $e) { |
|
97 | + // Log an continue. We can be fault tolerant here. |
|
98 | + $this->logger->error('Could not load initial state provider dynamically: ' . $e->getMessage(), [ |
|
99 | + 'exception' => $e, |
|
100 | + 'app' => $initialState->getAppId(), |
|
101 | + ]); |
|
102 | + continue; |
|
103 | + } |
|
104 | + |
|
105 | + if (!($provider instanceof InitialStateProvider)) { |
|
106 | + // Log an continue. We can be fault tolerant here. |
|
107 | + $this->logger->error('Initial state provider is not an InitialStateProvider instance: ' . $initialState->getService(), [ |
|
108 | + 'app' => $initialState->getAppId(), |
|
109 | + ]); |
|
110 | + } |
|
111 | + |
|
112 | + $this->provideInitialState($initialState->getAppId(), $provider->getKey(), $provider); |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + public function getInitialStates(): array { |
|
117 | + $this->invokeLazyStateCallbacks(); |
|
118 | + $this->loadLazyStates(); |
|
119 | + |
|
120 | + $appStates = []; |
|
121 | + foreach ($this->states as $app => $states) { |
|
122 | + foreach ($states as $key => $value) { |
|
123 | + $appStates["$app-$key"] = $value; |
|
124 | + } |
|
125 | + } |
|
126 | + return $appStates; |
|
127 | + } |
|
128 | 128 | } |
@@ -64,301 +64,301 @@ |
||
64 | 64 | use Psr\Log\LoggerInterface; |
65 | 65 | |
66 | 66 | class DIContainer extends SimpleContainer implements IAppContainer { |
67 | - protected string $appName; |
|
68 | - private array $middleWares = []; |
|
69 | - private ServerContainer $server; |
|
70 | - |
|
71 | - public function __construct(string $appName, array $urlParams = [], ?ServerContainer $server = null) { |
|
72 | - parent::__construct(); |
|
73 | - $this->appName = $appName; |
|
74 | - $this->registerParameter('appName', $appName); |
|
75 | - $this->registerParameter('urlParams', $urlParams); |
|
76 | - |
|
77 | - /** @deprecated 32.0.0 */ |
|
78 | - $this->registerDeprecatedAlias('Request', IRequest::class); |
|
79 | - |
|
80 | - if ($server === null) { |
|
81 | - $server = \OC::$server; |
|
82 | - } |
|
83 | - $this->server = $server; |
|
84 | - $this->server->registerAppContainer($appName, $this); |
|
85 | - |
|
86 | - // aliases |
|
87 | - /** @deprecated 26.0.0 inject $appName */ |
|
88 | - $this->registerDeprecatedAlias('AppName', 'appName'); |
|
89 | - /** @deprecated 26.0.0 inject $webRoot*/ |
|
90 | - $this->registerDeprecatedAlias('WebRoot', 'webRoot'); |
|
91 | - /** @deprecated 26.0.0 inject $userId */ |
|
92 | - $this->registerDeprecatedAlias('UserId', 'userId'); |
|
93 | - |
|
94 | - /** |
|
95 | - * Core services |
|
96 | - */ |
|
97 | - /* Cannot be an alias because Output is not in OCA */ |
|
98 | - $this->registerService(IOutput::class, fn (ContainerInterface $c): IOutput => new Output($c->get('webRoot'))); |
|
99 | - |
|
100 | - $this->registerService(Folder::class, function () { |
|
101 | - return $this->getServer()->getUserFolder(); |
|
102 | - }); |
|
103 | - |
|
104 | - $this->registerService(IAppData::class, function (ContainerInterface $c): IAppData { |
|
105 | - return $c->get(IAppDataFactory::class)->get($c->get('appName')); |
|
106 | - }); |
|
107 | - |
|
108 | - $this->registerService(IL10N::class, function (ContainerInterface $c) { |
|
109 | - return $this->getServer()->getL10N($c->get('appName')); |
|
110 | - }); |
|
111 | - |
|
112 | - // Log wrappers |
|
113 | - $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { |
|
114 | - /* Cannot be an alias because it uses LoggerInterface so it would infinite loop */ |
|
115 | - return new ScopedPsrLogger( |
|
116 | - $c->get(PsrLoggerAdapter::class), |
|
117 | - $c->get('appName') |
|
118 | - ); |
|
119 | - }); |
|
120 | - |
|
121 | - $this->registerService(IServerContainer::class, function () { |
|
122 | - return $this->getServer(); |
|
123 | - }); |
|
124 | - /** @deprecated 32.0.0 */ |
|
125 | - $this->registerDeprecatedAlias('ServerContainer', IServerContainer::class); |
|
126 | - |
|
127 | - $this->registerAlias(\OCP\WorkflowEngine\IManager::class, Manager::class); |
|
128 | - |
|
129 | - $this->registerService(ContainerInterface::class, fn (ContainerInterface $c) => $c); |
|
130 | - $this->registerDeprecatedAlias(IAppContainer::class, ContainerInterface::class); |
|
131 | - |
|
132 | - // commonly used attributes |
|
133 | - $this->registerService('userId', function (ContainerInterface $c): ?string { |
|
134 | - return $c->get(ISession::class)->get('user_id'); |
|
135 | - }); |
|
136 | - |
|
137 | - $this->registerService('webRoot', function (ContainerInterface $c): string { |
|
138 | - return $c->get(IServerContainer::class)->getWebRoot(); |
|
139 | - }); |
|
140 | - |
|
141 | - $this->registerService('OC_Defaults', function (ContainerInterface $c): object { |
|
142 | - return $c->get(IServerContainer::class)->get('ThemingDefaults'); |
|
143 | - }); |
|
144 | - |
|
145 | - /** @deprecated 32.0.0 */ |
|
146 | - $this->registerDeprecatedAlias('Protocol', Http::class); |
|
147 | - $this->registerService(Http::class, function (ContainerInterface $c) { |
|
148 | - $protocol = $c->get(IRequest::class)->getHttpProtocol(); |
|
149 | - return new Http($_SERVER, $protocol); |
|
150 | - }); |
|
151 | - |
|
152 | - /** @deprecated 32.0.0 */ |
|
153 | - $this->registerDeprecatedAlias('Dispatcher', Dispatcher::class); |
|
154 | - $this->registerService(Dispatcher::class, function (ContainerInterface $c) { |
|
155 | - return new Dispatcher( |
|
156 | - $c->get(Http::class), |
|
157 | - $c->get(MiddlewareDispatcher::class), |
|
158 | - $c->get(IControllerMethodReflector::class), |
|
159 | - $c->get(IRequest::class), |
|
160 | - $c->get(IConfig::class), |
|
161 | - $c->get(IDBConnection::class), |
|
162 | - $c->get(LoggerInterface::class), |
|
163 | - $c->get(EventLogger::class), |
|
164 | - $c, |
|
165 | - ); |
|
166 | - }); |
|
167 | - |
|
168 | - /** |
|
169 | - * App Framework default arguments |
|
170 | - */ |
|
171 | - $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
172 | - $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
173 | - $this->registerParameter('corsMaxAge', 1728000); |
|
174 | - |
|
175 | - /** |
|
176 | - * Middleware |
|
177 | - */ |
|
178 | - /** @deprecated 32.0.0 */ |
|
179 | - $this->registerDeprecatedAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); |
|
180 | - $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) { |
|
181 | - $server = $this->getServer(); |
|
182 | - |
|
183 | - $dispatcher = new MiddlewareDispatcher(); |
|
184 | - |
|
185 | - $dispatcher->registerMiddleware($c->get(CompressionMiddleware::class)); |
|
186 | - $dispatcher->registerMiddleware($c->get(NotModifiedMiddleware::class)); |
|
187 | - $dispatcher->registerMiddleware($c->get(ReloadExecutionMiddleware::class)); |
|
188 | - $dispatcher->registerMiddleware($c->get(SameSiteCookieMiddleware::class)); |
|
189 | - $dispatcher->registerMiddleware($c->get(CORSMiddleware::class)); |
|
190 | - $dispatcher->registerMiddleware($c->get(OCSMiddleware::class)); |
|
191 | - |
|
192 | - $dispatcher->registerMiddleware($c->get(FlowV2EphemeralSessionsMiddleware::class)); |
|
193 | - |
|
194 | - $securityMiddleware = new SecurityMiddleware( |
|
195 | - $c->get(IRequest::class), |
|
196 | - $c->get(IControllerMethodReflector::class), |
|
197 | - $c->get(INavigationManager::class), |
|
198 | - $c->get(IURLGenerator::class), |
|
199 | - $c->get(LoggerInterface::class), |
|
200 | - $c->get('appName'), |
|
201 | - $server->getUserSession()->isLoggedIn(), |
|
202 | - $c->get(IGroupManager::class), |
|
203 | - $c->get(ISubAdmin::class), |
|
204 | - $c->get(IAppManager::class), |
|
205 | - $server->getL10N('lib'), |
|
206 | - $c->get(AuthorizedGroupMapper::class), |
|
207 | - $c->get(IUserSession::class), |
|
208 | - $c->get(IRemoteAddress::class), |
|
209 | - ); |
|
210 | - $dispatcher->registerMiddleware($securityMiddleware); |
|
211 | - $dispatcher->registerMiddleware($c->get(CSPMiddleware::class)); |
|
212 | - $dispatcher->registerMiddleware($c->get(FeaturePolicyMiddleware::class)); |
|
213 | - $dispatcher->registerMiddleware($c->get(PasswordConfirmationMiddleware::class)); |
|
214 | - $dispatcher->registerMiddleware($c->get(TwoFactorMiddleware::class)); |
|
215 | - $dispatcher->registerMiddleware($c->get(BruteForceMiddleware::class)); |
|
216 | - $dispatcher->registerMiddleware($c->get(RateLimitingMiddleware::class)); |
|
217 | - $dispatcher->registerMiddleware($c->get(PublicShareMiddleware::class)); |
|
218 | - $dispatcher->registerMiddleware($c->get(AdditionalScriptsMiddleware::class)); |
|
219 | - |
|
220 | - $coordinator = $c->get(\OC\AppFramework\Bootstrap\Coordinator::class); |
|
221 | - $registrationContext = $coordinator->getRegistrationContext(); |
|
222 | - if ($registrationContext !== null) { |
|
223 | - $appId = $this->get('appName'); |
|
224 | - foreach ($registrationContext->getMiddlewareRegistrations() as $middlewareRegistration) { |
|
225 | - if ($middlewareRegistration->getAppId() === $appId |
|
226 | - || $middlewareRegistration->isGlobal()) { |
|
227 | - $dispatcher->registerMiddleware($c->get($middlewareRegistration->getService())); |
|
228 | - } |
|
229 | - } |
|
230 | - } |
|
231 | - foreach ($this->middleWares as $middleWare) { |
|
232 | - $dispatcher->registerMiddleware($c->get($middleWare)); |
|
233 | - } |
|
234 | - |
|
235 | - $dispatcher->registerMiddleware($c->get(SessionMiddleware::class)); |
|
236 | - return $dispatcher; |
|
237 | - }); |
|
238 | - |
|
239 | - $this->registerAlias(IAppConfig::class, \OC\AppFramework\Services\AppConfig::class); |
|
240 | - $this->registerAlias(IInitialState::class, \OC\AppFramework\Services\InitialState::class); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @return \OCP\IServerContainer |
|
245 | - */ |
|
246 | - public function getServer() { |
|
247 | - return $this->server; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * @param string $middleWare |
|
252 | - */ |
|
253 | - public function registerMiddleWare($middleWare): bool { |
|
254 | - if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
255 | - return false; |
|
256 | - } |
|
257 | - $this->middleWares[] = $middleWare; |
|
258 | - return true; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * used to return the appname of the set application |
|
263 | - * @return string the name of your application |
|
264 | - */ |
|
265 | - public function getAppName() { |
|
266 | - return $this->query('appName'); |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * @deprecated 12.0.0 use IUserSession->isLoggedIn() |
|
271 | - * @return boolean |
|
272 | - */ |
|
273 | - public function isLoggedIn() { |
|
274 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * @deprecated 12.0.0 use IGroupManager->isAdmin($userId) |
|
279 | - * @return boolean |
|
280 | - */ |
|
281 | - public function isAdminUser() { |
|
282 | - $uid = $this->getUserId(); |
|
283 | - return \OC_User::isAdminUser($uid); |
|
284 | - } |
|
285 | - |
|
286 | - private function getUserId(): string { |
|
287 | - return $this->getServer()->getSession()->get('user_id'); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Register a capability |
|
292 | - * |
|
293 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
294 | - */ |
|
295 | - public function registerCapability($serviceName) { |
|
296 | - $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
297 | - return $this->query($serviceName); |
|
298 | - }); |
|
299 | - } |
|
300 | - |
|
301 | - public function has($id): bool { |
|
302 | - if (parent::has($id)) { |
|
303 | - return true; |
|
304 | - } |
|
305 | - |
|
306 | - if ($this->server->has($id, true)) { |
|
307 | - return true; |
|
308 | - } |
|
309 | - |
|
310 | - return false; |
|
311 | - } |
|
312 | - |
|
313 | - public function query(string $name, bool $autoload = true) { |
|
314 | - if ($name === 'AppName' || $name === 'appName') { |
|
315 | - return $this->appName; |
|
316 | - } |
|
317 | - |
|
318 | - $isServerClass = str_starts_with($name, 'OCP\\') || str_starts_with($name, 'OC\\'); |
|
319 | - if ($isServerClass && !$this->has($name)) { |
|
320 | - return $this->getServer()->query($name, $autoload); |
|
321 | - } |
|
322 | - |
|
323 | - try { |
|
324 | - return $this->queryNoFallback($name); |
|
325 | - } catch (QueryException $firstException) { |
|
326 | - try { |
|
327 | - return $this->getServer()->query($name, $autoload); |
|
328 | - } catch (QueryException $secondException) { |
|
329 | - if ($firstException->getCode() === 1) { |
|
330 | - throw $secondException; |
|
331 | - } |
|
332 | - throw $firstException; |
|
333 | - } |
|
334 | - } |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @param string $name |
|
339 | - * @return mixed |
|
340 | - * @throws QueryException if the query could not be resolved |
|
341 | - */ |
|
342 | - public function queryNoFallback($name) { |
|
343 | - $name = $this->sanitizeName($name); |
|
344 | - |
|
345 | - if ($this->offsetExists($name)) { |
|
346 | - return parent::query($name); |
|
347 | - } elseif ($this->appName === 'settings' && str_starts_with($name, 'OC\\Settings\\')) { |
|
348 | - return parent::query($name); |
|
349 | - } elseif ($this->appName === 'core' && str_starts_with($name, 'OC\\Core\\')) { |
|
350 | - return parent::query($name); |
|
351 | - } elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName) . '\\')) { |
|
352 | - return parent::query($name); |
|
353 | - } elseif ( |
|
354 | - str_starts_with($name, 'OC\\AppFramework\\Services\\') |
|
355 | - || str_starts_with($name, 'OC\\AppFramework\\Middleware\\') |
|
356 | - ) { |
|
357 | - /* AppFramework services are scoped to the application */ |
|
358 | - return parent::query($name); |
|
359 | - } |
|
360 | - |
|
361 | - throw new QueryException('Could not resolve ' . $name . '!' |
|
362 | - . ' Class can not be instantiated', 1); |
|
363 | - } |
|
67 | + protected string $appName; |
|
68 | + private array $middleWares = []; |
|
69 | + private ServerContainer $server; |
|
70 | + |
|
71 | + public function __construct(string $appName, array $urlParams = [], ?ServerContainer $server = null) { |
|
72 | + parent::__construct(); |
|
73 | + $this->appName = $appName; |
|
74 | + $this->registerParameter('appName', $appName); |
|
75 | + $this->registerParameter('urlParams', $urlParams); |
|
76 | + |
|
77 | + /** @deprecated 32.0.0 */ |
|
78 | + $this->registerDeprecatedAlias('Request', IRequest::class); |
|
79 | + |
|
80 | + if ($server === null) { |
|
81 | + $server = \OC::$server; |
|
82 | + } |
|
83 | + $this->server = $server; |
|
84 | + $this->server->registerAppContainer($appName, $this); |
|
85 | + |
|
86 | + // aliases |
|
87 | + /** @deprecated 26.0.0 inject $appName */ |
|
88 | + $this->registerDeprecatedAlias('AppName', 'appName'); |
|
89 | + /** @deprecated 26.0.0 inject $webRoot*/ |
|
90 | + $this->registerDeprecatedAlias('WebRoot', 'webRoot'); |
|
91 | + /** @deprecated 26.0.0 inject $userId */ |
|
92 | + $this->registerDeprecatedAlias('UserId', 'userId'); |
|
93 | + |
|
94 | + /** |
|
95 | + * Core services |
|
96 | + */ |
|
97 | + /* Cannot be an alias because Output is not in OCA */ |
|
98 | + $this->registerService(IOutput::class, fn (ContainerInterface $c): IOutput => new Output($c->get('webRoot'))); |
|
99 | + |
|
100 | + $this->registerService(Folder::class, function () { |
|
101 | + return $this->getServer()->getUserFolder(); |
|
102 | + }); |
|
103 | + |
|
104 | + $this->registerService(IAppData::class, function (ContainerInterface $c): IAppData { |
|
105 | + return $c->get(IAppDataFactory::class)->get($c->get('appName')); |
|
106 | + }); |
|
107 | + |
|
108 | + $this->registerService(IL10N::class, function (ContainerInterface $c) { |
|
109 | + return $this->getServer()->getL10N($c->get('appName')); |
|
110 | + }); |
|
111 | + |
|
112 | + // Log wrappers |
|
113 | + $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { |
|
114 | + /* Cannot be an alias because it uses LoggerInterface so it would infinite loop */ |
|
115 | + return new ScopedPsrLogger( |
|
116 | + $c->get(PsrLoggerAdapter::class), |
|
117 | + $c->get('appName') |
|
118 | + ); |
|
119 | + }); |
|
120 | + |
|
121 | + $this->registerService(IServerContainer::class, function () { |
|
122 | + return $this->getServer(); |
|
123 | + }); |
|
124 | + /** @deprecated 32.0.0 */ |
|
125 | + $this->registerDeprecatedAlias('ServerContainer', IServerContainer::class); |
|
126 | + |
|
127 | + $this->registerAlias(\OCP\WorkflowEngine\IManager::class, Manager::class); |
|
128 | + |
|
129 | + $this->registerService(ContainerInterface::class, fn (ContainerInterface $c) => $c); |
|
130 | + $this->registerDeprecatedAlias(IAppContainer::class, ContainerInterface::class); |
|
131 | + |
|
132 | + // commonly used attributes |
|
133 | + $this->registerService('userId', function (ContainerInterface $c): ?string { |
|
134 | + return $c->get(ISession::class)->get('user_id'); |
|
135 | + }); |
|
136 | + |
|
137 | + $this->registerService('webRoot', function (ContainerInterface $c): string { |
|
138 | + return $c->get(IServerContainer::class)->getWebRoot(); |
|
139 | + }); |
|
140 | + |
|
141 | + $this->registerService('OC_Defaults', function (ContainerInterface $c): object { |
|
142 | + return $c->get(IServerContainer::class)->get('ThemingDefaults'); |
|
143 | + }); |
|
144 | + |
|
145 | + /** @deprecated 32.0.0 */ |
|
146 | + $this->registerDeprecatedAlias('Protocol', Http::class); |
|
147 | + $this->registerService(Http::class, function (ContainerInterface $c) { |
|
148 | + $protocol = $c->get(IRequest::class)->getHttpProtocol(); |
|
149 | + return new Http($_SERVER, $protocol); |
|
150 | + }); |
|
151 | + |
|
152 | + /** @deprecated 32.0.0 */ |
|
153 | + $this->registerDeprecatedAlias('Dispatcher', Dispatcher::class); |
|
154 | + $this->registerService(Dispatcher::class, function (ContainerInterface $c) { |
|
155 | + return new Dispatcher( |
|
156 | + $c->get(Http::class), |
|
157 | + $c->get(MiddlewareDispatcher::class), |
|
158 | + $c->get(IControllerMethodReflector::class), |
|
159 | + $c->get(IRequest::class), |
|
160 | + $c->get(IConfig::class), |
|
161 | + $c->get(IDBConnection::class), |
|
162 | + $c->get(LoggerInterface::class), |
|
163 | + $c->get(EventLogger::class), |
|
164 | + $c, |
|
165 | + ); |
|
166 | + }); |
|
167 | + |
|
168 | + /** |
|
169 | + * App Framework default arguments |
|
170 | + */ |
|
171 | + $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
172 | + $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
173 | + $this->registerParameter('corsMaxAge', 1728000); |
|
174 | + |
|
175 | + /** |
|
176 | + * Middleware |
|
177 | + */ |
|
178 | + /** @deprecated 32.0.0 */ |
|
179 | + $this->registerDeprecatedAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); |
|
180 | + $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) { |
|
181 | + $server = $this->getServer(); |
|
182 | + |
|
183 | + $dispatcher = new MiddlewareDispatcher(); |
|
184 | + |
|
185 | + $dispatcher->registerMiddleware($c->get(CompressionMiddleware::class)); |
|
186 | + $dispatcher->registerMiddleware($c->get(NotModifiedMiddleware::class)); |
|
187 | + $dispatcher->registerMiddleware($c->get(ReloadExecutionMiddleware::class)); |
|
188 | + $dispatcher->registerMiddleware($c->get(SameSiteCookieMiddleware::class)); |
|
189 | + $dispatcher->registerMiddleware($c->get(CORSMiddleware::class)); |
|
190 | + $dispatcher->registerMiddleware($c->get(OCSMiddleware::class)); |
|
191 | + |
|
192 | + $dispatcher->registerMiddleware($c->get(FlowV2EphemeralSessionsMiddleware::class)); |
|
193 | + |
|
194 | + $securityMiddleware = new SecurityMiddleware( |
|
195 | + $c->get(IRequest::class), |
|
196 | + $c->get(IControllerMethodReflector::class), |
|
197 | + $c->get(INavigationManager::class), |
|
198 | + $c->get(IURLGenerator::class), |
|
199 | + $c->get(LoggerInterface::class), |
|
200 | + $c->get('appName'), |
|
201 | + $server->getUserSession()->isLoggedIn(), |
|
202 | + $c->get(IGroupManager::class), |
|
203 | + $c->get(ISubAdmin::class), |
|
204 | + $c->get(IAppManager::class), |
|
205 | + $server->getL10N('lib'), |
|
206 | + $c->get(AuthorizedGroupMapper::class), |
|
207 | + $c->get(IUserSession::class), |
|
208 | + $c->get(IRemoteAddress::class), |
|
209 | + ); |
|
210 | + $dispatcher->registerMiddleware($securityMiddleware); |
|
211 | + $dispatcher->registerMiddleware($c->get(CSPMiddleware::class)); |
|
212 | + $dispatcher->registerMiddleware($c->get(FeaturePolicyMiddleware::class)); |
|
213 | + $dispatcher->registerMiddleware($c->get(PasswordConfirmationMiddleware::class)); |
|
214 | + $dispatcher->registerMiddleware($c->get(TwoFactorMiddleware::class)); |
|
215 | + $dispatcher->registerMiddleware($c->get(BruteForceMiddleware::class)); |
|
216 | + $dispatcher->registerMiddleware($c->get(RateLimitingMiddleware::class)); |
|
217 | + $dispatcher->registerMiddleware($c->get(PublicShareMiddleware::class)); |
|
218 | + $dispatcher->registerMiddleware($c->get(AdditionalScriptsMiddleware::class)); |
|
219 | + |
|
220 | + $coordinator = $c->get(\OC\AppFramework\Bootstrap\Coordinator::class); |
|
221 | + $registrationContext = $coordinator->getRegistrationContext(); |
|
222 | + if ($registrationContext !== null) { |
|
223 | + $appId = $this->get('appName'); |
|
224 | + foreach ($registrationContext->getMiddlewareRegistrations() as $middlewareRegistration) { |
|
225 | + if ($middlewareRegistration->getAppId() === $appId |
|
226 | + || $middlewareRegistration->isGlobal()) { |
|
227 | + $dispatcher->registerMiddleware($c->get($middlewareRegistration->getService())); |
|
228 | + } |
|
229 | + } |
|
230 | + } |
|
231 | + foreach ($this->middleWares as $middleWare) { |
|
232 | + $dispatcher->registerMiddleware($c->get($middleWare)); |
|
233 | + } |
|
234 | + |
|
235 | + $dispatcher->registerMiddleware($c->get(SessionMiddleware::class)); |
|
236 | + return $dispatcher; |
|
237 | + }); |
|
238 | + |
|
239 | + $this->registerAlias(IAppConfig::class, \OC\AppFramework\Services\AppConfig::class); |
|
240 | + $this->registerAlias(IInitialState::class, \OC\AppFramework\Services\InitialState::class); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @return \OCP\IServerContainer |
|
245 | + */ |
|
246 | + public function getServer() { |
|
247 | + return $this->server; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * @param string $middleWare |
|
252 | + */ |
|
253 | + public function registerMiddleWare($middleWare): bool { |
|
254 | + if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
255 | + return false; |
|
256 | + } |
|
257 | + $this->middleWares[] = $middleWare; |
|
258 | + return true; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * used to return the appname of the set application |
|
263 | + * @return string the name of your application |
|
264 | + */ |
|
265 | + public function getAppName() { |
|
266 | + return $this->query('appName'); |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * @deprecated 12.0.0 use IUserSession->isLoggedIn() |
|
271 | + * @return boolean |
|
272 | + */ |
|
273 | + public function isLoggedIn() { |
|
274 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * @deprecated 12.0.0 use IGroupManager->isAdmin($userId) |
|
279 | + * @return boolean |
|
280 | + */ |
|
281 | + public function isAdminUser() { |
|
282 | + $uid = $this->getUserId(); |
|
283 | + return \OC_User::isAdminUser($uid); |
|
284 | + } |
|
285 | + |
|
286 | + private function getUserId(): string { |
|
287 | + return $this->getServer()->getSession()->get('user_id'); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Register a capability |
|
292 | + * |
|
293 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
294 | + */ |
|
295 | + public function registerCapability($serviceName) { |
|
296 | + $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
297 | + return $this->query($serviceName); |
|
298 | + }); |
|
299 | + } |
|
300 | + |
|
301 | + public function has($id): bool { |
|
302 | + if (parent::has($id)) { |
|
303 | + return true; |
|
304 | + } |
|
305 | + |
|
306 | + if ($this->server->has($id, true)) { |
|
307 | + return true; |
|
308 | + } |
|
309 | + |
|
310 | + return false; |
|
311 | + } |
|
312 | + |
|
313 | + public function query(string $name, bool $autoload = true) { |
|
314 | + if ($name === 'AppName' || $name === 'appName') { |
|
315 | + return $this->appName; |
|
316 | + } |
|
317 | + |
|
318 | + $isServerClass = str_starts_with($name, 'OCP\\') || str_starts_with($name, 'OC\\'); |
|
319 | + if ($isServerClass && !$this->has($name)) { |
|
320 | + return $this->getServer()->query($name, $autoload); |
|
321 | + } |
|
322 | + |
|
323 | + try { |
|
324 | + return $this->queryNoFallback($name); |
|
325 | + } catch (QueryException $firstException) { |
|
326 | + try { |
|
327 | + return $this->getServer()->query($name, $autoload); |
|
328 | + } catch (QueryException $secondException) { |
|
329 | + if ($firstException->getCode() === 1) { |
|
330 | + throw $secondException; |
|
331 | + } |
|
332 | + throw $firstException; |
|
333 | + } |
|
334 | + } |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @param string $name |
|
339 | + * @return mixed |
|
340 | + * @throws QueryException if the query could not be resolved |
|
341 | + */ |
|
342 | + public function queryNoFallback($name) { |
|
343 | + $name = $this->sanitizeName($name); |
|
344 | + |
|
345 | + if ($this->offsetExists($name)) { |
|
346 | + return parent::query($name); |
|
347 | + } elseif ($this->appName === 'settings' && str_starts_with($name, 'OC\\Settings\\')) { |
|
348 | + return parent::query($name); |
|
349 | + } elseif ($this->appName === 'core' && str_starts_with($name, 'OC\\Core\\')) { |
|
350 | + return parent::query($name); |
|
351 | + } elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName) . '\\')) { |
|
352 | + return parent::query($name); |
|
353 | + } elseif ( |
|
354 | + str_starts_with($name, 'OC\\AppFramework\\Services\\') |
|
355 | + || str_starts_with($name, 'OC\\AppFramework\\Middleware\\') |
|
356 | + ) { |
|
357 | + /* AppFramework services are scoped to the application */ |
|
358 | + return parent::query($name); |
|
359 | + } |
|
360 | + |
|
361 | + throw new QueryException('Could not resolve ' . $name . '!' |
|
362 | + . ' Class can not be instantiated', 1); |
|
363 | + } |
|
364 | 364 | } |
@@ -15,37 +15,37 @@ |
||
15 | 15 | * @see \OCP\AppFramework\Services\IInitialState |
16 | 16 | */ |
17 | 17 | interface IInitialStateService { |
18 | - /** |
|
19 | - * Allows an app to provide its initial state to the template system. |
|
20 | - * Use this if you know your initial state still be used for example if |
|
21 | - * you are in the render function of you controller. |
|
22 | - * |
|
23 | - * @since 16.0.0 |
|
24 | - * |
|
25 | - * @param string $appName |
|
26 | - * @param string $key |
|
27 | - * @param bool|int|float|string|array|\JsonSerializable $data |
|
28 | - * |
|
29 | - * @deprecated 21 Use {@see \OCP\AppFramework\Services\IInitialState} or {@see \OCP\AppFramework\Services\InitialStateProvider} |
|
30 | - * @see \OCP\AppFramework\Services\IInitialState::provideInitialState() |
|
31 | - */ |
|
32 | - public function provideInitialState(string $appName, string $key, $data): void; |
|
18 | + /** |
|
19 | + * Allows an app to provide its initial state to the template system. |
|
20 | + * Use this if you know your initial state still be used for example if |
|
21 | + * you are in the render function of you controller. |
|
22 | + * |
|
23 | + * @since 16.0.0 |
|
24 | + * |
|
25 | + * @param string $appName |
|
26 | + * @param string $key |
|
27 | + * @param bool|int|float|string|array|\JsonSerializable $data |
|
28 | + * |
|
29 | + * @deprecated 21 Use {@see \OCP\AppFramework\Services\IInitialState} or {@see \OCP\AppFramework\Services\InitialStateProvider} |
|
30 | + * @see \OCP\AppFramework\Services\IInitialState::provideInitialState() |
|
31 | + */ |
|
32 | + public function provideInitialState(string $appName, string $key, $data): void; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Allows an app to provide its initial state via a lazy method. |
|
36 | - * This will call the closure when the template is being generated. |
|
37 | - * Use this if your app is injected into pages. Since then the render method |
|
38 | - * is not called explicitly. But we do not want to load the state on webdav |
|
39 | - * requests for example. |
|
40 | - * |
|
41 | - * @since 16.0.0 |
|
42 | - * |
|
43 | - * @param string $appName |
|
44 | - * @param string $key |
|
45 | - * @param Closure $closure returns a primitive or an object that implements JsonSerializable |
|
46 | - * |
|
47 | - * @deprecated 21 Use {@see \OCP\AppFramework\Services\IInitialState} or {@see \OCP\AppFramework\Services\InitialStateProvider} |
|
48 | - * @see \OCP\AppFramework\Services\IInitialState::provideLazyInitialState() |
|
49 | - */ |
|
50 | - public function provideLazyInitialState(string $appName, string $key, Closure $closure): void; |
|
34 | + /** |
|
35 | + * Allows an app to provide its initial state via a lazy method. |
|
36 | + * This will call the closure when the template is being generated. |
|
37 | + * Use this if your app is injected into pages. Since then the render method |
|
38 | + * is not called explicitly. But we do not want to load the state on webdav |
|
39 | + * requests for example. |
|
40 | + * |
|
41 | + * @since 16.0.0 |
|
42 | + * |
|
43 | + * @param string $appName |
|
44 | + * @param string $key |
|
45 | + * @param Closure $closure returns a primitive or an object that implements JsonSerializable |
|
46 | + * |
|
47 | + * @deprecated 21 Use {@see \OCP\AppFramework\Services\IInitialState} or {@see \OCP\AppFramework\Services\InitialStateProvider} |
|
48 | + * @see \OCP\AppFramework\Services\IInitialState::provideLazyInitialState() |
|
49 | + */ |
|
50 | + public function provideLazyInitialState(string $appName, string $key, Closure $closure): void; |
|
51 | 51 | } |