-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
217 lines (198 loc) · 5.71 KB
/
utilities.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
General Web Services Additional Functions
Andres Cardenas
28/02/2014
*/
var mysql = require('mysql');
var connect = require ('./connect.js');
var Q = require('q');
//Create connection to mysql database
var connection = mysql.createConnection(
{
host:connect.host,
user:connect.user,
password:connect.password,
database:connect.database
});
//Start connection to MySQL
connection.connect(function(err)
{
if (err) throw err;
});
function queryDB(sql,args)
{
sql.toLowerCase();
sql = mysql.format(sql,args);
var defered = Q.defer();
connection.query(sql,defered.makeNodeResolver());
return defered.promise;
}
function bulkQueryDB(sql,args)
{
sql.toLowerCase();
var defered = Q.defer();
connection.query(sql,[args],defered.makeNodeResolver());
return defered.promise;
}
var mysql_date = function(date)
{
return date.toISOString().slice(0, 19).replace('T', ' ');
}
var tokenValid = function(token)
{
return (token.length==44);
}
var getUserID = function(token)
{
var deferred = Q.defer();
var sql = 'SELECT User_id from token WHERE token = ? AND Expire_date > ?';
var now = new Date();
var myDate = mysql_date(now);
var params = [token,myDate];
queryDB(sql,params).then(function(result)
{
if (result[0].length<1)
{
var response = {};
response.code = 'user_not_found';
deferred.reject(response);
}
else
{
deferred.resolve(result[0][0].User_id);
}
}, function(err)
{
deferred.reject(err);
}
);
return deferred.promise;
}
var get_permission = function(tournament,user)
{
var deferred = Q.defer();
var sql = 'SELECT distinct T.Tournament_id, IF(user_id = ?,G.Privilege_id,5) as privilege_id FROM tournament as T LEFT JOIN tournament_rights as G on T.tournament_id = G.tournament_id where (user_id = ? or (user_id <> ? AND public = true AND G.Tournament_id not in (select T.Tournament_id FROM tournament as T LEFT JOIN tournament_rights as G on T.tournament_id = G.tournament_id where user_id = ?))) AND T.tournament_id = ? order by privilege_id;'
var params = [user,user,user,user,tournament];
queryDB(sql,params).then(function(result)
{
if (result[0].length<1)
{
deferred.resolve(0);
}
else
{
deferred.resolve(result[0][0].privilege_id);
}
}, function(err)
{
deferred.reject(err);
}
);
return deferred.promise;
}
//Dictionary implementation from http://stackoverflow.com/questions/5745960/javascript-dictionary-with-names
function JSdict() {
this.Keys = [];
this.Values = [];
}
// Check if dictionary extensions aren't implemented yet.
// Returns value of a key
if (!JSdict.prototype.getVal) {
JSdict.prototype.getVal = function (key) {
if (key == null) {
return "Key cannot be null";
}
for (var i = 0; i < this.Keys.length; i++) {
if (this.Keys[i] == key) {
return this.Values[i];
}
}
return "Key not found!";
}
}
// Check if dictionary extensions aren't implemented yet.
// Updates value of a key
if (!JSdict.prototype.update) {
JSdict.prototype.update = function (key, val) {
if (key == null || val == null) {
return "Key or Value cannot be null";
}
// Verify dict integrity before each operation
if (keysLength != valsLength) {
return "Dictionary inconsistent. Keys length don't match values!";
}
var keysLength = this.Keys.length;
var valsLength = this.Values.length;
var flag = false;
for (var i = 0; i < keysLength; i++) {
if (this.Keys[i] == key) {
this.Values[i] = val;
flag = true;
break;
}
}
if (!flag) {
return "Key does not exist";
}
}
}
// Check if dictionary extensions aren't implemented yet.
// Adds a unique key value pair
if (!JSdict.prototype.add) {
JSdict.prototype.add = function (key, val) {
// Allow only strings or numbers as keys
if (typeof (key) == "number" || typeof (key) == "string") {
if (key == null || val == null) {
return "Key or Value cannot be null";
}
if (keysLength != valsLength) {
return "Dictionary inconsistent. Keys length don't match values!";
}
var keysLength = this.Keys.length;
var valsLength = this.Values.length;
for (var i = 0; i < keysLength; i++) {
if (this.Keys[i] == key) {
return "Duplicate keys not allowed!";
}
}
this.Keys.push(key);
this.Values.push(val);
}
else {
return "Only number or string can be key!";
}
}
}
// Check if dictionary extensions aren't implemented yet.
// Removes a key value pair
if (!JSdict.prototype.remove) {
JSdict.prototype.remove = function (key) {
if (key == null) {
return "Key cannot be null";
}
if (keysLength != valsLength) {
return "Dictionary inconsistent. Keys length don't match values!";
}
var keysLength = this.Keys.length;
var valsLength = this.Values.length;
var flag = false;
for (var i = 0; i < keysLength; i++) {
if (this.Keys[i] == key) {
this.Keys.shift(key);
this.Values.shift(this.Values[i]);
flag = true;
break;
}
}
if (!flag) {
return "Key does not exist";
}
}
}
module.exports.get_permission = get_permission;
module.exports.mysql_date = mysql_date;
module.exports.query = queryDB;
module.exports.bulkQuery = bulkQueryDB;
module.exports.tokenValid = tokenValid;
module.exports.getUserID = getUserID;
module.exports.JSdict = JSdict;