Skip to content

Commit

Permalink
Fix pre-complete PAT permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
gsvd committed Feb 23, 2025
1 parent 9e75c54 commit 14e44d2
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 18 deletions.
9 changes: 9 additions & 0 deletions routers/web/user/setting/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package setting

import (
"encoding/json"

Check failure on line 8 in routers/web/user/setting/applications.go

View workflow job for this annotation

GitHub Actions / lint-backend

import 'encoding/json' is not allowed from list 'main': use gitea's modules/json instead of encoding/json (depguard)

Check failure on line 8 in routers/web/user/setting/applications.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

import 'encoding/json' is not allowed from list 'main': use gitea's modules/json instead of encoding/json (depguard)

Check failure on line 8 in routers/web/user/setting/applications.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

import 'encoding/json' is not allowed from list 'main': use gitea's modules/json instead of encoding/json (depguard)
"net/http"

auth_model "code.gitea.io/gitea/models/auth"
Expand Down Expand Up @@ -42,6 +43,14 @@ func ApplicationsPost(ctx *context.Context) {
if ctx.HasError() {
loadApplicationsData(ctx)

// Send back the previously selected scopes as a JSON string
scopes, err := json.Marshal(form.Scope)
if err != nil {
ctx.ServerError("json.Marshal", err)
return
}
ctx.Data["Scopes"] = string(scopes)

ctx.HTML(http.StatusOK, tplSettingsApplications)
return
}
Expand Down
1 change: 1 addition & 0 deletions templates/user/settings/applications.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
data-no-access-label="{{ctx.Locale.Tr "settings.permission_no_access"}}"
data-read-label="{{ctx.Locale.Tr "settings.permission_read"}}"
data-write-label="{{ctx.Locale.Tr "settings.permission_write"}}"
data-scopes="{{.Scopes}}"
data-locale-component-failed-to-load="{{ctx.Locale.Tr "graphs.component_failed_to_load"}}"
>
</div>
Expand Down
114 changes: 96 additions & 18 deletions web_src/js/components/ScopedAccessTokenSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,104 @@ const props = defineProps<{
noAccessLabel: string;
readLabel: string;
writeLabel: string;
scopes: string[];
}>();
const categories = computed(() => {
const categories = [
'activitypub',
];
const categories = {
'activitypub': {
read: {
value: 'read:activitypub',
selected: props.scopes.includes('read:activitypub'),
},
write: {
value: 'write:activitypub',
selected: props.scopes.includes('write:activitypub'),
},
},
};
if (props.isAdmin) {
categories.push('admin');
categories['admin'] = {

Check failure on line 27 in web_src/js/components/ScopedAccessTokenSelector.vue

View workflow job for this annotation

GitHub Actions / frontend

Element implicitly has an 'any' type because expression of type '"admin"' can't be used to index type '{ activitypub: { read: { value: string; selected: boolean; }; write: { value: string; selected: boolean; }; }; }'.
read: {
value: 'read:admin',
selected: props.scopes.includes('read:admin'),
},
write: {
value: 'write:admin',
selected: props.scopes.includes('write:admin'),
},
};
}
categories.push(
'issue',
'misc',
'notification',
'organization',
'package',
'repository',
'user');
categories['issue'] = {

Check failure on line 38 in web_src/js/components/ScopedAccessTokenSelector.vue

View workflow job for this annotation

GitHub Actions / frontend

Element implicitly has an 'any' type because expression of type '"issue"' can't be used to index type '{ activitypub: { read: { value: string; selected: boolean; }; write: { value: string; selected: boolean; }; }; }'.
read: {
value: 'read:issue',
selected: props.scopes.includes('read:issue'),
},
write: {
value: 'write:issue',
selected: props.scopes.includes('write:issue'),
},
};
categories['misc'] = {

Check failure on line 48 in web_src/js/components/ScopedAccessTokenSelector.vue

View workflow job for this annotation

GitHub Actions / frontend

Element implicitly has an 'any' type because expression of type '"misc"' can't be used to index type '{ activitypub: { read: { value: string; selected: boolean; }; write: { value: string; selected: boolean; }; }; }'.
read: {
value: 'read:misc',
selected: props.scopes.includes('read:misc'),
},
write: {
value: 'write:misc',
selected: props.scopes.includes('write:misc'),
},
};
categories['notification'] = {

Check failure on line 58 in web_src/js/components/ScopedAccessTokenSelector.vue

View workflow job for this annotation

GitHub Actions / frontend

Element implicitly has an 'any' type because expression of type '"notification"' can't be used to index type '{ activitypub: { read: { value: string; selected: boolean; }; write: { value: string; selected: boolean; }; }; }'.
read: {
value: 'read:notification',
selected: props.scopes.includes('read:notification'),
},
write: {
value: 'write:notification',
selected: props.scopes.includes('write:notification'),
},
};
categories['organization'] = {

Check failure on line 68 in web_src/js/components/ScopedAccessTokenSelector.vue

View workflow job for this annotation

GitHub Actions / frontend

Element implicitly has an 'any' type because expression of type '"organization"' can't be used to index type '{ activitypub: { read: { value: string; selected: boolean; }; write: { value: string; selected: boolean; }; }; }'.
read: {
value: 'read:organization',
selected: props.scopes.includes('read:organization'),
},
write: {
value: 'write:organization',
selected: props.scopes.includes('write:organization'),
},
};
categories['package'] = {

Check failure on line 78 in web_src/js/components/ScopedAccessTokenSelector.vue

View workflow job for this annotation

GitHub Actions / frontend

Element implicitly has an 'any' type because expression of type '"package"' can't be used to index type '{ activitypub: { read: { value: string; selected: boolean; }; write: { value: string; selected: boolean; }; }; }'.
read: {
value: 'read:package',
selected: props.scopes.includes('read:package'),
},
write: {
value: 'write:package',
selected: props.scopes.includes('write:package'),
},
};
categories['repository'] = {

Check failure on line 88 in web_src/js/components/ScopedAccessTokenSelector.vue

View workflow job for this annotation

GitHub Actions / frontend

Element implicitly has an 'any' type because expression of type '"repository"' can't be used to index type '{ activitypub: { read: { value: string; selected: boolean; }; write: { value: string; selected: boolean; }; }; }'.
read: {
value: 'read:repository',
selected: props.scopes.includes('read:repository'),
},
write: {
value: 'write:repository',
selected: props.scopes.includes('write:repository'),
},
};
categories['user'] = {

Check failure on line 98 in web_src/js/components/ScopedAccessTokenSelector.vue

View workflow job for this annotation

GitHub Actions / frontend

Element implicitly has an 'any' type because expression of type '"user"' can't be used to index type '{ activitypub: { read: { value: string; selected: boolean; }; write: { value: string; selected: boolean; }; }; }'.
read: {
value: 'read:user',
selected: props.scopes.includes('read:user'),
},
write: {
value: 'write:user',
selected: props.scopes.includes('write:user'),
},
};
return categories;
});
Expand Down Expand Up @@ -56,7 +137,7 @@ function onClickSubmit(e: Event) {
</script>

<template>
<div v-for="category in categories" :key="category" class="field tw-pl-1 tw-pb-1 access-token-category">
<div v-for="(permissions, category) in categories" :key="category" class="field tw-pl-1 tw-pb-1 access-token-category">
<label class="category-label" :for="'access-token-scope-' + category">
{{ category }}
</label>
Expand All @@ -69,11 +150,8 @@ function onClickSubmit(e: Event) {
<option value="">
{{ noAccessLabel }}
</option>
<option :value="'read:' + category">
{{ readLabel }}
</option>
<option :value="'write:' + category">
{{ writeLabel }}
<option v-for="(permission, action) in permissions" :key="permission.value" :value="permission.value" :selected="permission.selected">
{{ action === 'read' ? readLabel : writeLabel }}
</option>
</select>
</div>
Expand Down
1 change: 1 addition & 0 deletions web_src/js/features/scoped-access-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export async function initScopedAccessTokenCategories() {
noAccessLabel: el.getAttribute('data-no-access-label'),
readLabel: el.getAttribute('data-read-label'),
writeLabel: el.getAttribute('data-write-label'),
scopes: JSON.parse(el.getAttribute('data-scopes')),
});
View.mount(el);
} catch (err) {
Expand Down

0 comments on commit 14e44d2

Please sign in to comment.