plugins: api.getAccount, addAccount, delAccount, updateAccount, renameAccount

Massimo Melina committed Feb 5, 2025 at 20:00 UTC ce0fb922f8b13551083d2cafd35598487815244f
3 files changed +34 -2
dev-plugins.md
+28 -1
@@ -277,6 +277,32 @@ The `api` object you get as parameter of the `init` contains the following:
277 These are not documented, probably never will, and are subject to change without notifications,
278 but you can study the sources if you are interested in using them. It's just a shorter version of `api.require('./misc')`
279
280 +- `getAccount(username: string): Account | undefined` retrieve an account object, or undefined of not found.
281 + The `Account` object has the following properties:
282 + `username: string`
283 + `srp?: string` if this value is not present, then it's a group
284 + `belongs?: string[]` list of groups this account belongs to
285 + `ignore_limits?: boolean` don't apply limits to this account
286 + `disable_password_change?: boolean` don't allow password change
287 + `admin?: boolean` allow access to admin-panel
288 + `redirect?: string` redirect to this URL as soon as the user logs in
289 + `disabled?: boolean` forbid login
290 + `expire?: Date` account expiration date
291 + `days_to_live?: number` set expiration date (after this many days) automatically at next login
292 + `allow_net?: string` allow login of this account only from this network mask
293 + `require_password_change?: boolean` ask user to change password at next login
294 +
295 +- `getAccounts(): string[]` retrieve list of all usernames
296 +
297 +- `addAccount(username: string, properties: Partial<Account>, updateExisting=false): Account | undefined`
298 + If username already exists, it will ignore the request and return undefined, unless you set `updateExisting` to true.
299 +
300 +- `delAccount(username: string): boolean` returns true if it succeeds.
301 +
302 +- `updateAccount(account: Account, changes: Partial<Account>)` apply specified changes.
303 +
304 +- `renameAccount(from: string, to: string): boolean` returns true if it succeeds.
305 +
306 ## Frontend specific
307
308 The following information applies to the default frontend, and may not apply to a custom one.
@@ -739,7 +765,8 @@ If you want to override a text regardless of the language, use the special langu
765 - api.setError
766 - frontend events: afterBreadcrumbs, afterFolderStats, afterFilter
767 - config.type.vfs_path: folders, files
742 - - api.subscribeConfig supports multiple keys
768 + - api.subscribeConfig supports multiple keys
769 + - api.getAccount, addAccount, delAccount, updateAccount, renameAccount, getUsernames
770 - 10.3 (v0.55.0)
771 - HFS.copyTextToClipboard
772 - HFS.urlParams
src/perm.ts
+4
@@ -49,6 +49,10 @@ export function ctxBelongsTo(ctx: Koa.Context, usernames: string[]) {
49 .some((u: string) => usernames.includes(u))
50 }
51
52 +export function getUsernames() {
53 + return Object.keys(accounts)
54 +}
55 +
56 export function getAccount(username:string, normalize=true) : Account | undefined {
57 if (normalize)
58 username = normalizeUsername(username)
src/plugins.ts
+2 -1
@@ -30,7 +30,7 @@ import { app } from './index'
30 import { addBlock } from './block'
31 import { getLangData } from './lang'
32 import { i18nFromTranslations } from './i18n'
33 -import { ctxBelongsTo } from './perm'
33 +import { addAccount, ctxBelongsTo, delAccount, getAccount, getUsernames, renameAccount, updateAccount } from './perm'
34 import { getCurrentUsername } from './auth'
35 import { CustomizedIcons, watchIconsFolder } from './icons'
36
@@ -128,6 +128,7 @@ async function initPlugin(pl: any, morePassedToInit?: { id: string } & Dict<any>
128 misc,
129 ctxBelongsTo,
130 getCurrentUsername,
131 + getAccount, getUsernames, addAccount, delAccount, updateAccount, renameAccount,
132 ...morePassedToInit
133 })
134 Object.assign(pl, typeof res === 'function' ? { unload: res } : res)