refs #62 add admin option to toggle default token usage for anonymous users
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
This commit is contained in:
Родитель
057fa8125b
Коммит
b3748773a5
|
@ -382,18 +382,44 @@ class GithubAPIService {
|
|||
return $this->request($userId, $endpoint, [], 'GET', true, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $userId
|
||||
* @param bool $endpointUsesDefaultToken
|
||||
* @return string
|
||||
*/
|
||||
public function getAccessToken(?string $userId, bool $endpointUsesDefaultToken = false): string {
|
||||
// use user access token in priority
|
||||
$accessToken = '';
|
||||
// for logged in users
|
||||
if ($userId !== null) {
|
||||
$accessToken = $this->config->getUserValue($userId, Application::APP_ID, 'token');
|
||||
// fallback to admin default token if $useDefaultToken
|
||||
if ($accessToken === '' && $endpointUsesDefaultToken) {
|
||||
$accessToken = $this->config->getAppValue(Application::APP_ID, 'default_link_token');
|
||||
}
|
||||
} elseif ($endpointUsesDefaultToken) {
|
||||
// anonymous users
|
||||
$allowDefaultTokenToAnonymous = $this->config->getAppValue(Application::APP_ID, 'default_link_token_for_anonymous', '1') === '1';
|
||||
if ($allowDefaultTokenToAnonymous) {
|
||||
$accessToken = $this->config->getAppValue(Application::APP_ID, 'default_link_token');
|
||||
}
|
||||
}
|
||||
|
||||
return $accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an authenticated HTTP request to GitHub API
|
||||
* @param string|null $userId
|
||||
* @param string $endPoint The path to reach in api.github.com
|
||||
* @param array $params Query parameters (key/val pairs)
|
||||
* @param string $method HTTP query method
|
||||
* @param bool $useDefaultToken
|
||||
* @param bool $endpointUsesDefaultToken
|
||||
* @param int $timeout
|
||||
* @return array decoded request result or error
|
||||
*/
|
||||
public function request(?string $userId, string $endPoint, array $params = [], string $method = 'GET',
|
||||
bool $useDefaultToken = false, int $timeout = 30): array {
|
||||
bool $endpointUsesDefaultToken = false, int $timeout = 30): array {
|
||||
try {
|
||||
$url = 'https://api.github.com/' . $endPoint;
|
||||
$options = [
|
||||
|
@ -402,15 +428,7 @@ class GithubAPIService {
|
|||
'User-Agent' => 'Nextcloud GitHub integration',
|
||||
],
|
||||
];
|
||||
// use user access token in priority
|
||||
// fallback to admin default token if $useDefaultToken
|
||||
$accessToken = '';
|
||||
if ($userId !== null) {
|
||||
$accessToken = $this->config->getUserValue($userId, Application::APP_ID, 'token');
|
||||
}
|
||||
if ($accessToken === '' && $useDefaultToken) {
|
||||
$accessToken = $this->config->getAppValue(Application::APP_ID, 'default_link_token');
|
||||
}
|
||||
$accessToken = $this->getAccessToken($userId, $endpointUsesDefaultToken);
|
||||
if ($accessToken !== '') {
|
||||
$options['headers']['Authorization'] = 'token ' . $accessToken;
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ class Admin implements ISettings {
|
|||
$usePopup = $this->config->getAppValue(Application::APP_ID, 'use_popup', '0');
|
||||
$adminLinkPreviewEnabled = $this->config->getAppValue(Application::APP_ID, 'link_preview_enabled', '1') === '1';
|
||||
$defaultLinkToken = $this->config->getAppValue(Application::APP_ID, 'default_link_token');
|
||||
$allowDefaultTokenToAnonymous = $this->config->getAppValue(Application::APP_ID, 'default_link_token_for_anonymous', '1') === '1';
|
||||
|
||||
$adminConfig = [
|
||||
'client_id' => $clientID,
|
||||
|
@ -40,6 +41,7 @@ class Admin implements ISettings {
|
|||
'use_popup' => ($usePopup === '1'),
|
||||
'link_preview_enabled' => $adminLinkPreviewEnabled,
|
||||
'default_link_token' => $defaultLinkToken,
|
||||
'default_link_token_for_anonymous' => $allowDefaultTokenToAnonymous,
|
||||
];
|
||||
$this->initialStateService->provideInitialState('admin-config', $adminConfig);
|
||||
return new TemplateResponse(Application::APP_ID, 'adminSettings');
|
||||
|
|
|
@ -69,6 +69,12 @@
|
|||
@input="onInput"
|
||||
@focus="readonly = false">
|
||||
</div>
|
||||
<NcCheckboxRadioSwitch
|
||||
:checked="state.default_link_token_for_anonymous"
|
||||
:disabled="!state.default_link_token"
|
||||
@update:checked="onCheckboxChanged($event, 'default_link_token_for_anonymous')">
|
||||
{{ t('integration_github', 'Use default access token for anonymous users') }}
|
||||
</NcCheckboxRadioSwitch>
|
||||
<NcCheckboxRadioSwitch
|
||||
:checked="state.use_popup"
|
||||
@update:checked="onCheckboxChanged($event, 'use_popup')">
|
||||
|
|
Загрузка…
Ссылка в новой задаче