<commit_before>import _ from 'underscore';
import $ from 'jquery';

import events from 'girder/events';
import eventStream from 'girder/utilities/EventStream';
import {getCurrentUser, setCurrentUser} from 'girder/auth';
import UserModel from 'girder/models/UserModel';
import {restRequest} from 'girder/rest';

// Fallback variable for anonymous user with no local storage
let acceptTerms = null;

// Add additional instance methods
UserModel.prototype.name = function () {
    let realName;
    if (this.has('login')) {
        realName = `${this.get('firstName')} ${this.get('lastName')} (${this.get('login')})`;
    }

    let displayName;
    if (this.has('name')) {
        displayName = this.get('name');
        if (realName) {
            displayName += ` [${realName}]`;
        }
    } else {
        // The user should always have either a 'login' or a 'name'
        displayName = realName;
    }
    return displayName;
};

UserModel.prototype.canAcceptTerms = function () {
    return this.get('permissions').acceptTerms === true;
};

UserModel.prototype.setAcceptTerms = function () {
    return restRequest({
        path: 'user/acceptTerms',
        type: 'POST'
    })
    .then((resp) => {
        // TODO: In jQuery3, just return or throw the "resp" to resolve or reject
        const deferred = $.Deferred();
        if (_.has(resp, 'extra') && resp.extra === 'hasPermission') {
            // Directly update user permissions
            this.get('permissions').acceptTerms = true;
            this.trigger('change:permissions');
            deferred.resolve(resp);
        } else {
            // This should not fail
            deferred.reject(resp);
        }
        return deferred.promise();
    });
};

UserModel.prototype.canCreateDataset = function () {
    return this.get('permissions').createDataset;
};

UserModel.prototype.setCanCreateDataset = function () {
    return restRequest({
        path: 'user/requestCreateDatasetPermission',
        type: 'POST'
    })
    .then((resp) => {
        // TODO: In jQuery3, just return or throw the "resp" to resolve or reject
        const deferred = $.Deferred();
        if (_.has(resp, 'extra') && resp.extra === 'hasPermission') {
            // Directly update user permissions
            this.get('permissions').createDataset = true;
            this.trigger('change:permissions');
            deferred.resolve(resp);
        } else {
            deferred.reject(resp);
        }
        return deferred.promise();
    });
};

UserModel.prototype.canReviewDataset = function () {
    return this.get('permissions').reviewDataset;
};

UserModel.prototype.getSegmentationSkill = function () {
    return this.get('permissions').segmentationSkill;
};

UserModel.prototype.canAdminStudy = function () {
    return this.get('permissions').adminStudy;
};

// Patch upstream changePassword to return a promise
// TODO: Remove this once Girder is updated<commit_msg>Utilize Promise/A+ compliant behavior of .then<commit_after>import _ from 'underscore';
import $ from 'jquery';

import events from 'girder/events';
import eventStream from 'girder/utilities/EventStream';
import {getCurrentUser, setCurrentUser} from 'girder/auth';
import UserModel from 'girder/models/UserModel';
import {restRequest} from 'girder/rest';

// Fallback variable for anonymous user with no local storage
let acceptTerms = null;

// Add additional instance methods
UserModel.prototype.name = function () {
    let realName;
    if (this.has('login')) {
        realName = `${this.get('firstName')} ${this.get('lastName')} (${this.get('login')})`;
    }

    let displayName;
    if (this.has('name')) {
        displayName = this.get('name');
        if (realName) {
            displayName += ` [${realName}]`;
        }
    } else {
        // The user should always have either a 'login' or a 'name'
        displayName = realName;
    }
    return displayName;
};

UserModel.prototype.canAcceptTerms = function () {
    return this.get('permissions').acceptTerms === true;
};

UserModel.prototype.setAcceptTerms = function () {
    return restRequest({
        path: 'user/acceptTerms',
        type: 'POST'
    })
    .then((resp) => {
        if (_.has(resp, 'extra') && resp.extra === 'hasPermission') {
            // Directly update user permissions
            this.get('permissions').acceptTerms = true;
            this.trigger('change:permissions');
            return resp;
        } else {
            // This should not fail
            throw resp;
        }
    });
};

UserModel.prototype.canCreateDataset = function () {
    return this.get('permissions').createDataset;
};

UserModel.prototype.setCanCreateDataset = function () {
    return restRequest({
        path: 'user/requestCreateDatasetPermission',
        type: 'POST'
    })
    .then((resp) => {
        if (_.has(resp, 'extra') && resp.extra === 'hasPermission') {
            // Directly update user permissions
            this.get('permissions').createDataset = true;
            this.trigger('change:permissions');
            return resp;
        } else {
            throw resp;
        }
    });
};

UserModel.prototype.canReviewDataset = function () {
    return this.get('permissions').reviewDataset;
};

UserModel.prototype.getSegmentationSkill = function () {
    return this.get('permissions').segmentationSkill;
};

UserModel.prototype.canAdminStudy = function () {
    return this.get('permissions').adminStudy;
};

// Patch upstream changePassword to return a promise
// TODO: Remove this once Girder is updated
{'fraction_non_alphanumeric': 0.1363391413687803, 'fraction_numerical': 0.00035925992455541585, 'mean_word_length': 2.7774762550881955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5915921', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1572, 'n_words': 376}