Skip to content

Commit

Permalink
Merge pull request #186 from MCS-Lite/admin-user
Browse files Browse the repository at this point in the history
delete isActive: true feature
  • Loading branch information
iamblue authored Aug 30, 2017
2 parents 5e295f0 + 70b125b commit 5caad22
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 9 deletions.
22 changes: 22 additions & 0 deletions adminServer/controllers/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var $admin = require('../../configs/admin');


module.exports = function ($db) {
var users = $db.users;

var startService = function(req, res, next) {
global.startMCSLiteService();
Expand Down Expand Up @@ -95,12 +96,33 @@ module.exports = function ($db) {
return res.send(200, { data: JSON.stringify(global.logs) });
};

var resetData = function(req, res, next) {
users.retrieveAdminUsers()
.then(function(data) {
var adminUsersContent = '';

data.forEach(function(k, v) {
adminUsersContent += k.toString() + '\n';
});

fs.writeFileSync(path.resolve(__dirname, '../../db/users.json'), adminUsersContent, 'utf8');
fs.writeFileSync(path.resolve(__dirname, '../../db/datapoints.json'), '', 'utf8');
fs.writeFileSync(path.resolve(__dirname, '../../db/devices.json'), '', 'utf8');
fs.writeFileSync(path.resolve(__dirname, '../../db/prototypes.json'), '', 'utf8');
return res.send(200, "success.");
})
.catch(function(err) {
return (400, err);
});
};

return {
serviceStatus: false,
retrieveServiceSetting: retrieveServiceSetting,
editServiceSetting: editServiceSetting,
resetServiceSetting: resetServiceSetting,
getServiceIp: getServiceIp,
resetData: resetData,
getServiceLog: getServiceLog,
startService: startService,
stopService: stopService,
Expand Down
152 changes: 148 additions & 4 deletions adminServer/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,6 @@ module.exports = function ($db) {
});
};

var retrieveUsers = function(req, res, next) {

};

var createAnAdmin = function(req, res, next) {
return users.checkDefaultUserCount()
.then(function(status) {
Expand Down Expand Up @@ -299,6 +295,149 @@ module.exports = function ($db) {
});
};

var editUser = function(req, res, next) {
if (req.body.password) {
return users.changePassword(req.params.userId, req.body.password)
.then(function(data) {
return res.send(200, data);
})
.catch(function(err) {
return res.send(400, err);
});
} else if (req.body.hasOwnProperty('isActive')) {
return users.editUser({
userId: req.params.userId,
isActive: !req.body.isActive,
}, {
isActive: req.body.isActive,
})
.then(function(data) {
return res.send(200, data);
})
.catch(function(err) {
return res.send(400, err);
});
}
};

var deleteUser = function(req, res, next) {
var userId = [];

if (req.params.userId) userId = req.params.userId.split(',');
if (req.body.userId) userId = req.body.userId;

return users.deleteUser({
userId: userId,
})
.then(function(data) {
return res.send(200, "success.");
})
.catch(function(err) {
return res.send(400, err);
});
};

var addNewUser = function(req, res, next) {
return users.addNewUser({
userName: req.body.userName,
email: req.body.email,
password: req.body.password,
isAdmin: req.body.isAdmin,
})
.then(function(data) {
return res.send(200, data);
})
.catch(function(err) {
return res.send(400, err);
})
};

var retrieveUsers = function(req, res, next) {
var query = {
// isActive: true,
};

var userName = req.query.userName;
var email = req.query.email;
var q = req.query.q;

if (q) {
query['$or'] = [];
query['$or'].push({email: { $regex: new RegExp(q)}});
query['$or'].push({userName: { $regex: new RegExp(q)}});
}

if (userName) {
query.userName = { $regex: new RegExp(userName) };
}

if (email) {
query.email = { $regex: new RegExp(email) };
}

var sort = req.query.sort;
var skip = req.query.skip;
var limit = req.query.limit;

return users.retrieveUserByQuery(query, sort, skip, limit)
.then(function(data) {
return res.send(200, data);
})
.catch(function(err) {
return res.send(400, err);
});

};

var batchAddNewUserByCSV = function(req, res, next) {
var rawData;
var content = [];

if (Object.keys(req.body).length === 0){
return res.send(400, { message: 'Raw body is null.' });
} else {
rawData = Object.keys(req.body)[0].toString().split('\n');
}

rawData.forEach(function(key, item) {
if (key.split(',').length === 3) {
content.push({
userName: key.split(',')[0],
email: key.split(',')[1],
password: key.split(',')[2],
});
}
});
var queue = [];
content.forEach(function(key, item) {
queue.push(users.addNewUser({
userName: key.userName,
email: key.email,
password: key.password,
isAdmin: true,
})
);
});

return Promise.all(queue)
.then(function(values) {
return res.send(200, 'success.');
})
.catch(function(err) {
return res.send(400, err);
});
};

var clearAllUserExceptAdmin = function(req, res, next) {
return users.clearAllUser()
.then(function() {
return res.send(200, 'success.');
})
.catch(function(err) {
return res.send(400, err);
});
};

return {
login: login,
loginInterface: loginInterface,
Expand All @@ -307,6 +446,11 @@ module.exports = function ($db) {
createAnAdmin: createAnAdmin,
checkAdminExist: checkAdminExist,
signupInterface: signupInterface,
editUser: editUser,
deleteUser: deleteUser,
addNewUser: addNewUser,
batchAddNewUserByCSV: batchAddNewUserByCSV,
clearAllUserExceptAdmin: clearAllUserExceptAdmin,
};

}
53 changes: 53 additions & 0 deletions adminServer/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,45 @@ module.exports = function($db, $app, $admin) {
handler: usersController.checkCookies,
};

this.deleteUser = {
path: $admin.apiRoute + '/users/:userId',
methods: ['delete'],
middleware: [$app.oauth.authorise()],
handler: usersController.deleteUser,
};

this.deleteUserByPost = {
path: $admin.apiRoute + '/users/delete',
methods: ['post'],
middleware: [$app.oauth.authorise()],
handler: usersController.deleteUser,
};

this.editUser = { // Include disable User & changeUserPassword
path: $admin.apiRoute + '/users/:userId',
methods: ['put'],
middleware: [$app.oauth.authorise()],
handler: usersController.editUser,
};

this.retrieveUsers = {
path: $admin.apiRoute + '/users',
methods: ['get'],
middleware: [$app.oauth.authorise()],
handler: usersController.retrieveUsers,
};

this.addNewUser = {
path: $admin.apiRoute + '/users',
methods: ['post'],
middleware: [$app.oauth.authorise()],
handler: usersController.addNewUser,
};

// this.addNewUserByCSV = {

// };

this.startService = {
path: $admin.apiRoute + '/service/start',
methods: ['get'],
Expand Down Expand Up @@ -147,4 +186,18 @@ module.exports = function($db, $app, $admin) {
handler: serviceController.getServiceLog,
};

this.batchAddNewUserByCSV = {
path: $admin.apiRoute + '/users.csv',
methods: ['post'],
middleware: [$app.oauth.authorise(), bodyParser.raw({ type: 'text/csv' })],
handler: usersController.batchAddNewUserByCSV,
};

this.clearAllUserExceptAdmin = {
path: $admin.apiRoute + '/clear',
methods: ['delete'],
middleware: [$app.oauth.authorise()],
handler: usersController.clearAllUserExceptAdmin,
};

};
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ cp -R index.js ./build/index.js
cp -R package.json ./build/package.json

mkdir out
nwbuild -p win32,win64,osx64 -v 0.20.3 ./build -o ./out

nwbuild -p win32,win64,osx64,linux32,linux64 -v 0.20.3 ./build -o ./out

cd ./out/mcs-lite-app/win64
mkdir mcs-lite-app
Expand Down

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion client/mcs-layout.pot
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
msgid ""
msgstr ""
"POT-Creation-Date: 2017-08-07T02:15:04.594Z\n"

"POT-Creation-Date: 2017-08-07T04:03:31.042Z\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"MIME-Version: 1.0\n"
Expand Down
2 changes: 1 addition & 1 deletion client/messages/translations.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion db/users.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{"userName":"Laurence Abbott","email":"[email protected]","password":"9005371724f8483141728b781db0ebb02d67b524c7a1038548a3eb2b088eabc0","userId":"B1gphzpb8l","isActive":true,"createdAt":1484014261096,"updatedAt":1484014261096,"_id":"00fN53TU44g1lxlc"}
{"userName":"Dangelo Morissette","email":"[email protected]","password":"8ffb1dd70e71ab72c19b08e6b0946d70b9f43f65c6a3ca45cd21c5dd9fdd336d","userId":"rJWUuuab8e","isActive":true,"createdAt":1484015725872,"updatedAt":1484015725872,"_id":"02bJIOwzgyIV14Hy","isAdmin":true}
{"userName":"iamblue","email":"[email protected]","password":"8ffb1dd70e71ab72c19b08e6b0946d70b9f43f65c6a3ca45cd21c5dd9fdd336d","userId":"HktV1dVvl","isActive":true,"createdAt":1485238064541,"updatedAt":1485238064541,"_id":"04ri9WybCIl51Pj4","isAdmin":true}
{"userName":"Guiseppe Shanahan","email":"[email protected]","password":"8ffb1dd70e71ab72c19b08e6b0946d70b9f43f65c6a3ca45cd21c5dd9fdd336d","userId":"BybnaATbIx","isActive":true,"createdAt":1484017347942,"updatedAt":1484017347942,"_id":"06ASzrQBHNZfYixK","isAdmin":true}
{"userName":"Coy Ziemann","email":"[email protected]","password":"8ffb1dd70e71ab72c19b08e6b0946d70b9f43f65c6a3ca45cd21c5dd9fdd336d","userId":"B1ZkdseUx","isActive":true,"createdAt":1483941849282,"updatedAt":1483941849282,"_id":"07icGAWMYwvID7M1","isAdmin":true}
Expand Down
Loading

0 comments on commit 5caad22

Please sign in to comment.