Skip to content

Commit

Permalink
Merge pull request #41 from blank-project/dev
Browse files Browse the repository at this point in the history
Release Auth #23
  • Loading branch information
erwanosouf authored Oct 6, 2017
2 parents a70f101 + f6e3eba commit b3aa30c
Show file tree
Hide file tree
Showing 41 changed files with 732 additions and 62 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ public/lib
npm-debug.log

public/css/style.css

data/
data/uploads/
data/downloads/
13 changes: 8 additions & 5 deletions app/controllers/contacts.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
var express = require('express')
, co = require('co')
, router = express.Router()
, ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn
, Contact = require('../models/Contact')
, Tag = require('../models/Tag');

// Exports a function to bind Controller
module.exports = function (app) {
app.use('/contacts', router);
app.use('/contacts', ensureLoggedIn('/login'), router);
};

router.get('/', function (req, res, next) {
var query = {}, first = parseInt(req.query.first), size = parseInt(req.query.size);
// Search by all provided tags
if (req.query.tagId) {
query.tags = { $all : req.query.tagId };
}
Expand Down Expand Up @@ -51,12 +53,12 @@ router.get('/', function (req, res, next) {
}
data.size = size;
data.title = 'Liste de Contact';
res.render('contactList', data);
res.render('contacts/contactList', data);
}).catch(err => { next(err); });
});

router.get('/edit/', function (req, res, next) {
res.render('contactEdit', { contact : {} });
res.render('contacts/contactEdit', { contact : {} });
});

