Skip to content

Commit

Permalink
Merge pull request #56 from nus-mtp/develop
Browse files Browse the repository at this point in the history
Merge develop to master for Sprint 2 release
  • Loading branch information
linxea committed Feb 17, 2016
2 parents 7f2f89b + cbb05f4 commit 1e53d33
Show file tree
Hide file tree
Showing 50 changed files with 1,874 additions and 404 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ language: node_js
node_js:
- "0.10"

before_install: npm install -g grunt-cli

install: npm install

script: grunt test

branches:
only:
- develop
- develop
- master
84 changes: 84 additions & 0 deletions api/controllers/PageController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* PageController
*
* @description :: Server-side logic for managing pages
* @help :: See http://links.sailsjs.org/docs/controllers
* https://github.com/irlnathan/activityoverlord20/blob/master/api/controllers/PageController.js
*/

module.exports = {

showHomePage: function (req, res) {

// If not logged in, show the public view.
if (!req.session.me) {
//return res.view('homepage');
return res.view('homepage', {
me: [],
video: []
});
}

// Otherwise, look up the logged-in user and show the logged-in view,
// bootstrapping basic user data in the HTML sent from the server
User.findOne(req.session.me, function (err, user){
if (err) {
return res.negotiate(err);
}

if (!user) {
sails.log.verbose('Session refers to a user who no longer exists- did you delete a user, then try to refresh the page with an open tab logged-in as that user?');
//return res.view('homepage');
return res.view('homepage', {
me: [],
video: []
});
}

return res.view('dashboard', {
me: user,
video: []
});

});
},

showEditPage: function (req, res) {

// If not logged in, show the public view.
if (!req.session.me) {
return res.view('homepage');
}

// Otherwise, look up the logged-in user and show the logged-in view,
// bootstrapping basic user data in the HTML sent from the server
User.findOne(req.session.me, function (err, user){
if (err) {
return res.negotiate(err);
}

if (!user) {
sails.log.verbose('Session refers to a user who no longer exists- did you delete a user, then try to refresh the page with an open tab logged-in as that user?');
return res.view('homepage', {
me: [],
video: []
});
}

// retreive the video object using the id
Video.findOne(req.param('id'), function(err, video){
if (err) {
// return error
}

// if successful, return user and video object to frontend
return res.view('edit', {
me: user,
video: video
});

});
});
}

};
64 changes: 60 additions & 4 deletions api/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = {

/**
* `UserController.login()`
* Usage: POST /api/user/login
* Content: {username: ':username', password: ':password'}
*/
login: function (req, res) {
// Look for user using given username
Expand Down Expand Up @@ -49,19 +51,73 @@ module.exports = {

/**
* `UserController.signup()`
* Usage: POST /api/user/signup
* Content: {username: ':username', password: ':password', email: ':emailaddress'}
*/
signup: function (req, res) {
return res.json({
todo: 'signup() is not implemented yet!'
Passwords.encryptPassword({
// Encrypt with BCrypt algo
password: req.param('password'),
difficulty: 10,
}).exec({
error: function(err) {
return res.negotiate(err);
},

success: function(encryptedPassword) {
User.create({
username: req.param('username'),
encryptedPassword: encryptedPassword,
email: req.param('email')
}).exec(function(err, newUser) {
if (err) {
console.log("err: ", err);
console.log("err.invalidAttributes: ", err.invalidAttributes);

// If this is a uniqueness error about the email attribute,
// send back an easily parseable status code.
if (err.invalidAttributes && err.invalidAttributes.email && err.invalidAttributes.email[0]
&& err.invalidAttributes.email[0].rule === 'unique') {
return res.emailAddressInUse();
}

return res.negotitate(err);
}

// Log user in
req.session.me = newUser.id;

// Send back the id of the new user
return res.json({
id: newUser.id
});
});
},
});
},

/**
* `UserController.logout()`
* Usage: GET /api/user/logout
*/
logout: function (req, res) {
return res.json({
todo: 'logout() is not implemented yet!'
// Look up the user record from the database which is
// referenced by the id in the user session (req.session.me)
User.findOne(req.session.me, function foundUser(err, user) {
if (err) return res.negotiate(err);

// If session refers to a user who no longer exists, still allow logout.
if (!user) {
sails.log.verbose('Session refers to a user who no longer exists.');
return res.backToHomePage();
}

// Wipe out the session (log out)
req.session.me = null;

// Either send a 200 OK or redirect to the home page
return res.backToHomePage();

});
},

Expand Down
1 change: 0 additions & 1 deletion api/controllers/VideoController.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ module.exports = {
Video.destroy({
id: req.param('id')
}).exec(function (err, video) {
console.log(req.body.videoId);
if (err) throw err;
res.json(video);
});
Expand Down
15 changes: 13 additions & 2 deletions api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ module.exports = {
columnName: 'encrypted_password'
},

lastLoggedIn: {
type: 'datetime',
defaultsTo: function() {return new Date(); }
},

email: {
type: 'string',
email: true,
required: true,
unique: true
},

// 0 for admin, 1 for normal user
permission: {
type: 'integer',
defaultTo: 1,
required: true
defaultsTo: 1
}
},
}
1 change: 0 additions & 1 deletion api/models/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = {
// 0 for self only, 1 for public
privacy: {
type: 'integer',
required: true,
defaultsTo: 1
},

Expand Down
2 changes: 1 addition & 1 deletion api/policies/sessionAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function(req, res, next) {

// User is allowed, proceed to the next policy,
// or if this is the last policy, the controller
if (req.session.authenticated) {
if (req.session.me) {
return next();
}

Expand Down
22 changes: 22 additions & 0 deletions api/responses/backToHomePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Usage:
* res.backToHomePage(); // (default to 200 "OK" status code)
* res.backToHomePage(400);
*
*/

module.exports = function backToHomePage (statusCode){

// Get access to `req` and `res`
// (since the arguments are up to us)
var req = this.req;
var res = this.res;

// All done- either send back an empty response w/ just the status code
// (e.g. for AJAX requests)
if (req.wantsJSON) {
return res.send(statusCode||200);
}
// or redirect to the home page
return res.redirect('/');
};
17 changes: 17 additions & 0 deletions api/responses/emailAddressInUse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 409 (Conflict) Handler
*
* Usage:
* res.emailAddressInUse();
*
* @reference: https://github.com/irlnathan/activityoverlord20/blob/master/api/responses/emailAddressInUse.js
*/

module.exports = function emailAddressInUse (){

// Get access to `res`
// (since the arguments are up to us)
var res = this.res;

return res.send(409, 'Email address is already taken by another user.');
};
79 changes: 79 additions & 0 deletions assets/images/zoomable_logo_black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 0 additions & 29 deletions assets/index.html

This file was deleted.

Loading

0 comments on commit 1e53d33

Please sign in to comment.