<commit_before>import AdhConfig = require("../Config/Config");
import AdhHttp = require("../Http/Http");
import AdhMovingColumns = require("../MovingColumns/MovingColumns");
import AdhPermissions = require("../Permissions/Permissions");
import AdhTopLevelState = require("../TopLevelState/TopLevelState");

import AdhUser = require("./User");

import RIUser = require("../../Resources_/adhocracy_core/resources/principal/IUser");
import SIUserBasic = require("../../Resources_/adhocracy_core/sheets/principal/IUserBasic");

var pkgLocation = "/User";


export interface IScopeLogin {
    user : AdhUser.Service;
    credentials : {
        nameOrEmail : string;
        password : string;
    };
    errors : string[];
    supportEmail : string;

    resetCredentials : () => void;
    cancel : () => void;
    logIn : () => angular.IPromise<void>;
}


export interface IScopeRegister {
    input : {
        username : string;
        email : string;
        password : <PASSWORD>;
        passwordRepeat : string;
    };
    siteName : string;
    termsUrl : string;
    errors : string[];
    supportEmail : string;
    success : boolean;

    register : () => angular.IPromise<void>;
    cancel : () => void;
}


var bindServerErrors = (
    $scope : {errors : string[]},
    errors : AdhHttp.IBackendErrorItem[]
) => {
    $scope.errors = [];
    if (!errors.length) {
        $scope.errors.push("Unknown error from server (no details provided)");
    } else {
        errors.forEach((e) => {
            $scope.errors.push(e.description);
        });
    }
};


export var activateController = (
    adhUser : AdhUser.Service,
    adhTopLevelState : AdhTopLevelState.Service,
    adhDone,
    $location : angular.ILocationService
) : void => {
    var key = $location.path().split("/")[2];
    var path = "/activate/" + key;

    var success = () => {
        // FIXME show success message in UI
        // FIXME extract cameFrom from activation key (involves BE)
        adhTopLevelState.redirectToCameFrom("/");
    };

    var error = () => {
        $location.url("activation_error");
    };

    adhUser.activate(path)
        .then(success, error)
        .then(adhDone);
};


export var loginDirective = (
    adhConfig : AdhConfig.IService,
    adhUser : AdhUser.Service,
    adhTopLevelState : AdhTopLevelState.Service
) => {
    return {
        restrict: "E",
        templateUrl: adhConfig.pkg_path + pkgLocation + "/Login.html",
        scope: {},
        link: (scope : IScopeLogin) => {
            scope.errors = [];
            scope.supportEmail = adhConfig.support_email;

            scope.credentials = {
                nameOrEmail: "",
                password: ""
            };

            scope.resetCredentials = () => {
                scope.credentials.nameOrEmail = "";
                scope.credentials.password = "";
            };

            scope.cancel = () => {
                 adhTopLevelState.redirectToCameFrom("/");
            };

            scope.logIn = () => {
                return adhUser.logIn(
                    scope.credentials.nameOrEmail,
                    scope.credentials.password
                ).then(() => {
                    adhTopLevelState.redirectToCameFrom("/");
                }, (errors) => {
                    bindServerErrors(scope, errors);
                    scope.credentials.password = "";
                });
            };
        }
    };
};


export var registerDirective = (
    adhConfig : AdhConfig.IService,
    adhUser : AdhUser.Service,
    adhTopLevelState : AdhTopLevelState.Service
) => {
    return {
        restrict: "E",
        templateUrl: adhConfig.pkg_path + pkgLocation + "/Register.html",
        scope: {},
        link: (scope : IScopeRegister) => {
            scope.siteName = adhConfig.site_name;
            scope.termsUrl = adhConfig.terms_url;

            scope.input = {
                username: "",
                email: "",
                password: "",
                passwordRepeat: ""
            };

            scope.cancel = () => {
                 adhTopLevelState.redirectToCameFrom("/");
            };

            scope.errors = [];
            scope.supportEmail = adhConfig.support_email;

            scope.register = () : angular.IPromise<void> => {
                return adhUser.register(scope.input.username, scope.input.email, scope.input.password, scope.input.passwordRepeat)
                    .then((response) => {
                        scope.errors = [];
                        scope.success = true;
                    }, (errors) => bindServerErrors(scope, errors));
            };
        }
    };
};

export var passwordResetDirective = (
    adhConfig : AdhConfig.IService,
    adhHttp : AdhHttp.Service<any>,
    adhTopLevelState : AdhTopLevelState.Service
) => {
    return {
        restrict: "E",
        templateUrl: adhConfig.pkg_path + pkgLocation + "/PasswordReset.html",
        scope: {},
        link: (scope) => {
            scope.input = {
                password: "",
                passwordRepeat: ""
            };

            scope.cancel = () => {
                 adhTopLevelState.redirectToCameFrom("/");
            };

            scope.errors = [];

            scope.passwordReset = () => {
                return adhHttp.postRaw(adhConfig.rest_url + "/password_reset", {
                    path: adhTopLevelState.get("path"),
                    password: scope.input.password
                }).then(() => {
                    adhTopLevelState.redirectToCameFrom("/login");
                }, AdhHttp.logBackendError)
                .catch((errors) => bindServerErrors(scope, errors));
            };
        }
    };
};

