From 92220fc11c94b806c9de917f9548171cd1ab2607 Mon Sep 17 00:00:00 2001 From: Erik Roemmelt Date: Sat, 20 Jul 2019 23:16:42 +0200 Subject: [PATCH] Update 'userField' from createMsg after Login; Add API for user get, post, put; Refactor API /tag/id --- server/ldap_test.js => ldap_test.js | 6 +- public/routes/auth.js | 5 ++ public/routes/createMessage.js | 24 ++++++-- public/search.js | 2 +- server.js | 2 +- server/authorization.js | 1 - server/dbs.js | 88 +++++++++++++++++++++++++++-- server/ldap_ohm.js | 9 ++- 8 files changed, 120 insertions(+), 17 deletions(-) rename server/ldap_test.js => ldap_test.js (86%) diff --git a/server/ldap_test.js b/ldap_test.js similarity index 86% rename from server/ldap_test.js rename to ldap_test.js index a899f74..6ad583a 100644 --- a/server/ldap_test.js +++ b/ldap_test.js @@ -1,6 +1,6 @@ // Terminal call: node server/ldap_test.js - needs VPN or eduroam const inquirer = require('inquirer'), - ldap = require('./ldap_ohm.js'); + ldap = require('./server/ldap_ohm.js'); inquirer.prompt([ { @@ -30,6 +30,8 @@ inquirer.prompt([ .then(answers => { ldap.init(null); ldap.authorize(answers.username,answers.password,function(user) { - console.log(JSON.stringify(user)); + console.log(JSON.stringify(user)); + process.exit(); }); }); + diff --git a/public/routes/auth.js b/public/routes/auth.js index 48b4137..351da04 100644 --- a/public/routes/auth.js +++ b/public/routes/auth.js @@ -42,6 +42,7 @@ Vue.component('login-panel', { type="password" placeholder="Password" :value="pwd" + @change="pwd_changed" @keyup.enter.native="login" required> @@ -62,6 +63,10 @@ Vue.component('login-panel', { error: false, }; }, +// TODO: Check if enough for session cookie re-login; checkbox +// mounted: function() { +// this.checkData(); +// }, methods: { closeLoginPanel: function() { this.$emit('close-login-panel'); diff --git a/public/routes/createMessage.js b/public/routes/createMessage.js index a76c949..b70adb5 100644 --- a/public/routes/createMessage.js +++ b/public/routes/createMessage.js @@ -8,7 +8,7 @@ const CreateMsgRouter = { template: `
-
+

Neue Nachricht erstellen

