diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..764c027 --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +#flAuth + +# Compiled source # +################### +*.com +*.class +*.dll +*.exe +*.o +*.so + +# Packages # +############ +# it's better to unpack these files and commit the raw source +# git has its own built in compression methods +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Logs # +######## +*.log +logfile.txt + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db +*~ +node_modules + +# Local settings # +################## +.env diff --git a/api/controllers/aasController.js b/api/controllers/aasController.js new file mode 100644 index 0000000..2bc3e44 --- /dev/null +++ b/api/controllers/aasController.js @@ -0,0 +1,167 @@ +'use strict'; +var mysql = require('mysql'); +var connection = mysql.createConnection({ + host: '192.168.14.21', + user: 'flauth', + password: 'FabLab', + database: 'flauth' +}); + +connection.connect(function(error) { + if(!!error){ + console.log(error); + } else { + console.log('Connected'); + } +}); + + +// MACHINES +exports.list_all_machines = function(req, res) { + connection.query('SELECT * FROM machines', function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.list_all_machine_tags = function(req, res) { + connection.query('SELECT t.*, r.start, r.end FROM tags t LEFT JOIN rights r ON t.tid=r.tid WHERE r.end>=now() AND r.mid=' + req.params.mid, function(error, rows, fields) { + //connection.query('SELECT t.*, r.end FROM tags t LEFT JOIN rights r ON r.tid=t.tid WHERE tid IN (SELECT tid FROM rights WHERE start<=now() AND end>=now() AND mid=' + req.params.mid + ')', function(error, rows, fields) { + //connection.query('SELECT * FROM machines', function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.list_all_machine_logs = function(req, res) { + connection.query('SELECT * FROM logs WHERE mid=' + req.params.mid, function(error, rows, fields) { +// connection.query('SELECT * FROM machines', function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.create_a_machine = function(req, res) { + res.json(req.body); +}; + +exports.read_a_machine = function(req, res) { + connection.query('SELECT * FROM machines WHERE mid=' + req.params.mid, function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.update_a_machine = function(req, res) { +}; + +exports.delete_a_machine = function(req, res) { +}; + + +// TAGS +exports.list_all_tags = function(req, res) { + connection.query('SELECT * FROM tags', function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.create_a_tag = function(req, res) { + res.json(req.body); +}; + +exports.read_a_tag = function(req, res) { + connection.query('SELECT * FROM tags WHERE tid=' + req.params.tid, function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.update_a_tag = function(req, res) { +}; + +exports.delete_a_tag = function(req, res) { +}; + + +// RIGHTS +exports.list_all_rights = function(req, res) { + connection.query('SELECT * FROM rights', function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.create_a_right = function(req, res) { + res.json(req.body); +}; + +exports.read_a_right = function(req, res) { + connection.query('SELECT * FROM rights WHERE rid=' + req.params.rid, function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.update_a_right = function(req, res) { +}; + +exports.delete_a_right = function(req, res) { +}; + + +// LOGS +exports.list_all_logs = function(req, res) { + connection.query('SELECT * FROM logs', function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.create_a_log = function(req, res) { + res.json(req.body); +}; + +exports.read_a_log = function(req, res) { + connection.query('SELECT * FROM logs WHERE lid=' + req.params.lid, function(error, rows, fields) { + if (error) { + res.send(error); + } else { + res.json(rows); + } + }); +}; + +exports.update_a_log = function(req, res) { +}; + +exports.delete_a_log = function(req, res) { +}; diff --git a/api/models/aasModel.js b/api/models/aasModel.js new file mode 100644 index 0000000..f1d8b3a --- /dev/null +++ b/api/models/aasModel.js @@ -0,0 +1,24 @@ +'use strict'; +var mysql = require('mysql'); +var Schema = mysql.Schema; + + +var TaskSchema = new Schema({ + name: { + type: String, + Required: 'Kindly enter the name of the task' + }, + Created_date: { + type: Date, + default: Date.now + }, + status: { + type: [{ + type: String, + enum: ['pending', 'ongoing', 'completed'] + }], + default: ['pending'] + } +}); + +module.exports = mongoose.model('Tasks', TaskSchema); \ No newline at end of file diff --git a/api/routes/aasRoutes.js b/api/routes/aasRoutes.js new file mode 100644 index 0000000..38f0227 --- /dev/null +++ b/api/routes/aasRoutes.js @@ -0,0 +1,55 @@ +'use strict'; + +module.exports = function(app) { + var aas = require('../controllers/aasController'); + + // machine Routes + app.route('/machines') + .get(aas.list_all_machines) + .post(aas.create_a_machine); + + app.route('/machines/:mid') + .get(aas.read_a_machine) + .put(aas.update_a_machine) + .delete(aas.delete_a_machine); + + app.route('/machines/:mid/tags') + .get(aas.list_all_machine_tags); + + app.route('/machines/:mid/logs') + .get(aas.list_all_machine_logs); + + + // tags Routes + app.route('/tags') + .get(aas.list_all_tags) + .post(aas.create_a_tag); + + app.route('/tags/:tid') + .get(aas.read_a_tag) + .put(aas.update_a_tag) + .delete(aas.delete_a_tag); + + + // rights Routes + app.route('/rights') + .get(aas.list_all_rights) + .post(aas.create_a_right); + + app.route('/rights/:rid') + .get(aas.read_a_right) + .put(aas.update_a_right) + .delete(aas.delete_a_right); + + + // logs Routes + app.route('/logs') + .get(aas.list_all_logs) + .post(aas.create_a_log); + + app.route('/logs/:tid') + .get(aas.read_a_log) + .put(aas.update_a_log) + .delete(aas.delete_a_log); + +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..8214160 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "Access Authentication Server", + "version": "1.0.0", + "description": "FabLab Access Authentication Server with REST API", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/fablabwinti/access_auth_server.git" + }, + "author": "Claudio Prezzi", + "license": "LGPL-3.0", + "bugs": { + "url": "https://github.com/fablabwinti/access_auth_server/issues" + }, + "homepage": "https://github.com/fablabwinti/access_auth_server#readme", + "devDependencies": { + "nodemon": "^1.11.0" + }, + "dependencies": { + "body-parser": "^1.17.2", + "express": "^4.15.3", + "mysql": "^2.13.0" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..495faf1 --- /dev/null +++ b/server.js @@ -0,0 +1,19 @@ +var express = require('express'); +var app = express(); +var port = process.env.PORT || 3000; +var mysql = require('mysql'); +//var model = require('./api/models/aasModel'); +var bodyParser = require('body-parser'); + +app.use(bodyParser.urlencoded({ extended: true })); +app.use(bodyParser.json()); + + +var routes = require('./api/routes/aasRoutes'); +routes(app); + + +app.listen(port); + + +console.log('AAS RESTful API server started on: ' + port); diff --git a/sql/flauth.sql b/sql/flauth.sql new file mode 100644 index 0000000..e4906a6 --- /dev/null +++ b/sql/flauth.sql @@ -0,0 +1,173 @@ +-- phpMyAdmin SQL Dump +-- version 4.6.5 +-- https://www.phpmyadmin.net/ +-- +-- Host: localhost +-- Erstellungszeit: 20. Jun 2017 um 17:20 +-- Server-Version: 5.5.54-MariaDB +-- PHP-Version: 5.6.30 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- Datenbank: `flauth` +-- + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `logs` +-- + +CREATE TABLE `logs` ( + `lid` int(11) NOT NULL, + `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `tid` int(11) DEFAULT NULL, + `mid` int(11) DEFAULT NULL, + `event` int(11) NOT NULL, + `remarks` text +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Daten für Tabelle `logs` +-- + +INSERT INTO `logs` (`lid`, `timestamp`, `tid`, `mid`, `event`, `remarks`) VALUES +(1, '2017-06-08 20:24:15', 1, 3, 1, 'Machine login'); + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `machines` +-- + +CREATE TABLE `machines` ( + `mid` int(11) NOT NULL, + `name` varchar(30) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Daten für Tabelle `machines` +-- + +INSERT INTO `machines` (`mid`, `name`) VALUES +(1, 'Lasersaur'), +(2, 'Laser klein'), +(3, 'CNC-Fräse'), +(4, 'Drehbank gross'), +(5, 'Drehbank klein'), +(6, '3d-Drucker Einstein'), +(7, '3d-Drucker Newton'), +(8, '3d-Drucker Hawking'), +(9, '3d-Drucker X400'), +(10, '3d-Drucker Solidoodle'), +(11, '3d-Drucker p3Steel'); + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `rights` +-- + +CREATE TABLE `rights` ( + `rid` int(11) NOT NULL, + `tid` int(11) NOT NULL, + `mid` int(11) NOT NULL, + `start` date DEFAULT NULL, + `end` date DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Daten für Tabelle `rights` +-- + +INSERT INTO `rights` (`rid`, `tid`, `mid`, `start`, `end`) VALUES +(1, 1, 1, '2017-06-01', '2018-01-01'), +(2, 1, 2, '2017-06-01', '2018-01-01'), +(3, 1, 3, '2017-06-01', '2018-01-11'), +(4, 2, 1, '2017-06-20', '2018-01-01'), +(5, 2, 2, '2017-06-25', '2017-07-01'); + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `tags` +-- + +CREATE TABLE `tags` ( + `tid` int(11) NOT NULL, + `uid` varchar(10) NOT NULL, + `owner` varchar(50) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT; + +-- +-- Daten für Tabelle `tags` +-- + +INSERT INTO `tags` (`tid`, `uid`, `owner`) VALUES +(1, '0856898896', 'Claudio Prezzi'), +(2, '0079830412', 'Admin'); + +-- +-- Indizes der exportierten Tabellen +-- + +-- +-- Indizes für die Tabelle `logs` +-- +ALTER TABLE `logs` + ADD PRIMARY KEY (`lid`); + +-- +-- Indizes für die Tabelle `machines` +-- +ALTER TABLE `machines` + ADD PRIMARY KEY (`mid`) USING BTREE; + +-- +-- Indizes für die Tabelle `rights` +-- +ALTER TABLE `rights` + ADD PRIMARY KEY (`rid`); + +-- +-- Indizes für die Tabelle `tags` +-- +ALTER TABLE `tags` + ADD PRIMARY KEY (`tid`), + ADD UNIQUE KEY `uid` (`uid`); + +-- +-- AUTO_INCREMENT für exportierte Tabellen +-- + +-- +-- AUTO_INCREMENT für Tabelle `logs` +-- +ALTER TABLE `logs` + MODIFY `lid` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; +-- +-- AUTO_INCREMENT für Tabelle `machines` +-- +ALTER TABLE `machines` + MODIFY `mid` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12; +-- +-- AUTO_INCREMENT für Tabelle `rights` +-- +ALTER TABLE `rights` + MODIFY `rid` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6; +-- +-- AUTO_INCREMENT für Tabelle `tags` +-- +ALTER TABLE `tags` + MODIFY `tid` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;