<commit_before>/**
 * /api/services/AuthService.js
 *
 * Generic auth service, which is used to check .
 */
var async = require("async");

/**
 * Method checks if specified user has access to specified project or not.
 *
 * @param   {User}      user        Signed in user object
 * @param   {Number}    projectId   Project id to check
 * @param   {Function}  next        Main callback function, which is called after checks
 */
exports.hasAccessToProject = function(user, projectId, next) {
    // Admin user, always true
    if (user.admin) {
        next(null, true);
    } else { // Otherwise fetch user role
        /**
         * Make parallel jobs to determine if user has access to specified project or not.
         * Basically we check if user is
         *  a) primary project manager
         *  b) attached to project in some role
         *
         * If current user is either of these provided next callback is called by false.
         */
        async.parallel(
            {
                /**
                 * Function to fetch possible Project object for signed in user and specified project.
                 * User must be project manager.
                 *
                 * Note that this query may return undefined.
                 *
                 * @param   {Function}  callback
                 */
                primary: function(callback) {
                    Project
                        .findOne({
                            id: projectId,
                            managerId: user.id
                        })
                        .done(function(error, /** sails.json.project */project) {
                            if (error) {
                                callback(error, null);
                            } else {
                                callback(null, project)
                            }
                        });
                },

                /**
                 * Function to fetch possible ProjectUser object for signed in user and specified project.
                 *
                 * Note that this query may return undefined.
                 *
                 * @param   {Function}  callback
                 */
                contributor: function(callback) {
                    ProjectUser
                        .findOne({
                            projectId: projectId,
                            userId: user.id
                        })
                        .done(function(error, /** sails.json.projectUser */projectUser) {
                            if (error) {
                                callback(error, null);
                            } else {
                                callback(null, projectUser)
                            }
                        });
                }
            },

            /**
             * Callback function which is been called after all parallel jobs are processed.
             *
             * @param   {{}}    error
             * @param   {{}}    results
             */
            function(error, results) {
                var output = false;

                if (results.primary || results.contributor) {
                    output = true
                }

                next(error, output);
            }
        );
    }
};<commit_msg>Implemented hasAccessToSprint method to AuthService. Also refactored hasAccessToProject method.<commit_after>/**
 * /api/services/AuthService.js
 *
 * Generic auth service, which is used to check .
 */
var async = require("async");

/**
 * Method checks if specified user has access to specified project or not.
 *
 * @param   {sails.req.user}    user        Signed in user object
 * @param   {Number}            projectId   Project id to check
 * @param   {Function}          next        Main callback function, which is called after checks
 */
exports.hasAccessToProject = function(user, projectId, next) {
    /**
     * Make parallel jobs to determine if user has access to specified project or not.
     *
     *  1)  Fetch project data (check that project exists)
     *  2)  Fetch project primary manager data (project id + current user id)
     *  3)  Fetch project contributor data (project id + current user id)
     *
     * In other words we need basically check if user is
     *
     *  a) primary project manager
     *  b) attached to project in some role
     *
     * If current user is either of these provided next callback is called by false.
     *
     * Note that administrator users have always access to all existing projects.
     */
    async.parallel(
        {
            /**
             * Check that specified project exists.
             *
             * @param   {Function}  callback
             */
            project: function(callback) {
                DataService.getProject(projectId, callback);
            },

            /**
             * Function to fetch possible Project object for signed in user and specified project.
             * User must be project manager.
             *
             * Note that this query may return undefined.
             *
             * @param   {Function}  callback
             */
            primary: function(callback) {
                Project
                    .findOne({
                        id: projectId,
                        managerId: user.id
                    })
                    .done(function(error, /** sails.json.project */project) {
                        if (error) {
                            callback(error, null);
                        } else {
                            callback(null, project)
                        }
                    });
            },

            /**
             * Function to fetch possible ProjectUser object for signed in user and specified project.
             *
             * Note that this query may return undefined.
             *
             * @param   {Function}  callback
             */
            contributor: function(callback) {
                ProjectUser
                    .findOne({
                        projectId: projectId,
                        userId: user.id
                    })
                    .done(function(error, /** sails.json.projectUser */projectUser) {
                        if (error) {
                            callback(error, null);
                        } else {
                            callback(null, projectUser)
                        }
                    });
            }
        },

        /**
         * Callback function which is been called after all parallel jobs are processed.
         *
         * @param   {Error|string}  error
         * @param   {{}}            results
         */
        function(error, results) {
            var output = false;

            // User is administrator, primary project manager or contributor in project
            if (user.admin || results.primary || results.contributor) {
                output = true
            }

            next(error, output);
        }
    );
};

/**
 * Method checks if specified user has access to specified sprint or not.
 *
 * @param   {sails.req.user}    user        Signed in user object
 * @param   {Number}            sprintId    Sprint id to check
 * @param   {Function}          next        Main callback function, which is called after checks
 */
exports.hasAccessToSprint = function(user, sprintId, next) {
    /**
     * Make waterfall jobs to check if user has access to specified sprint or not:
     *
     *  1)  Fetch sprint data (check that sprint exists)
     *  2)  Call AuthService.hasAccessToProject to check actual project right
     *
     * After these jobs call specified callback function with result data.
     */
    async.waterfall(
        [
            /**
             * Check that sprint exists.
             *
             * @param   {Function}  callback
             */
            function(callback) {
                DataService.getSprint(sprintId, callback);
            },

            /**
             * Check that user has access to sprint project.
             *
             * @param   {sails.json.sprint}     sprint      Sprint data
             * @param   {Function}              callback
             */
            function(sprint, callback) {
                AuthService.hasAccessToProject(user, sprint.projectId, callback);
            }
        ],

        /**
         * Callback function which is been called after all parallel jobs are processed.
         *
         * @param   {Error|String}  error
         * @param   {Boolean}       results
         */
        function(error, results) {
            next(error, results);
        }
    );
};
{'fraction_non_alphanumeric': 0.0771264367816092, 'fraction_numerical': 0.0005747126436781609, 'mean_word_length': 1.1531799059638703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4709381', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1900, 'n_words': 654}