<%
include("/jagg/jagg.jag");

(function () {
    response.contentType = "application/json; charset=UTF-8";
    var mod, obj, result, username,
            action = request.getParameter("action"),
            site = require("/site/conf/site.json"),
            msg = require("/site/conf/ui-messages.jag");
    if (action === "login" && request.getMethod() == 'POST') {
        mod = jagg.module("user");
        username = request.getParameter("username");

	if (username.indexOf("@") != -1) {
            var splitUserName = username.split("@");
            username = splitUserName[0] + "@" + splitUserName[1].toLowerCase();
            if(splitUserName[2] != null) {
                username = username + "@" + splitUserName[2].toLowerCase();
            }
        }
	
        var tenant = request.getParameter("tenant");
        result = mod.login(username, request.getParameter("password"),tenant);
        if (result.error) {
            obj = {
                error:result.error,
                message:result.message
            };
        } else {
            obj = {
                error:false
            }
        }
        print(obj);
    } else if (action === "logout" && request.getMethod() == 'POST') {
        jagg.setUser(null);
        print({
            error:false
        });

    } else if (action === "loginWithSAMLToken" && request.getMethod() == 'POST') {
        var log = new Log();

        mod = jagg.module("user");
        var samlToken = request.getParameter("samlToken");
        result = mod.loginWithSAMLToken(samlToken);

        if (result.error) {
            obj = {
                error:result.error,
                message:result.message
            };
        } else {
            obj = {
                error:false
            }
        }

        jagg.setUser({username:result.username, cookie:result.cookie});
        session.put("showLogin", "false");

        print(obj);

    } else {
        print({
            error:true,
            message:msg.error.invalidAction(action)
        });
    }
}());
%>
