summaryrefslogtreecommitdiffstats
path: root/app/core/account.js
blob: 539eb4e14b55f97257c6b331c668de7103b5a3ce (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
'use strict';

var mongoose = require('mongoose');


function AccountManager(options) {
    this.core = options.core;
}

AccountManager.prototype.create = function(provider, options, cb) {
    var User = mongoose.model('User');
    var user = new User({ provider: provider });

    Object.keys(options).forEach(function(key) {
        user.set(key, options[key]);
    });

    user.save(cb);
};

AccountManager.prototype.update = function(id, options, cb) {
    var User = mongoose.model('User');
    var usernameChange = false;

    User.findById(id, function (err, user) {
        if (err) {
            return cb(err);
        }

        if (options.firstName) {
            user.firstName = options.firstName;
        }
        if (options.lastName) {
            user.lastName = options.lastName;
        }
        if (options.displayName) {
            user.displayName = options.displayName;
        }
        if (options.email) {
            user.email = options.email;
        }

        if (options.username && options.username !== user.username) {
            var xmppConns = this.core.presence.system.connections.query({
                userId: user._id,
                type: 'xmpp'
            });

            if (xmppConns.length) {
                return cb(null, null, 'You can not change your username ' +
                          'with active XMPP sessions.');
            }

            usernameChange = true;
            user.username = options.username;
        }

        if (user.local) {

            if (options.password || options.newPassword) {
                user.password = options.password || options.newPassword;
            }

        }

        user.save(function(err, user) {
            if (err) {
                return cb(err);
            }

            this.core.emit('account:update', {
                usernameChanged: usernameChange,
                user: user.toJSON()
            });

            if (cb) {
                cb(null, user);
            }

        }.bind(this));
    }.bind(this));
};

AccountManager.prototype.generateToken = function(id, cb) {
    var User = mongoose.model('User');

    User.findById(id, function (err, user) {
        if (err) {
            return cb(err);
        }

        user.generateToken(function(err, token) {
            if (err) {
                return cb(err);
            }

            user.save(function(err) {
                if (err) {
                    return cb(err);
                }

                cb(null, token);
            });
        });
    });
};

AccountManager.prototype.revokeToken = function(id, cb) {
    var User = mongoose.model('User');

    User.update({_id: id}, {$unset: {token: 1}}, cb);
};

module.exports = AccountManager;