move db to promise

parent 37dac1e1
......@@ -10,14 +10,14 @@ var Client = require('./models/mongoose').Client;
// RefreshToken
exports.saveClient = function(client, secret, redirect, callback) {
dbUtils.dbCreateItem(Client,{ client: client, secret: secret, redirect: redirect }, callback);
exports.saveClient = function(client, secret, redirect) {
return dbUtils.dbCreateItem(Client,{ client: client, secret: secret, redirect: redirect });
};
exports.getClient = function (client, callback) {
dbUtils.dbFindOne(Client, {client: client}, callback);
exports.getClient = function (client) {
return dbUtils.dbFindOne(Client, {client: client});
};
exports.removeClient = function (client, callback) {
dbUtils.dbRemoveItem(Client, {client: client}, callback);
exports.removeClient = function (client) {
return dbUtils.dbRemoveItem(Client, {client: client});
};
......@@ -8,14 +8,14 @@ var dbUtils = require('./dbUtils');
var Code = require('./models/mongoose').Code;
exports.saveCode = function(code, userId, scope, clientId, redirect, callback) {
dbUtils.dbCreateItem(Code,{ code: code, user: userId, scope: scope, client: clientId, redirect:redirect }, callback);
exports.saveCode = function(code, userId, scope, clientId, redirect) {
return dbUtils.dbCreateItem(Code,{ code: code, user: userId, scope: scope, client: clientId, redirect:redirect });
};
exports.getCode = function (code, callback) {
dbUtils.dbFindOne(Code, {code: code}, [{ path: 'client', select: 'client' }, { path: 'user', select: 'username' }], callback);
exports.getCode = function (code) {
return dbUtils.dbFindOne(Code, {code: code}, [{ path: 'client', select: 'client' }, { path: 'user', select: 'username' }]);
};
exports.removeCode = function (code, callback) {
dbUtils.dbRemoveItem(Code, {code: code}, callback);
exports.removeCode = function (code) {
return dbUtils.dbRemoveItem(Code, {code: code});
};
......@@ -11,32 +11,32 @@ var RefreshToken = require('./models/mongoose').RefreshToken;
// AccessToken
exports.saveAccessToken = function(token, userId, scope, refreshToken, callback) {
dbUtils.dbCreateItem(AccessToken,{token: token, refreshToken: refreshToken, user: userId, scope: scope}, callback);
exports.saveAccessToken = function(token, userId, scope, refreshToken) {
return dbUtils.dbCreateItem(AccessToken,{token: token, refreshToken: refreshToken, user: userId, scope: scope});
};
exports.getAccessToken = function (token, callback) {
dbUtils.dbFindOne(AccessToken, {token: token}, callback);
exports.getAccessToken = function (token) {
return dbUtils.dbFindOne(AccessToken, {token: token});
};
exports.removeAccessToken = function (token, callback) {
dbUtils.dbRemoveItem(AccessToken, {token: token}, callback);
exports.removeAccessToken = function (token) {
return dbUtils.dbRemoveItem(AccessToken, {token: token});
};
exports.removeAccessTokenByRefresh = function (token, callback) {
dbUtils.dbRemoveItem(AccessToken, {refreshToken: token}, callback);
exports.removeAccessTokenByRefresh = function (token) {
return dbUtils.dbRemoveItem(AccessToken, {refreshToken: token});
};
// RefreshToken
exports.saveRefreshToken = function(token, userId, scope, clientId, callback) {
dbUtils.dbCreateItem(RefreshToken,{ token: token, user: userId, scope: scope, client: clientId }, callback);
exports.saveRefreshToken = function(token, userId, scope, clientId) {
return dbUtils.dbCreateItem(RefreshToken,{ token: token, user: userId, scope: scope, client: clientId });
};
exports.getRefreshToken = function (token, callback) {
dbUtils.dbFindOne(RefreshToken, {token: token}, [{ path: 'client', select: 'client' }, { path: 'user', select: 'username' }], callback);
exports.getRefreshToken = function (token) {
return dbUtils.dbFindOne(RefreshToken, {token: token}, [{ path: 'client', select: 'client' }, { path: 'user', select: 'username' }]);
};
exports.removeRefreshToken = function (token, callback) {
dbUtils.dbRemoveItem(RefreshToken, {token: token}, callback);
exports.removeRefreshToken = function (token) {
return dbUtils.dbRemoveItem(RefreshToken, {token: token});
};
......@@ -8,31 +8,31 @@ var dbUtils = require('./dbUtils');
var User = require('./models/mongoose').User;
exports.getUsers = function (callback) {
dbUtils.dbFind(User, {}, callback);
exports.getUsers = function () {
return dbUtils.dbFind(User, {});
};
exports.getUsersWithType = function (type, callback) {
dbUtils.dbFind(User, {accountType : type}, callback);
exports.getUsersWithType = function (type) {
return dbUtils.dbFind(User, {accountType : type});
};
exports.getUserById = function (uId, callback) {
dbUtils.dbFindOne(User, {_id: uId}, callback);
exports.getUserById = function (uId) {
return dbUtils.dbFindOne(User, {_id: uId});
};
exports.getUserByLogin = function (login, callback) {
dbUtils.dbFindOne(User, {login: login}, callback);
exports.getUserByLogin = function (login) {
return dbUtils.dbFindOne(User, {login: login});
};
exports.getUserByIds = function (ids, callback) {
dbUtils.dbFind(User, { _id: { $in: ids }}, callback);
exports.getUserByIds = function (ids) {
return dbUtils.dbFind(User, { _id: { $in: ids }});
};
var createUser = function (user_object, callback) {
dbUtils.dbCreateItem(User, user_object, callback);
var createUser = function (user_object) {
return dbUtils.dbCreateItem(User, user_object);
};
exports.createUser = function (password, accountType, mail, name, surname, meta, callback) {
exports.createUser = function (password, accountType, mail, name, surname, meta) {
var newUser = {
username : surname + " " + name[0] + ".",
login : mail,
......@@ -45,10 +45,10 @@ exports.createUser = function (password, accountType, mail, name, surname, meta,
activated : false
};
createUser(newUser,callback);
return createUser(newUser,callback);
};
exports.createSocialUser = function (socialId, accountType, mail, name, surname, meta, callback) {
exports.createSocialUser = function (socialId, accountType, mail, name, surname, meta) {
var newUser = {
username : surname + " " + name[0] + ".",
login : socialId,
......@@ -62,30 +62,30 @@ exports.createSocialUser = function (socialId, accountType, mail, name, surname,
isSocial : true
};
createUser(newUser,callback);
return createUser(newUser,callback);
};
exports.changeUserAccountType = function (userId, accountType, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {accountType : accountType}, callback);
exports.changeUserAccountType = function (userId, accountType) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {accountType : accountType});
};
exports.activateUser = function (userId, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {activated : true}, callback);
exports.activateUser = function (userId) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {activated : true});
};
exports.blockUser = function (userId, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id: userId}, {activated: false}, callback);
exports.blockUser = function (userId) {
return dbUtils.dbFindOneAndUpdate(User, {_id: userId}, {activated: false});
};
exports.updateUserLastLogin = function (userId, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {lastLogin : new Date()}, callback);
exports.updateUserLastLogin = function (userId) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {lastLogin : new Date()});
};
exports.updatePassword = function (userId, password, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {password : password}, callback);
exports.updatePassword = function (userId, password) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {password : password});
};
exports.updateUserInfo = function (userId, mail, name, surname, username, login, callback) {
exports.updateUserInfo = function (userId, mail, name, surname, username, login) {
var update = {
username : username,
mail : mail,
......@@ -94,5 +94,5 @@ exports.updateUserInfo = function (userId, mail, name, surname, username, login,
login : login
};
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, update, callback);
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, update);
};
\ No newline at end of file
......@@ -9,18 +9,12 @@ var safeCallback = function(callback) {
};
var Mongoose = require('./models/mongoose').mongoose;
var log = global.log;
exports.str2ObjectId = function (str) {
return new Mongoose.Types.ObjectId(str);
};
exports.dbFind = function (table, search, population, callback) {
if (callback === undefined) {
callback = population;
population = undefined;
}
exports.dbFind = function (table, search, population) {
var find = table.find(search);
if(population) {
......@@ -29,15 +23,10 @@ exports.dbFind = function (table, search, population, callback) {
}
}
find.exec (safeCallback(callback));
return find.exec();
};
exports.dbFindOne = function (table, search, population, callback) {
if (callback === undefined) {
callback = population;
population = undefined;
}
exports.dbFindOne = function (table, search, population) {
var find = table.findOne(search);
if(population) {
......@@ -46,49 +35,50 @@ exports.dbFindOne = function (table, search, population, callback) {
}
}
find.exec (safeCallback(callback));
return find.exec ();
};
exports.dbFindOneAndUpdate = function (table, search, update, callback) {
table.findOneAndUpdate(search, update, {new: true}).exec (safeCallback(callback));
exports.dbFindOneAndUpdate = function (table, search, update) {
return table.findOneAndUpdate(search, update, {new: true}).exec ();
};
exports.dbFindAndUpdate = function (table, search, update, callback) {
table.update(search, update, {multi: true}).exec (safeCallback(callback));
exports.dbFindAndUpdate = function (table, search, update) {
return table.update(search, update, {multi: true}).exec ();
};
exports.dbCreateItem = function (table, data, callback) {
exports.dbCreateItem = function (table, data) {
var item = new table(data);
item.save(safeCallback(callback));
return item.save();
};
exports.dbRemoveItem = function (table, data, callback) {
table.remove(data, safeCallback(callback));
exports.dbRemoveItem = function (table, data) {
return table.remove(data).exec();
};
exports.dbSaveItem = function (item, callback) {
item.save(safeCallback(callback));
exports.dbSaveItem = function (item) {
return item.save();
};
exports.dbDuplicateAndUpdate = function (table, search, update, callback) {
exports.findOne (table, search, function (err, res) {
if (err && res) {
res = res.data.toObject();
delete res._id;
for (var u in update) {
if (update.hasOwnProperty(u)) {
res[u] = update[u];
exports.dbDuplicateAndUpdate = function (table, search, update) {
return exports.findOne (table, search)
.then((doc) => {
if (doc) {
doc = doc.data.toObject();
delete doc._id;
for (var u in update) {
if (update.hasOwnProperty(u)) {
res[u] = update[u];
}
}
}
exports.dbCreateItem(table, res, callback);
} else if (callback) {
callback(err, res);
}
});
return exports.dbCreateItem(table, res);
} else {
throw new Error('No document');
}
});
};
exports.dbAggregate = function (table, aggregate, callback) {
table.aggregate(aggregate, safeCallback(callback));
exports.dbAggregate = function (table, aggregate) {
return table.aggregate(aggregate).exec();
};
......@@ -19,6 +19,11 @@ var ClientSchema = new mongoose.Schema({
type: [],
required: true,
default: []
},
trusted : {
type: Boolean,
required: true,
default: false
}
});
......
......@@ -2,18 +2,21 @@
* Created by antonded on 23.11.14.
*/
var mongoose = require('mongoose');
var bcrypt = require('bcrypt');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const SALT_WORK_FACTOR = 10;
var isValidPassword = function(candidatePassword, callback) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) {
callback(err);
} else {
callback(null, isMatch);
}
var isValidPassword = function(candidatePassword) {
var self = this;
return new Promise((resolve, reject) => {
bcrypt.compare(candidatePassword, self.password, function(err, isMatch) {
if (err) {
reject(err);
} else {
resolve(isMatch);
}
});
});
};
......
'use strict';
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var config = global.config;
var log = global.log;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment