Skip to content

Commit

Permalink
New changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
petersirka committed Jul 27, 2016
1 parent d281c03 commit cae6f9f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 46 deletions.
21 changes: 13 additions & 8 deletions controllers/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ exports.install = function() {
F.route('/*', 'login', ['unauthorize']);
F.route('/logoff', redirect_logoff, ['authorize']);

// Files
F.file('/download/*', file_download);
F.file('/photos/*.jpg', file_photo);
F.file('/download/*', file_download);

// Templates
F.localize('/templates/*.html', ['compress']);
};

// Signs out the user
function redirect_logoff() {
var self = this;
delete F.SESSION[self.user.id];
self.cookie(CONFIG('auth.cookie'), '', '-1 day');
self.redirect('/');
}

// Reads photo from DB
function file_photo(req, res) {
var id = req.split[1].substring(0, req.split[1].length - 4).split('x').first();
var token = HelpDesk.filename(id, '.jpg');
Expand Down Expand Up @@ -42,6 +53,7 @@ function file_photo(req, res) {
});
}

// Performs file download
function file_download(req, res) {
var id = req.split[1].substring(0, req.split[1].length - 4).split('x').first();
var token = HelpDesk.filename(id, '.' + req.extension);
Expand All @@ -64,11 +76,4 @@ function file_download(req, res) {
stream.pipe(writer);
});
});
}

function redirect_logoff() {
var self = this;
delete F.SESSION[self.user.id];
self.cookie(CONFIG('auth.cookie'), '', '-1 day');
self.redirect('/');
}
2 changes: 2 additions & 0 deletions definitions/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
F.SESSION = {};

// User authorization
F.onAuthorize = function(req, res, flags, callback) {

var cookie = req.cookie(CONFIG('auth.cookie'));
Expand Down Expand Up @@ -42,6 +43,7 @@ F.onAuthorize = function(req, res, flags, callback) {
}, 'item');
};

// Clears old session
F.on('service', function(interval) {

if (interval % 10 !== 0)
Expand Down
1 change: 1 addition & 0 deletions definitions/database.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Database initialization
require('sqlagent/pg').init(CONFIG('database'));
15 changes: 15 additions & 0 deletions definitions/helpdesk.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const HelpDesk = global.HelpDesk = {};

/**
* Signs filename
* @param {Number} oid
* @param {String} extension
* @return {String}
*/
HelpDesk.filename = function(oid, extension) {

var name = oid.toString();
Expand All @@ -11,6 +17,14 @@ HelpDesk.filename = function(oid, extension) {
return name + 'x' + count + extension;
};

/**
* Sends email (notification)
* @param {Number} type Notification type.
* @param {User} user User session.
* @param {UID} idticket
* @param {UID} idcomment Optional.
* @return {HelpDesk}
*/
HelpDesk.notify = function(type, user, idticket, idcomment) {

// 0 == create
Expand Down Expand Up @@ -181,4 +195,5 @@ HelpDesk.notify = function(type, user, idticket, idcomment) {

});

return HelpDesk;
};
37 changes: 0 additions & 37 deletions models/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,4 @@ NEWSCHEMA('Login').make(function(schema) {
}, 'item');
});

});

NEWSCHEMA('Token').make(function(schema) {

schema.define('token', String, true);

schema.addWorkflow('exec', function(error, model, controller, callback) {

var sql = DB(error);

sql.select('item', 'tbl_user').make(function(builder) {
builder.fields('id', 'isactivated');
builder.where('token', model.token);
builder.first();
});

sql.validate('item', 'error-user-credentials');

sql.update('tbl_user').make(function(builder) {
builder.set('isconfirmed', true);
builder.set('dateconfirmed', F.datetime);
builder.where('id', sql.expected('item', 'id'));
builder.where('isconfirmed', false);
});

sql.exec(function(err, response) {
if (err)
return callback();

if (!response.isactivated)
error.push('error-user-activated');

response.isactivated && controller.cookie(CONFIG('auth.cookie'), F.encrypt({ id: response.id, date: F.datetime.getTime(), ip: controller.ip }, CONFIG('auth.secret')), '1 month');
callback(SUCCESS(true));

}, 'item');
});
});
36 changes: 36 additions & 0 deletions models/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
NEWSCHEMA('Token').make(function(schema) {

schema.define('token', String, true);

schema.addWorkflow('exec', function(error, model, controller, callback) {

var sql = DB(error);

sql.select('item', 'tbl_user').make(function(builder) {
builder.fields('id', 'isactivated');
builder.where('token', model.token);
builder.first();
});

sql.validate('item', 'error-user-credentials');

sql.update('tbl_user').make(function(builder) {
builder.set('isconfirmed', true);
builder.set('dateconfirmed', F.datetime);
builder.where('id', sql.expected('item', 'id'));
builder.where('isconfirmed', false);
});

sql.exec(function(err, response) {
if (err)
return callback();

if (!response.isactivated)
error.push('error-user-activated');

response.isactivated && controller.cookie(CONFIG('auth.cookie'), F.encrypt({ id: response.id, date: F.datetime.getTime(), ip: controller.ip }, CONFIG('auth.secret')), '1 month');
callback(SUCCESS(true));

}, 'item');
});
});
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![MIT License][license-image]][license-url]

[![Support](https://www.totaljs.com/img/button-support.png)](https://www.totaljs.com/support/)
[![Support](https://www.totaljs.com/img/button-support.png)](https://www.totaljs.com/support/) [![Donate](https://www.totaljs.com/img/button-donate.png)](https://www.totaljs.com/#make-a-donation)

# Installation

Expand Down

0 comments on commit cae6f9f

Please sign in to comment.