From 5b43f6509cf9da465571525183a2146c8e616b75 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Tue, 10 Sep 2024 16:57:58 +0200 Subject: [PATCH 1/3] refactor(settings): Use Promise for API requests Signed-off-by: provokateurin --- package-lock.json | 10 ---------- package.json | 1 - src/settings/Api.ts | 31 +++++++++++++++---------------- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index f210b9e69..88a81def4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,7 +36,6 @@ "@nextcloud/webpack-vue-config": "^5.5.1", "@types/bootstrap": "^5.2.10", "@types/jest": "^29.5.12", - "@types/jquery": "^3.5.29", "@types/react": "^17.0.43", "@types/react-dom": "^17.0.0", "@types/webpack": "^5.28.5", @@ -4988,15 +4987,6 @@ "pretty-format": "^29.0.0" } }, - "node_modules/@types/jquery": { - "version": "3.5.29", - "resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.5.29.tgz", - "integrity": "sha512-oXQQC9X9MOPRrMhPHHOsXqeQDnWeCDT3PelUIg/Oy8FAbzSZtFHRjc7IpbfFVmpLtJ+UOoywpRsuO5Jxjybyeg==", - "dev": true, - "dependencies": { - "@types/sizzle": "*" - } - }, "node_modules/@types/jsdom": { "version": "20.0.1", "resolved": "https://registry.npmjs.org/@types/jsdom/-/jsdom-20.0.1.tgz", diff --git a/package.json b/package.json index f8d103607..f81fd3c2c 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "@nextcloud/webpack-vue-config": "^5.5.1", "@types/bootstrap": "^5.2.10", "@types/jest": "^29.5.12", - "@types/jquery": "^3.5.29", "@types/react": "^17.0.43", "@types/react-dom": "^17.0.0", "@types/webpack": "^5.28.5", diff --git a/src/settings/Api.ts b/src/settings/Api.ts index 806bd02c2..cb968a57c 100644 --- a/src/settings/Api.ts +++ b/src/settings/Api.ts @@ -1,7 +1,6 @@ import { generateUrl } from '@nextcloud/router' import { OCSResult, AxiosOCSResult } from 'NC' import axios from '@nextcloud/axios' -import Thenable = JQuery.Thenable; export interface Group { gid: string; @@ -46,25 +45,25 @@ export class Api { return OC.generateUrl(`apps/groupfolders/${endpoint}`) } - listFolders(): Thenable { + async listFolders(): Promise { return $.getJSON(this.getUrl('folders')) .then((data: OCSResult) => Object.keys(data.ocs.data).map(id => data.ocs.data[id])) } // Returns all NC groups - listGroups(): Thenable { + async listGroups(): Promise { return $.getJSON(this.getUrl('delegation/groups')) .then((data: OCSResult) => data.ocs.data) } // Returns all visible NC circles - listCircles(): Thenable { + async listCircles(): Promise { return $.getJSON(this.getUrl('delegation/circles')) .then((data: OCSResult) => data.ocs.data) } // Returns all groups that have been granted delegated admin or subadmin rights on groupfolders - listDelegatedGroups(classname: string): Thenable { + async listDelegatedGroups(classname: string): Promise { return axios.get(this.getUrl('/delegation/authorized-groups'), { params: { classname } }) .then((data: AxiosOCSResult) => { // The admin group is always there. We don't want the user to remove it @@ -74,46 +73,46 @@ export class Api { } // Updates the list of groups that have been granted delegated admin or subadmin rights on groupfolders - updateDelegatedGroups(newGroups: Group[], classname: string): Thenable { + async updateDelegatedGroups(newGroups: Group[], classname: string): Promise { return axios.post(generateUrl('/apps/settings/') + '/settings/authorizedgroups/saveSettings', { newGroups, class: classname, }).then((data) => data.data) } - createFolder(mountPoint: string): Thenable { + async createFolder(mountPoint: string): Promise { return $.post(this.getUrl('folders'), { mountpoint: mountPoint }, null, 'json').then((data: OCSResult<{ id: number; }>) => data.ocs.data.id) } - deleteFolder(id: number): Thenable { + async deleteFolder(id: number): Promise { return $.ajax({ url: this.getUrl(`folders/${id}`), type: 'DELETE' }) } - addGroup(folderId: number, group: string): Thenable { + async addGroup(folderId: number, group: string): Promise { return $.post(this.getUrl(`folders/${folderId}/groups`), { group }) } - removeGroup(folderId: number, group: string): Thenable { + async removeGroup(folderId: number, group: string): Promise { return $.ajax({ url: this.getUrl(`folders/${folderId}/groups/${group}`), type: 'DELETE' }) } - setPermissions(folderId: number, group: string, permissions: number): Thenable { + async setPermissions(folderId: number, group: string, permissions: number): Promise { return $.post(this.getUrl(`folders/${folderId}/groups/${group}`), { permissions }) } - setManageACL(folderId: number, type: string, id: string, manageACL: boolean): Thenable { + async setManageACL(folderId: number, type: string, id: string, manageACL: boolean): Promise { return $.post(this.getUrl(`folders/${folderId}/manageACL`), { mappingType: type, mappingId: id, @@ -121,25 +120,25 @@ export class Api { }) } - setQuota(folderId: number, quota: number): Thenable { + async setQuota(folderId: number, quota: number): Promise { return $.post(this.getUrl(`folders/${folderId}/quota`), { quota }) } - setACL(folderId: number, acl: boolean): Thenable { + async setACL(folderId: number, acl: boolean): Promise { return $.post(this.getUrl(`folders/${folderId}/acl`), { acl: acl ? 1 : 0 }) } - renameFolder(folderId: number, mountpoint: string): Thenable { + async renameFolder(folderId: number, mountpoint: string): Promise { return $.post(this.getUrl(`folders/${folderId}/mountpoint`), { mountpoint }) } - aclMappingSearch(folderId: number, search: string): Thenable<{groups: OCSGroup[], users: OCSUser[]}> { + async aclMappingSearch(folderId: number, search: string): Promise<{groups: ManageRuleProps[], users: ManageRuleProps[]}> { return $.getJSON(this.getUrl(`folders/${folderId}/search?format=json&search=${search}`)) .then((data: OCSResult<{ groups: OCSGroup[]; users: OCSUser[]; }>) => { return { From 91e060283943774afcdc3fb2af7979593bdee164 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Tue, 10 Sep 2024 17:53:48 +0200 Subject: [PATCH 2/3] refactor(settings): Remove usage of jQuery Signed-off-by: provokateurin --- src/settings/Api.ts | 105 +++++++++++++++--------------------- src/settings/Nextcloud.d.ts | 32 ----------- 2 files changed, 43 insertions(+), 94 deletions(-) diff --git a/src/settings/Api.ts b/src/settings/Api.ts index cb968a57c..944aae1ff 100644 --- a/src/settings/Api.ts +++ b/src/settings/Api.ts @@ -1,6 +1,7 @@ import { generateUrl } from '@nextcloud/router' -import { OCSResult, AxiosOCSResult } from 'NC' import axios from '@nextcloud/axios' +// eslint-disable-next-line n/no-unpublished-import +import type { OCSResponse } from '@nextcloud/typings/lib/ocs' export interface Group { gid: string; @@ -28,7 +29,6 @@ export interface ManageRuleProps { displayname: string; } - export interface Folder { id: number; mount_point: string; @@ -46,117 +46,98 @@ export class Api { } async listFolders(): Promise { - return $.getJSON(this.getUrl('folders')) - .then((data: OCSResult) => Object.keys(data.ocs.data).map(id => data.ocs.data[id])) + const response = await axios.get>(this.getUrl('folders')) + return Object.keys(response.data.ocs.data).map(id => response.data.ocs.data[id]) } // Returns all NC groups async listGroups(): Promise { - return $.getJSON(this.getUrl('delegation/groups')) - .then((data: OCSResult) => data.ocs.data) + const response = await axios.get>(this.getUrl('delegation/groups')) + return response.data.ocs.data } // Returns all visible NC circles async listCircles(): Promise { - return $.getJSON(this.getUrl('delegation/circles')) - .then((data: OCSResult) => data.ocs.data) + const response = await axios.get>(this.getUrl('delegation/circles')) + return response.data.ocs.data } // Returns all groups that have been granted delegated admin or subadmin rights on groupfolders async listDelegatedGroups(classname: string): Promise { - return axios.get(this.getUrl('/delegation/authorized-groups'), { params: { classname } }) - .then((data: AxiosOCSResult) => { - // The admin group is always there. We don't want the user to remove it - const groups = data.data.ocs.data.filter(g => g.gid !== 'admin') - return groups - }) + const response = await axios.get>(this.getUrl('/delegation/authorized-groups'), { params: { classname } }) + return response.data.ocs.data.filter(g => g.gid !== 'admin') } // Updates the list of groups that have been granted delegated admin or subadmin rights on groupfolders async updateDelegatedGroups(newGroups: Group[], classname: string): Promise { - return axios.post(generateUrl('/apps/settings/') + '/settings/authorizedgroups/saveSettings', { + await axios.post(generateUrl('/apps/settings/') + '/settings/authorizedgroups/saveSettings', { newGroups, class: classname, - }).then((data) => data.data) + }) } async createFolder(mountPoint: string): Promise { - return $.post(this.getUrl('folders'), { - mountpoint: mountPoint - }, null, 'json').then((data: OCSResult<{ id: number; }>) => data.ocs.data.id) + const response = await axios.post>(this.getUrl('folders'), { mountpoint: mountPoint }) + return response.data.ocs.data } async deleteFolder(id: number): Promise { - return $.ajax({ - url: this.getUrl(`folders/${id}`), - type: 'DELETE' - }) + await axios.delete(this.getUrl(`folders/${id}`)) } async addGroup(folderId: number, group: string): Promise { - return $.post(this.getUrl(`folders/${folderId}/groups`), { - group - }) + await axios.post(this.getUrl(`folders/${folderId}/groups`), { group }) } async removeGroup(folderId: number, group: string): Promise { - return $.ajax({ - url: this.getUrl(`folders/${folderId}/groups/${group}`), - type: 'DELETE' - }) + await axios.delete(this.getUrl(`folders/${folderId}/groups/${group}`)) } async setPermissions(folderId: number, group: string, permissions: number): Promise { - return $.post(this.getUrl(`folders/${folderId}/groups/${group}`), { - permissions - }) + await axios.post(this.getUrl(`folders/${folderId}/groups/${group}`), { permissions }) } async setManageACL(folderId: number, type: string, id: string, manageACL: boolean): Promise { - return $.post(this.getUrl(`folders/${folderId}/manageACL`), { + await axios.post(this.getUrl(`folders/${folderId}/manageACL`), { mappingType: type, mappingId: id, - manageAcl: manageACL ? 1 : 0 + manageAcl: manageACL ? 1 : 0, }) } async setQuota(folderId: number, quota: number): Promise { - return $.post(this.getUrl(`folders/${folderId}/quota`), { - quota - }) + await axios.post(this.getUrl(`folders/${folderId}/quota`), { quota }) } async setACL(folderId: number, acl: boolean): Promise { - return $.post(this.getUrl(`folders/${folderId}/acl`), { - acl: acl ? 1 : 0 - }) + await axios.post(this.getUrl(`folders/${folderId}/acl`), { acl: acl ? 1 : 0 }) } async renameFolder(folderId: number, mountpoint: string): Promise { - return $.post(this.getUrl(`folders/${folderId}/mountpoint`), { - mountpoint - }) + await axios.post(this.getUrl(`folders/${folderId}/mountpoint`), { mountpoint }) } - async aclMappingSearch(folderId: number, search: string): Promise<{groups: ManageRuleProps[], users: ManageRuleProps[]}> { - return $.getJSON(this.getUrl(`folders/${folderId}/search?format=json&search=${search}`)) - .then((data: OCSResult<{ groups: OCSGroup[]; users: OCSUser[]; }>) => { + async aclMappingSearch(folderId: number, search: string): Promise<{ + groups: ManageRuleProps[], + users: ManageRuleProps[] + }> { + const response = await axios.get>(this.getUrl(`folders/${folderId}/search`), { params: { search } }) + return { + groups: Object.values(response.data.ocs.data.groups).map((item) => { return { - groups: Object.values(data.ocs.data.groups).map((item) => { - return { - type: 'group', - id: item.gid, - displayname: item.displayname - } - }), - users: Object.values(data.ocs.data.users).map((item) => { - return { - type: 'user', - id: item.uid, - displayname: item.displayname - } - }) + type: 'group', + id: item.gid, + displayname: item.displayname, } - }) + }), + users: Object.values(response.data.ocs.data.users).map((item) => { + return { + type: 'user', + id: item.uid, + displayname: item.displayname, + } + }), + } } + } diff --git a/src/settings/Nextcloud.d.ts b/src/settings/Nextcloud.d.ts index 41dd6cbd8..6dab658ae 100644 --- a/src/settings/Nextcloud.d.ts +++ b/src/settings/Nextcloud.d.ts @@ -72,35 +72,3 @@ declare namespace OC { } declare function t(app: string, string: string, vars?: { [key: string]: string }, count?: number, options?: EscapeOptions): string; - -declare module 'NC' { - export interface OCSResult { - ocs: { - data: T; - meta: { - status: 'ok' | 'failure'; - message: string; - statuscode: number; - totalitems: number; - itemsperpage: number; - } - } - } -} - -declare module 'NC' { - export interface AxiosOCSResult { - data: { - ocs: { - data: T; - meta: { - status: 'ok' | 'failure'; - message: string; - statuscode: number; - totalitems: number; - itemsperpage: number; - } - } - } - } -} From 6ba331de2c36cde5210695343047d02316a89bbe Mon Sep 17 00:00:00 2001 From: provokateurin Date: Tue, 10 Sep 2024 18:16:52 +0200 Subject: [PATCH 3/3] fix(settings): Fix config handling Signed-off-by: provokateurin --- lib/Controller/FolderController.php | 11 ++++++++ package-lock.json | 39 +++++++++++++++++++++++++++++ package.json | 1 + src/settings/Api.ts | 21 ++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index 22e9da7f0..0b3c9df88 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -27,6 +27,7 @@ use OCA\GroupFolders\Service\DelegationService; use OCA\GroupFolders\Service\FoldersFilter; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; use OCP\Files\IRootFolder; @@ -160,6 +161,7 @@ private function getRootFolderStorageId(): ?int { * @RequireGroupFolderAdmin * @NoAdminRequired */ + #[PasswordConfirmationRequired] public function addFolder(string $mountpoint): DataResponse { $id = $this->manager->createFolder(trim($mountpoint)); return new DataResponse(['id' => $id]); @@ -169,6 +171,7 @@ public function addFolder(string $mountpoint): DataResponse { * @NoAdminRequired * @RequireGroupFolderAdmin */ + #[PasswordConfirmationRequired] public function removeFolder(int $id): DataResponse { $response = $this->checkFolderExists($id); if ($response) { @@ -184,6 +187,7 @@ public function removeFolder(int $id): DataResponse { * @NoAdminRequired * @RequireGroupFolderAdmin */ + #[PasswordConfirmationRequired] public function setMountPoint(int $id, string $mountPoint): DataResponse { $this->manager->renameFolder($id, trim($mountPoint)); return new DataResponse(['success' => true]); @@ -193,6 +197,7 @@ public function setMountPoint(int $id, string $mountPoint): DataResponse { * @NoAdminRequired * @RequireGroupFolderAdmin */ + #[PasswordConfirmationRequired] public function addGroup(int $id, string $group): DataResponse { $response = $this->checkFolderExists($id); if ($response) { @@ -206,6 +211,7 @@ public function addGroup(int $id, string $group): DataResponse { * @NoAdminRequired * @RequireGroupFolderAdmin */ + #[PasswordConfirmationRequired] public function removeGroup(int $id, string $group): DataResponse { $response = $this->checkFolderExists($id); if ($response) { @@ -219,6 +225,7 @@ public function removeGroup(int $id, string $group): DataResponse { * @NoAdminRequired * @RequireGroupFolderAdmin */ + #[PasswordConfirmationRequired] public function setPermissions(int $id, string $group, int $permissions): DataResponse { $response = $this->checkFolderExists($id); if ($response) { @@ -233,6 +240,7 @@ public function setPermissions(int $id, string $group, int $permissions): DataRe * @RequireGroupFolderAdmin * @throws \OCP\DB\Exception */ + #[PasswordConfirmationRequired] public function setManageACL(int $id, string $mappingType, string $mappingId, bool $manageAcl): DataResponse { $response = $this->checkFolderExists($id); if ($response) { @@ -246,6 +254,7 @@ public function setManageACL(int $id, string $mappingType, string $mappingId, bo * @NoAdminRequired * @RequireGroupFolderAdmin */ + #[PasswordConfirmationRequired] public function setQuota(int $id, int $quota): DataResponse { $response = $this->checkFolderExists($id); if ($response) { @@ -259,6 +268,7 @@ public function setQuota(int $id, int $quota): DataResponse { * @NoAdminRequired * @RequireGroupFolderAdmin */ + #[PasswordConfirmationRequired] public function setACL(int $id, bool $acl): DataResponse { $response = $this->checkFolderExists($id); if ($response) { @@ -272,6 +282,7 @@ public function setACL(int $id, bool $acl): DataResponse { * @NoAdminRequired * @RequireGroupFolderAdmin */ + #[PasswordConfirmationRequired] public function renameFolder(int $id, string $mountpoint): DataResponse { $response = $this->checkFolderExists($id); if ($response) { diff --git a/package-lock.json b/package-lock.json index 88a81def4..0ecd1c1f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@nextcloud/files": "^3.8.0", "@nextcloud/initial-state": "^2.2.0", "@nextcloud/l10n": "^2.2.0", + "@nextcloud/password-confirmation": "^5.1.1", "@nextcloud/router": "^3.0.1", "@nextcloud/vue": "^8.17.0", "nextcloud-server": "^0.15.10", @@ -4405,6 +4406,44 @@ "npm": "^10.0.0" } }, + "node_modules/@nextcloud/password-confirmation": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/@nextcloud/password-confirmation/-/password-confirmation-5.1.1.tgz", + "integrity": "sha512-UlQcjVe/fr/JaJ6TWaRM+yBLIEZRU6RWMy0JoExcA6UVJs2HJrRIyVMuiCLuIYlH23ReJH+z7zFI3+V7vdeJ1Q==", + "license": "MIT", + "dependencies": { + "@nextcloud/axios": "^2.5.0", + "@nextcloud/l10n": "^3.1.0", + "@nextcloud/router": "^3.0.1" + }, + "engines": { + "node": "^20.0.0", + "npm": "^10.0.0" + }, + "peerDependencies": { + "@nextcloud/vue": "^8.0.0", + "vue": "^2.7.16" + } + }, + "node_modules/@nextcloud/password-confirmation/node_modules/@nextcloud/l10n": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@nextcloud/l10n/-/l10n-3.1.0.tgz", + "integrity": "sha512-unciqr8QSJ29vFBw9S1bquyoj1PTWHszNL8tcUNuxUAYpq0hX+8o7rpB5gimELA4sj4m9+VCJwgLtBZd1Yj0lg==", + "license": "GPL-3.0-or-later", + "dependencies": { + "@nextcloud/router": "^3.0.1", + "@nextcloud/typings": "^1.8.0", + "@types/dompurify": "^3.0.5", + "@types/escape-html": "^1.0.4", + "dompurify": "^3.1.2", + "escape-html": "^1.0.3", + "node-gettext": "^3.0.0" + }, + "engines": { + "node": "^20.0.0", + "npm": "^10.0.0" + } + }, "node_modules/@nextcloud/paths": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@nextcloud/paths/-/paths-2.2.1.tgz", diff --git a/package.json b/package.json index f81fd3c2c..71493326d 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "@nextcloud/files": "^3.8.0", "@nextcloud/initial-state": "^2.2.0", "@nextcloud/l10n": "^2.2.0", + "@nextcloud/password-confirmation": "^5.1.1", "@nextcloud/router": "^3.0.1", "@nextcloud/vue": "^8.17.0", "nextcloud-server": "^0.15.10", diff --git a/src/settings/Api.ts b/src/settings/Api.ts index 944aae1ff..e9e6d5399 100644 --- a/src/settings/Api.ts +++ b/src/settings/Api.ts @@ -1,5 +1,6 @@ import { generateUrl } from '@nextcloud/router' import axios from '@nextcloud/axios' +import { confirmPassword } from '@nextcloud/password-confirmation' // eslint-disable-next-line n/no-unpublished-import import type { OCSResponse } from '@nextcloud/typings/lib/ocs' @@ -70,6 +71,8 @@ export class Api { // Updates the list of groups that have been granted delegated admin or subadmin rights on groupfolders async updateDelegatedGroups(newGroups: Group[], classname: string): Promise { + await confirmPassword() + await axios.post(generateUrl('/apps/settings/') + '/settings/authorizedgroups/saveSettings', { newGroups, class: classname, @@ -77,27 +80,39 @@ export class Api { } async createFolder(mountPoint: string): Promise { + await confirmPassword() + const response = await axios.post>(this.getUrl('folders'), { mountpoint: mountPoint }) return response.data.ocs.data } async deleteFolder(id: number): Promise { + await confirmPassword() + await axios.delete(this.getUrl(`folders/${id}`)) } async addGroup(folderId: number, group: string): Promise { + await confirmPassword() + await axios.post(this.getUrl(`folders/${folderId}/groups`), { group }) } async removeGroup(folderId: number, group: string): Promise { + await confirmPassword() + await axios.delete(this.getUrl(`folders/${folderId}/groups/${group}`)) } async setPermissions(folderId: number, group: string, permissions: number): Promise { + await confirmPassword() + await axios.post(this.getUrl(`folders/${folderId}/groups/${group}`), { permissions }) } async setManageACL(folderId: number, type: string, id: string, manageACL: boolean): Promise { + await confirmPassword() + await axios.post(this.getUrl(`folders/${folderId}/manageACL`), { mappingType: type, mappingId: id, @@ -106,14 +121,20 @@ export class Api { } async setQuota(folderId: number, quota: number): Promise { + await confirmPassword() + await axios.post(this.getUrl(`folders/${folderId}/quota`), { quota }) } async setACL(folderId: number, acl: boolean): Promise { + await confirmPassword() + await axios.post(this.getUrl(`folders/${folderId}/acl`), { acl: acl ? 1 : 0 }) } async renameFolder(folderId: number, mountpoint: string): Promise { + await confirmPassword() + await axios.post(this.getUrl(`folders/${folderId}/mountpoint`), { mountpoint }) }