<commit_before>/*!
 * Copyright 2014 Apereo Foundation (AF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var _ = require('underscore');
var ShortId = require('shortid');
var util = require('util');

var AuthzAPI = require('oae-authz');
var AuthzConstants = require('oae-authz/lib/constants').AuthzConstants;
var AuthzUtil = require('oae-authz/lib/util');
var Config = require('oae-config/lib/api').config('oae-principals');
var LibraryAPI = require('oae-library');
var log = require('oae-logger').logger('oae-principals');
var OaeUtil = require('oae-util/lib/util');
var Signature = require('oae-util/lib/signature');
var TenantsUtil = require('oae-tenants/lib/util');
var Validator = require('oae-authz/lib/validator').Validator;

var Group = require('./model.group').Group;
var PrincipalsConstants = require('./constants').PrincipalsConstants;
var PrincipalsDAO = require('./internal/dao');
var PrincipalsMembersLibrary = require('./libraries/members');
var PrincipalsEmitter = require('./internal/emitter');
var PrincipalsUtil = require('./util');

/**
 * Get the basic profile for a group.
 *
 * @param  {Context}  ctx             Standard context object containing the current user and the current tenant
 * @param  {String}   groupId         An identifier for a group. ex: g:cam:oae-team
 * @param  {Function} callback        Standard callback function
 * @param  {Object}   callback.err    An error that occurred, if any
 * @param  {Group}    callback.group  The group object
 */
var getGroup = module.exports.getGroup = function(ctx, groupId, callback) {
    var validator = new Validator();
    validator.check(groupId, {'code': 400,'msg': 'An invalid group id was specified'}).isGroupId();
    if (validator.hasErrors()) {
        return callback(validator.getFirstError());
    }

    return PrincipalsUtil.getPrincipal(ctx, groupId, callback);
};

/**
 * Get a full group profile. In addition to the basic profile, this includes access information such as:
 *  `isManager`: Whether or not the user in context can manage the group
 *  `isMember`: Whether or not the user in context is a member of the group
 *  `canJoin`: Whether or not the user in context can join the group
 *
 * This also differs from `getGroup` in that if the current user is not allowed to access the group, rather than "scrubbing" sensitive information from the group, it will send back a 401 error.
 *
 * @param  {Context}   ctx             Standard context object containing the current user and the current tenant
 * @param  {String}    groupId         The id of the group to get
 * @param  {Function}  callback        Standard callback function
 * @param  {Object}    callback.err    An error that occurred, if any
 * @param  {Group}     callback.group  The agumented group object
 */
