-
Notifications
You must be signed in to change notification settings - Fork 4
/
nodepal.js
222 lines (213 loc) · 7.81 KB
/
nodepal.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
218
219
220
221
222
exports.rolesByUid = function(client, uid, callback) {
var roles = new Array();
client.query(
'SELECT rid FROM users_roles WHERE uid=?',
[uid],
(function selectCb(err, results, fields) {
if (err) {
throw err;
}
for (result in results) {
roles[result] = results[result].rid;
}
if (callback) callback(roles);
})
);
}
exports.permissionsByUid = function(client, rid, callback) {
var permissions = new Array();
client.query(
'SELECT pid, perm, tid FROM permission WHERE rid=?',
[rid],
(function selectCb(err, results, fields) {
if (err) {
throw err;
}
for (result in results) {
permissions[result] = {
"pid": results[result].pid,
"perm": results[result].perm,
"tid": results[result].tid
};
}
if (callback) callback(permissions);
})
);
}
exports.userByUid = function(client, uid, callback) {
var user = new Array();
client.query(
'SELECT * FROM users WHERE uid=?',
[uid],
(function selectCb(err, results, fields) {
if (err) {
throw err;
}
for (result in results) {
user[result] = {
"uid": results[result].uid,
"name": results[result].name,
"pass": results[result].pass,
"mail": results[result].mail,
"mode": results[result].mode,
"sort": results[result].sort,
"threshold": results[result].threshold,
"theme": results[result].theme,
"signature": results[result].signature,
"signature_format": results[result].signature_format,
"created": results[result].created,
"access": results[result].access,
"login": results[result].login,
"status": results[result].status,
"timezone": results[result].timezone,
"language": results[result].language,
"picture": results[result].picture,
"init": results[result].init,
"data": results[result].data
};
}
if (callback) callback(user);
})
);
}
// nodeByNid returns the contents of the {node} table and at the current time doesn't contain any extra fields that
// other 3rd-party modules might piggy-bag, e.g. CCK. If you need the later, please open a ticket, or even better
// submit a pacth :)
exports.nodeByNid = function(client, nid, callback) {
var node = new Array();
client.query(
'SELECT * FROM node WHERE nid=?',
[nid],
(function selectCb(err, results, fields) {
if (err) {
throw err;
}
for (result in results) {
node[result] = {
"nid": results[result].nid,
"vid": results[result].vid,
"type": results[result].type,
"language": results[result].language,
"title": results[result].title,
"uid": results[result].uid,
"status": results[result].status,
"created": results[result].created,
"changed": results[result].changed,
"comment": results[result].comment,
"promote": results[result].promote,
"moderate": results[result].moderate,
"sticky": results[result].sticky,
"tnid": results[result].tnid,
"translate": results[result].translate
};
}
if (callback) callback(node);
})
);
}
exports.sessionByUid = function(client, uid, callback) {
var session = new Array();
client.query(
'SELECT * FROM sessions WHERE uid=?',
[uid],
(function selectCb(err, results, fields) {
if (err) {
throw err;
}
for (result in results) {
session[result] = {
"sid": results[result].sid,
"hostname": results[result].hostname,
"timestamp": results[result].timestamp,
"cache": results[result].cache,
"session": results[result].session
};
}
if (callback) callback(session);
})
);
}
exports.uidBySid = function(client, sid, callback) {
var uid = new Array();
client.query(
'SELECT uid FROM sessions WHERE sid=?',
[sid],
(function selectCb(err, results, fields) {
if (err) {
throw err;
}
for (result in results) {
uid[result] = {
"uid": results[result].uid
};
}
if (callback) callback(uid);
})
);
}
// nodeAccess checks {node_access} table and returns true/false depending if the grant is available. In the case of
// taxonomy access the gid maps directly to an rid (role id), and is one of the most common groupings. But for something
// like OG, the gid is a nid which points to the organic group node that represents the group. Other modules could have
// other ways to group users together.
exports.nodeAccess = function(client, nid, operation, gid, callback) {
var access = new Array();
var grantOperation = "";
switch (operation) {
case "view":
grantOperation = "grant_view";
break;
case "update":
grantOperation = "grant_update";
break;
case "delete":
grantOperation = "grant_delete";
break;
default:
grantOperation = "grant_view";
}
client.query(
'SELECT COUNT(*) FROM node_access WHERE nid=? AND gid=? AND ' + grantOperation + '=1',
[nid, gid],
(function selectCb(err, results, fields) {
if (err) {
throw err;
}
for (result in results) {
access[result] = results[result];
if (JSON.stringify(access) == '[{"COUNT(*)":1}]') access = true;
else access = false;
}
if (callback) callback(access);
})
);
}
// I've only tested it with single-valued text fields
exports.getField = function(client, nid, fieldName, callback) {
var field = new Array();
var fieldColumnName = "field_" + fieldName + "_value";
client.query(
'SELECT type FROM node WHERE nid=?',
[nid],
(function selectCb(err, results, fields) {
if (err) {
throw err;
}
for (result in results) {
field[result] = results[result].type;
client.query(
'SELECT ' + fieldColumnName + ' FROM content_type_' + field[result] + ' WHERE nid=?',
[nid],
(function selectCb(err, results, fields) {
if (err) {
throw err;
}
for (result in results) {
field[result] = results[result];
}
if (callback) callback(eval("field[result]." + fieldColumnName));
})
);
}
})
);
}