@adonis-agora/media
Version:
Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a
93 lines • 3.77 kB
JavaScript
/**
* The building blocks of media authorization. None of them knows about a specific auth library:
* the identity comes from the configured resolver, and the authkit/authz-specific rules live in
* `@adonis-agora/media/authkit` and `@adonis-agora/media/authz`.
*
* ```ts
* import { authorizers } from '@adonis-agora/media'
* import { authz } from '@adonis-agora/media/authz'
*
* authorizers.any(authorizers.owner(), authz.can('media.read.any'))
* ```
*/
export const authorizers = {
/** Everyone, signed in or not. */
public() {
return () => true;
},
/** Any signed-in user. */
authenticated() {
return ({ subject }) => subject !== null;
},
/**
* The signed-in user owns the record: `record.ownerId` is their {@link MediaSubject.id} (the
* account they act as). `ownerTypes` restricts which owner types count as "the user" — e.g.
* `['User']`, so a `Team` that happens to share the id does not. For ownership that goes through
* the app's own model (a family member's profile, a course's teacher), write the rule yourself.
*
* A write has no record yet, so `owner()` cannot allow one; pair it with `authenticated()` for
* uploads into the user's own namespace.
*/
owner(options = {}) {
return ({ subject, record }) => {
if (!subject || !record)
return false;
if (options.ownerTypes && !options.ownerTypes.includes(record.ownerType))
return false;
return String(record.ownerId) === String(subject.id);
};
},
/**
* The file belongs to the user's active tenant (organization): `record.tenantId` equals the
* identity's `tenantId` (both empty counts as the same: no tenant). With tenancy on, reads are
* already confined to the active tenant; use this to say so explicitly in a rule, or to compare
* against an identity resolver's tenant. A write has no record yet, so it is not restricted here.
*/
sameTenant() {
return ({ subject, record, action }) => {
if (action === 'write' || !record)
return true;
return (record.tenantId ?? null) === (subject?.tenantId ?? null);
};
},
/** The identity carries one of these global roles (e.g. an authkit token's roles). */
roles(roles) {
return ({ subject }) => Boolean(subject?.roles?.some((role) => roles.includes(role)));
},
/** Allowed when ANY rule allows — evaluated in order, stopping at the first yes. */
any(...rules) {
return async (request) => {
for (const rule of rules)
if (await rule(request))
return true;
return false;
};
},
/** Allowed only when EVERY rule allows — evaluated in order, stopping at the first no. */
all(...rules) {
return async (request) => {
for (const rule of rules)
if (!(await rule(request)))
return false;
return rules.length > 0;
};
},
};
/**
* Default identity: AdonisJS auth's `ctx.auth.user` (its `id`), so an app on `@adonisjs/auth`
* needs no configuration. Apps on authkit use `authkitIdentity()` from
* `@adonis-agora/media/authkit`.
*/
export const adonisAuthIdentity = (ctx) => {
const user = ctx.auth?.user;
if (!user || user.id === undefined || user.id === null)
return null;
return { id: String(user.id), user };
};
/** Normalize `'public'` / an authorizer / nothing into an authorizer (nothing ⇒ deny). */
export function toAuthorizer(rule) {
if (rule === 'public')
return authorizers.public();
return rule ?? (() => false);
}
//# sourceMappingURL=access.js.map