Skip to content

Commit 8c49ba2

Browse files
committed
feat: add share list to stat
1 parent 0dbc6aa commit 8c49ba2

File tree

2 files changed

+108
-5
lines changed

2 files changed

+108
-5
lines changed

packages/backend/src/filesystem/FSNodeContext.js

+49-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const { Context } = require("../util/context");
2525
const { MultiDetachable } = require("../util/listenerutil");
2626
const { NodeRawEntrySelector } = require("./node/selectors");
2727
const { DB_READ } = require("../services/database/consts");
28+
const { UserActorType } = require("../services/auth/Actor");
29+
const { PermissionUtil } = require("../services/auth/PermissionService");
2830

2931
/**
3032
* Container for information collected about a node
@@ -387,13 +389,55 @@ module.exports = class FSNodeContext {
387389
* then, stores them on the `permissions` property
388390
* of the fsentry.
389391
* @param {bool} force fetch shares if they were already fetched
390-
*
391-
* @deprecated sharing will use user-to-user permissions
392392
*/
393393
async fetchShares (force) {
394-
// NOOP: this was for legacy sharing functionality;
395-
// this is being re-implemented with permissions
396-
return;
394+
if (this.entry.shares && ! force ) return;
395+
396+
const actor = Context.get('actor');
397+
if ( ! actor ) {
398+
this.entry.shares = { users: [], apps: [] };
399+
return;
400+
}
401+
402+
if ( ! (actor.type instanceof UserActorType) ) {
403+
this.entry.shares = { users: [], apps: [] };
404+
return;
405+
}
406+
407+
const svc_permission = this.services.get('permission');
408+
409+
const permissions =
410+
await svc_permission.query_issuer_permissions_by_prefix(
411+
actor.type.user, `fs:${await this.get('uid')}:`);
412+
413+
this.entry.shares = { users: [], apps: [] };
414+
415+
for ( const user_perm of permissions.users ) {
416+
const access =
417+
PermissionUtil.split(user_perm.permission).slice(-1)[0];
418+
this.entry.shares.users.push({
419+
user: {
420+
uid: user_perm.user.uuid,
421+
username: user_perm.user.username,
422+
},
423+
access,
424+
permission: user_perm.permission,
425+
});
426+
}
427+
428+
for ( const app_perm of permissions.apps ) {
429+
const access =
430+
PermissionUtil.split(app_perm.permission).slice(-1)[0];
431+
this.entry.shares.apps.push({
432+
app: {
433+
icon: app_perm.app.icon,
434+
uid: app_perm.app.uid,
435+
name: app_perm.app.name,
436+
},
437+
access,
438+
permission: app_perm.permission,
439+
});
440+
}
397441
}
398442

399443
/**

packages/backend/src/services/auth/PermissionService.js

+59
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,13 @@ class PermissionService extends BaseService {
587587
/**
588588
* List the users that have any permissions granted to the
589589
* specified user.
590+
*
591+
* This is a "flat" (non-cascading) view.
592+
*
593+
* Use History:
594+
* - This was written for use in ll_listusers to display
595+
* home directories of users that shared files with the
596+
* current user.
590597
*/
591598
async list_user_permission_issuers (user) {
592599
const rows = await this.db.read(
@@ -602,6 +609,58 @@ class PermissionService extends BaseService {
602609

603610
return users;
604611
}
612+
613+
/**
614+
* List the permissions that the specified actor (the "issuer")
615+
* has granted to all other users which have some specified
616+
* prefix in the permission key (ex: "fs:FILE-UUID")
617+
*
618+
* Note that if the prefix contains a literal '%' character
619+
* the behavior may not be as expected.
620+
*
621+
* This is a "flat" (non-cascading) view.
622+
*
623+
* Use History:
624+
* - This was written for FSNodeContext.fetchShares to query
625+
* all the "shares" associated with a file.
626+
*/
627+
async query_issuer_permissions_by_prefix (issuer, prefix) {
628+
const user_perms = await this.db.read(
629+
'SELECT DISTINCT holder_user_id, permission ' +
630+
'FROM `user_to_user_permissions` ' +
631+
'WHERE issuer_user_id = ? ' +
632+
'AND permission LIKE ?',
633+
[issuer.id, prefix + '%'],
634+
);
635+
636+
const app_perms = await this.db.read(
637+
'SELECT DISTINCT app_id, permission ' +
638+
'FROM `user_to_app_permissions` ' +
639+
'WHERE user_id = ? ' +
640+
'AND permission LIKE ?',
641+
[issuer.id, prefix + '%'],
642+
);
643+
644+
const retval = { users: [], apps: [] };
645+
646+
for ( const user_perm of user_perms ) {
647+
const { holder_user_id, permission } = user_perm;
648+
retval.users.push({
649+
user: await get_user({ id: holder_user_id }),
650+
permission,
651+
});
652+
}
653+
654+
for ( const app_perm of app_perms ) {
655+
const { app_id, permission } = app_perm;
656+
retval.apps.push({
657+
app: await get_app({ id: app_id }),
658+
permission,
659+
});
660+
}
661+
662+
return retval;
663+
}
605664

606665
get_parent_permissions (permission) {
607666
const parent_perms = [];

0 commit comments

Comments
 (0)