Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { Request, Response } from 'express';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_GET_POSTS } from '../../../../utils/constants';
import { maybe, optional, checkType, restrictToSchema }from 'rulr';
import { isValidSortObject } from '../../../../utils/validate';
const validateGetPosts = maybe(
restrictToSchema({
limit: optional(checkType(String)),
offset: optional(checkType(String)),
sort: optional(isValidSortObject())
}),
);
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({ permissions, permissionName: CAN_GET_POSTS});
validateGetPosts(req.query,['posts']);
const {limit, offset, sort} = req.query;
const posts = await config.service.getPosts({limit, offset, order: sort});
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_UPDATE_USER } from '../../../../utils/constants';
import { minLength, isEmail, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, optional, checkType, composeRules, first, restrictToSchema }from 'rulr';
import * as R from 'ramda';
const validateUpdateUser = maybe(composeRules([
restrictToSchema({
firstname: optional(checkType(String)),
lastname: optional(checkType(String)),
bio: optional(checkType(String)),
email: optional(isEmail),
password: optional(minLength(6)),
password_confirmation: optional(checkType(String)),
}),
first(checkType(Object), optional(validateMatchingPasswords))
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({permissions, permissionName: CAN_UPDATE_USER});
validateUpdateUser(req.body, ['user']);
const {user_id} = req.params;
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { Request, Response } from 'express';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_GET_POSTS } from '../../../../utils/constants';
import { maybe, optional, checkType, restrictToSchema }from 'rulr';
import { isValidSortObject } from '../../../../utils/validate';
const validateGetPosts = maybe(
restrictToSchema({
limit: optional(checkType(String)),
offset: optional(checkType(String)),
sort: optional(isValidSortObject())
}),
);
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({ permissions, permissionName: CAN_GET_POSTS});
validateGetPosts(req.query,['posts']);
const {limit, offset, sort} = req.query;
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_UPDATE_POST, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, optional, checkType,composeRules, restrictToSchema }from 'rulr';
const validateUpdatePost = maybe(composeRules([
restrictToSchema({
user_id: optional(checkType(Number)),
title: optional(maxLength(VARCHAR_FIELD_LENGTH)),
body: optional(maxLength(TEXT_FIELD_LENGTH)),
})
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({permissions, permissionName: CAN_UPDATE_POST});
validateUpdatePost(req.body, ['post']);
const {post_id} = req.params;
const updatePost = await config.service.updatePost({id: post_id, data: req.body});
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_CREATE_PERMISSION, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, required, optional, checkType,composeRules, first, restrictToSchema } from 'rulr';
import * as R from 'ramda';
const validateCreatePermission = maybe(composeRules([
restrictToSchema({
name: required(maxLength(VARCHAR_FIELD_LENGTH)),
label: optional(maxLength(VARCHAR_FIELD_LENGTH)),
description: optional(maxLength(TEXT_FIELD_LENGTH)),
})
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({permissions, permissionName: CAN_CREATE_PERMISSION});
validateCreatePermission(req.body, ['Permission']);
const createdPermission = await config.service.createPermission(req.body);
res.status(CREATED).json(createdPermission);
});
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_UPDATE_USER } from '../../../../utils/constants';
import { minLength, isEmail, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, optional, checkType, composeRules, first, restrictToSchema }from 'rulr';
import * as R from 'ramda';
const validateUpdateUser = maybe(composeRules([
restrictToSchema({
firstname: optional(checkType(String)),
lastname: optional(checkType(String)),
bio: optional(checkType(String)),
email: optional(isEmail),
password: optional(minLength(6)),
password_confirmation: optional(checkType(String)),
}),
first(checkType(Object), optional(validateMatchingPasswords))
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({permissions, permissionName: CAN_UPDATE_USER});
validateUpdateUser(req.body, ['user']);
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { Request, Response } from 'express';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_GET_POSTS } from '../../../../utils/constants';
import { maybe, optional, checkType, restrictToSchema }from 'rulr';
import { isValidSortObject } from '../../../../utils/validate';
const validateGetPosts = maybe(
restrictToSchema({
limit: optional(checkType(String)),
offset: optional(checkType(String)),
sort: optional(isValidSortObject())
}),
);
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({ permissions, permissionName: CAN_GET_POSTS});
validateGetPosts(req.query,['posts']);
const {limit, offset, sort} = req.query;
const posts = await config.service.getPosts({limit, offset, order: sort});
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_CREATE_ROLE, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, required, optional, composeRules, restrictToSchema } from 'rulr';
const validateCreateRole = maybe(composeRules([
restrictToSchema({
name: required(maxLength(VARCHAR_FIELD_LENGTH)),
description: optional(maxLength(TEXT_FIELD_LENGTH)),
})
]));
export default (config: Config) => {
return catchErrors(config, async (req, res) => {
const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
hasPermission({permissions, permissionName: CAN_CREATE_ROLE});
validateCreateRole(req.body, ['role']);
const createRole = await config.service.createRole(req.body);
res.status(CREATED).json(createRole);
});