summaryrefslogtreecommitdiff
path: root/http/src/js/pages.login.js
blob: c751d496876fde3aaf1cbb90cf7eb50dedaedaf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
epgd.pages.login = {
    useCookie: document.cookie.match(/login=([^;]+)/),
    render: function () {
        if (epgd.login.session)
            this.doLogin('logout');
        this.showForm();
    },
    showForm: function(callback){
        var tr = epgd.tr.pages.login,
            $con = $('#login');
        if (!$con.length) {
            $con = $('<div id="login" title="' + tr.label + '" class="searchDialog ui-dialog" style="margin:5% auto"><form class="ui-dialog-content ui-widget-content"><table cellpadding="5">'
            + '<tr><td>' + tr.userName + '</td><td><input type="text" id="lUser" /></td></tr>'
            + '<tr><td>' + tr.password + '</td><td><input type="password" id="lPass" /></td></tr>'
            + '<tr><td colspan="2"><input type="checkbox" id="lAlways" /> <label for="lAlways">' + tr.loggedAlwaysIn + '</label></td></tr>'
          + '</table></form></div>');
            $con.dialog({
                modal: true, width: "auto",
                closeOnEscape: false,
                buttons: [{
                    text: tr.label,
                    icons: {primary: "ui-icon-unlocked"},
                    click: function () {
                        var form = $con.find('form')[0];
                        if (!form.lUser.value)
                            return !!epgd.utils.topInfo(epgd.tr.error.emptyField.replace('$field$', tr.userName), { isError: 1 });
                        $(this).dialog("close");
                        epgd.pages.login.useCookie = form.lAlways.checked ? [] : null;
                        epgd.pages.login.doLogin(form.lUser.value, form.lPass.value, callback);
                    }
                }]
            }).keypress(function (e) {
                if (e.keyCode == 13) {
                    $(this).parent().find(".ui-dialog-buttonpane button:eq(0)").trigger("click");
                }
            }).parent().find('.ui-dialog-titlebar-close').remove();
        } else
            $con.dialog('open');
    },
    doLogin: function (user, pass, callback) {
        var curLogin = epgd.login,
            uKey = '',
            storage = sessionStorage;

        try {
            storage.setItem('_storagetest_', 1);
            storage.removeItem('_storagetest_');
        } catch (ex) {
            storage = { removeItem: function () { }, setItem: function () { }, getItem: function () { return null; } }
        }

        function trigger_changed() {
            if (epgd.login.user != curLogin.user || epgd.login.rights != curLogin.rights) {
                if (!epgd.login.session) {
                    if (epgd.pages.login.useCookie) {
                        epgd.pages.login.useCookie[1] = null;
                        document.cookie = "login=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
                    }
                    storage.removeItem('login');
                    epgd.login.url = '';
                } else {
                    if (epgd.pages.login.useCookie) {
                        epgd.pages.login.useCookie[1] = uKey;
                        var d = new Date();
                        d.setTime(d.getTime() + 365 * 24 * 60 * 60 * 1000);
                        document.cookie = "login=" + uKey + "; expires=" + d.toUTCString();
                    }
                    storage.setItem('login', uKey);
                    epgd.login.url = 'sid' + epgd.login.session + '/';
                }
                $(window).trigger("login_changed", curLogin);
            }
            epgd.login.url = epgd.login.session ? 'sid' + epgd.login.session + '/' : '';
            if ($.isFunction(callback))
                callback();
        }

        if (user != 'logout') {
            if (curLogin.session) {
                $.ajax({ url: curLogin.url + "data/login", data: JSON.stringify({ key: '' }), type: 'post', contentType: "application/json; charset=utf-8", dataType: 'json' });
                epgd.login.url = '';
            }
            if (epgd.profile.needLogin == "0") {
                epgd.login = { user: '', session: '', rights: 4294967295, url: '' };
                return trigger_changed();
            }
            if (user)
                uKey = YaMD5.hashStr(user + pass);
            else {
                uKey = storage.getItem('login');
                if (!uKey && epgd.pages.login.useCookie)
                    uKey = epgd.pages.login.useCookie[1];
                if (!uKey)
                    return this.showForm(callback);
            }
        }
        $.ajax({ url: epgd.login.url + "data/login", data: JSON.stringify({ key: uKey }), type: 'post', contentType: "application/json; charset=utf-8", dataType: 'json' }).done(function (data) {
	        var tr = epgd.tr.pages.login;
	        epgd.login = data.login || { user: '', session: '', rights: 0, url: '' };
	        if (!epgd.login.session) {
	            if (user == 'logout') {
	                epgd.utils.topInfo(tr.logoutMessage);
	            } else
	                epgd.utils.topInfo(tr.error, { isError: 1 });
	            $('#menu_login a').text(tr.label);
	        } else {
	            $('#menu_login a').text(epgd.login.user + ' ' + tr.logout);
	            if (location.hash == "#menu_login" || location.hash == "#")
	                location.hash = "";
	        }
	        return trigger_changed();
	    });
    }
};