router.get('/edit/:contactId', function (req, res, next) {
Expand All @@ -65,7 +67,7 @@ router.get('/edit/:contactId', function (req, res, next) {
console.log('id :' + id);
Contact.findById(id).populate('tags').exec().
then(data => {
res.render('contactEdit', {
res.render('contacts/contactEdit', {
contact : data
});
}).
Expand All @@ -85,14 +87,15 @@ router.get('/:contactId', function (req, res, next) {
}).exec();
contact.tags.forEach(tag => { ids.push(tag._id) });

// Load other tags for edition
tags = yield Tag.find({ _id : { $nin : ids }}).sort('name').exec();

data.contact = contact;
data.tags = tags;
return data;
}).then(data => {
data.title = 'Fiche Contact ' + data.contact.fullName;
res.render('contactView', data);
res.render('contacts/contactView', data);
}).catch(err => { next(err); });
});

Expand Down
41 changes: 38 additions & 3 deletions app/controllers/home.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
var express = require('express'),
router = express.Router();
router = express.Router(),
auth = require('../../config/auth');

module.exports = function (app) {
app.use('/', router);
};

router.get('/', function (req, res, next) {
res.render('index', {
title : 'Contact Manager'
res.render('home/index', {
title : 'Contacts Manager'
});
});

router.route('/login')
.get(function (req, res, next) {
var data = {
title : 'Contacts Manager - Login'
}, flash = req.flash();
console.log(flash);
if (flash && flash.error) {
data.message = {
level : 'error',
message : flash.error[0]
}
}
res.render('home/login', data);
})
.post(auth.authenticate('local', {
successRedirect: '/contacts',
failureRedirect: '/login',
failureFlash: true
}));

router.get('/logout', function (req, res, next) {
req.logout();
res.render('home/index', {
title : 'Contacts Manager - Logout'
});
});

router.route('/signup')
.get(function (req, res, next) {
res.render('users/userEdit', {
title : 'Contacts Manager - Sign Up'
});
});
43 changes: 43 additions & 0 deletions app/controllers/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var express = require('express')
, router = express.Router()
, ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn
, upload = require('multer')({ dest: 'data/uploads/' })
, conf = require('../../config/config')
, path = require('path')
, db = conf.db
, cwd = conf.root
, execFile = require('child_process').execFile
, scriptDir = path.join(cwd, 'scripts/')
, script = path.join(scriptDir, 'contacts-import.sh');

// Exports a function to bind Controller
module.exports = function (app) {
app.use('/import', ensureLoggedIn('/login'), router);
};

router.get('/', function (req, res, next) {
res.render('import/contactImport', { title : 'Import de contact' });
});

router.post('/', upload.single('upload'), function (req, res, next) {
if (!req.file) {
res.render('contactImport', { title : 'Import de contact', message : 'Fichier manquant' });
return;
}
var filename = path.join(cwd, req.file.path)
, options = {
cwd : scriptDir,
env : db
};
execFile(script, [ filename ], options, (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);

if (error) {
next(error);
return;
}

res.redirect("/contacts");
});
});
11 changes: 6 additions & 5 deletions app/controllers/tags.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
var express = require('express')
, router = express.Router()
, ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn
, Tag = require('../models/Tag');

// Exports a function to bind Controller
module.exports = function (app) {
app.use('/tags', router);
app.use('/tags', ensureLoggedIn('/login'), router);
};

router.get('/edit/', function (req, res, next) {
res.render('tagEdit', { tag : {} });
res.render('tags/tagEdit', { tag : {} });
});

router.get('/edit/:tagId', function (req, res, next) {
Expand All @@ -17,7 +18,7 @@ router.get('/edit/:tagId', function (req, res, next) {
console.log('id :' + id);
Tag.findById(id).exec().
then(data => {
res.render('tagEdit', {
res.render('tags/tagEdit', {
tag : data
});
}).
Expand All @@ -28,7 +29,7 @@ router.get('/', function (req, res, next) {
console.log('Listing tags');
var tags = Tag.find({}).sort('name').exec();
tags.then(data => {
res.render('tagList', {
res.render('tags/tagList', {
title : 'Liste d\'etiquettes',
tags : data
});
Expand All @@ -41,7 +42,7 @@ router.get('/:tagId', function (req, res, next) {
console.log('id :' + id);
Tag.findById(id).exec().
then(data => {
res.render('tagView', {
res.render('tags/tagView', {
tag : data
});
}).
Expand Down
83 changes: 83 additions & 0 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var express = require('express')
, co = require('co')
, ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn
, router = express.Router()
, User = require('../models/User')
, Tag = require('../models/Tag');

// Exports a function to bind Controller
module.exports = function (app) {
app.use('/users', router);
};

function userView(req, res, next, id) {
console.log('Displaying user ' + id);

co(function* () {
var user, data = {};

user = yield User.findById(id).exec();

data.user = user;
return data;
}).then(data => {
data.title = 'Profil ' + data.user.username;
res.render('users/userView', data);
}).catch(err => { next(err); });
}

router.route('/')
.get(function (req, res, next) {
console.log(req.user);
console.log(req.session.user);
res.redirect('/users/me');
})
.post(function (req, res, next) {
console.log('Submitting User ');
var user = new User();
user.set({
username : req.body.username,
name : {
first : req.body.firstname,
last : req.body.lastname
},
email : req.body.email,
phone : req.body.phone,
organization : req.body.organization,
title : req.body.title,
meta : {
// Disable self registration of user for now.
disabled : new Date()
}
});
User.register(user, req.body.password, function(err) {
var data;
if (err) {
console.log(err);
data = req.body || {};
data.message = {
level : 'error',
message : err.message
}
res.render('users/userEdit', data);
return;
}

console.log('user registered!');

res.redirect('/');
});
});

router.get('/me', function (req, res, next) {
var data = {
user : req.user,
title : 'Profil ' + req.user.username
};
res.render('users/userView', data);
});

router.get('/:userId', ensureLoggedIn('/login'), function (req, res, next) {
var id = req.params.userId;
userView(req, res, next, id);
});
20 changes: 20 additions & 0 deletions app/models/Profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, Profile;

// Base Schema
var schema = new Schema({
name : { type: String, required : true, trim : true },
roles : [{ type: String, trim : true }]
}, {
collection : 'profiles',
timestamps: {
createdAt : 'meta.creationDate',
updatedAt : 'meta.modificationDate'
}
});

Profile = mongoose.model('Profile', schema);
Profile.schema = schema;

module.exports = Profile;
Loading

0 comments on commit b3aa30c

Please sign in to comment.