aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHafeez2018-04-15 18:22:09 +0100
committerHafeez2018-04-15 18:22:09 +0100
commit42e30930b635eb91bdb97d0a889081cd291f3740 (patch)
treec355a2b8083f9f69beb0d424038e3460713f8b38 /src
parentstarted modifying file upload (diff)
parentfixed fetching current_user (diff)
downloadmedicine-42e30930b635eb91bdb97d0a889081cd291f3740.tar.gz
medicine-42e30930b635eb91bdb97d0a889081cd291f3740.zip

Merge branch ‘trh/gets’ of gitlab.cs.st-andrews.ac.uk:cs3099group-be-4/project-code into trh/gets

Diffstat (limited to 'src')
-rw-r--r--src/FileRouter.ts34
-rw-r--r--src/app.ts20
-rw-r--r--src/db/model/User.ts10
-rw-r--r--src/logger.ts61
-rw-r--r--src/matcher/options.ts1
5 files changed, 67 insertions, 59 deletions
diff --git a/src/FileRouter.ts b/src/FileRouter.ts
index 0a8d06a..d6cea53 100644
--- a/src/FileRouter.ts
+++ b/src/FileRouter.ts
@@ -65,6 +65,7 @@ const getProtocols: Middleware = (
const postLog: Middleware = (
req: Request, res: Response, next: NextFunction
): void => {
+ res.locals.modified = true;
logger.debug('Adding forwarded log');
if (!req.user.hasPrivilege('logging')) {
next(new RequestError(401, 'invalid_privilege'));
@@ -108,9 +109,15 @@ const getLog: Middleware = (
req: Request, res: Response, next: NextFunction
): void => {
logger.debug('Fetching log');
+ res.locals.modified = true;
if (!req.user.hasPrivilege('admin')) {
next(new RequestError(401, 'invalid_privilege'));
}
+ /* tslint:disable */
+ if(!req.params.level) req.params.level = 'info';
+ if(!req.params.before) req.params.before = new Date();
+ if(!req.params.after) req.params.after = new Date(0);
+ /* tslint:enable */
res.locals.data = logger.fetch(
req.params.level, {
before: new Date(req.params.before),
@@ -165,6 +172,7 @@ const postProperties: Middleware = (
req: Request, res: Response, next: NextFunction
): void => {
logger.debug('Editing server properties');
+ res.locals.modified = true;
for (const newProp of req.body) {
const prop: Property | undefined = serverConfig.find(
(p: Property): boolean => p.id === newProp.id
@@ -257,6 +265,7 @@ const getUserProperties: Middleware = (
req: Request, res: Response, next: NextFunction
): void => {
logger.debug('Listing additional properties for a user');
+ res.locals.modified = true;
if (res.locals.user == null) {
next(new RequestError(404, 'user_not_found'));
return;
@@ -289,6 +298,7 @@ const getUsername: Middleware = (
req: Request, res: Response, next: NextFunction
): void => {
logger.debug(`Listing user ${res.locals.user}`);
+ res.locals.modified = true;
if (res.locals.user == null) {
next(new RequestError(404, 'user_not_found'));
return;
@@ -327,6 +337,7 @@ const postCurUser: Middleware = async(
req: Request, res: Response, next: NextFunction
): Promise<void> => {
logger.debug('Editing current user');
+ res.locals.modified = true;
try {
await req.user.updateInfo(req.body);
await req.user.save();
@@ -357,14 +368,15 @@ const postUsername: Middleware = async(
req: Request, res: Response, next: NextFunction
): Promise<void> => {
let user: User | null | undefined = res.locals.user;
+ res.locals.modified = true;
if (user == null) {
- console.log('Adding new user');
+ logger.debug('Adding new user');
user = new User({
username: req.params.username,
password: req.params.password
});
} else {
- console.log('Editing user');
+ logger.debug('Editing user');
user.password = req.params.password;
}
if (req.body) {
@@ -387,6 +399,7 @@ const postUsername: Middleware = async(
const getProjectRoles: Middleware = async(
req: Request, res: Response, next: NextFunction
): Promise<void> => {
+ res.locals.modified = true;
logger.debug('Listing available project roles');
const roles: ContributorGroup[] = await ContributorGroup.findAll();
res.locals.data = roles.filter(
@@ -418,6 +431,7 @@ const getProjects: Middleware = async(
req: Request, res: Response, next: NextFunction
): Promise<void> => {
logger.debug('Listing all projects');
+ res.locals.modified = true;
const projects: Project[] = await Project.findAll({
include: [
User
@@ -425,7 +439,7 @@ const getProjects: Middleware = async(
});
res.locals.data = projects.map(
(p: Project): ProjectFullInfo => p.fullInfo
- );
+ );
next();
};
@@ -450,6 +464,7 @@ const getProjectName: Middleware = async(
req: Request, res: Response, next: NextFunction
): Promise<void> => {
logger.debug('Getting project properties');
+ res.locals.modified = true;
const project: Project | null = res.locals.project;
if (project == null) {
return next(new RequestError(404, 'project_not_found'));
@@ -476,6 +491,7 @@ const postProjectName: Middleware = async(
req: Request, res: Response, next: NextFunction
): Promise<void> => {
let project: Project | null = res.locals.project;
+ res.locals.modified = true;
let promise: PromiseLike<File> | null = null;
if (project == null) {
logger.debug('Adding new project');
@@ -505,6 +521,7 @@ const getProjectProperties: Middleware = (
req: Request, res: Response, next: NextFunction
): void => {
logger.debug('Getting project properties');
+ res.locals.modified = true;
if (res.locals.project == null) {
next(new RequestError(404, 'project_not_found'));
} else {
@@ -520,6 +537,7 @@ const getFile: Middleware = async(
req: Request, res: Response, next: NextFunction
): Promise<void> => {
logger.debug(`Getting file in ${req.query.view} view`);
+ res.locals.modified = true;
const file: File | null = res.locals.file;
if (file == null) {
return next(new RequestError(404, 'file_not_found'));
@@ -547,7 +565,8 @@ const getFileId: Middleware = getFile;
const postFilePath: Middleware = (
req: Request, res: Response, next: NextFunction
): void => {
- logger.debug('Receiving file to ' + res.locals.parentFolder);
+ logger.debug(`Receiving file to ${res.locals.parentFolder}`);
+ res.locals.modified = true;
if (res.locals.file != null) {
next(new RequestError(400, 'file_exists'));
}
@@ -612,7 +631,7 @@ export class FileRouter {
this.router.get ('/projects/:project_name/properties', getProjectProperties);
// File access
this.router.get ('/projects/:project_name/files/:path', getFilePath);
- this.router.post('/projects/:project_name/files/:id', postFilePath);
+ this.router.post('/projects/:project_name/files/:path', postFilePath);
this.router.get ('/projects/:project_name/files_by_id/:id', getFileId);
}
@@ -669,7 +688,7 @@ export class FileRouter {
req: Request, res: Response, next: NextFunction,
filename: string
): Promise<void> => {
- console.log("wrong precond")
+ console.log(`parsing path ${filename}`)
logger.debug(`Searching for file ${filename}`);
const fileNames: string[] = req.params.path.split('/');
const project: Project | null = res.locals.project;
@@ -722,7 +741,7 @@ export class FileRouter {
req: Request, res: Response, next: NextFunction,
fileId: string
): Promise<void> => {
- console.log(`Searching for file ${fileId}`);
+ console.log(`parsing id ${fileId}`);
const file: File | null = await File.findOne({
include: [{all: true}],
where: {
@@ -730,7 +749,6 @@ export class FileRouter {
}
});
res.locals.file = file;
- console.log("precondition: " + file)
next();
}
);
diff --git a/src/app.ts b/src/app.ts
index 1d1c9cb..4dfeeda 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -77,7 +77,7 @@ class App {
.unless({path: [
'/',
'/cs3099group-be-4/oauth/token',
- '/cs3099group-be-4/_supported_protocols_'
+ '/cs3099group-be-4/_supported_protocols_'
] })
);
this.express.use(passport.initialize());
@@ -110,26 +110,22 @@ class App {
message: 'Welcome to the CS3099 BE4 server!',
important: 'Endpoints start from /cs3099group-be-4/'
});
- });
- //const errRouter: ex.Router = ex.Router();
-
+ });
this.express.use('/cs3099group-be-4', FileRouter);
this.express.use('/cs3099group-be-4', authRouter);
- this.express.use('/', defRouter);
- this.express.use((req: ex.Request, res: ex.Response, next: ex.NextFunction): void => {
- console.log(res.locals)
+ this.express.use('/', defRouter);
+ this.express.use((req: ex.Request, res: ex.Response,
+ next: ex.NextFunction): void => {
if (Object.keys(res.locals).length === 0) {
- console.log(404)
res.status(404)
.json({
status: 'error',
error : 'invalid_route',
error_description: 'Endpoints start from /cs3099group-be-4/'
});
+ } else {
+ next();
}
- else {
- next();
- }
});
}
@@ -149,5 +145,5 @@ export default new App(true).express;
*/
// tslint:disable-next-line:variable-name
export const TestApp: () => ex.Express = (): ex.Express => {
- return new App(false).express;
+ return new App(true).express;
};
diff --git a/src/db/model/User.ts b/src/db/model/User.ts
index 171e899..3a0ef26 100644
--- a/src/db/model/User.ts
+++ b/src/db/model/User.ts
@@ -126,18 +126,20 @@ export default class User extends Model<User> {
}
public get fullInfo(): UserFullInfo {
+ console.log("fullinfo called")
// FIXME: Fetch these things if they aren't already present
if (this.userGroups === undefined) {
- throw new Error('userGroups undefined');
+ this.userGroups = [];
} else if (this.projects === undefined) {
- throw new Error('projects undefined');
+ this.projects = [];
}
+ console.log("2")
return {
username : this.username,
- privileges: this.userGroups
+ privileges: this.userGroups!
.filter((ug: UserGroup): boolean => ug.name !== null)
.map((ug: UserGroup): string => ug.name as string),
- projects : this.projects.map(
+ projects : this.projects!.map(
(p: Project): ProjectInfo => this.getProjectInfo(p)
),
// TODO: Check if we have anything to use these for yet?
diff --git a/src/logger.ts b/src/logger.ts
index aefb751..9025814 100644
--- a/src/logger.ts
+++ b/src/logger.ts
@@ -4,6 +4,7 @@ import * as colors from 'colors/safe';
import * as winston from 'winston';
import { files } from './files';
+import { QueryOptions } from 'sequelize';
type Colour = (s: string) => string;
@@ -252,6 +253,11 @@ const makeLogFunction: (level: LogLevel) => LogFunction = (
};
};
+function forwardLog(level: LogLevel, message: string, meta: LoggingMetadata)
+ : any {
+ basicLogger.log(level, message, meta);
+}
+
interface FetchParams {
before?: Date;
after?: Date;
@@ -265,40 +271,27 @@ interface LogItem {
timestamp: string;
}
-const fetchLogs: (level?: LogLevel, params?: FetchParams) => LogItem[] = (
- level?: LogLevel, params: FetchParams = {}
-): LogItem[] => {
- const matches: (log: any, level?: LogLevel, params?: FetchParams)
- => boolean = (
- log: any, minLevel?: LogLevel, fetchParams: FetchParams = {}
- ): boolean => {
- if (!minLevel) {
- // tslint:disable-next-line:no-parameter-reassignment
- minLevel = 'info';
- }
- const time: Date = new Date(log.timestamp);
- if (params.before && params.before < time) {
- return false;
- } else if (params.after && params.after > time) {
- return false;
- } else {
- return true;
- }
+/* tslint:disable */
+const fetchLogs2: (level?: LogLevel, params?: FetchParams) => Promise<LogItem[]> = (level?: LogLevel, params: FetchParams = {}): Promise<LogItem[]> => {
+ const options: winston.QueryOptions = {
+ from: params.after,
+ until: params.before,
+ start: 0,
+ order: 'desc',
+ fields: ['user', 'component', 'level', 'message', 'label', 'timestamp']
};
- const out: LogItem[] = [];
- basicLogger.stream({ level }).on('log', (log: any): void => {
- if (matches(log, level, params)) {
- out.push({
- component: log.component,
- level: log.level,
- value: log.message,
- username: log.user,
- timestamp: log.timestamp
- });
- }
+
+ return new Promise((res, rej): void => {
+ basicLogger.query(options, function (err, results) {
+ if (err) {
+ /* TODO: handle me */
+ throw err;
+ }
+ res(results.basicFileTransport);
+ });
});
- return out;
};
+/* tslint:enable */
export const logger: {
7: LogFunction;
@@ -339,7 +332,7 @@ export const logger: {
shutdown: LogFunction;
isEnabled: () => boolean;
forward: (level: LogLevel, message: string, meta: LoggingMetadata) => void;
- fetch: (level: LogLevel, params?: FetchParams) => LogItem[];
+ fetch: (level: LogLevel, params?: FetchParams) => Promise<LogItem[]>;
} = {
7: makeLogFunction('7'),
debug: makeLogFunction('debug'),
@@ -378,8 +371,8 @@ export const logger: {
emergency: makeLogFunction('emergency'),
shutdown: makeLogFunction('shutdown'),
isEnabled: (): boolean => !basicFileTransport.silent,
- forward: basicLogger.log,
- fetch: fetchLogs
+ forward: forwardLog,
+ fetch: fetchLogs2
};
export const logQuery: (query: string, duration: string) => void = (
diff --git a/src/matcher/options.ts b/src/matcher/options.ts
index 988d22b..0513eb3 100644
--- a/src/matcher/options.ts
+++ b/src/matcher/options.ts
@@ -18,7 +18,6 @@ const matchNoCurry:
if (types.atom(template)) {
return template === value;
} else if (types.function(template)) {
- //console.log("hi there " + value)
return template(value);
} else if (types.atom(value) || value === undefined) {
return false;