summaryrefslogtreecommitdiffstats
path: root/install/ui/user.js
blob: 1b499a8242190765d3e824a9c47c657381fa4ad4 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*jsl:import ipa.js */

/*  Authors:
 *    Pavel Zuna <pzuna@redhat.com>
 *
 * Copyright (C) 2010 Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

/* REQUIRES: ipa.js, details.js, search.js, add.js, entity.js */

IPA.user = function (){

    var that = IPA.entity({
        name: 'user'
    });

    that.init = function() {

        that.create_association({
            'name': 'group',
            'associator': 'serial'
        });

        that.create_association({
            'name': 'netgroup',
            'associator': 'serial'
        });

        var search_facet = IPA.search_facet({
            'name': 'search',
            'label': 'Search',
            entity_name: that.name
        });
        that.add_facet(search_facet);

        search_facet.create_column({name:'cn'});
        search_facet.create_column({name:'uid'});
        search_facet.create_column({name:'uidnumber'});
        search_facet.create_column({name:'mail'});
        search_facet.create_column({name:'telephonenumber'});
        search_facet.create_column({name:'title'});

        that.add_facet(details_facet({name:'details'}));

        var dialog = IPA.add_dialog({
            'name': 'add',
            'title': 'Add User'
        });
        that.add_dialog(dialog);

        dialog.add_field(IPA.text_widget({ name: 'uid', undo: false }));
        dialog.add_field(IPA.text_widget({ name: 'givenname', undo: false }));
        dialog.add_field(IPA.text_widget({ name: 'sn', undo: false }));
        dialog.init();

        /*eventually,  we need to call
          entity.create_association_facets();
          but we are currently defining the associator using the global
          function after the registration of the entity */
      that.create_association_facets();

        that.entity_init();
    };

    function details_facet(spec) {
        spec = spec || {};
        var that = IPA.details_facet(spec);

        var sections =[
            IPA.stanza({name:'identity', label:IPA.messages.details.identity}).
                input({name:'title'}).
                input({name:'givenname'}).
                input({name:'sn'}).
                input({name:'cn'}).
                input({name:'displayname'}).
                input({name:'initials'}),
            IPA.stanza({name:'account', label:IPA.messages.details.account}).
                custom_input(user_status_widget({name:'nsaccountlock'})).
                input({name:'uid'}).
                input({name:'userpassword', load: user_password_load}).
                input({name:'uidnumber'}).
                input({name:'gidnumber'}).
                input({name:'loginshell'}).
                input({name:'homedirectory'}),
            IPA.stanza({name:'contact', label:IPA.messages.details.contact}).
                input({name:'mail'}).
                input({name:'telephonenumber'}).
                input({name:'pager'}).
                input({name:'mobile'}).
                input({name:'facsimiletelephonenumber'}),
            IPA.stanza({name:'address', label: IPA.messages.details.mailing}).
                input({name:'street'}).
                input({name:'location'}).
                input({name:'state', load:user_state_load}).
                input({name:'postalcode'}),
            IPA.stanza({name:'employee', label:IPA.messages.details.employee}).
                input({name:'ou', label:'Org. Unit'}).
                input({name:'manager'}),
            IPA.stanza({name:'misc', label:IPA.messages.details.misc}).
                input({name:'carlicense'})
        ];
        for (var i = 0; i < sections.length; i += 1){
            that.add_section(sections[i]);
        }
        return that;
    }
    return that;
};


IPA.add_entity(IPA.user());

/* ATTRIBUTE CALLBACKS */


function user_status_widget(spec) {

    spec = spec || {};

    var that = IPA.widget(spec);

    that.update = function() {

        if (!that.record) return;

        $('dd', that.container).remove();

        var dd = IPA.create_first_dd(this.name);
        dd.appendTo(that.container);

        var lock_field = 'nsaccountlock';

        var locked  = that.record[lock_field] &&
            that.record[lock_field][0].toLowerCase() === 'true';
        var title = "Active";
        var text = "Active:  Click to Deactivate";
        if (locked) {
            title = "Inactive";
            text = "Inactive:  Click to Activate";
        }

        function on_lock_win(data, textStatus, xhr){
            var entity = IPA.get_entity(that.entity_name);
            var facet = entity.get_facet('details');
            facet.refresh();
            return false;
        }

        function on_lock_fail(data, textStatus, xhr){
            $("#userstatuslink").text = "Error changing account status";
            return false;
        }

        var status_field =
            $('<a/>',
              {
                  id: 'userstatuslink',
                  title: title,
                  href: "jslink",
                  text: text,
                  click: function() {
                      var jobj = $(this);
                      var val = jobj.attr('title');
                      var pkey =  $.bbq.getState('user-pkey');
                      var command = 'user_enable';
                      if (val == 'Active') {
                          command = 'user_disable';
                      }
                      IPA.cmd(command, [pkey], {}, on_lock_win,on_lock_fail);

                      return (false);
                  }
              });
        status_field.appendTo(dd);
    };

    return that;
}

function resetpwd_on_click(){

    function reset_password(new_password){
        var dialog =  resetpwd_dialog;

        var user_pkey = $.bbq.getState('user-pkey');
        var pw_pkey;
        if (user_pkey === IPA.whoami.uid[0]){
            pw_pkey = [];
        }else{
            pw_pkey = [user_pkey];
        }

        IPA.cmd('passwd',
                pw_pkey, {"password":new_password},
                function(){
                    alert("Password change complete");
                    dialog.dialog("close");
                },
                function(){});
    }


    var resetpwd_dialog =
        $('<div ><dl class="modal">'+
          '<dt>New Password</dt>'+
          '<dd class="first" ><input id="password_1" type="password"/></dd>'+
          '<dt>Repeat Password</dt>'+
          '<dd class="first"><input id="password_2" type="password"/></dd>'+
          '</dl></div>');
    resetpwd_dialog.dialog(
        { modal: true,
          minWidth:400,
          buttons: {
              'Reset Password': function(){
                  var p1 = $("#password_1").val();
                  var p2 = $("#password_2").val();
                  if (p1 != p2){
                      alert("passwords must match");
                      return;
                  }
                  reset_password(p1);
              },
              'Cancel':function(){
                  resetpwd_dialog.dialog('close');
              }
          }});
    return false;
}

function user_password_load(result) {

    var that = this;

    $('dd', that.container).remove();

    var dd = IPA.create_first_dd(this.name);
    dd.appendTo(that.container);

    var link = $('<a/>',{
        href:"jslink",
        click:resetpwd_on_click,
        title:'userpassword',
        text: 'reset password'
    });
    link.appendTo(dd);

}

var select_temp = '<select title="st"></select>';
var option_temp = '<option value="V">V</option>';
var states = [
    'AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FM',
    'FL', 'GA', 'GU', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA',
    'ME', 'MH', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV',
    'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'MP', 'OH', 'OK', 'OR', 'PW',
    'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VI', 'VA',
    'WA', 'WV', 'WI', 'WY', ''
];
function user_state_load(result) {

    var that = this;

    $('dd', that.container).remove();

    //var next = dt.next();
    //next.css('clear', 'none');
    //next.css('width', '70px');

    var dd = IPA.create_first_dd(this.name);
    dd.append(select_temp);
    dd.appendTo(that.container);

    var sel = dd.children().first();
    for (var i = 0; i < states.length; ++i)
        sel.append(option_temp.replace(/V/g, states[i]));

    var st = result['st'];
    if (st)
        sel.val(st);
    else
        sel.val('');
}