var getFullGroupProfile = module.exports.getFullGroupProfile = function(ctx, groupId, callback) {
    getGroup(ctx, groupId, function(err, group) {
        if (err) {
            return callback(err);
        } else if (group.deleted) {
            return callback({'code': 404, 'msg': util.format('Couldn\'t find principal: %s', groupId)});
        }

        // Determine what roles, if any, the current user has on the group
        _getAllRoles(ctx, groupId, function(err, roles) {
            if (err) {
                return callback(err);
            }

            var hasRole = (!_.isEmpty(roles));
            if (!_canView(ctx, group, hasRole)) {
                return callback({'code': 401, 'msg': 'You do not have access to this group'});
            }

            // A tenant or global administrator should be marked as a member and a manager
            var isAdmin =  (ctx.user() && ctx.user().isAdmin(group.tenant.alias));

            group.isMember = (isAdmin || _.contains(roles, PrincipalsConstants.roles.MEMBER) || _.contains(roles, PrincipalsConstants.roles.MANAGER));
            group.isManager = (isAdmin || _.contains(roles, PrincipalsConstants.roles.MANAGER));
            group.canJoin = _canJoin(ctx, group, hasRole);

            if (group.isMember) {
                // Generate a signature that can be used for push notifications
                group.signature = Signature.createExpiringResourceSignature(ctx, groupId);

            }

            // Only fetch the group creator if there is one
            OaeUtil.invokeIfNecessary(group.createdBy, PrincipalsUtil.getPrincipal, ctx, group.createdBy, function(err, createdBy) {
                if (err) {
                    return callback(err);
                } else if (createdBy) {
                    group.createdBy = createdBy;
                }

                // As part of the group profile, get the top 9 members in the members library
                _getMembersLibrary(ctx, group, hasRole, null, 9, function(err, members) {
                    if (err) {<commit_msg>(GroupProfiles) Reduce number of featured members to 8<commit_after>/*!
 * Copyright 2014 Apereo Foundation (AF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var _ = require('underscore');
var ShortId = require('shortid');
var util = require('util');

var AuthzAPI = require('oae-authz');
var AuthzConstants = require('oae-authz/lib/constants').AuthzConstants;
var AuthzUtil = require('oae-authz/lib/util');
var Config = require('oae-config/lib/api').config('oae-principals');
var LibraryAPI = require('oae-library');
var log = require('oae-logger').logger('oae-principals');
var OaeUtil = require('oae-util/lib/util');
var Signature = require('oae-util/lib/signature');
var TenantsUtil = require('oae-tenants/lib/util');
var Validator = require('oae-authz/lib/validator').Validator;

var Group = require('./model.group').Group;
var PrincipalsConstants = require('./constants').PrincipalsConstants;
var PrincipalsDAO = require('./internal/dao');
var PrincipalsMembersLibrary = require('./libraries/members');
var PrincipalsEmitter = require('./internal/emitter');
var PrincipalsUtil = require('./util');

/**
 * Get the basic profile for a group.
 *
 * @param  {Context}  ctx             Standard context object containing the current user and the current tenant
 * @param  {String}   groupId         An identifier for a group. ex: g:cam:oae-team
 * @param  {Function} callback        Standard callback function
 * @param  {Object}   callback.err    An error that occurred, if any
 * @param  {Group}    callback.group  The group object
 */
var getGroup = module.exports.getGroup = function(ctx, groupId, callback) {
    var validator = new Validator();
    validator.check(groupId, {'code': 400,'msg': 'An invalid group id was specified'}).isGroupId();
    if (validator.hasErrors()) {
        return callback(validator.getFirstError());
    }

    return PrincipalsUtil.getPrincipal(ctx, groupId, callback);
};

/**
 * Get a full group profile. In addition to the basic profile, this includes access information such as:
 *  `isManager`: Whether or not the user in context can manage the group
 *  `isMember`: Whether or not the user in context is a member of the group
 *  `canJoin`: Whether or not the user in context can join the group
 *
 * This also differs from `getGroup` in that if the current user is not allowed to access the group, rather than "scrubbing" sensitive information from the group, it will send back a 401 error.
 *
 * @param  {Context}   ctx             Standard context object containing the current user and the current tenant
 * @param  {String}    groupId         The id of the group to get
 * @param  {Function}  callback        Standard callback function
 * @param  {Object}    callback.err    An error that occurred, if any
 * @param  {Group}     callback.group  The agumented group object
 */
var getFullGroupProfile = module.exports.getFullGroupProfile = function(ctx, groupId, callback) {
    getGroup(ctx, groupId, function(err, group) {
        if (err) {
            return callback(err);
        } else if (group.deleted) {
            return callback({'code': 404, 'msg': util.format('Couldn\'t find principal: %s', groupId)});
        }

        // Determine what roles, if any, the current user has on the group
        _getAllRoles(ctx, groupId, function(err, roles) {
            if (err) {
                return callback(err);
            }

            var hasRole = (!_.isEmpty(roles));
            if (!_canView(ctx, group, hasRole)) {
                return callback({'code': 401, 'msg': 'You do not have access to this group'});
            }

            // A tenant or global administrator should be marked as a member and a manager
            var isAdmin =  (ctx.user() && ctx.user().isAdmin(group.tenant.alias));

            group.isMember = (isAdmin || _.contains(roles, PrincipalsConstants.roles.MEMBER) || _.contains(roles, PrincipalsConstants.roles.MANAGER));
            group.isManager = (isAdmin || _.contains(roles, PrincipalsConstants.roles.MANAGER));
            group.canJoin = _canJoin(ctx, group, hasRole);

            if (group.isMember) {
                // Generate a signature that can be used for push notifications
                group.signature = Signature.createExpiringResourceSignature(ctx, groupId);

            }

            // Only fetch the group creator if there is one
            OaeUtil.invokeIfNecessary(group.createdBy, PrincipalsUtil.getPrincipal, ctx, group.createdBy, function(err, createdBy) {
                if (err) {
                    return callback(err);
                } else if (createdBy) {
                    group.createdBy = createdBy;
                }

                // As part of the group profile, get the top 8 members in the members library
                _getMembersLibrary(ctx, group, hasRole, null, 8, function(err, members) {
                    if (err) {
{'fraction_non_alphanumeric': 0.10707989236336643, 'fraction_numerical': 0.004175559060963162, 'mean_word_length': 2.9712601326455417, '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': '7049292', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2871, 'n_words': 1035}