<commit_before>(function (angular, _) {
    "use strict";

    angular.module("mfl.auth.services", [
        "sil.api.wrapper",
        "mfl.auth.oauth2",
        "mfl.auth.permissions",
        "ui.router"
    ])

    .service("mfl.auth.services.login", ["api", "$window", "$q", "api.oauth2",
        function (API, $window, $q, oauth2) {
            var url = {
                curr_user : "api/rest-auth/user/"
            };
            var store_key = "auth.user";
            var api = API.getApi();
            var storage = $window.localStorage;

            this.login = function (user) {
                return oauth2.fetchToken(user.username, user.password);
            };

            this.currentUser = function () {
                return api.callApi("GET", api.makeUrl(url.curr_user))
                    .success(function (data) {
                        storage.setItem(store_key, JSON.stringify(data));
                    });
            };

            this.getUser = function(){
                return JSON.parse(storage.getItem(store_key));
            };

            this.isLoggedIn = function () {
                var user = this.getUser();
                var has_token = oauth2.getToken();
                return (! _.isNull(user)) && (! _.isNull(has_token));
            };

            this.logout = function () {
                storage.removeItem(store_key);
                return oauth2.revokeToken(oauth2.getToken());
            };
        }
    ])

    .service("mfl.auth.services.statecheck",
        ["$rootScope", "$injector", "mfl.auth.services.login",
        "mfl.auth.permissions.checker", "HOME_PAGE_NAME",
        function ($rootScope, $injector, loginService, permChecker, HOME_PAGE_NAME) {
            var cancel_listen;

            var change_state = function (name, args) {
                var $state = $injector.get("$state");
                $state.go(name, args);
            };

            var page_check = function (evt, toState) {
                if (loginService.isLoggedIn()) {
                    if (_.contains(["reset_pwd", "reset_pwd_confirm", "login"], toState.name)) {
                        evt.preventDefault();
                        change_state(HOME_PAGE_NAME);
                    } else if (! permChecker.hasPermission(toState.permission)) {
                        evt.preventDefault();
                        window.alert("You don't have permission to access the page.");
                        change_state(HOME_PAGE_NAME);
                    }
                    return;
                }

                if (_.contains(["login", "logout", "reset_pwd", "reset_pwd_confirm", ""],
                        toState.name)) {
                    return;
                }

                evt.preventDefault();
                change_state("login", {"next": toState.name});
            };

            var start = function () {
                cancel_listen = $rootScope.$on("$stateChangeStart", page_check);
            };

            var stop = function () {
                try {
                    cancel_listen();
                } catch (e) {
                }<commit_msg>Fix broken test<commit_after>(function (angular, _) {
    "use strict";

    angular.module("mfl.auth.services", [
        "sil.api.wrapper",
        "mfl.auth.oauth2",
        "mfl.auth.permissions",
        "ui.router"
    ])

    .service("mfl.auth.services.login", ["api", "$window", "$q", "api.oauth2",
        function (API, $window, $q, oauth2) {
            var url = {
                curr_user : "api/rest-auth/user/"
            };
            var store_key = "auth.user";
            var api = API.getApi();
            var storage = $window.localStorage;

            this.login = function (user) {
                return oauth2.fetchToken(user.username, user.password);
            };

            this.currentUser = function () {
                return api.callApi("GET", api.makeUrl(url.curr_user))
                    .success(function (data) {
                        storage.setItem(store_key, JSON.stringify(data));
                    });
            };

            this.getUser = function(){
                return JSON.parse(storage.getItem(store_key));
            };

            this.isLoggedIn = function () {
                var user = this.getUser();
                var has_token = oauth2.getToken();
                return (! _.isNull(user)) && (! _.isNull(has_token));
            };

            this.logout = function () {
                storage.removeItem(store_key);
                return oauth2.revokeToken(oauth2.getToken());
            };
        }
    ])

    .service("mfl.auth.services.statecheck",
        ["$rootScope", "$injector", "mfl.auth.services.login",
        "mfl.auth.permissions.checker", "HOME_PAGE_NAME",
        function ($rootScope, $injector, loginService, permChecker, HOME_PAGE_NAME) {
            var cancel_listen;

            var change_state = function (name, args) {
                var $state = $injector.get("$state");
                $state.go(name, args);
            };

            var page_check = function (evt, toState) {
                if (loginService.isLoggedIn()) {
                    if (_.contains(["reset_pwd", "reset_pwd_confirm", "login"], toState.name)) {
                        evt.preventDefault();
                        change_state(HOME_PAGE_NAME);
                    } else if (! permChecker.hasPermission(toState.permission)) {
                        evt.preventDefault();
                        window.alert("You don't have permission to access the page.");
                    }
                    return;
                }

                if (_.contains(["login", "logout", "reset_pwd", "reset_pwd_confirm", ""],
                        toState.name)) {
                    return;
                }

                evt.preventDefault();
                change_state("login", {"next": toState.name});
            };

            var start = function () {
                cancel_listen = $rootScope.$on("$stateChangeStart", page_check);
            };

            var stop = function () {
                try {
                    cancel_listen();
                } catch (e) {
                }
{'fraction_non_alphanumeric': 0.13106641123882504, 'fraction_numerical': 0.002234993614303959, 'mean_word_length': 1.3731060606060606, 'pattern_counts': {'":': 2, '<': 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': '205969', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1631, 'n_words': 287}