@@ -41,8 +41,8 @@ const CreateMsgRouter = {
-
-
+
+

Neue Nachricht erstellen

@@ -78,6 +78,8 @@ const CreateMsgRouter = { data: function () { return { + auth: auth, + isDisabled: false, selected: [], taglist: data, }; @@ -91,7 +93,7 @@ const CreateMsgRouter = { var _subject = $("#subject").val(); var _message = $("#message").val(); var _tag = tagArray; - var _user = $("#user").val(); + var _user = auth.name ? auth.name : $("#user").val(); //console.log("Message Created: " + _tag + " " + _message + " " + _user); $.ajax({ url: "api/msg", @@ -140,6 +142,18 @@ const CreateMsgRouter = { } }, mounted: function () { + var userField = $('#user'); + if (auth != null && auth.mail != '') { +// userField.prop('placeholder',auth.name); + var authorName = auth.name.split(' '); + authorName = authorName[1] + ', ' + authorName[0]; + userField.val(authorName); + userField.prop('disabled',true); + } else { + userField.prop('placeholder','User'); + userField.prop('disabled',false); + } + this.list_tags(); if ($(this).bootstrapMaterialDesign) $(this).bootstrapMaterialDesign(); @@ -147,7 +161,7 @@ const CreateMsgRouter = { }; function get_insert_tag(id){ - $.ajax({ url: "api/tag/"+id, method: "GET" }).done(function (tag) { + $.ajax({ url: "api/tag/id/"+id, method: "GET" }).done(function (tag) { data.push("#" + tag.name); console.log("Array:"+this.data); }).fail(function (e, f, g) { diff --git a/public/search.js b/public/search.js index fd6533d..dd86a98 100644 --- a/public/search.js +++ b/public/search.js @@ -93,7 +93,7 @@ Vue.component('search',{ // Global Functions !!! function get_insert_tag(id) { $.ajax({ - url: "api/tag/" + id, + url: "api/tag/id/" + id, method: "GET" }).done(function(tag) { dat.push("#" + tag.name); diff --git a/server.js b/server.js index aa9fd1c..305ce44 100644 --- a/server.js +++ b/server.js @@ -62,7 +62,7 @@ app.use(session({ name: 'om.sid', store: new MongoStore({ mongooseConnection: mong.connection, - ttl: 30 * 24 * 3600 + ttl: 30 * 24 * 3600, }), // mongoose + connect-mongo //store: new MemoryStore ({checkPeriod: 24*3600*1000}), // memorystore })); diff --git a/server/authorization.js b/server/authorization.js index 800a5d9..2bfe759 100644 --- a/server/authorization.js +++ b/server/authorization.js @@ -60,7 +60,6 @@ const authorization = { }); } - // TODO Auth: validate session ID // Check whether to just validate current session ID if (user === '' && pwd === '') { console.log ("auth revalidate: " + req.session.user); diff --git a/server/dbs.js b/server/dbs.js index 6b7ff76..0362fbd 100644 --- a/server/dbs.js +++ b/server/dbs.js @@ -131,14 +131,15 @@ const dbs = { }); }, }, - "tag": { + "tag/id": { params: ":id", - /* GET /api/tag/[tag-id] + /* GET /api/tag/id/[tag-id] * -> Tag schema * Get a particular tag */ get: function(req, res) { - model.Tags.findById(req.params.id) .exec(function(err, result) { + model.Tags.findById(req.params.id) + .exec(function(err, result) { if (err) { console.log (err); res.status(404).json(err); @@ -149,6 +150,85 @@ const dbs = { }); }, }, + "usr/id": { + params: ":id", + /* GET /api/usr/[usr-id] + * -> User Schema + * Get a particular user + */ + get: function(req, res) { + model.Users.findById(req.params.id) + .exec(function(err, result) { + if (err) { + console.log(err); + res.status(404).json(err); + } else { + //console.log(JSON.stringify(result)); + res.json(result); + } + }); + }, + }, + "usr": { + /* POST /api/usr + * <- User schema + * -> User schema + * Create a new user + */ + post: function(req, res) { + model.User.create({ + _id: req.body.id, + name: req.body.name, + type: req.body.type, + roles: req.body.roles, + hash: req.body.hash, + salt: req.body.salt, + pwd: req.body.pwd, + abos: req.body.abos, + bookmarks: req.body.bookmarks, + }, function(err, result) { + if (err) { + console.log (err); + return res.status(401).json(err.message); + } else { + res.json({message: "User created!!"}); + } + if (result == null) { + return res.status(500).json("Can not create user.") + } + }); + }, + /* PUT /api/usr + * <- User schema + * -> User schema + * Change a user + */ + put: function(req, res) { + model.Users.findById(reg.body._id) + .exec(function(err, entry) { + if (err) + console.log (err); + if (entry == null) + return res.status(404).json(err.message); + // TODO Catch edge cases + entry.save(function(err, data) { + // TODO Save/Update user + /* + _id: req.body.id, + name: req.body.name, + type: req.body.type, + roles: req.body.roles, + hash: req.body.hash, + salt: req.body.salt, + pwd: req.body.pwd, + abos: req.body.abos, + bookmarks: req.body.bookmarks, + */ + res.json(data); + }); + }); + }, + }, }, /* Initialize requirements * - DB connection @@ -210,7 +290,7 @@ const dbs = { type: { type: String }, roles: { type: [String], required: true, _comment: "" }, - tags: { type: [String], + abos: { type: [String], _comment: "" }, //deactivated: { type: Boolean }, //host: { type: Boolean }, diff --git a/server/ldap_ohm.js b/server/ldap_ohm.js index 5ae2f06..ced4a34 100644 --- a/server/ldap_ohm.js +++ b/server/ldap_ohm.js @@ -7,7 +7,7 @@ const ldap = require('ldapjs'); const ldap_escape = require('ldap-escape'); -// TODO: Where do I get the URL from?? A: Is given. +// NOTE: Where do I get the URL from?? A: Is given. var ldap_client = ldap.createClient({ //url: 'ldap://gso2.ads1.fh-nuernberg.de/', url: 'ldap://sso.cs.ohm-hochschule.de:389/', @@ -16,7 +16,7 @@ var ldap_client = ldap.createClient({ // timeouts don't work reliably }); -// TODO: Where do I get the 'bindpath' parameters info from? A: Is given. +// NOTE: Where do I get the 'bindpath' parameters info from? A: Is given. const ldap_config = { bindpath: 'cn=Users,dc=ohm-hochschule,dc=de', timeout: 2000 @@ -89,14 +89,17 @@ const ldap_ohm = { console.log("ldap search error after bind for user " + user); return cb (null); } + // ldap_test output +// return_object = entry.object; return cb (return_object); }); res.on('error', function(err) { console.log('ldap error: ' + err.message); }); res.on('end', function(result) { - // TODO: Did we forget something? // TODO: analyze result.status? +// console.info('ldap result: '); +// console.info(result); }); }); });