{"size": 1105, "ext": "js", "max_stars_repo_path": "users/role.middleware.js", "max_stars_repo_name": "chrispaskvan/destiny-ghost-api", "max_stars_repo_stars_event_min_datetime": "2016-01-16T14:23:17.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-21T04:09:40.000Z", "max_issues_repo_path": "users/role.middleware.js", "max_issues_repo_name": "chrispaskvan/destiny-ghost-api", "max_issues_repo_issues_event_min_datetime": "2017-11-05T21:28:39.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-07T02:41:26.000Z", "max_forks_repo_path": "users/role.middleware.js", "max_forks_repo_name": "chrispaskvan/destiny-ghost-api", "max_forks_repo_forks_event_min_datetime": "2020-04-12T14:05:47.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-12T14:05:47.000Z"}
{"max_stars_count": 6, "max_issues_count": 15, "max_forks_count": 1, "avg_line_length": 25.1136363636, "max_line_length": 89, "alphanum_fraction": 0.5104072398}
const { administrators } = require('../helpers/config');

/**
 * User Authentication Middleware Class
 */
class RoleMiddleware {
    /**
     * @constructor
     * @param authenticationController
     */
    constructor({ authenticationController }) {
        this.authentication = authenticationController;
    }

    /**
     * Authenticate user request.
     * @param req
     * @param res
     * @param next
     * @returns {Promise.<void>}
     */
    async administrativeUser(req, res, next) {
        try {
            const user = await this.authentication.authenticate(req);

            if (user) {
                if (administrators
                    .find(administrator => administrator.displayName === user.displayName
                        && administrator.membershipType === user.membershipType)) {
                    next();
                } else {
                    res.status(401).end();
                }
            } else {
                res.status(401).end();
            }
        } catch (err) {
            next(err);
        }
    }
}

module.exports = RoleMiddleware;
