-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
170 lines (138 loc) · 3.5 KB
/
users.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
const { to } = require('utils')
const log = require('./log.js')
const groupme = require('./groupme.js')
var db // the database
const request = function(module){
const output = new Promise(function(resolve, reject){
try {
const result = require(module)
resolve(result)
} catch(err) {
reject(new Error(`Could not register module: ${module}`))
}
})
return to(output)
}
exports.init = async function(){
const [err, sqlite] = await request('sqlite3')
if(err) {
log.error(err.message)
return
} else {
log.info('registered module')
}
}
exports.init()
try {
sqlite = require("sqlite3")
db = new sqlite.Database("./users.db")
} catch(err) {
db = null
}
if(db) log.info('user database registered successfully')
else log.warn('could not register database')
const SQL = require("./sql_commands.js");
//wrap sqlite commands in a Promise API for ease of use
sql_run = function(method, args){
// make sure that SQL is enabled
if(db == null) {
return // do not execute the rest of the function
}
return new Promise((resolve, reject) => {
db.all(method, args, function(err, rows){
if(err) reject(err.message)
else resolve(rows)
})
})
}
// expose some methods
exports.lookup = async function(id) {
const [err, result] = await to(sql_run(SQL.LOOK_UP, [id]))
if(err) {
log.error(`Invalid SQL syntax in lookup: ${err.message}`)
throw err
}
return result
}
exports.register = async function(data) {
const args = [
data.firstname,
data.lastname,
data.communitygroup,
data.accessgroup,
data.id,
]
await sql_run(SQL.NEW_USER, args)
}
exports.all = async function(){
return await sql_run(SQL.ALL, [])
}
exports.get_permissions = async function(id){
let output = {}
const [err, result] = await to(exports.lookup(id))
if(err) {
log.error(`No permissions found for ${id}: Invalid SQL`)
return {} // no permissions
}
if(result.length < 1) {
log.info(`No permissions found for ${id}: Could not find in database`)
return {} // no permissions
}
const user = result[0]
// Hazing Neil
// TODO: remove this line when it gets tired
if(user.firstname == 'Neil' && user.lastname == 'McEnerney'){
groupme.post(`\
Attention: @Neil McEnerney has just entered the station.
This message is to remind him of who wrote the software controlling the door he just used
and of the name of the current chief engineer
<3 Connor\
`)
}
const { accessgroup } = user
if(accessgroup == 'dj' || accessgroup =='admin') {
output.studio_door = true
} else {
log.info(`Access Denied for user: ${user.firstname} ${user.lastname}. Not permitted to open studio door`)
output.studio_door = false
}
return output
}
// exports.register({
// firstname: "hello",
// lastname: "hello",
// communitygroup: "DJ",
// accessgroup: "IDK",
// id: "b67asdm",
// })
// .catch(err => console.log(err))
exports.all()
// COMMENTED OUT FOR SAFETY
// -------------------------
// exports.clearAll = function() {
// db.run(SQL.CLEAR_ALL)
// }
// exports.lookup = function(id) {
// return new Promise((resolve, reject) => {
// db.all(SQL.LOOK_UP, [id], (err, rows) => {
// console.log(rows)
// if(err || rows.length == 0) reject(err)
// else resolve(rows[0])
// })
// })
// }
// exports.register = function(data) {
// console.log(data)
// return new Promise((resolve, reject) => {
// db.run(SQL.NEW_USER, [
// data.firstname,
// data.lastname,
// data.communitygroup,
// data.accessgroup,
// data.id
// ], function(err){
// if(err) reject(err)
// else resolve()
// })
// })
// }