<commit_before>﻿(function (ng, app) {
   
    var subnav = [
            { state: "global-admin", text: "Opret organisation" },
            { state: "global-admins", text: "Globale administratorer" },
            { state: "local-admins", text: "Lokale administratorer" }
    ];

    
    app.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {

        $stateProvider.state('global-admin', {
            url: '/global-admin',
            templateUrl: 'partials/global-admin/new-organization.html',
            controller: 'globalAdmin.NewOrganizationCtrl',
            authRoles: ['GlobalAdmin']
        }).state('local-admins', {
            url: '/global-admin/local-admins',
            templateUrl: 'partials/global-admin/local-admins.html',
            controller: 'globalAdmin.LocalAdminsCtrl',
            authRoles: ['GlobalAdmin']
        })
            .state('global-admins', {
                url: '/global-admin/global-admins',
                templateUrl: 'partials/global-admin/global-admins.html',
                controller: 'globalAdmin.GlobalAdminsCtrl',
                authRoles: ['GlobalAdmin']
            });

    }]);
    
    
    app.controller('globalAdmin.NewOrganizationCtrl', ['$rootScope', '$scope', '$http', 'notify', function ($rootScope, $scope, $http, notify) {
        $rootScope.page.title = 'Ny organisation';
        $rootScope.page.subnav = subnav;

        function init() {
            $scope.org = {};
            $scope.org.accessModifier = 0;
            $scope.org.type = 1; // set type to municipality by default
        };
        init();

        $scope.submit = function() {
            if ($scope.addForm.$invalid) return;

            var payload = $scope.org;
            $http.post('api/organization', payload)
                .success(function(result) {
                    notify.addSuccessMessage("Organisationen " + result.response.name + " er blevet oprettet!");
                    delete $scope.org;
                    init();
                })
                .error(function(result) {
                    notify.addErrorMessage("Organisationen " + $scope.org.name + " kunne ikke oprettes!");
                });
        };
    }]);
    
    app.controller('globalAdmin.LocalAdminsCtrl', ['$rootScope', '$scope', '$http', 'notify', function ($rootScope, $scope, $http, notify) {
        $rootScope.page.title = 'Lokal administratorer';
        $rootScope.page.subnav = subnav;

        $scope.organizations = {};
        $http.get("api/organization").success(function (result) {
            _.each(result.response, function (org) {
                $scope.organizations[org.id] = org;
            });
        });

        var localAdminRole = null;
        $http.get("api/adminrole?getLocalAdminRole=true").success(function(result) {
            localAdminRole = result.response;
        });

        $scope.adminRights = [];
        function pushRight(right) {
            right.show = true;

            right.orgForSelect = right.objectId;
            right.userForSelect = {
                id: right.user.id,
                text: right.user.name,
                user: right.user
            };

            $scope.adminRights.push(right);
        }

        $http.get("api/organization?rights").success(function(result) {
            _.each(result.response, pushRight);
        });
        
        function newLocalAdmin() {
            if (!(localAdminRole && $scope.newOrg && $scope.newUser)) return;

            var user = $scope.newUser;
            var uId = user.id;
            var oId = parseInt($scope.newOrg);
            var orgName = $scope.organizations[oId].name;
            
            var rId = localAdminRole.id;

            if (!(uId && oId && rId)) return;

            var data = {
                userId: uId,
                roleId: rId,
            };

            console.log(data);
            var msg = notify.addInfoMessage("Arbejder ...", false);
            
            $http.post("api/organization/" + oId, data, { handleBusy: true }).success(function(result) {
                msg.toSuccessMessage(user.text + " er blevet lokal administrator for " + orgName);
                $scope.newUser = null;
                $scope.newOrg = null;

                pushRight(result.response);

            }).error(function (result) {
                msg.toErrorMessage("Kunne ikke gøre " + user.text + " til lokal administrator for " + orgName);
            });
        }

        $scope.$watch("newUser", function(newVal, oldVal) {
            if (newVal === oldVal || !newVal) return;

            newLocalAdmin();
        });
        
        $scope.$watch("newOrg", function (newVal, oldVal) {
            if (newVal === oldVal || !newVal) return;

            newLocalAdmin();
        });

        $scope.deleteLocalAdmin = function(right) {

            var oId = right.objectId;
            var rId = right.roleId;
            var uId = right.userId;

            var msg = notify.addInfoMessage("Arbejder ...", false);
            $http.delete("api/organization/" + oId + "?rId=" + rId + "&uId=" + uId).success(function (deleteResult) {
                right.show = false;
                msg.toSuccessMessage(right.user.name + " er ikke længere lokal administrator");
            }).error(function (deleteResult) {

                msg.toErrorMessage("Kunne ikke fjerne " + right.user.name + " som lokal administrator");
            });
        };

        $scope.updateLocalAdmin = function(right) {

            if (!right.orgForSelect || !right.userForSelect || !localAdminRole) return;

            //old values
            var oIdOld = right.objectId;
            var uIdOld = right.userId;

            //new values
            var oIdNew = parseInt(right.orgForSelect);
            var uIdNew = right.userForSelect.id;

            //if nothing was changed, just exit edit-mode
            if (oIdOld == oIdNew && uIdOld == uIdNew) {
                right.edit = false;
                return;
            }
            
            //otherwise, we should delete the old entry, then add a new one

            var rId = localAdminRole.id;

            var msg = notify.addInfoMessage("Arbejder ...", false);
            
            $http.delete("api/organization/" + oIdOld + "?rId=" + rId + "&uId=" + uIdOld, { handleBusy: true }).success(function (deleteResult) {

                var newData = {
                    userId: uIdNew,
                    roleId: rId,
                };
                
                var orgName = $scope.organizations[oIdNew].name;

                $http.post("api/organization/" + oIdNew, newData).success(function (result) {

                    right.roleId = result.response.roleId;
                    right.user = result.response.user;
                    right.userId = result.response.userId;
<commit_msg>New rights url<commit_after>﻿(function (ng, app) {
   
    var subnav = [
            { state: "global-admin", text: "Opret organisation" },
            { state: "global-admins", text: "Globale administratorer" },
            { state: "local-admins", text: "Lokale administratorer" }
    ];

    
    app.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {

        $stateProvider.state('global-admin', {
            url: '/global-admin',
            templateUrl: 'partials/global-admin/new-organization.html',
            controller: 'globalAdmin.NewOrganizationCtrl',
            authRoles: ['GlobalAdmin']
        }).state('local-admins', {
            url: '/global-admin/local-admins',
            templateUrl: 'partials/global-admin/local-admins.html',
            controller: 'globalAdmin.LocalAdminsCtrl',
            authRoles: ['GlobalAdmin']
        })
            .state('global-admins', {
                url: '/global-admin/global-admins',
                templateUrl: 'partials/global-admin/global-admins.html',
                controller: 'globalAdmin.GlobalAdminsCtrl',
                authRoles: ['GlobalAdmin']
            });

    }]);
    
    
    app.controller('globalAdmin.NewOrganizationCtrl', ['$rootScope', '$scope', '$http', 'notify', function ($rootScope, $scope, $http, notify) {
        $rootScope.page.title = 'Ny organisation';
        $rootScope.page.subnav = subnav;

        function init() {
            $scope.org = {};
            $scope.org.accessModifier = 0;
            $scope.org.type = 1; // set type to municipality by default
        };
        init();

        $scope.submit = function() {
            if ($scope.addForm.$invalid) return;

            var payload = $scope.org;
            $http.post('api/organization', payload)
                .success(function(result) {
                    notify.addSuccessMessage("Organisationen " + result.response.name + " er blevet oprettet!");
                    delete $scope.org;
                    init();
                })
                .error(function(result) {
                    notify.addErrorMessage("Organisationen " + $scope.org.name + " kunne ikke oprettes!");
                });
        };
    }]);
    
    app.controller('globalAdmin.LocalAdminsCtrl', ['$rootScope', '$scope', '$http', 'notify', function ($rootScope, $scope, $http, notify) {
        $rootScope.page.title = 'Lokal administratorer';
        $rootScope.page.subnav = subnav;

        $scope.organizations = {};
        $http.get("api/organization").success(function (result) {
            _.each(result.response, function (org) {
                $scope.organizations[org.id] = org;
            });
        });

        var localAdminRole = null;
        $http.get("api/adminrole?getLocalAdminRole=true").success(function(result) {
            localAdminRole = result.response;
        });

        $scope.adminRights = [];
        function pushRight(right) {
            right.show = true;

            right.orgForSelect = right.objectId;
            right.userForSelect = {
                id: right.user.id,
                text: right.user.name,
                user: right.user
            };

            $scope.adminRights.push(right);
        }

        $http.get("api/adminrights").success(function(result) {
            _.each(result.response, pushRight);
        });
        
        function newLocalAdmin() {
            if (!(localAdminRole && $scope.newOrg && $scope.newUser)) return;

            var user = $scope.newUser;
            var uId = user.id;
            var oId = parseInt($scope.newOrg);
            var orgName = $scope.organizations[oId].name;
            
            var rId = localAdminRole.id;

            if (!(uId && oId && rId)) return;

            var data = {
                userId: uId,
                roleId: rId,
            };

            console.log(data);
            var msg = notify.addInfoMessage("Arbejder ...", false);
            
            $http.post("api/adminrights/" + oId, data, { handleBusy: true }).success(function (result) {
                msg.toSuccessMessage(user.text + " er blevet lokal administrator for " + orgName);
                $scope.newUser = null;
                $scope.newOrg = null;

                pushRight(result.response);

            }).error(function (result) {
                msg.toErrorMessage("Kunne ikke gøre " + user.text + " til lokal administrator for " + orgName);
            });
        }

        $scope.$watch("newUser", function(newVal, oldVal) {
            if (newVal === oldVal || !newVal) return;

            newLocalAdmin();
        });
        
        $scope.$watch("newOrg", function (newVal, oldVal) {
            if (newVal === oldVal || !newVal) return;

            newLocalAdmin();
        });

        $scope.deleteLocalAdmin = function(right) {

            var oId = right.objectId;
            var rId = right.roleId;
            var uId = right.userId;

            var msg = notify.addInfoMessage("Arbejder ...", false);
            $http.delete("api/adminrights/" + oId + "?rId=" + rId + "&uId=" + uId).success(function (deleteResult) {
                right.show = false;
                msg.toSuccessMessage(right.user.name + " er ikke længere lokal administrator");
            }).error(function (deleteResult) {

                msg.toErrorMessage("Kunne ikke fjerne " + right.user.name + " som lokal administrator");
            });
        };

        $scope.updateLocalAdmin = function(right) {

            if (!right.orgForSelect || !right.userForSelect || !localAdminRole) return;

            //old values
            var oIdOld = right.objectId;
            var uIdOld = right.userId;

            //new values
            var oIdNew = parseInt(right.orgForSelect);
            var uIdNew = right.userForSelect.id;

            //if nothing was changed, just exit edit-mode
            if (oIdOld == oIdNew && uIdOld == uIdNew) {
                right.edit = false;
                return;
            }
            
            //otherwise, we should delete the old entry, then add a new one

            var rId = localAdminRole.id;

            var msg = notify.addInfoMessage("Arbejder ...", false);
            
            $http.delete("api/adminrights/" + oIdOld + "?rId=" + rId + "&uId=" + uIdOld, { handleBusy: true }).success(function (deleteResult) {

                var newData = {
                    userId: uIdNew,
                    roleId: rId,
                };
                
                var orgName = $scope.organizations[oIdNew].name;

                $http.post("api/adminrights/" + oIdNew, newData).success(function (result) {

                    right.roleId = result.response.roleId;
                    right.user = result.response.user;
                    right.userId = result.response.userId;

{'fraction_non_alphanumeric': 0.131590382387022, 'fraction_numerical': 0.00028968713789107763, 'mean_word_length': 1.6720201238390093, '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': '3631052', 'n_tokens_mistral': 3917, 'n_tokens_neox': 3743, 'n_words': 731}