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
223
|
/**
* Module dependencies
*/
var mongoose = require('mongoose')
, env = process.env.NODE_ENV || 'development'
, config = require('../config/config.js')[env]
, Project = mongoose.model('Project')
, Access = mongoose.model('Access')
, User = mongoose.model('User')
, pPost = mongoose.model('pPost')
, Validator = require('validator').Validator
, v = new Validator()
, sanitize = require('validator').sanitize;
// validation error handling. This collects all errors before pushing them out in getErrors()
Validator.prototype.error = function(msg) {
this._errors.push(msg);
return this;
}
Validator.prototype.getErrors = function() {
var returnThis = this._errors;
this._errors = ''; // need to reset errors between sessions because of object model
return returnThis;
}
/**
* Before the user log in
* ===============================================================
*/
exports.index = function(req, res) {
res.render('index', { title: 'DERS' });
};
exports.test = function(req, res) {
res.render('test', {
title: 'test',
loggedin: false
});
};
exports.home = function(req, res) {
res.render('home', {
title: 'home',
loggedin: false
});
};
exports.faq = function(req, res) {
res.render('faq', {
title: 'faq',
loggedin: false
});
}
exports.contact = function(req, res) {
res.render('contact', {
title: 'contact',
loggedin: false
});
}
/**
* After the user has logged in
* ===============================================================
*/
exports.dashboard = function(req, res) {
console.log('/dashboard - ' + req.user._id);
/*
Access.find({ user: req.user._id }, function(err, accesses) {
if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
console.log('accesses ' + accesses);
accesses.forEach(function(access) {
Project.load(access.project, function(err, project) {
if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
projectList.push(project);
console.log(project.user.username);
});
});
});
*/
Access.loadUser(req.user._id, function(err, projects) {
if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
Project.populate(projects, { path: 'project.user', model: User }, function(err, projects) {
console.log('accesses: ' + projects);
res.render('dashboard', {
title: 'Dashboard',
loggedin: true,
projects: projects
});
});
});
/*
Project.find(function(err, projects) {
if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
res.render('dashboard', {
title: 'Dashboad',
loggedin: true,
projects: projects
});
});*/
}
exports.project = function(req, res) {
Project.loadShort(req.params.short, function(err, project) {
if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
res.render('project', { title: 'Harepus', loggedin: true, req: req, project: project });
});
}
exports.projectParticipants = function(req, res) {
res.render('projectParticipants', { title: 'Prosjektdeltakere', loggedin: true });
}
exports.projectPost = function(req, res) {
/** ###################################
* Need to check if user has access to this project!!
*/
console.log(req.loggedin);
Project.loadShort(req.params.short, function(err, project) {
if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
req.project = project;
res.render('projectPost', { title: 'Legg til utgift', loggedin: true, req: req, project: project });
});
}
exports.postProjectPost = function(req, res) {
// Validation
v.check('project', 'The project was lost').notEmpty();
v.check('what', 'You need to fill in the what-field').notEmpty();
v.check('value', 'The value must be a positive number').notEmpty().isInt().min(0);
// error when validation fails
var errors = v.getErrors();
if (errors.length !== 0) return res.status(500).render('error', { title: '500', text: 'Det oppstod en valideringsfeil', error: errors.stack });
Project.load(req.sanitize('project').escape(), function(err, project) {
if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
// check if access
Access.checkAccess(req.user._id, project._id, 0, function(err, access) {
if (err || !access) return res.status(403).render('error', { title: '403', text: 'no sir.' });
// Time to fill in the model!
var ppost = new pPost();
ppost.user = req.user._id;
ppost.for = req.user._id;
ppost.project = project._id;
ppost.what = sanitize(req.body.what).escape();
ppost.comment = sanitize(req.body.comment).xss(); // xss will remove cross-site-scripting in the textfield.
ppost.participants = sanitize(req.body.participants).escape();
ppost.value = sanitize(req.body.value).toInt(); // this will remove leading zeroes. '0123' => '123'
ppost.when = new Date(sanitize(req.body.date).escape() + ' ' + sanitize(req.body.time).escape() + ':00');
ppost.save(function(err) {
if (err) {
console.log(err.errors);
res.render('projectPost', { title: 'Legg til utgift - en feil oppstod', loggedin: true, req: req, project: project });
}
return res.redirect('/project/' + project.shortURL);
});
});
});
}
exports.newProject = function(req, res) {
res.render('newProject', { title: 'Nytt prosjekt', loggedin: true });
}
exports.postNewProject = function(req, res) {
var project = new Project(req.body);
project.user = req.user._id;
project.save(function(err) {
if (err) {
console.log(err.errors);
return res.render('newproject', { title: 'Nytt prosjekt - en feil oppstod', loggedin: true, errors: err.errors, project: project });
}
var access = new Access();
access.user = req.user._id;
access.creator = req.user._id;
access.project = project._id;
access.permissions = 9;
access.save(function(err) {
if (err) {
console.log(err.errors);
return res.render('newproject', { title: 'Nytt prosjekt - en feil oppstod', loggedin: true });
}
return res.redirect('/dashboard');
});
});
}
|