Start of DB development

parent be3bc3a2
/**
* Created by danilaeremin on 08.07.15.
*/
'use strict';
var importModule = function (path) {
var module = require(path);
for (var f in module) {
exports[f] = module[f];
}
};
importModule('./db/dbUtils');
importModule('./db/dbTokens');
importModule('./db/dbUser');
importModule('./db/dbCode');
importModule('./db/dbClient');
/**
* Created by danilaeremin on 17.06.16.
*/
'use strict';
var dbUtils = require('./dbUtils');
var AccessToken = require('./models/mongoose').AccessToken;
var RefreshToken = require('./models/mongoose').RefreshToken;
// AccessToken
exports.saveAccessToken = function(token, userId, scope, refreshToken, callback) {
dbUtils.dbCreateItem(AccessToken,{token: token, refreshToken: refreshToken, userId: userId, scope: scope}, callback);
};
exports.getAccessToken = function (token, callback) {
dbUtils.dbFindOne(AccessToken, {token: token}, callback);
};
exports.removeAccessToken = function (token, callback) {
dbUtils.dbRemoveItem(AccessToken, {token: token}, callback);
};
exports.removeAccessTokenByRefresh = function (token, callback) {
dbUtils.dbRemoveItem(AccessToken, {refreshToken: token}, callback);
};
// RefreshToken
exports.saveRefreshToken = function(token, userId, scope, clientId, callback) {
dbUtils.dbCreateItem(RefreshToken,{ token: token, userId: userId, scope: scope, client: clientId }, callback);
};
exports.getRefreshToken = function (token, callback) {
dbUtils.dbFindOne(RefreshToken, {token: token}, callback);
};
exports.removeRefreshToken = function (token, callback) {
dbUtils.dbRemoveItem(RefreshToken, {token: token}, callback);
};
/**
* Created by danilaeremin on 14.06.16.
*/
'use strict';
var dbUtils = require('./dbUtils');
var User = require('./models/mongoose').User;
exports.getUsers = function (callback) {
dbUtils.dbFind(User, {}, callback);
};
exports.getUsersWithType = function (type, callback) {
dbUtils.dbFind(User, {accountType : type}, function(res) {
callback({status: 'ok', users: res.data});
});
};
exports.getUserById = function (uId, callback) {
dbUtils.dbFindOne(User, {_id: uId}, function (res) {
callback({status : res.status, user : res.data});
});
};
exports.getUserByLogin = function (login, callback) {
dbUtils.dbFindOne(User, {login: login}, function (res) {
callback({status : res.status, user : res.data});
});
};
exports.getUserByIds = function (ids, callback) {
dbUtils.dbFind(User, { _id: { $in: ids }}, function(res) {
callback({status : res.status, users : res.data});
});
};
var createUser = function (user_object, callback) {
dbUtils.dbCreateItem(User,user_object, function (res){
callback({status : res.status, user : res.data});
});
};
exports.createUser = function (password, accountType, mail, name, surname, callback) {
var newUser = {
username : surname + " " + name[0] + ".",
login : mail,
password : password,
accountType : accountType,
mail : mail,
name : name,
surname : surname,
activated : false
};
createUser(newUser,callback);
};
exports.createSocialUser = function (socialId, accountType, mail, name, surname, callback) {
var newUser = {
username : surname + " " + name[0] + ".",
login : socialId,
password : ' ',
accountType : accountType,
mail : mail,
name : name,
surname : surname,
activated : true,
isSocial : true
};
createUser(newUser,callback);
};
exports.changeUserAccountType = function (userId, accountType, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {accountType : accountType}, callback);
};
exports.activateUser = function (userId, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {activated : true}, callback);
};
exports.blockUser = function (userId, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id: userId}, {activated: false}, callback);
};
exports.addGroupTag = function (userId, tag, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {$addToSet : {groupTags : tag}}, callback);
};
exports.addWorldTag = function (userId, tag, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {$addToSet : {worldTags : tag}}, callback);
};
exports.deleteGroupTag = function (userId, tag, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {$pull : {groupTags : { $in: [ tag ] }}}, callback);
};
exports.deleteWorldTag = function (userId, tag, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {$pull : {worldTags : { $in: [ tag ] }}}, callback);
};
exports.updateUserLastLogin = function (userId, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {lastLogin : new Date()}, callback);
};
exports.updateUserLastResultView = function (userId, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {lastResultView : new Date()}, callback);
};
exports.updatePassword = function (userId, password, callback) {
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {password : password}, callback);
};
exports.updateUserInfo = function (userId, mail, name, surname, username, login, callback) {
var update = {
username : username,
mail : mail,
name : name,
surname : surname,
login : login
};
dbUtils.dbFindOneAndUpdate(User, {_id : userId}, update, function (data) {
callback({status : data.status, user : data.data});
});
};
\ No newline at end of file
/**
* Created by danilaeremin on 14.06.16.
*/
'use strict';
var safeCallback = function(callback) {
return callback ? callback : function(){};
}
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;
}
var find = table.find(search);
if(population) {
for (var i = 0; i < population.length; ++i) {
find = find.populate(population[i]);
}
}
find.exec (safeCallback(callback));
};
exports.dbFindOne = function (table, search, population, callback) {
if (callback === undefined) {
callback = population;
population = undefined;
}
var find = table.findOne(search);
if(population) {
for (var i = 0; i < population.length; ++i) {
find = find.populate(population[i]);
}
}
find.exec (safeCallback(callback));
};
exports.dbFindOneAndUpdate = function (table, search, update, callback) {
table.findOneAndUpdate(search, update, {new: true}).exec (safeCallback(callback));
};
exports.dbFindAndUpdate = function (table, search, update, callback) {
table.update(search, update, {multi: true}).exec (safeCallback(callback));
};
exports.dbCreateItem = function (table, data, callback) {
var item = new table(data);
item.save(safeCallback(callback));
};
exports.dbRemoveItem = function (table, data, callback) {
table.remove(data, safeCallback(callback));
};
exports.dbSaveItem = function (item, callback) {
item.save(safeCallback(callback));
};
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.dbCreateItem(table, res, callback);
} else if (callback) {
callback(err, res);
}
});
};
exports.dbAggregate = function (table, aggregate, callback) {
table.aggregate(aggregate, safeCallback(callback));
};
/**
* Created by antonded on 23.11.14.
*/
var mongoose = require('mongoose');
var AccessTokenSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
token: {
type: String,
unique: true,
required: true
},
refreshToken: {
type: mongoose.Schema.Types.ObjectId,
ref: 'RefreshToken',
required: true
},
scope : {
type: String,
default: "",
required: true
},
created: {
type: Date,
default: Date.now,
expires: config.get('security:tokenLife')
}
});
var AccessToken = mongoose.model('AccessToken', AccessTokenSchema);
module.exports = AccessToken;
\ No newline at end of file
/**
* Created by antonded on 23.11.14.
*/
var mongoose = require('mongoose');
var ClientSchema = new mongoose.Schema({
client: {
type: String,
unique: true,
required: true
},
secret: {
type: String,
unique: true,
required: true
},
redirect: {
type: [],
required: true,
default: []
}
});
var Client = mongoose.model('Client', ClientSchema);
module.exports = Client;
\ No newline at end of file
/**
* Created by antonded on 23.11.14.
*/
var mongoose = require('mongoose');
var RefreshTokenSchema = new mongoose.Schema({
code: {
type: String,
unique: true,
required: true
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
client: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Client',
required: true
},
scope : {
type: String,
default: "",
required: true
},
redirect: {
type: String,
required: true
},
created: {
type: Date,
default: Date.now,
required: true
}
});
var RefreshToken = mongoose.model('RefreshToken', RefreshTokenSchema);
module.exports = RefreshToken;
\ No newline at end of file
/**
* Created by antonded on 23.11.14.
*/
var mongoose = require('mongoose');
var RefreshTokenSchema = new mongoose.Schema({
token: {
type: String,
unique: true,
required: true
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
client: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Client',
required: true
},
scope : {
type: String,
default: "",
required: true
},
created: {
type: Date,
default: Date.now,
required: true
}
});
var RefreshToken = mongoose.model('RefreshToken', RefreshTokenSchema);
module.exports = RefreshToken;
\ No newline at end of file
/**
* Created by antonded on 23.11.14.
*/
var mongoose = require('mongoose');
var 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);
}
});
};
module.exports.hashPassword = function(next) {
var user = this;
if (!user.isModified('password')) {
next();
} else {
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) {
next(err);
} else {
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) {
next(err);
} else {
user.password = hash;
next();
}
});
}
});
}
};
var hashPasswordOnUpdate = function(next) {
var update = this;
if (update._update.password) {
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) {
next(err);
} else {
bcrypt.hash(update._update.password, salt, function(err, hash) {
if (err) {
next(err);
} else {
update._update.password = hash;
next();
}
});
}
});
} else {
next();
}
};
global.USER_TYPE = {
ADMIN : "admin",
USER : "user"
};
var UserSchema = new mongoose.Schema({
username: {
type: String,
required: true
},
login: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
accountType: {
type: String,
required: true,
default: global.USER_TYPE.USER
},
mail: {
type: String,
required: true
},
name: {
type: String,
required: true
},
surname: {
type: String,
required: true
},
activated : {
type: Boolean,
required: true,
default: false
},
lastLogin : {
type: Date,
equired: true,
default: Date.now
},
isSocial: {
type: Boolean,
required: true,
default: false
},
meta: {
type: {},
required: true,
default: {}
}
});
UserSchema.methods.validPassword = isValidPassword;
UserSchema.pre('save', hashPassword);
UserSchema.pre('findOneAndUpdate', hashPasswordOnUpdate);
var User = mongoose.model('User', UserSchema);
module.exports = User;
\ No newline at end of file
'use strict';
var mongoose = require('mongoose');
var config = global.config;
var log = global.log;
mongoose.connect(config.get('mongoose:uri'));
var db = mongoose.connection;
db.on('error', function (err) {
log.error('connection error:', err.message);
});
db.once('open', function callback () {
log.info("Connected to DB!");
});
module.exports = {
AccessToken: require('./AccessToken'),
RefreshToken: require('./RefreshToken'),
User: require('./User'),
Code: require('./Code'),
Client: require('./Client'),
mongoose: mongoose
};
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