<commit_before>﻿function SetUserGroupPermissions($scope, navigationService, notificationsService, $http) {

    // Variables used in the view.
    $scope.pageName = $scope.currentNode.name;
    $scope.userOptions = {
        applyPermissions: false
    };
    $scope.processingMessage = "Processing request. Please wait.";
    $scope.successMessage = "Operation successful.";
    $scope.failureMessage = "An unknown error occurred.";
    $scope.showSelection = false;
    $scope.showConfirmButtons = false;
    $scope.showSuccess = false;
    $scope.showFailure = false;
    $scope.showDoneButton = false;
    $scope.showProgress = true;
    $scope.applyPermissionsId = "ugp-apply-permissions-" + Math.random().toString().replace(".", "");
    $scope.userTypes = [];
    $scope.permissions = [];

    // Gets permissions from the server.
    function getPermissions() {

        // Variables.
        var id = parseInt($scope.currentNode.id);
        var url = "/umbraco/backoffice/UGP/UserGroupPermissions/GetGroupPermissions";
        var options = {
            cache: false,
            params: {
                "NodeId": id,
                // Cache buster ensures requests aren't cached.
                "CacheBuster": Math.random()
            }
        };

        // Request user type permissions from server.
        $http.get(url, options).success(function (data) {

            // Set scope variables based on permissions.
            $scope.userTypes = data.UserTypePermissions;
            for (var i = 0; i < data.UserTypePermissions[0].Permissions.length; i++) {
                var permission = data.UserTypePermissions[0].Permissions[i];
                var userTypes = [];
                for (var j = 0; j < data.UserTypePermissions.length; j++) {
                    var userType = data.UserTypePermissions[j];
                    userTypes.push({
                        UserTypeId: userType.UserTypeId,
                        HasPermission: userType.Permissions[i].HasPermission
                    });
                }
                $scope.permissions.push({
                    Label: permission.Label,
                    Letter: permission.Letter,
                    UserTypes: userTypes
                });
            }

            // Update UI.
            $scope.showProgress = false;
            $scope.showSelection = true;
            $scope.showConfirmButtons = true;

        });
    }
    getPermissions();

    // Update the group permissions.
    $scope.updatePermissions = function () {

        // Store permissions by user type ID.
        var userTypePermissions = {};
        for (var i = 0; i < $scope.permissions.length; i++) {
            var permission = $scope.permissions[i];
            for (var j = 0; j < permission.UserTypes.length; j++) {
                var userType = permission.UserTypes[j];
                if (userType.HasPermission) {
                    var existingUserType = userTypePermissions[userType.UserTypeId];
                    existingUserType = existingUserType || {
                        UserTypeId: userType.UserTypeId,
                        Permissions: []
                    };
                    existingUserType.Permissions.push(permission.Letter);
                    userTypePermissions[userType.UserTypeId] = existingUserType;
                }
            }
        }

        // Convert permissions to format expected by server.
        var dataPermissions = [];
        for (var i = 0; i < $scope.permissions[0].UserTypes.length; i++) {
            var userTypeId = $scope.permissions[0].UserTypes[i].UserTypeId;
            dataPermissions.push(userTypePermissions[userTypeId]);
        }

        // Variables.
        var id = parseInt($scope.currentNode.id);
        var url = "/umbraco/backoffice/UGP/UserGroupPermissions/SetGroupPermissions";
        var data = {
            "NodeId": id,<commit_msg>Fixed an error when no permissions are selected for a given user type: https://github.com/Nicholas-Westby/usergrouppermissions/issues/8#issuecomment-160923700<commit_after>﻿function SetUserGroupPermissions($scope, navigationService, notificationsService, $http) {

    // Variables used in the view.
    $scope.pageName = $scope.currentNode.name;
    $scope.userOptions = {
        applyPermissions: false
    };
    $scope.processingMessage = "Processing request. Please wait.";
    $scope.successMessage = "Operation successful.";
    $scope.failureMessage = "An unknown error occurred.";
    $scope.showSelection = false;
    $scope.showConfirmButtons = false;
    $scope.showSuccess = false;
    $scope.showFailure = false;
    $scope.showDoneButton = false;
    $scope.showProgress = true;
    $scope.applyPermissionsId = "ugp-apply-permissions-" + Math.random().toString().replace(".", "");
    $scope.userTypes = [];
    $scope.permissions = [];

    // Gets permissions from the server.
    function getPermissions() {

        // Variables.
        var id = parseInt($scope.currentNode.id);
        var url = "/umbraco/backoffice/UGP/UserGroupPermissions/GetGroupPermissions";
        var options = {
            cache: false,
            params: {
                "NodeId": id,
                // Cache buster ensures requests aren't cached.
                "CacheBuster": Math.random()
            }
        };

        // Request user type permissions from server.
        $http.get(url, options).success(function (data) {

            // Set scope variables based on permissions.
            $scope.userTypes = data.UserTypePermissions;
            for (var i = 0; i < data.UserTypePermissions[0].Permissions.length; i++) {
                var permission = data.UserTypePermissions[0].Permissions[i];
                var userTypes = [];
                for (var j = 0; j < data.UserTypePermissions.length; j++) {
                    var userType = data.UserTypePermissions[j];
                    userTypes.push({
                        UserTypeId: userType.UserTypeId,
                        HasPermission: userType.Permissions[i].HasPermission
                    });
                }
                $scope.permissions.push({
                    Label: permission.Label,
                    Letter: permission.Letter,
                    UserTypes: userTypes
                });
            }

            // Update UI.
            $scope.showProgress = false;
            $scope.showSelection = true;
            $scope.showConfirmButtons = true;

        });
    }
    getPermissions();

    // Update the group permissions.
    $scope.updatePermissions = function () {

        // Store permissions by user type ID.
        var userTypePermissions = {};
        for (var i = 0; i < $scope.permissions.length; i++) {
            var permission = $scope.permissions[i];
            for (var j = 0; j < permission.UserTypes.length; j++) {
                var userType = permission.UserTypes[j];
                var existingUserType = userTypePermissions[userType.UserTypeId];
                existingUserType = existingUserType || {
                    UserTypeId: userType.UserTypeId,
                    Permissions: []
                };
                userTypePermissions[userType.UserTypeId] = existingUserType;
                if (userType.HasPermission) {
                    existingUserType.Permissions.push(permission.Letter);
                }
            }
        }

        // Convert permissions to format expected by server.
        var dataPermissions = [];
        for (var i = 0; i < $scope.permissions[0].UserTypes.length; i++) {
            var userTypeId = $scope.permissions[0].UserTypes[i].UserTypeId;
            var permission = userTypePermissions[userTypeId];
            if (permission) {
                dataPermissions.push(userTypePermissions[userTypeId]);
            }
        }

        // Variables.
        var id = parseInt($scope.currentNode.id);
        var url = "/umbraco/backoffice/UGP/UserGroupPermissions/SetGroupPermissions";
        var data = {
            "NodeId": id,
{'fraction_non_alphanumeric': 0.10814170292106899, 'fraction_numerical': 0.003480422622747048, 'mean_word_length': 1.816240812040602, 'pattern_counts': {'":': 6, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '6545876', 'n_tokens_mistral': 2080, 'n_tokens_neox': 2030, 'n_words': 441}