aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/users/user.js
blob: 3c041571ddb572fe6bb6ff73e3ecd20d8ee6c8c5 (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
import _ from 'underscore';
import Backbone from 'backbone';

export default Backbone.Model.extend({
  idAttribute: 'login',

  urlRoot: function () {
    return baseUrl + '/api/users';
  },

  defaults: function () {
    return {
      groups: [],
      scmAccounts: []
    };
  },

  toQuery: function () {
    var q = this.toJSON();
    _.each(q, function (value, key) {
      if (_.isArray(value)) {
        q[key] = value.join(',');
      }
    });
    return q;
  },

  isNew: function () {
    // server never sends a password
    return this.has('password');
  },

  sync: function (method, model, options) {
    var opts = options || {};
    if (method === 'create') {
      _.defaults(opts, {
        url: this.urlRoot() + '/create',
        type: 'POST',
        data: _.pick(model.toQuery(), 'login', 'name', 'email', 'password', 'scmAccounts')
      });
    }
    if (method === 'update') {
      _.defaults(opts, {
        url: this.urlRoot() + '/update',
        type: 'POST',
        data: _.pick(model.toQuery(), 'login', 'name', 'email', 'scmAccounts')
      });
    }
    if (method === 'delete') {
      _.defaults(opts, {
        url: this.urlRoot() + '/deactivate',
        type: 'POST',
        data: { login: this.id }
      });
    }
    return Backbone.ajax(opts);
  },

  changePassword: function (password, options) {
    var opts = _.defaults(options || {}, {
      url: this.urlRoot() + '/change_password',
      type: 'POST',
      data: {
        login: this.id,
        password: password
      }
    });
    return Backbone.ajax(opts);
  }
});