HFS.userBelongsTo

Massimo Melina committed Sep 7, 2024 at 16:36 UTC 96a39b7539648023cefcbfc055988250a1a9d695
4 files changed +12 -4
dev-plugins.md
+4 -1
@@ -268,7 +268,9 @@ The HFS objects contains many properties:
268 - `debounceAsync: function` like lodash.debounce, but also avoids async invocations to overlap.
269 For details please refer to `src/debounceAsync.ts`.
270 - `loadScript(uri: string): Promise` load a js file. If uri is relative, it is based on the plugin's public folder.
271 -- `customRestCall(name: string, parameters?: object): Promise<any>` call backend functions exported with `customRest`.
271 +- `customRestCall(name: string, parameters?: object): Promise<any>` call backend functions exported with `customRest`.
272 +- `userBelongsTo(groupOrUsername: string): boolean` returns true if logged in account belongs to the specified group name.
273 + Returns true if the specified name is the one of the logged in account.
274
275 The following properties are accessible only immediately at top-level; don't call it later in a callback.
276 - `getPluginConfig()` returns object of all config keys that are declared frontend-accessible by this plugin.
@@ -643,6 +645,7 @@ If you want to override a text regardless of the language, use the special langu
645 - exports.customRest + HFS.customRestCall
646 - config.type: vfs_path
647 - frontend event: sortCompare
648 + - HFS.userBelongsTo
649 - 8.891 (v0.53.0)
650 - api.openDb
651 - frontend event: menuZip
frontend/src/state.ts
+2
@@ -38,7 +38,9 @@ export const state = proxy<typeof FRONTEND_OPTIONS & {
38 uri: string
39 uploadOnExisting: 'skip' | 'overwrite' | 'rename'
40 searchOptions: any
41 + expandedUsername: string[]
42 }>({
43 + expandedUsername: [],
44 searchOptions: { wild: true },
45 uploadOnExisting: getHFS().dontOverwriteUploading ? 'rename' : 'skip',
46 uri: '',
shared/index.ts
+2 -1
@@ -27,6 +27,7 @@ Object.assign(HFS, {
27 getPluginPublic: () => getScriptAttr('src')?.match(/^.*\//)?.[0],
28 getPluginConfig: () => HFS.plugins[HFS.getPluginKey()] || {},
29 loadScript: (uri: string) => loadScript(uri.includes('//') || uri.startsWith('/') ? uri : HFS.getPluginPublic() + uri),
30 + userBelongsTo: (groupOrUser: string) => HFS.state.expandedUsername.includes(groupOrUser),
31 cpuSpeedIndex,
32 })
33
@@ -113,7 +114,7 @@ export function makeSessionRefresher(state: any) {
114 if (!response) return
115 const { exp } = response
116 Object.assign(initial, response) // keep it updated, not necessary, just in case someone is looking at this instead of the state
116 - Object.assign(state, _.pick(response, ['username', 'adminUrl', 'canChangePassword', 'accountExp']))
117 + Object.assign(state, _.pick(response, ['username', 'adminUrl', 'canChangePassword', 'accountExp', 'expandedUsername']))
118 if (!response.username || !exp) return
119 const delta = new Date(exp).getTime() - Date.now()
120 const t = _.clamp(delta - 30_000, 4_000, 600_000)
src/api.auth.ts
+4 -2
@@ -1,6 +1,6 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 -import { Account, accountCanLogin, changeSrpHelper, getAccount, getFromAccount } from './perm'
3 +import { Account, accountCanLogin, changeSrpHelper, expandUsername, getAccount, getFromAccount } from './perm'
4 import { ApiError, ApiHandler } from './apiMiddleware'
5 import { SRPServerSessionStep1 } from 'tssrp6a'
6 import { ADMIN_URI, HTTP_UNAUTHORIZED, HTTP_BAD_REQUEST, HTTP_SERVER_ERROR, HTTP_CONFLICT, HTTP_NOT_FOUND } from './const'
@@ -77,8 +77,10 @@ export const logout: ApiHandler = async ({}, ctx) => {
77 }
78
79 export const refresh_session: ApiHandler = async ({}, ctx) => {
80 + const username = getCurrentUsername(ctx)
81 return !ctx.session ? new ApiError(HTTP_SERVER_ERROR) : {
81 - username: getCurrentUsername(ctx),
82 + username,
83 + expandedUsername: expandUsername(username),
84 adminUrl: ctxAdminAccess(ctx) ? ctx.state.revProxyPath + ADMIN_URI : undefined,
85 canChangePassword: canChangePassword(ctx.state.account),
86 exp: keepSessionAlive.get() ? new Date(Date.now() + sessionDuration.compiled()) : undefined,