Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
getPropertyRefs(refs, { parseDir = false, checkAllowed = true } = {}) {
refs = isString(refs) ? refs.split(/\s*\|\s*/) : refs
const cache = this._propertyRefsCache
// Pass on _propertyRefsAllowed to make sure there are only allowed
// properties in the query parameters.
return refs.map(ref => cache[ref] ||
(cache[ref] = new PropertyRef(ref, this.modelClass(), parseDir,
checkAllowed && this._propertyRefsAllowed)))
}
processAuthorize(authorize) {
if (authorize == null) {
return () => true
} else if (isBoolean(authorize)) {
return () => authorize
} else if (isFunction(authorize)) {
return async (...args) => {
const res = await authorize(...args)
// Pass res through `processAuthorize()` to support strings & arrays.
return this.processAuthorize(res)(...args)
}
} else if (isString(authorize) || isArray(authorize)) {
return (ctx, member) => {
const { user } = ctx.state
return user && !!asArray(authorize).find(
// Support 3 scenarios:
// - '$self': The requested member is checked against `ctx.state.user`
// and the action is only authorized if it matches the member.
// - '$owner': The member is asked if it is owned by `ctx.state.user`
// through the optional `Model.$hasOwner()` method.
// - any string: `ctx.state.user` is checked for this role through
// the overridable `UserModel.hasRole()` method.
value => {
return value === '$self'
? member
// member actions
? member.$is(user)
// collection actions: match id and modelClass
export function convertSchema(schema, options = {}) {
if (isString(schema)) {
// TODO: Consider removing string short-hand
// Nested shorthand expansion
schema = { type: schema }
} else if (isArray(schema)) {
// Needed for anyOf, allOf, oneOf, items:
schema = schema.map(entry => convertSchema(entry, options))
}
if (isObject(schema)) {
// Create a shallow clone so we can modify and return:
schema = { ...schema }
const { type } = schema
if (isString(type)) {
// Convert schema property notation to JSON schema
const jsonType = jsonTypes[type]
if (jsonType) {
schema.type = jsonType
if (jsonType === 'object') {
let setAdditionalProperties = false
if (schema.properties) {
const { properties, required } = expandProperties(
schema.properties,
options
)
schema.properties = properties
if (required.length > 0) {
schema.required = required
}
setAdditionalProperties = true
export function getResource(resource, defaults = {}) {
const { parent, ...defs } = defaults
resource = isObject(resource) ? { ...defs, ...resource }
: isString(resource) ? { ...defs, path: resource }
: null
// Only set parent if path doesn't start with '/', so relative URLs are
// dealt with correctly.
if (
resource &&
parent &&
!resource.parent &&
parent.path &&
!resource.path?.startsWith('/')
) {
resource.parent = parent
if (!resource.path) {
resource.path = '.'
}
}
return resource
modify(arg, ...args) {
return arg === undefined
? this
: isString(arg)
? this.applyModifier(arg, ...args)
: super.modify(arg, ...args)
}
describeAuthorize(authorize) {
return isFunction(authorize)
? describeFunction(authorize)
: isString(authorize)
? `'${authorize}'`
: isArray(authorize)
? `[${authorize.map(value => `'${value}'`).join(', ')}]`
: ''
}
function createArgumentsValidator(modelClass, args = [], options = {}) {
if (args.length > 0) {
let properties = null
for (const arg of args) {
if (arg) {
const property = isString(arg) ? { type: arg } : arg
const { name, type, ...rest } = property
properties = properties || {}
properties[name || 'root'] = type ? { type, ...rest } : rest
}
}
if (properties) {
const schema = convertSchema(properties, options)
return modelClass.compileValidator(schema)
}
}
return () => true
}
_handle(method, event, callback) {
if (isString(event)) {
super[method](event, callback)
} else if (isArray(event)) {
for (const ev of event) {
super[method](ev, callback)
}
} else if (isPlainObject(event)) {
for (const key in event) {
super[method](key, event[key])
}
}
return this
}