summaryrefslogtreecommitdiffstats
path: root/base/tps-tomcat/shared/webapps/tps/js/authenticator.js
blob: 8e67a598314ace42e456104704fd2265690c6063 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
/* --- BEGIN COPYRIGHT BLOCK ---
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2013 Red Hat, Inc.
 * All rights reserved.
 * --- END COPYRIGHT BLOCK ---
 *
 * @author Endi S. Dewata
 */

var AuthenticatorModel = Model.extend({
    urlRoot: "/tps/rest/authenticators",
    parseResponse: function(response) {
        return {
            id: response.Authenticator["@id"],
            status: response.Authenticator.Status
        };
    },
    parseXML: function(data) {
        var xml = $(data);
        var entry = $("Authenticator", xml);
        var status = $("Status", entry);
        return {
            id: entry.attr("id"),
            status: status.text()
        };
    },
    createRequest: function(attributes) {
        return {
            Authenticator: {
                "@id": attributes.id,
                Status: attributes.status
            }
        };
    },
    enable: function(options) {
        var self = this;
        $.post(self.url() + "?action=enable")
            .done(function(data, textStatus, jqXHR) {
                self.set(self.parseXML(data));
                options.success.call(self, data, textStatus, jqXHR);
            })
            .fail(function(jqXHR, textStatus, errorThrown) {
                options.error.call(self, jqXHR, textStatus, errorThrown);
            });
    },
    disable: function(options) {
        var self = this;
        $.post(self.url() + "?action=disable")
            .done(function(data, textStatus, jqXHR) {
                self.set(self.parseXML(data));
                options.success.call(self, data, textStatus, jqXHR);
            })
            .fail(function(jqXHR, textStatus, errorThrown) {
                options.error.call(self, jqXHR, textStatus, errorThrown);
            });
    }
});

var AuthenticatorCollection = Collection.extend({
    urlRoot: "/tps/rest/authenticators",
    getEntries: function(response) {
        return response.Authenticators.Authenticator;
    },
    getLinks: function(response) {
        return response.Authenticators.Link;
    },
    parseEntry: function(entry) {
        return new AuthenticatorModel({
            id: entry["@id"],
            status: entry.Status
        });
    }
});

var AuthenticatorDialog = Dialog.extend({
    render: function() {
        var self = this;
        var status = self.model.get("status");
        if (status == "Enabled") {
            self.actions = ["disable", "cancel"];
        } else if (status == "Disabled") {
            self.actions = ["enable", "cancel"];
        } else {
            alert("ERROR: Invalid status: " + status);
        }
        AuthenticatorDialog.__super__.render.call(self);
    },
    performAction: function(action) {
        var self = this;

        if (action == "enable") {
            self.model.enable({
                success: function(data,textStatus, jqXHR) {
                    self.close();
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert("ERROR: " + textStatus);
                }
            });

        } else if (action == "disable") {
            self.model.disable({
                success: function(data,textStatus, jqXHR) {
                    self.close();
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert("ERROR: " + textStatus);
                }
            });

        } else {
            AuthenticatorDialog.__super__.performAction.call(self, action);
        }
    }
});