<commit_before>﻿'use strict';

import express = require( 'express' );
import bodyParser = require( 'body-parser' );
import { UserManager, UserPrivileges } from '../core/users';
import { ownerRights, adminRights, identifyUser } from '../utils/permission-controllers';
import { Controller } from './controller'
import { okJson, errJson } from '../utils/serializers';
import { IGetUser, IResponse, IGetUsers, IAuthReq, IUserEntry } from 'modepress';
import * as compression from 'compression';
import { Model } from '../models/model';
import { UsersModel } from '../models/users-model';
import { IBaseControler } from 'modepress';
import * as mongodb from 'mongodb';

/**
 * Main class to use for managing user data
 */
export class UserController extends Controller {
    private _options: IBaseControler;

	/**
	 * Creates an instance of the user manager
	 */
    constructor( options: IBaseControler ) {
        super( [ Model.registerModel( UsersModel ) ] );
        this._options = options;
    }

    /**
     * Called to initialize this controller and its related database objects
     */
    async initialize( e: express.Express, db: mongodb.Db ): Promise<Controller> {

        // Setup the rest calls
        const router = express.Router();
        router.use( compression() );
        router.use( bodyParser.urlencoded( { 'extended': true } ) );
        router.use( bodyParser.json() );
        router.use( bodyParser.json( { type: 'application/vnd.api+json' } ) );

        router.get( '/', <any>[ identifyUser, this.getUsers.bind( this ) ] );
        router.post( '/', <any>[ adminRights, this.createUser.bind( this ) ] );
        router.get( '/:user/meta', <any>[ ownerRights, this.getData.bind( this ) ] );
        router.get( '/:user/meta/:name', <any>[ ownerRights, this.getVal.bind( this ) ] );
        router.get( '/:username', <any>[ ownerRights, this.getUser.bind( this ) ] );
        router.delete( '/:user', <any>[ ownerRights, this.removeUser.bind( this ) ] );
        router.post( '/:user/meta/:name', <any>[ adminRights, this.setVal.bind( this ) ] );
        router.post( '/:user/meta', <any>[ adminRights, this.setData.bind( this ) ] );

        // Register the path
        e.use( this._options + '/users', router );

        await super.initialize( e, db );
        return this;
    }

    /**
	 * Gets a specific user by username or email - the 'username' parameter must be set. Some of the user data will be obscured unless the verbose parameter
     * is specified. Specify the verbose=true parameter in order to get all user data.
	 */
    private async getUser( req: IAuthReq, res: express.Response ) {
        try {
            const user = await UserManager.get.getUser( req.params.username );

            if ( !user )
                throw new Error( 'No user found' );

            okJson<IGetUser>( {
                error: false,
                message: `Found ${user.dbEntry.username}`,
                data: user.generateCleanedData( Boolean( req.query.verbose ) )
            }, res );

        } catch ( err ) {
            return errJson( err, res );
        };
    }
<commit_msg>Bug fix: Check for root path<commit_after>﻿'use strict';

import express = require( 'express' );
import bodyParser = require( 'body-parser' );
import { UserManager, UserPrivileges } from '../core/users';
import { ownerRights, adminRights, identifyUser } from '../utils/permission-controllers';
import { Controller } from './controller'
import { okJson, errJson } from '../utils/serializers';
import { IGetUser, IResponse, IGetUsers, IAuthReq, IUserEntry } from 'modepress';
import * as compression from 'compression';
import { Model } from '../models/model';
import { UsersModel } from '../models/users-model';
import { IBaseControler } from 'modepress';
import * as mongodb from 'mongodb';

/**
 * Main class to use for managing user data
 */
export class UserController extends Controller {
    private _options: IBaseControler;

	/**
	 * Creates an instance of the user manager
	 */
    constructor( options: IBaseControler ) {
        super( [ Model.registerModel( UsersModel ) ] );
        this._options = options;
    }

    /**
     * Called to initialize this controller and its related database objects
     */
    async initialize( e: express.Express, db: mongodb.Db ): Promise<Controller> {

        // Setup the rest calls
        const router = express.Router();
        router.use( compression() );
        router.use( bodyParser.urlencoded( { 'extended': true } ) );
        router.use( bodyParser.json() );
        router.use( bodyParser.json( { type: 'application/vnd.api+json' } ) );

        router.get( '/', <any>[ identifyUser, this.getUsers.bind( this ) ] );
        router.post( '/', <any>[ adminRights, this.createUser.bind( this ) ] );
        router.get( '/:user/meta', <any>[ ownerRights, this.getData.bind( this ) ] );
        router.get( '/:user/meta/:name', <any>[ ownerRights, this.getVal.bind( this ) ] );
        router.get( '/:username', <any>[ ownerRights, this.getUser.bind( this ) ] );
        router.delete( '/:user', <any>[ ownerRights, this.removeUser.bind( this ) ] );
        router.post( '/:user/meta/:name', <any>[ adminRights, this.setVal.bind( this ) ] );
        router.post( '/:user/meta', <any>[ adminRights, this.setData.bind( this ) ] );

        // Register the path
        e.use(( this._options.rootPath || '' ) + '/users', router );

        await super.initialize( e, db );
        return this;
    }

    /**
	 * Gets a specific user by username or email - the 'username' parameter must be set. Some of the user data will be obscured unless the verbose parameter
     * is specified. Specify the verbose=true parameter in order to get all user data.
	 */
    private async getUser( req: IAuthReq, res: express.Response ) {
        try {
            const user = await UserManager.get.getUser( req.params.username );

            if ( !user )
                throw new Error( 'No user found' );

            okJson<IGetUser>( {
                error: false,
                message: `Found ${user.dbEntry.username}`,
                data: user.generateCleanedData( Boolean( req.query.verbose ) )
            }, res );

        } catch ( err ) {
            return errJson( err, res );
        };
    }

{'fraction_non_alphanumeric': 0.14101543848480025, 'fraction_numerical': 0.0, 'mean_word_length': 2.6239907727797003, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6868782', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1766, 'n_words': 531}