<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 (evt, name, args) {
                var $state = $injector.get("$state");
                evt.preventDefault();
                return $state.go(name, args);
            };

            var page_check = function (evt, toState, toParams) {
                if (loginService.isLoggedIn()) {
                    if (toState.name === "logout") {
                        return;
                    }
                    if (toState.redirectTo) {
                        change_state(evt, toState.redirectTo, toParams);
                    } else if (toState.requireUser === false) {
                        change_state(evt, HOME_PAGE_NAME);
                    } else if (! permChecker.hasPermission(toState.permission)) {
                        change_state(evt, "common_403");
                    } else if (loginService.getUser().requires_password_change &&
                               toState.name !== "profile.password") {
                        change_state(evt, "profile.password", {"required": true});
                    }
                    return;
                }

                if ((toState.requireUser === false) || (toState.name === "logout")) {
                    return;
                }

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

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

            var stop = function () {
                try {
                    cancel_listen();
                } catch (e) {
                }
            };

            return {
                "startListening": start,
                "stopListening":  stop
            };
        }
    ])

    .service("mfl.auth.services.profile", ["$q", "api", function ($q, API) {
        var urls = {
            user_profile: "api/rest-auth/user/",
            password_change: "api/rest-auth/password/change/",<commit_msg>Exclude logout state from all state checks<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 (evt, name, args) {
                var $state = $injector.get("$state");
                evt.preventDefault();
                return $state.go(name, args);
            };

            var page_check = function (evt, toState, toParams) {
                if (toState.name === "logout") {
                    return;
                }
                if (loginService.isLoggedIn()) {
                    if (toState.redirectTo) {
                        change_state(evt, toState.redirectTo, toParams);
                    } else if (toState.requireUser === false) {
                        change_state(evt, HOME_PAGE_NAME);
                    } else if (! permChecker.hasPermission(toState.permission)) {
                        change_state(evt, "common_403");
                    } else if (loginService.getUser().requires_password_change &&
                               toState.name !== "profile.password") {
                        change_state(evt, "profile.password", {"required": true});
                    }
                    return;
                }

                if (toState.requireUser === false) {
                    return;
                }

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

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

            var stop = function () {
                try {
                    cancel_listen();
                } catch (e) {
                }
            };

            return {
                "startListening": start,
                "stopListening":  stop
            };
        }
    ])

    .service("mfl.auth.services.profile", ["$q", "api", function ($q, API) {
        var urls = {
            user_profile: "api/rest-auth/user/",
            password_change: "api/rest-auth/password/change/",
{'fraction_non_alphanumeric': 0.13016804481194985, 'fraction_numerical': 0.002667377967457989, 'mean_word_length': 1.3478396994364434, 'pattern_counts': {'":': 8, '<': 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': '4790049', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1957, 'n_words': 347}