export var createPasswordResetDirective = (
    adhConfig : AdhConfig.IService,
    adhHttp : AdhHttp.Service<any>,
    adhTopLevelState : AdhTopLevelState.Service
) => {<commit_msg>add FIXME about autologin after reset<commit_after>import AdhConfig = require("../Config/Config");
import AdhHttp = require("../Http/Http");
import AdhMovingColumns = require("../MovingColumns/MovingColumns");
import AdhPermissions = require("../Permissions/Permissions");
import AdhTopLevelState = require("../TopLevelState/TopLevelState");

import AdhUser = require("./User");

import RIUser = require("../../Resources_/adhocracy_core/resources/principal/IUser");
import SIUserBasic = require("../../Resources_/adhocracy_core/sheets/principal/IUserBasic");

var pkgLocation = "/User";


export interface IScopeLogin {
    user : AdhUser.Service;
    credentials : {
        nameOrEmail : string;
        password : string;
    };
    errors : string[];
    supportEmail : string;

    resetCredentials : () => void;
    cancel : () => void;
    logIn : () => angular.IPromise<void>;
}


export interface IScopeRegister {
    input : {
        username : string;
        email : string;
        password : string;
        passwordRepeat : string;
    };
    siteName : string;
    termsUrl : string;
    errors : string[];
    supportEmail : string;
    success : boolean;

    register : () => angular.IPromise<void>;
    cancel : () => void;
}


var bindServerErrors = (
    $scope : {errors : string[]},
    errors : AdhHttp.IBackendErrorItem[]
) => {
    $scope.errors = [];
    if (!errors.length) {
        $scope.errors.push("Unknown error from server (no details provided)");
    } else {
        errors.forEach((e) => {
            $scope.errors.push(e.description);
        });
    }
};


export var activateController = (
    adhUser : AdhUser.Service,
    adhTopLevelState : AdhTopLevelState.Service,
    adhDone,
    $location : angular.ILocationService
) : void => {
    var key = $location.path().split("/")[2];
    var path = "/activate/" + key;

    var success = () => {
        // FIXME show success message in UI
        // FIXME extract cameFrom from activation key (involves BE)
        adhTopLevelState.redirectToCameFrom("/");
    };

    var error = () => {
        $location.url("activation_error");
    };

    adhUser.activate(path)
        .then(success, error)
        .then(adhDone);
};


export var loginDirective = (
    adhConfig : AdhConfig.IService,
    adhUser : AdhUser.Service,
    adhTopLevelState : AdhTopLevelState.Service
) => {
    return {
        restrict: "E",
        templateUrl: adhConfig.pkg_path + pkgLocation + "/Login.html",
        scope: {},
        link: (scope : IScopeLogin) => {
            scope.errors = [];
            scope.supportEmail = adhConfig.support_email;

            scope.credentials = {
                nameOrEmail: "",
                password: ""
            };

            scope.resetCredentials = () => {
                scope.credentials.nameOrEmail = "";
                scope.credentials.password = "";
            };

            scope.cancel = () => {
                 adhTopLevelState.redirectToCameFrom("/");
            };

            scope.logIn = () => {
                return adhUser.logIn(
                    scope.credentials.nameOrEmail,
                    scope.credentials.password
                ).then(() => {
                    adhTopLevelState.redirectToCameFrom("/");
                }, (errors) => {
                    bindServerErrors(scope, errors);
                    scope.credentials.password = "";
                });
            };
        }
    };
};


export var registerDirective = (
    adhConfig : AdhConfig.IService,
    adhUser : AdhUser.Service,
    adhTopLevelState : AdhTopLevelState.Service
) => {
    return {
        restrict: "E",
        templateUrl: adhConfig.pkg_path + pkgLocation + "/Register.html",
        scope: {},
        link: (scope : IScopeRegister) => {
            scope.siteName = adhConfig.site_name;
            scope.termsUrl = adhConfig.terms_url;

            scope.input = {
                username: "",
                email: "",
                password: "",
                passwordRepeat: ""
            };

            scope.cancel = () => {
                 adhTopLevelState.redirectToCameFrom("/");
            };

            scope.errors = [];
            scope.supportEmail = adhConfig.support_email;

            scope.register = () : angular.IPromise<void> => {
                return adhUser.register(scope.input.username, scope.input.email, scope.input.password, scope.input.passwordRepeat)
                    .then((response) => {
                        scope.errors = [];
                        scope.success = true;
                    }, (errors) => bindServerErrors(scope, errors));
            };
        }
    };
};

export var passwordResetDirective = (
    adhConfig : AdhConfig.IService,
    adhHttp : AdhHttp.Service<any>,
    adhTopLevelState : AdhTopLevelState.Service
) => {
    return {
        restrict: "E",
        templateUrl: adhConfig.pkg_path + pkgLocation + "/PasswordReset.html",
        scope: {},
        link: (scope) => {
            scope.input = {
                password: "",
                passwordRepeat: ""
            };

            scope.cancel = () => {
                 adhTopLevelState.redirectToCameFrom("/");
            };

            scope.errors = [];

            scope.passwordReset = () => {
                return adhHttp.postRaw(adhConfig.rest_url + "/password_reset", {
                    path: adhTopLevelState.get("path"),
                    password: scope.input.password
                }).then(() => {
                    // FIXME: automatically log in
                    adhTopLevelState.redirectToCameFrom("/login");
                }, AdhHttp.logBackendError)
                .catch((errors) => bindServerErrors(scope, errors));
            };
        }
    };
};

export var createPasswordResetDirective = (
    adhConfig : AdhConfig.IService,
    adhHttp : AdhHttp.Service<any>,
    adhTopLevelState : AdhTopLevelState.Service
) => {
{'fraction_non_alphanumeric': 0.13229473684210527, 'fraction_numerical': 0.00016842105263157895, 'mean_word_length': 1.817556346381969, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 74, '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': '2499422', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3207, 'n_words': 555}