This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
181 lines (143 loc) · 3.72 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var config = require('./config.js');
var express = require('express');
var session = require('express-session');
var app = express();
// var mysql = require('mysql');
// var connection = mysql.createConnection({
// host: config.db.host,
// user: config.db.user,
// password: config.db.password,
// database: config.db.database
// });
var mustache = require('mustache-express');
app.engine('html', mustache());
app.set('view engine', 'html');
app.set('views', __dirname+"/views");
app.use(express.static(__dirname+"/public"));
var cors = require('cors');
app.use(cors());
app.options("*", cors());
app.use(session({
secret : 'monSuperSecretDeLaMortquiTue',
resave : false,
saveUninitialized: true
}));
var CASAuthentication = require('cas-authentication');
var cas = new CASAuthentication({
cas_url : 'https://cas.utc.fr/cas',
service_url : 'http://localhost:9797',
cas_version : '2.0',
renew : false,
is_dev_mode : false,
dev_mode_user : '',
dev_mode_info : {},
session_name : 'cas_user',
session_info : 'cas_userinfo',
destroy_session : false
});
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get("/", cas.bounce, function (req, res, err){
return res.render("index", {
user:{
login: req.session.cas_user,
name: req.session.cas_userinfo.displayname
},
money:{
user: req.session.cas_user,
debt: 0,
profit: 0,
balance: 0,
color: "green"
}
});
var error = true;
var sql = "SELECT * FROM `users` WHERE login=" + connection.escape(req.session.cas_user);
console.log("SQL:", sql);
connection.query(sql, function (err, results){
console.log("Resutls:", results);
if(err){
res.send(err);
console.log(err);
return;
}
if(results.length > 1){
res.sendStatus(403);
res.end();
return;
}
if(results.length == 0){
console.log("Entered results.length==0");
sql = "INSERT INTO `users` (login, gcm_registration) VALUES ("+connection.escape(req.session.cas_user)+ ", NULL);";
connection.query(sql, function (err, results_insert){
if(err){
res.send(err);
console.log("Error in insert: ", err);
return;
}
res.render("index", {
user:{
login: req.session.cas_user,
name: req.session.cas_userinfo.displayname
},
money:{
user: req.session.cas_user,
debt: 0,
profit: 0,
balance: 0,
color: "green"
}
});
return;
});
return;
}
console.log("Entered results.length == 1");
var sql = "SELECT * FROM `debts` WHERE participant=" + connection.escape(req.session.cas_user) + " AND payed=false";
connection.query(sql, function (err, results_payed){
if(err){
res.send(err);
return;
}
});
res.render("index", {
user:{
login: req.session.cas_user,
name: req.session.cas_userinfo.displayname
},
money:{
user: req.session.cas_user,
debt: 3598 / 100,
profit: 457/100,
balance: ((457 - 3598)/ 100),
color: "red"
}
});
return;
});
});
app.get("/admin", cas.block, function (req, res, err){
res.render("admin", {
});
});
app.get("/logout", cas.logout);
app.post("/transaction/new", function (req, res, err){
var payer_id = req.body.payer_id;
var debtFor = req.body.debt;
var transaction_amount = req.body.transaction_amount;
var res = {};
var sql = "INSERT INTO `payments` (id, payer, price) VALUES (0," + connection.escape(payer_id) + " ," + connection.escape(transaction_amount) + ");";
connection.query(sql, function (err,results){
if(err){
error(err);
return;
}else{
res.success = true;
}
});
sql = "INSERT INTO `payments` ()"
});
app.listen(9797, function(){
console.log("Listening!");
});