-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
138 lines (132 loc) · 3.72 KB
/
util.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
const validate = require("jsonschema").validate;
const { PendingArtistMural, PendingViewerMural } = require("./models/mural.js");
const GeoJSONSchema = {
"id": "/GeoJSON",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["Feature"],
"required": true
},
"geometry": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"Point",
"LineString",
"Polygon",
"MultiPoint",
"MultiLineString",
"MultiPolygon"
]
},
"coordinates": {
"type": "array"
}
},
"required": true
},
"properties": {
"type": "object",
"required": true,
"properties": {
"title": {
"type": "string",
"maxLength": process.env.MAX_TITLE_LEN || 80
},
"desc": {
"type": "string",
"maxLength": process.env.MAX_DESC_LEN || 1500
},
"artist": {
"type": "string",
"maxLength": process.env.MAX_ARTIST_LEN || 255
},
"email": {
"type": "string",
"maxLength": process.env.MAX_EMAIL_LEN || 254
},
"images": {
"type": "array",
"items": {
"type": "string",
"format": "uri",
"maxItems": process.env.MAX_IMAGES || 10
}
},
"id": {
"type": "string"
},
"reject": {
"type": "boolean"
},
"notes": {
"type": "string",
"maxLength": process.env.MAX_NOTES_LEN || 1750
}
}
}
}
};
/**
* Validate given Mural object.
* @param {Object} geojson GeoJSON object to validate.
* @returns Object with "error" array and "valid" boolean.
*/
exports.validateMural = function(geojson) {
return validate(geojson, GeoJSONSchema);
};
/**
* Pending mural type identifier middleware
* Sets req.type to the appropriate model, or sets it to null if type path param is invalid.
*/
exports.getPendingType = function (req, res, next) {
switch (req.params.type) {
case "artist":
req.type = PendingArtistMural;
next();
break;
case "viewer":
req.type = PendingViewerMural;
next();
break;
default:
req.type = null;
res.sendStatus(404);
break;
}
};
/**
* Register a new user to the db.
* @param {String} username
* @param {String} password
* @param {String} perm
* @returns {Promise}
*/
exports.registerUser = function(User, username, password, perm) {
return User.register(new User({ username: username, perm: perm }), password);
};
/**
* Check if user is logged in middleware.
*/
exports.isLoggedIn = function(req, res, next) {
if (!req.isAuthenticated()) {
res.sendStatus(401);
} else {
next();
}
};
/**
* Check if user has admin permission. Should be AFTER isLoggedIn.
*/
exports.isAdmin = function(req, res, next) {
if (req.user) {
if (req.user.perm == "admin") {
return next();
}
}
res.sendStatus(403);
};