summaryrefslogtreecommitdiffstats
path: root/base/tps-tomcat/shared/webapps/tps/js/user.js
blob: 670780c40ccbc2ee283f62b19420a5b7fbe22c25 (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
/* --- 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 UserModel = Model.extend({
    urlRoot: "/tps/rest/admin/users",
    parseResponse: function(response) {

        if (!response || !response.User) return {};

        var attrs = {};
        if (response.User.Attributes) {
            var attributes = response.User.Attributes.Attribute;
            attributes = attributes == undefined ? [] : [].concat(attributes);

            _(attributes).each(function(attribute) {
                var name = attribute["@name"];
                var value = attribute["$"];
                attrs[name] = value;
            });
        }

        return {
            id: response.User["@id"],
            userID: response.User.UserID,
            fullName: response.User.FullName,
            email: response.User.Email,
            state: response.User.State,
            type: response.User.Type,
            attributes: attrs
        };
    },
    createRequest: function(attributes) {
        var attrs = [];
        _(attributes.attributes).each(function(value, name) {
            attrs.push({
                Attribute: {
                    "@name": name,
                    "$": value
                }
            });
        });

        return {
            User: {
                "@id": this.id,
                UserID: attributes.userID,
                FullName: attributes.fullName,
                Email: attributes.email,
                State: attributes.state,
                Type: attributes.type,
                Attributes: attrs
            }
        };
    }
});

var UserCollection = Collection.extend({
    model: UserModel,
    urlRoot: "/tps/rest/admin/users",
    getEntries: function(response) {
        return response.Users.User;
    },
    getLinks: function(response) {
        return response.Users.Link;
    },
    parseEntry: function(entry) {
        return new UserModel({
            id: entry["@id"],
            userID: entry.UserID,
            fullName: entry.FullName
        });
    }
});

var UserDialog = Dialog.extend({
    loadField: function(input) {
        var self = this;

        var name = input.attr("name");
        if (name != "tpsProfiles") {
            UserDialog.__super__.loadField.call(self, input);
            return;
        }

        var attributes = self.model.get("attributes");
        if (attributes) {
            var value = attributes.tpsProfiles;
            input.val(value);
        }
    },
    saveField: function(input, attributes) {
        var self = this;

        var name = input.attr("name");
        if (name != "tpsProfiles") {
            UserDialog.__super__.saveField.call(self, input, attributes);
            return;
        }

        var attrs = attributes["attributes"];
        if (attrs == undefined) attrs = {};
        attrs.tpsProfiles = input.val();
        attributes["attributes"] = attrs;
    }
});