Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
} else {
// Update user's data
user.firstName = data.firstName;
user.lastName = data.lastName;
await user.save();
}
try {
await authPlugin.createUser({ data: args.data, user, permanent: true }, context);
result.authUser = true;
} catch {
// Update firstName/lastName, but do not touch the existing password
await authPlugin.updateUser({ data: omit(args.data, ["password"]), user }, context);
}
} catch (e) {
if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
const attrError = InvalidFieldsError.from(e);
return new ErrorResponse({
code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
message: attrError.message,
data: attrError.data
});
}
return new ErrorResponse({
code: e.code,
message: e.message,
data: e.data
});
}
return new Response(result);
};
user.lastName = data.lastName;
await user.save();
}
try {
await authPlugin.createUser({ data: args.data, user, permanent: true }, context);
result.authUser = true;
} catch {
// Update firstName/lastName, but do not touch the existing password
await authPlugin.updateUser({ data: omit(args.data, ["password"]), user }, context);
}
} catch (e) {
if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
const attrError = InvalidFieldsError.from(e);
return new ErrorResponse({
code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
message: attrError.message,
data: attrError.data
});
}
return new ErrorResponse({
code: e.code,
message: e.message,
data: e.data
});
}
return new Response(result);
};
export default ({
createBase,
SecurityRole,
SecurityRoles2Models,
SecurityGroup,
SecurityGroups2Models,
context
}) => {
const SecurityUser = flow(
withName("SecurityUser"),
withHooks(),
withFields(instance => ({
email: onSet(value => {
if (value === instance.email) {
return value;
}
value = value.toLowerCase().trim();
instance.registerHookCallback("beforeSave", async () => {
console.log("TODO: setOnce"); // eslint-disable-line
const existingUser = await SecurityUser.findOne({
query: { email: value }
});
if (existingUser) {
throw Error("User with given e-mail already exists.");
}
export default ({ context, FormSettings }) => {
// When installing the FormBuilder app on a blank system, defaultLocale will be blank, because I18N app wasn't
// installed yet, meaning no default locale was selected.
let defaultLocale = null;
if (context.i18n.getDefaultLocale()) {
defaultLocale = context.i18n.getDefaultLocale().id;
}
return withFields({
layout: fields({
value: {},
instanceOf: withFields({
renderer: string({ value: "default" })
})()
}),
submitButtonLabel: i18nString({ context }),
successMessage: i18nObject({ context }),
termsOfServiceMessage: fields({
instanceOf: withFields({
message: i18nObject({ context }),
errorMessage: i18nString({ context }),
enabled: boolean()
})()
}),
reCaptcha: fields({
instanceOf: flow(
// installed yet, meaning no default locale was selected.
let defaultLocale = null;
if (context.i18n.getDefaultLocale()) {
defaultLocale = context.i18n.getDefaultLocale().id;
}
return withFields({
layout: fields({
value: {},
instanceOf: withFields({
renderer: string({ value: "default" })
})()
}),
submitButtonLabel: i18nString({ context }),
successMessage: i18nObject({ context }),
termsOfServiceMessage: fields({
instanceOf: withFields({
message: i18nObject({ context }),
errorMessage: i18nString({ context }),
enabled: boolean()
})()
}),
reCaptcha: fields({
instanceOf: flow(
withProps({
settings: {
get enabled() {
return new Promise(async resolve => {
const settings = await FormSettings.load();
resolve(Boolean(get(settings, "data.reCaptcha.enabled")));
});
},
const authPlugin = context.plugins
.byType("security-authentication-provider")
.filter(pl => pl.hasOwnProperty("createUser"))
.pop();
try {
await authPlugin.createUser({ data: args.data, user }, context);
} catch {
// If user already exists we don't do anything on the auth provider side.
}
} catch (e) {
if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
const attrError = InvalidFieldsError.from(e);
return new ErrorResponse({
code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
message: attrError.message,
data: attrError.data
});
}
return new ErrorResponse({
code: e.code,
message: e.message,
data: e.data
});
}
return new Response(user);
};
try {
await user.populate(args.data).save();
const authPlugin = context.plugins
.byType("security-authentication-provider")
.filter(pl => pl.hasOwnProperty("createUser"))
.pop();
try {
await authPlugin.createUser({ data: args.data, user }, context);
} catch {
// If user already exists we don't do anything on the auth provider side.
}
} catch (e) {
if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
const attrError = InvalidFieldsError.from(e);
return new ErrorResponse({
code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
message: attrError.message,
data: attrError.data
});
}
return new ErrorResponse({
code: e.code,
message: e.message,
data: e.data
});
}
return new Response(user);
};
withStaticProps({
async load() {
let settings = await this.findOne({ query: { key: SETTINGS_KEY } });
if (!settings) {
settings = new FilesSettings();
await settings.save();
}
return settings;
}
}),
withFields({
key: setOnce()(string({ value: SETTINGS_KEY })),
data: fields({
value: {},
instanceOf: withFields({
installed: boolean({ value: false }),
srcPrefix: onSet(value => {
// Make sure srcPrefix always ends with forward slash.
if (typeof value === "string") {
return value.endsWith("/") ? value : value + "/";
}
return value;
})(
string({
validation: validation.create("required"),
value: "/files/"
})
)
})()
})
})
)(createBase());
this.version = await this.getNextVersion();
},
async afterDelete() {
// If the deleted form is the root form - delete its revisions
if (this.id === this.parent) {
// Delete all revisions.
const revisions = await Form.find({
query: { parent: this.parent }
});
return Promise.all(revisions.map(rev => rev.delete()));
}
}
}),
withProps({
get overallStats() {
return new Promise(async resolve => {
const plugin = context.plugins.byName("forms-resolver-overall-stats");
if (!plugin) {
throw Error(
`Resolver plugin "forms-resolver-overall-stats" is not configured!`
);
}
const stats = await plugin.resolve({ form: this, context });
if (!stats) {
return resolve({
submissions: 0,
views: 0,
title: onSet(value => (instance.locked ? instance.title : value))(
string({ validation: validation.create("required") })
),
snippet: onSet(value => (instance.locked ? instance.snippet : value))(string()),
url: onSet(value => (instance.locked ? instance.url : value))(
string({ validation: validation.create("required") })
),
content: onSet(value => (instance.locked ? instance.content : value))(
content({ context })
),
settings: onSet(value => (instance.locked ? instance.settings : value))(
fields({
instanceOf: PbPageSettings
})
),
version: number(),
parent: context.commodo.fields.id(),
published: flow(
onSet(value => {
// Deactivate previously published revision
if (value && value !== instance.published && instance.isExisting()) {
instance.locked = true;
instance.publishedOn = new Date();
instance.registerHookCallback("beforeSave", async () => {
// TODO: setOnce
// Deactivate previously published revision
const publishedRev: PbPage = (await PbPage.findOne({
query: { published: true, parent: instance.parent }
}): any);
if (publishedRev) {
publishedRev.published = false;