Skip to content

Commit 8cf9e5c

Browse files
committed
enh: warn about table use in app during manager demotion
Signed-off-by: Cleopatra Enjeck M <patrathewhiz@gmail.com>
1 parent 9bf572c commit 8cf9e5c

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

appinfo/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
['name' => 'ApiTables#update', 'url' => '/api/2/tables/{id}', 'verb' => 'PUT'],
133133
['name' => 'ApiTables#destroy', 'url' => '/api/2/tables/{id}', 'verb' => 'DELETE'],
134134
['name' => 'ApiTables#transfer', 'url' => '/api/2/tables/{id}/transfer', 'verb' => 'PUT'],
135+
['name' => 'ApiTables#indexContext', 'url' => '/api/2/tables/{id}/index-context', 'verb' => 'GET'],
135136

136137
['name' => 'ApiColumns#index', 'url' => '/api/2/columns/{nodeType}/{nodeId}', 'verb' => 'GET'],
137138
['name' => 'ApiColumns#show', 'url' => '/api/2/columns/{id}', 'verb' => 'GET'],

lib/Controller/ApiTablesController.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,31 @@ public function transfer(int $id, string $newOwnerUserId): DataResponse {
288288
return $this->handleNotFoundError($e);
289289
}
290290
}
291+
292+
293+
/**
294+
* [api v2] Index contexts
295+
*
296+
* Index contexts used in a table
297+
*
298+
* @param int $id Table ID
299+
*
300+
* @return DataResponse<Http::STATUS_OK, TablesTable, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
301+
*
302+
* 200: Contexts listed
303+
* 403: No permissions
304+
* 404: Table not found
305+
*/
306+
#[NoAdminRequired]
307+
public function indexContext(int $id): DataResponse {
308+
try {
309+
return new DataResponse(['contexts' => $this->service->listContextsByTable($id, $this->userId)]);
310+
} catch (PermissionError $e) {
311+
return $this->handlePermissionError($e);
312+
} catch (InternalError $e) {
313+
return $this->handleError($e);
314+
} catch (NotFoundError $e) {
315+
return $this->handleNotFoundError($e);
316+
}
317+
}
291318
}

lib/Service/ContextService.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ public function findById(int $id, ?string $userId): Context {
111111
return $this->contextMapper->findById($id, $userId);
112112
}
113113

114+
115+
/**
116+
* @throws Exception
117+
* @throws InternalError
118+
* @throws NotFoundError
119+
*/
120+
public function findByTableId(int $tableId, ?string $userId): array {
121+
if ($userId !== null && trim($userId) === '') {
122+
$userId = null;
123+
}
124+
if ($userId === null && !$this->isCLI) {
125+
$error = 'Try to set no user in context, but request is not allowed.';
126+
$this->logger->warning($error);
127+
throw new InternalError($error);
128+
}
129+
130+
return $this->contextMapper->findAllContainingNode(Application::NODE_TYPE_TABLE, $tableId, $userId);
131+
}
132+
114133
/**
115134
* @psalm-param list<array{id: int, type: int, permissions?: int, order?: int}> $nodes
116135
* @throws Exception|PermissionError|InvalidArgumentException

lib/Service/TableService.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,18 @@ public function getScheme(int $id): TableScheme {
562562
return new TableScheme($table->getTitle(), $table->getEmoji(), $columns, $table->getViews(), $table->getDescription(), $this->appManager->getAppVersion("tables"));
563563
}
564564

565+
566+
/**
567+
* @throws PermissionError
568+
* @throws NotFoundError
569+
* @throws InternalError
570+
*/
571+
public function listContextsByTable(int $id, ?string $userId = null): array {
572+
$contexts = $this->contextService->findByTableId($id, $userId);
573+
return $contexts;
574+
}
575+
576+
565577
// PRIVATE FUNCTIONS ---------------------------------------------------------------
566578

567579
/**

src/modules/sidebar/partials/ShareList.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ import Crown from 'vue-material-design-icons/Crown.vue'
127127
import Information from 'vue-material-design-icons/Information.vue'
128128
import Account from 'vue-material-design-icons/Account.vue'
129129
import moment from '@nextcloud/moment'
130+
import { showWarning } from '@nextcloud/dialogs'
131+
import '@nextcloud/dialogs/style.css'
130132
131133
export default {
132134
components: {
@@ -208,7 +210,13 @@ export default {
208210
promoteToManager(share) {
209211
this.$emit('update', { id: share.id, permission: 'manage', value: true })
210212
},
211-
demoteManager(share) {
213+
async demoteManager(share) {
214+
// TODO: might need to pass in the share receiver when fetching the contexts
215+
// it seems like we only get the contexts for the currently logged in user
216+
const contexts = await this.$store.dispatch('loadContextsForTable', { tableId: this.activeElement.id })
217+
if (Array.isArray(contexts.contexts) && contexts.contexts.length && contexts.contexts.find(context => context.owner === share.receiver)) {
218+
showWarning(t('tables', 'The account has created an application based on this table which will continue to consume its data'))
219+
}
212220
this.$emit('update', { id: share.id, permission: 'manage', value: false })
213221
},
214222
personHasTableManagePermission(userId) {

src/store/store.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ export default new Vuex.Store({
508508
return res?.data
509509
},
510510

511+
async loadContextsForTable({ commit, state, getters }, { tableId }) {
512+
const res = await axios.get(generateOcsUrl('/apps/tables/api/2/tables/' + tableId + '/index-context'))
513+
return res?.data.ocs.data
514+
},
515+
511516
async transferContext({ state, commit, dispatch }, { id, data }) {
512517
try {
513518
await axios.put(generateOcsUrl('/apps/tables/api/2/contexts/' + id + '/transfer'), data)

0 commit comments

Comments
 (0)