Browse Source

implemented users validation from admin accounts

master
arnaucode 7 years ago
parent
commit
5384336d57
5 changed files with 176 additions and 104 deletions
  1. +8
    -0
      README.md
  2. +161
    -102
      controllers/adminController.js
  3. +2
    -2
      controllers/travelController.js
  4. +1
    -0
      models/userModel.js
  5. +4
    -0
      server.js

+ 8
- 0
README.md

@ -30,6 +30,14 @@ code: https://github.com/arnaucode/carsincommonApp
- https (tls/ssl)
```
### Configuration before run:
In the file adminConfig.js, put the sha256 of the password that allows to create new admins:
```js
module.exports = {
'passwordHash': 'Bzij4hEeEUpmXTWyS+X0LR+YcA8WFjP2P7qhW0sxA6s='/*password raw: adminPassword*/
};
```
#### RESOURCES using:

+ 161
- 102
controllers/adminController.js

@ -2,6 +2,7 @@
var mongoose = require('mongoose');
var adminModel = mongoose.model('adminModel');
var userModel = mongoose.model('userModel');
var userController = require('../controllers/userController');
var notificationModel = mongoose.model('notificationModel');
var travelModel = mongoose.model('travelModel');
var travelCtrl = require('../controllers/travelController');
@ -388,121 +389,121 @@ exports.getUserNetwork = function(req, res) {
var nodes = [];
var edges = [];
//for (var i = 0; i < users.length; i++) {
var node = {
title: user.username,
label: user.username,
image: user.avatar,
shape: "image",
id: user._id,
group: user._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
nodes.push(node);
var uNode = nodes.length - 1;
}
for (var j = 0; j < user.likes.length; j++) {
/*console.log(i + ", " + j);
console.log(nodes);*/
var node = {
title: user.username,
label: user.username,
image: user.avatar,
title: user.likes[j].username,
label: user.likes[j].username,
image: user.likes[j].avatar,
shape: "image",
id: user._id,
group: user._id
id: user.likes[j]._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
//node no exist
nodes.push(node);
var uNode = nodes.length - 1;
}
for (var j = 0; j < user.likes.length; j++) {
/*console.log(i + ", " + j);
console.log(nodes);*/
var node = {
title: user.likes[j].username,
label: user.likes[j].username,
image: user.likes[j].avatar,
shape: "image",
id: user.likes[j]._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
//node no exist
nodes.push(node);
lNode = nodes.length - 1;
} else {
//node already exist
lNode = nodes.length - 1;
} else {
//node already exist
}
var edge = {
from: user._id,
to: user.likes[j]._id,
label: "like",
arrows: "to",
color: {
color: "#E57373" //red300
}
};
edges.push(edge);
}
for (var j = 0; j < user.travels.length; j++) {
/*console.log(i + ", " + j);
console.log(nodes);*/
var node = {
title: user.travels[j].title,
label: user.travels[j].title,
image: "img/" + user.travels[j].type + ".png",
shape: "image",
id: user.travels[j]._id,
value: "0.5",
group: user._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
//node no exist
nodes.push(node);
lNode = nodes.length - 1;
} else {
//node already exist
var edge = {
from: user._id,
to: user.likes[j]._id,
label: "like",
arrows: "to",
color: {
color: "#E57373" //red300
}
var edge = {
from: user._id,
to: user.travels[j]._id,
color: {
color: "#616161" //grey
}
};
edges.push(edge);
};
edges.push(edge);
}
for (var j = 0; j < user.travels.length; j++) {
/*console.log(i + ", " + j);
console.log(nodes);*/
var node = {
title: user.travels[j].title,
label: user.travels[j].title,
image: "img/" + user.travels[j].type + ".png",
shape: "image",
id: user.travels[j]._id,
value: "0.5",
group: user._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
//node no exist
nodes.push(node);
lNode = nodes.length - 1;
} else {
//node already exist
}
//add users that has add like to the user
userModel.find({
likes: req.params.userid
})
.exec(function(err, users) {
for (var i = 0; i < users.length; i++) {
/*console.log(i + ", " + j);
console.log(nodes);*/
var node = {
title: users[i].username,
label: users[i].username,
image: users[i].avatar,
shape: "image",
id: users[i]._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
//node no exist
nodes.push(node);
lNode = nodes.length - 1;
} else {
//node already exist
var edge = {
from: user._id,
to: user.travels[j]._id,
color: {
color: "#616161" //grey
}
};
edges.push(edge);
}
//add users that has add like to the user
userModel.find({
likes: req.params.userid
})
.exec(function(err, users) {
for (var i = 0; i < users.length; i++) {
/*console.log(i + ", " + j);
console.log(nodes);*/
var node = {
title: users[i].username,
label: users[i].username,
image: users[i].avatar,
shape: "image",
id: users[i]._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
//node no exist
nodes.push(node);
lNode = nodes.length - 1;
} else {
//node already exist
}
var edge = {
from: users[i]._id,
to: req.params.userid,
label: "like",
arrows: "to",
color: {
color: "#E57373" //red300
}
};
edges.push(edge);
}
var resp = {
nodes: nodes,
edges: edges
var edge = {
from: users[i]._id,
to: req.params.userid,
label: "like",
arrows: "to",
color: {
color: "#E57373" //red300
}
};
res.status(200).jsonp(resp);
});
edges.push(edge);
}
var resp = {
nodes: nodes,
edges: edges
};
res.status(200).jsonp(resp);
});
//}
@ -510,3 +511,61 @@ exports.getUserNetwork = function(req, res) {
}
});
};
// user validation
exports.validateUser = function(req, res) {
adminModel.findOne({
'token': req.headers['x-access-token']
})
.exec(function(err, admin) {
if (!admin) {
res.json({
success: false,
message: 'Admin not found'
});
} else if (admin) {
if (err) return res.send(500, err.message);
userModel.findOne({
_id: req.params.userid
})
.exec(function(err, user) {
if (err) return res.send(500, err.message);
user.validated = true;
user.save(function(err, user) {
if (err) return res.send(500, err.message);
userController.getUserById(req, res);
});
});
}
});
};
// user unvalidate
exports.unvalidateUser = function(req, res) {
adminModel.findOne({
'token': req.headers['x-access-token']
})
.exec(function(err, admin) {
if (!admin) {
res.json({
success: false,
message: 'Admin not found'
});
} else if (admin) {
if (err) return res.send(500, err.message);
userModel.findOne({
_id: req.params.userid
})
.exec(function(err, user) {
if (err) return res.send(500, err.message);
user.validated = false;
user.save(function(err, user) {
if (err) return res.send(500, err.message);
userController.getUserById(req, res);
});
});
}
});
};

+ 2
- 2
controllers/travelController.js

@ -15,7 +15,7 @@ exports.getAllTravels = function(req, res) {
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.lean()
.populate('user', 'username avatar')
.populate('user', 'username avatar validated')
.exec(function (err, travels) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(travels);
@ -25,7 +25,7 @@ exports.getAllTravels = function(req, res) {
exports.getTravelById = function (req, res) {
travelModel.findOne({_id: req.params.travelid})
.lean()
.populate('user', 'username avatar telegram phone')
.populate('user', 'username avatar validated telegram phone')
.populate('joins', 'username avatar')
.populate('joinPetitions', 'username avatar')
.populate('comments', 'comment user')

+ 1
- 0
models/userModel.js

@ -15,6 +15,7 @@ var userSchema = new Schema({
email: { type: String, required: true },
phone: { type: String },
telegram: { type: String },
validated: { type: Boolean, default: false },
valorations: [{
user: {
type: mongoose.Schema.Types.ObjectId,

+ 4
- 0
server.js

@ -126,6 +126,10 @@ apiRoutes.route('/admin/travels/id/:travelid')
.delete(adminCtrl.deleteTravel);
apiRoutes.route('/admin/users/id/:userid')
.delete(adminCtrl.deleteUser);
apiRoutes.route('/admin/users/validate/id/:userid')
.post(adminCtrl.validateUser);
apiRoutes.route('/admin/users/unvalidate/id/:userid')
.post(adminCtrl.unvalidateUser);
apiRoutes.route('/search/:searchstring')
.get(searchCtrl.searchByString);

Loading…
Cancel
Save