remerged with new dev_working
This commit is contained in:
commit
6badd3ad56
|
@ -36,56 +36,97 @@ exports.create = function(req, res) {
|
|||
/**
|
||||
* Upload PDF
|
||||
*/
|
||||
exports.uploadPDF = function(files, user, cb) {
|
||||
var _user = JSON.parse(''+user);
|
||||
console.log(_user.username);
|
||||
console.log(config.tmpUploadPath);
|
||||
exports.uploadPDF = function(req, res, next) {
|
||||
|
||||
if(files) {
|
||||
console.log('inside uploadPDF');
|
||||
if(req.files){
|
||||
var pdfFile = req.files.file;
|
||||
var _user = req.user;
|
||||
if (req.files.size === 0) {
|
||||
next(new Error('File uploaded is EMPTY'));
|
||||
}else if(req.files.size > 200000000){
|
||||
next(new Error('File uploaded exceeds MAX SIZE of 200MB'));
|
||||
}else {
|
||||
fs.exists(pdfFile.path, function(exists) {
|
||||
//If file exists move to user's tmp directory
|
||||
if(exists) {
|
||||
|
||||
console.log('inside uploadPDF');
|
||||
console.log(files.file[0]);
|
||||
var pdfFile = files.file[0];
|
||||
var newDestination = config.tmpUploadPath+_user.username;
|
||||
var stat = null;
|
||||
try {
|
||||
stat = fs.statSync(newDestination);
|
||||
} catch (err) {
|
||||
fs.mkdirSync(newDestination);
|
||||
}
|
||||
if (stat && !stat.isDirectory()) {
|
||||
console.log('Directory cannot be created');
|
||||
next(new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"'));
|
||||
}
|
||||
|
||||
fs.move(pdfFile.path, path.join(newDestination, pdfFile.name), function (err) {
|
||||
if (err) {
|
||||
next(new Error(err.message));
|
||||
}
|
||||
pdfFile.path = path.join(newDestination, pdfFile.name);
|
||||
console.log(pdfFile.name + ' uploaded to ' + pdfFile.path);
|
||||
res.status(200).send(pdfFile);
|
||||
});
|
||||
|
||||
if (pdfFile.size === 0) {
|
||||
throw new Error('Files uploaded are EMPTY');
|
||||
} else {
|
||||
next(new Error('Did NOT get your file!'));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//If file exists move to user's tmp directory
|
||||
fs.open(pdfFile.path,'r',function(err,fd){
|
||||
if (err && err.code === 'ENOENT') {
|
||||
return res.status(400).send({
|
||||
message: 'Did NOT get your file!'
|
||||
});
|
||||
}
|
||||
|
||||
var stat = null;
|
||||
try {
|
||||
stat = fs.statSync(newDestination);
|
||||
} catch (err) {
|
||||
fs.mkdirSync(newDestination);
|
||||
}
|
||||
if (stat && !stat.isDirectory()) {
|
||||
console.log('Directory cannot be created');
|
||||
throw new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"');
|
||||
}
|
||||
|
||||
fs.move(pdfFile.path, path.join(newDestination, pdfFile.name), function (err) {
|
||||
if (err) {
|
||||
throw new Error(err.message);
|
||||
}
|
||||
pdfFile.path = path.join(newDestination, pdfFile.name);
|
||||
|
||||
return cb(pdfFile);
|
||||
});
|
||||
return res.status(200);
|
||||
});
|
||||
}else {
|
||||
throw new Error('File NOT uploaded');
|
||||
next(new Error('Uploaded files were NOT detected'));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// exports.uploadPDF = function(pdfFile, _user, cb) {
|
||||
// // console.log(_user.username);
|
||||
// // console.log(config.tmpUploadPath);
|
||||
|
||||
|
||||
// console.log('inside uploadPDF');
|
||||
// // console.log(pdfFile);
|
||||
|
||||
// if (pdfFile.size === 0) {
|
||||
// cb(new Error('Files uploaded are EMPTY'), null);
|
||||
// }else {
|
||||
// fs.exists(pdfFile.path, function(exists) {
|
||||
// //If file exists move to user's tmp directory
|
||||
// if(exists) {
|
||||
|
||||
// var newDestination = config.tmpUploadPath+""+_user.username;
|
||||
// var stat = null;
|
||||
// try {
|
||||
// stat = fs.statSync(newDestination);
|
||||
// } catch (err) {
|
||||
// fs.mkdirSync(newDestination);
|
||||
// }
|
||||
// if (stat && !stat.isDirectory()) {
|
||||
// console.log('Directory cannot be created');
|
||||
// cb(new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"'), null);
|
||||
// }
|
||||
|
||||
// fs.move(pdfFile.path, path.join(newDestination, pdfFile.name), function (err) {
|
||||
// if (err) {
|
||||
// return cb(new Error(err.message), null);
|
||||
// }
|
||||
// pdfFile.path = path.join(newDestination, pdfFile.name);
|
||||
|
||||
// return cb(null, pdfFile);
|
||||
// });
|
||||
|
||||
// } else {
|
||||
// cb(new Error('Did NOT get your file!'), null);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
// };
|
||||
|
||||
/**
|
||||
* Show the current form
|
||||
*/
|
||||
|
@ -164,9 +205,11 @@ exports.listSubmissions = function(req, res) {
|
|||
* Update a form
|
||||
*/
|
||||
exports.update = function(req, res) {
|
||||
|
||||
console.log('in form.update()');
|
||||
|
||||
var form = req.form;
|
||||
form = _.extend(form, req.body);
|
||||
// console.log(req.body.form);
|
||||
form = _.extend(form, req.body.form);
|
||||
form.admin = req.user;
|
||||
|
||||
form.save(function(err) {
|
||||
|
|
|
@ -11,6 +11,7 @@ var mongoose = require('mongoose'),
|
|||
config = require('../../config/config'),
|
||||
path = require('path'),
|
||||
fs = require('fs-extra'),
|
||||
async = require('async'),
|
||||
Field = mongoose.model('Field', FieldSchema);
|
||||
|
||||
|
||||
|
@ -73,54 +74,6 @@ var FormSchema = new Schema({
|
|||
},
|
||||
});
|
||||
|
||||
//Update lastModified everytime we save
|
||||
FormSchema.pre('save', function (next) {
|
||||
this.lastModified = Date.now();
|
||||
next();
|
||||
});
|
||||
|
||||
//Move PDF to permanent location after new template is uploaded
|
||||
FormSchema.pre('save', function (next) {
|
||||
|
||||
if(this.pdf && this.isModified('pdf')){
|
||||
console.log('Relocating PDF');
|
||||
|
||||
var new_filename = this.pdf.title.trim()+'_template.pdf';
|
||||
|
||||
var newDestination = path.join(config.pdfUploadPath, this.pdf.title.trim()),
|
||||
stat = null;
|
||||
|
||||
try {
|
||||
stat = fs.statSync(newDestination);
|
||||
} catch (err) {
|
||||
fs.mkdirSync(newDestination);
|
||||
}
|
||||
if (stat && !stat.isDirectory()) {
|
||||
console.log('Directory cannot be created');
|
||||
next( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"') );
|
||||
}
|
||||
|
||||
console.log('about to move PDF');
|
||||
fs.move(this.pdf.path, path.join(newDestination, new_filename), function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
next( new Error(err.message) );
|
||||
}
|
||||
|
||||
this.pdf.path = path.join(newDestination, new_filename);
|
||||
this.pdf.name = new_filename;
|
||||
|
||||
console.log('PDF file:'+this.pdf.name+' successfully moved to: '+this.pdf.path);
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
}else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
//Delete template PDF of current Form
|
||||
FormSchema.pre('remove', function (next) {
|
||||
if(this.pdf){
|
||||
|
@ -132,74 +85,218 @@ FormSchema.pre('remove', function (next) {
|
|||
}
|
||||
});
|
||||
|
||||
//Autogenerate FORM from PDF if 'isGenerated' flag is 'true'
|
||||
//Create folder for user's pdfs
|
||||
FormSchema.pre('save', function (next) {
|
||||
var field, _form_fields;
|
||||
|
||||
if(this.isGenerated && this.pdf){
|
||||
var newDestination = path.join(config.pdfUploadPath, this.admin.username.replace(/ /g,'')),
|
||||
stat = null;
|
||||
|
||||
var _typeConvMap = {
|
||||
'Multiline': 'textarea',
|
||||
'Text': 'textfield',
|
||||
'Button': 'checkbox',
|
||||
'Choice': 'radio',
|
||||
'Password': 'password',
|
||||
'FileSelect': 'filefield',
|
||||
'Radio': 'radio'
|
||||
};
|
||||
try {
|
||||
stat = fs.statSync(newDestination);
|
||||
} catch (err) {
|
||||
fs.mkdirSync(newDestination);
|
||||
}
|
||||
if (stat && !stat.isDirectory()) {
|
||||
console.log('Directory cannot be created');
|
||||
next( new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"') );
|
||||
}else{
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
var that = this;
|
||||
console.log('autogenerating form');
|
||||
|
||||
try {
|
||||
pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){
|
||||
|
||||
//Map PDF field names to FormField field names
|
||||
for(var i = 0; i < _form_fields.length; i++){
|
||||
field = _form_fields[i];
|
||||
|
||||
//Convert types from FDF to 'FormField' types
|
||||
if(_typeConvMap[ field.fieldType+'' ]){
|
||||
field.fieldType = _typeConvMap[ field.fieldType+'' ];
|
||||
}
|
||||
|
||||
field.fieldValue = '';
|
||||
field.created = Date.now();
|
||||
field.required = true;
|
||||
field.disabled = false;
|
||||
|
||||
// field = new Field(field);
|
||||
// field.save(function(err) {
|
||||
// if (err) {
|
||||
// console.error(err.message);
|
||||
// throw new Error(err.message);
|
||||
// });
|
||||
// } else {
|
||||
// _form_fields[i] = this;
|
||||
// }
|
||||
// });
|
||||
_form_fields[i] = field;
|
||||
}
|
||||
|
||||
console.log('NEW FORM_FIELDS: ');
|
||||
console.log(_form_fields);
|
||||
|
||||
console.log('\n\nOLD FORM_FIELDS: ');
|
||||
console.log(that.form_fields);
|
||||
|
||||
that.form_fields = _form_fields;
|
||||
next();
|
||||
});
|
||||
} catch(err){
|
||||
next( new Error(err.message) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Throw error if we encounter form with invalid type
|
||||
//Update lastModified and created everytime we save
|
||||
FormSchema.pre('save', function (next) {
|
||||
var now = new Date();
|
||||
this.lastModified = now;
|
||||
if( !this.created ){
|
||||
this.created = now;
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
//Move PDF to permanent location after new template is uploaded
|
||||
FormSchema.pre('save', function (next) {
|
||||
|
||||
if(this.pdf){
|
||||
var that = this;
|
||||
async.series([
|
||||
function(callback){
|
||||
if(that.isModified('pdf')){
|
||||
console.log('about to move PDF');
|
||||
|
||||
var new_filename = that.title.replace(/ /g,'')+'_template.pdf';
|
||||
|
||||
var newDestination = path.join(config.pdfUploadPath, that.admin.username.replace(/ /g,''), that.title.replace(/ /g,'')),
|
||||
stat = null;
|
||||
|
||||
try {
|
||||
stat = fs.statSync(newDestination);
|
||||
} catch (err) {
|
||||
fs.mkdirSync(newDestination);
|
||||
}
|
||||
if (stat && !stat.isDirectory()) {
|
||||
console.log('Directory '+newDestination+' cannot be created');
|
||||
callback( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"') );
|
||||
}
|
||||
|
||||
console.log('about to move PDF');
|
||||
|
||||
fs.move(that.pdf.path, path.join(newDestination, new_filename), function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
callback( new Error(err.message) );
|
||||
}
|
||||
that.pdf.path = path.join(newDestination, new_filename);
|
||||
that.pdf.name = new_filename;
|
||||
|
||||
console.log('\n\n PDF file:'+that.pdf.name+' successfully moved to: '+that.pdf.path);
|
||||
|
||||
callback(null,null);
|
||||
});
|
||||
}
|
||||
callback(null,null);
|
||||
},
|
||||
function(callback){
|
||||
if(that.isGenerated){
|
||||
that.pdf.path = path.join(config.pdfUploadPath, that.admin.username.replace(/ /g,''), that.title.replace(/ /g,''), that.title.replace(/ /g,'')+'_template.pdf');
|
||||
that.pdf.name = that.title.replace(/ /g,'')+'_template.pdf';
|
||||
var _typeConvMap = {
|
||||
'Multiline': 'textarea',
|
||||
'Text': 'textfield',
|
||||
'Button': 'checkbox',
|
||||
'Choice': 'radio',
|
||||
'Password': 'password',
|
||||
'FileSelect': 'filefield',
|
||||
'Radio': 'radio'
|
||||
};
|
||||
|
||||
console.log('autogenerating form');
|
||||
console.log(that.pdf.path);
|
||||
|
||||
try {
|
||||
pdfFiller.generateFieldJson(that.pdf.path, function(_form_fields){
|
||||
|
||||
//Map PDF field names to FormField field names
|
||||
for(var i = 0; i < _form_fields.length; i++){
|
||||
var field = _form_fields[i];
|
||||
|
||||
//Convert types from FDF to 'FormField' types
|
||||
if(_typeConvMap[ field.fieldType+'' ]){
|
||||
field.fieldType = _typeConvMap[ field.fieldType+'' ];
|
||||
}
|
||||
|
||||
field.fieldValue = '';
|
||||
field.created = Date.now();
|
||||
field.required = true;
|
||||
field.disabled = false;
|
||||
|
||||
// field = new Field(field);
|
||||
// field.save(function(err) {
|
||||
// if (err) {
|
||||
// console.error(err.message);
|
||||
// throw new Error(err.message);
|
||||
// });
|
||||
// } else {
|
||||
// _form_fields[i] = that;
|
||||
// }
|
||||
// });
|
||||
_form_fields[i] = field;
|
||||
}
|
||||
|
||||
console.log('NEW FORM_FIELDS: ');
|
||||
console.log(_form_fields);
|
||||
|
||||
console.log('\n\nOLD FORM_FIELDS: ');
|
||||
console.log(that.form_fields);
|
||||
|
||||
that.form_fields = _form_fields;
|
||||
callback();
|
||||
});
|
||||
} catch(err){
|
||||
next( new Error(err.message) );
|
||||
}
|
||||
}
|
||||
callback(null,null);
|
||||
}
|
||||
], function(err, results) {
|
||||
if(err){
|
||||
next(new Error({
|
||||
message: err.message
|
||||
}));
|
||||
}
|
||||
next();
|
||||
});
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
//Autogenerate Form_fields from PDF if 'isGenerated' flag is set
|
||||
// FormSchema.pre('save', function (next) {
|
||||
// var field, _form_fields;
|
||||
|
||||
// if(this.pdf){
|
||||
// if(this.isGenerated){
|
||||
|
||||
// var _typeConvMap = {
|
||||
// 'Multiline': 'textarea',
|
||||
// 'Text': 'textfield',
|
||||
// 'Button': 'checkbox',
|
||||
// 'Choice': 'radio',
|
||||
// 'Password': 'password',
|
||||
// 'FileSelect': 'filefield',
|
||||
// 'Radio': 'radio'
|
||||
// };
|
||||
|
||||
// var that = this;
|
||||
// console.log('autogenerating form');
|
||||
|
||||
// try {
|
||||
// pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){
|
||||
|
||||
// //Map PDF field names to FormField field names
|
||||
// for(var i = 0; i < _form_fields.length; i++){
|
||||
// field = _form_fields[i];
|
||||
|
||||
// //Convert types from FDF to 'FormField' types
|
||||
// if(_typeConvMap[ field.fieldType+'' ]){
|
||||
// field.fieldType = _typeConvMap[ field.fieldType+'' ];
|
||||
// }
|
||||
|
||||
// field.fieldValue = '';
|
||||
// field.created = Date.now();
|
||||
// field.required = true;
|
||||
// field.disabled = false;
|
||||
|
||||
// // field = new Field(field);
|
||||
// // field.save(function(err) {
|
||||
// // if (err) {
|
||||
// // console.error(err.message);
|
||||
// // throw new Error(err.message);
|
||||
// // });
|
||||
// // } else {
|
||||
// // _form_fields[i] = this;
|
||||
// // }
|
||||
// // });
|
||||
// _form_fields[i] = field;
|
||||
// }
|
||||
|
||||
// console.log('NEW FORM_FIELDS: ');
|
||||
// console.log(_form_fields);
|
||||
|
||||
// console.log('\n\nOLD FORM_FIELDS: ');
|
||||
// console.log(that.form_fields);
|
||||
|
||||
// that.form_fields = _form_fields;
|
||||
// next();
|
||||
// });
|
||||
// } catch(err){
|
||||
// next( new Error(err.message) );
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// next();
|
||||
// });
|
||||
|
||||
FormSchema.methods.convertToFDF = function (cb) {
|
||||
var _keys = _.pluck(this.form_fields, 'title'),
|
||||
_values = _.pluck(this.form_fields, 'fieldValue');
|
||||
|
|
|
@ -90,12 +90,11 @@ FormSubmissionSchema.pre('save', function (next) {
|
|||
|
||||
if(_form.autofillPDFs){
|
||||
|
||||
dest_filename = _form.title.trim()+'_submission_'+Date.now()+'.pdf';
|
||||
dest_filename = _form.title.replace(/ /g,'')+'_submission_'+Date.now()+'.pdf';
|
||||
dest_path = path.join(config.pdfUploadPath, dest_filename);
|
||||
|
||||
this.pdfFilePath = dest_path;
|
||||
|
||||
// console.log('autofillPDFs check');
|
||||
|
||||
pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){
|
||||
console.log('fdfData: \n');
|
||||
|
@ -108,7 +107,7 @@ FormSubmissionSchema.pre('save', function (next) {
|
|||
console.log('\n err.message: '+err.message);
|
||||
next( new Error(err.message) );
|
||||
}
|
||||
console.log('Field data from Form: '+_form.title.trim()+' outputed to new PDF: '+dest_path);
|
||||
console.log('Field data from Form: '+_form.title.replace(/ /g,'')+' outputed to new PDF: '+dest_path);
|
||||
next();
|
||||
});
|
||||
} else {
|
||||
|
|
|
@ -15,15 +15,15 @@ module.exports = function(app) {
|
|||
.get(users.requiresLogin, forms.list)
|
||||
.post(users.requiresLogin, forms.create);
|
||||
|
||||
app.route('/forms/:formId/submissions')
|
||||
.get(forms.listSubmissions);
|
||||
|
||||
app.route('/forms/:formId')
|
||||
app.route('/forms/:formId([a-zA-Z0-9]+)')
|
||||
.get(forms.read)
|
||||
.post(forms.createSubmission)
|
||||
// .post(forms.createSubmission)
|
||||
.put(users.requiresLogin, forms.hasAuthorization, forms.update)
|
||||
.delete(users.requiresLogin, forms.hasAuthorization, forms.delete);
|
||||
|
||||
app.route('/forms/:formId([a-zA-Z0-9]+)/submissions')
|
||||
.get(forms.listSubmissions);
|
||||
|
||||
// Finish by binding the form middleware
|
||||
app.param('formId', forms.formByID);
|
||||
};
|
||||
|
|
|
@ -107,19 +107,29 @@ module.exports = function(db) {
|
|||
console.log(file.originalname + ' is starting ...');
|
||||
},
|
||||
onFileUploadComplete: function (file, req, res) {
|
||||
console.log(file.originalname + ' uploaded to ' + file.path);
|
||||
// console.log('\n\nheadersSent in onFileUploadComplete: ', res.headersSent);
|
||||
console.log(req.body.user);
|
||||
try{
|
||||
formCtrl.uploadPDF(req.files, req.body.user, function(_file){
|
||||
console.log(_file.filename + ' uploaded to ' + _file.path);
|
||||
res.status(200).send(_file);
|
||||
});
|
||||
}catch(err) {
|
||||
res.status(500).send({
|
||||
message: err.message
|
||||
});
|
||||
}
|
||||
// res.status(200).send(file);
|
||||
}
|
||||
// console.log('\n\nheadersSent in onFileUploadComplete: ', res.headersSent);
|
||||
// console.log(req.body.user);
|
||||
// console.log(req.user);
|
||||
// var _user = JSON.parse(req.body.user);
|
||||
// console.log(file)
|
||||
|
||||
// formCtrl.uploadPDF(file, _user, function(err, _file){
|
||||
// if(err){
|
||||
// console.log('\n\n ERROR: '+err.message)
|
||||
// res.status(500).send({
|
||||
// message: err.message
|
||||
// });
|
||||
// }else {
|
||||
// console.log(_file.filename + ' uploaded to ' + _file.path);
|
||||
// res.status(200).send(_file);
|
||||
// }
|
||||
|
||||
// });
|
||||
// }
|
||||
}));
|
||||
|
||||
// CookieParser should be above session
|
||||
|
|
|
@ -14,7 +14,7 @@ angular.module('forms').run(['Menus',
|
|||
return function(formObj){
|
||||
//get keys
|
||||
var formKeys = Object.keys(formObj);
|
||||
// console.log(formKeys);
|
||||
|
||||
//we only care about things that don't start with $
|
||||
var fieldKeys = formKeys.filter(function(key){
|
||||
return key[0] !== '$';
|
||||
|
|
|
@ -12,10 +12,6 @@ angular.module('forms').config(['$stateProvider',
|
|||
state('createForm', {
|
||||
url: '/forms/create',
|
||||
templateUrl: 'modules/forms/views/create-form.client.view.html',
|
||||
// parent: 'restricted',
|
||||
// data: {
|
||||
// roles: ['user', 'admin'],
|
||||
// },
|
||||
}).
|
||||
state('viewForm', {
|
||||
url: '/forms/:formId/admin',
|
||||
|
@ -32,10 +28,6 @@ angular.module('forms').config(['$stateProvider',
|
|||
state('editForm', {
|
||||
url: '/forms/:formId/edit',
|
||||
templateUrl: 'modules/forms/views/create-form.client.view.html',
|
||||
// parent: 'restricted',
|
||||
// data: {
|
||||
// roles: ['user', 'admin'],
|
||||
// },
|
||||
});
|
||||
}
|
||||
]);
|
|
@ -1,25 +1,22 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('forms').controller('EditFormController', ['$scope', '$state', '$rootScope', 'Upload', '$stateParams', 'FormFields', 'Forms', 'CurrentForm', '$modal', '$location',
|
||||
function ($scope, $state, $rootScope, Upload, $stateParams, FormFields, Forms, CurrentForm, $modal, $location) {
|
||||
angular.module('forms').controller('EditFormController', ['$scope', '$state', '$rootScope', 'Upload', '$stateParams', 'FormFields', 'Forms', 'CurrentForm', '$modal', '$location', '$http',
|
||||
function ($scope, $state, $rootScope, Upload, $stateParams, FormFields, Forms, CurrentForm, $modal, $location, $http) {
|
||||
|
||||
$scope.isNewForm = false;
|
||||
$scope.pdfLoading = false;
|
||||
var _current_upload = null;
|
||||
|
||||
$scope.form = {};
|
||||
$scope.log = '';
|
||||
var _current_upload = null;
|
||||
|
||||
// Get current form if it exists, or create new one
|
||||
if($stateParams.formId){
|
||||
$scope.form = {};
|
||||
var _form = Forms.get({ formId: $stateParams.formId}, function(form){
|
||||
_form.pdf = form.pdf;
|
||||
_form.$save();
|
||||
|
||||
$scope.form = angular.fromJson(angular.toJson(_form));
|
||||
console.log(JSON.stringify($scope.form.pdf));
|
||||
Forms.get({ formId: $stateParams.formId}, function(form){
|
||||
$scope.form = angular.fromJson(angular.toJson(form));
|
||||
console.log($scope.form);
|
||||
});
|
||||
} else {
|
||||
$scope.form = {};
|
||||
$scope.form.form_fields = [];
|
||||
$scope.isNewForm = true;
|
||||
}
|
||||
|
@ -28,12 +25,13 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$
|
|||
$scope.cancelUpload = function(){
|
||||
_current_upload.abort();
|
||||
$scope.pdfLoading = false;
|
||||
$scope.removePDF();
|
||||
};
|
||||
|
||||
$scope.removePDF = function(){
|
||||
$scope.form.pdf = null;
|
||||
$scope.isGenerated = false;
|
||||
$scope.autofillPDFs = false;
|
||||
$scope.form.isGenerated = false;
|
||||
$scope.form.autofillPDFs = false;
|
||||
|
||||
console.log('form.pdf: '+$scope.form.pdf+' REMOVED');
|
||||
};
|
||||
|
@ -56,13 +54,14 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$
|
|||
evt.config.file.name + '\n' + $scope.log;
|
||||
$scope.pdfLoading = true;
|
||||
}).success(function (data, status, headers, config) {
|
||||
$scope.log = 'file ' + data.originalname + 'uploaded as '+ data.name +'. JSON: ' + JSON.stringify(data) + '\n' + $scope.log;
|
||||
$scope.form.pdf = data;
|
||||
$scope.log = 'file ' + data.originalname + ' uploaded as '+ data.name +'. JSON: ' + JSON.stringify(data) + '\n' + $scope.log;
|
||||
console.log($scope.form.pdf);
|
||||
$scope.form.pdf = angular.fromJson(angular.toJson(data));
|
||||
$scope.pdfLoading = false;
|
||||
|
||||
console.log($scope.log);
|
||||
console.log('$scope.pdf: '+$scope.form.pdf.name);
|
||||
if(!$scope.$$phase) {
|
||||
if(!$scope.$$phase){
|
||||
$scope.$apply();
|
||||
}
|
||||
}).error(function(err){
|
||||
|
@ -84,23 +83,20 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$
|
|||
if($scope.isNewForm){
|
||||
// Create new Form object
|
||||
var form = new Forms($scope.form);
|
||||
form.$save(function(response) {
|
||||
|
||||
$http.post('/forms', {form: $scope.form})
|
||||
.success(function(data, status, headers){
|
||||
console.log('form created');
|
||||
// console.log(response.pdf);
|
||||
|
||||
// Clear form fields
|
||||
$scope.form = {};
|
||||
|
||||
// Redirect after save
|
||||
$scope.goToWithId('viewForm', response._id);
|
||||
|
||||
}, function(errorResponse) {
|
||||
$scope.goToWithId('viewForm', $scope.form._id);
|
||||
}).error(function(errorResponse){
|
||||
console.log(errorResponse.data.message);
|
||||
$scope.error = errorResponse.data.message;
|
||||
});
|
||||
} else{
|
||||
console.log('update form');
|
||||
$scope.update();
|
||||
}
|
||||
};
|
||||
|
@ -108,14 +104,24 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$
|
|||
// Update existing Form
|
||||
$scope.update = function() {
|
||||
var form = new Forms($scope.form);
|
||||
form.$update(function(response) {
|
||||
console.log('form updated');
|
||||
$scope.goToWithId('viewForm', response._id);
|
||||
// $location.path('forms/' + response._id + '/admin');
|
||||
}, function(errorResponse) {
|
||||
console.log(errorResponse.data.message);
|
||||
$scope.error = errorResponse.data.message;
|
||||
console.log('update form');
|
||||
console.log($scope.form);
|
||||
|
||||
$http.put('/forms/'+$scope.form._id, {form: $scope.form})
|
||||
.success(function(data, status, headers){
|
||||
console.log('form updated successfully');
|
||||
$scope.goToWithId('viewForm', $scope.form._id);
|
||||
}).error(function(err){
|
||||
console.log('Error occured during form UPDATE.\n');
|
||||
console.log(err);
|
||||
});
|
||||
// form.$update({formId: $scope.form._id}, function(response) {
|
||||
// console.log('form successfully updated');
|
||||
// $scope.goToWithId('viewForm', response._id);
|
||||
// }, function(errorResponse) {
|
||||
// console.log(errorResponse.data.message);
|
||||
// $scope.error = errorResponse.data.message;
|
||||
// });
|
||||
};
|
||||
|
||||
//Populate AddField with all available form field types
|
||||
|
@ -152,7 +158,6 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$
|
|||
|
||||
// put newField into fields array
|
||||
$scope.form.form_fields.push(newField);
|
||||
// console.log($scope.form.form_fields);
|
||||
};
|
||||
|
||||
// deletes particular field on button click
|
||||
|
@ -229,4 +234,4 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$
|
|||
return false;
|
||||
};
|
||||
}
|
||||
]);
|
||||
]);
|
||||
|
|
|
@ -0,0 +1,242 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('forms').controller('EditFormController', ['$scope', '$state', '$rootScope', 'Upload', '$stateParams', 'FormFields', 'Forms', 'CurrentForm', '$modal', '$location', '$http',
|
||||
function ($scope, $state, $rootScope, Upload, $stateParams, FormFields, Forms, CurrentForm, $modal, $location, $http) {
|
||||
|
||||
$scope.isNewForm = false;
|
||||
$scope.pdfLoading = false;
|
||||
|
||||
$scope.form = {};
|
||||
$scope.log = '';
|
||||
var _current_upload = null;
|
||||
|
||||
// Get current form if it exists, or create new one
|
||||
if($stateParams.formId){
|
||||
Forms.get({ formId: $stateParams.formId}, function(form){
|
||||
$scope.form = angular.fromJson(angular.toJson(form));
|
||||
console.log($scope.form);
|
||||
});
|
||||
} else {
|
||||
$scope.form.form_fields = [];
|
||||
$scope.isNewForm = true;
|
||||
}
|
||||
|
||||
//PDF Functions
|
||||
$scope.cancelUpload = function(){
|
||||
_current_upload.abort();
|
||||
$scope.pdfLoading = false;
|
||||
$scope.removePDF();
|
||||
};
|
||||
|
||||
$scope.removePDF = function(){
|
||||
$scope.form.pdf = null;
|
||||
$scope.form.isGenerated = false;
|
||||
$scope.form.autofillPDFs = false;
|
||||
|
||||
console.log('form.pdf: '+$scope.form.pdf+' REMOVED');
|
||||
};
|
||||
|
||||
$scope.uploadPDF = function(files) {
|
||||
|
||||
if (files && files.length) {
|
||||
// for (var i = 0; i < files.length; i++) {
|
||||
var file = files[0];
|
||||
_current_upload = Upload.upload({
|
||||
url: '/upload/pdf',
|
||||
fields: {
|
||||
'user': $scope.user,
|
||||
'form': $scope.form
|
||||
},
|
||||
file: file
|
||||
}).progress(function (evt) {
|
||||
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
|
||||
$scope.log = 'progress: ' + progressPercentage + '% ' +
|
||||
evt.config.file.name + '\n' + $scope.log;
|
||||
$scope.pdfLoading = true;
|
||||
}).success(function (data, status, headers, config) {
|
||||
$scope.log = 'file ' + data.originalname + ' uploaded as '+ data.name +'. JSON: ' + JSON.stringify(data) + '\n' + $scope.log;
|
||||
console.log($scope.form.pdf);
|
||||
$scope.form.pdf = angular.fromJson(angular.toJson(data));
|
||||
$scope.pdfLoading = false;
|
||||
|
||||
console.log($scope.log);
|
||||
console.log('$scope.pdf: '+$scope.form.pdf.name);
|
||||
if(!$scope.$$phase){
|
||||
$scope.$apply();
|
||||
}
|
||||
}).error(function(err){
|
||||
$scope.pdfLoading = false;
|
||||
console.log('Error occured during upload.\n');
|
||||
console.log(err);
|
||||
});
|
||||
// }
|
||||
}
|
||||
};
|
||||
|
||||
$scope.goToWithId = function(route, id) {
|
||||
$state.go(route, {'formId': id}, {reload: true});
|
||||
};
|
||||
|
||||
// Create new Form
|
||||
$scope.createOrUpdate = function() {
|
||||
|
||||
if($scope.isNewForm){
|
||||
// Create new Form object
|
||||
var form = new Forms($scope.form);
|
||||
<<<<<<< HEAD
|
||||
form.$save(function(response) {
|
||||
|
||||
=======
|
||||
|
||||
$http.post('/forms', {form: $scope.form})
|
||||
.success(function(data, status, headers){
|
||||
>>>>>>> dev_working
|
||||
console.log('form created');
|
||||
|
||||
// Clear form fields
|
||||
$scope.form = {};
|
||||
// Redirect after save
|
||||
$scope.goToWithId('viewForm', response._id);
|
||||
}).error(function(err){
|
||||
console.log(errorResponse.data.message);
|
||||
$scope.error = errorResponse.data.message;
|
||||
});
|
||||
} else{
|
||||
$scope.update();
|
||||
}
|
||||
};
|
||||
|
||||
// Update existing Form
|
||||
$scope.update = function() {
|
||||
var form = new Forms($scope.form);
|
||||
console.log('update form');
|
||||
console.log($scope.form);
|
||||
|
||||
$http.put('/forms/'+$scope.form._id, {form: $scope.form})
|
||||
.success(function(data, status, headers){
|
||||
console.log('form updated successfully');
|
||||
$scope.goToWithId('viewForm', $scope.form._id);
|
||||
}).error(function(err){
|
||||
console.log('Error occured during form UPDATE.\n');
|
||||
console.log(err);
|
||||
});
|
||||
// form.$update({formId: $scope.form._id}, function(response) {
|
||||
// console.log('form successfully updated');
|
||||
// $scope.goToWithId('viewForm', response._id);
|
||||
// }, function(errorResponse) {
|
||||
// console.log(errorResponse.data.message);
|
||||
// $scope.error = errorResponse.data.message;
|
||||
// });
|
||||
};
|
||||
|
||||
//Populate AddField with all available form field types
|
||||
$scope.addField = {};
|
||||
$scope.addField.types = FormFields.fields;
|
||||
$scope.addField.new = $scope.addField.types[0].name;
|
||||
$scope.addField.lastAddedID = 0;
|
||||
|
||||
// preview form mode
|
||||
$scope.previewMode = false;
|
||||
|
||||
// previewForm - for preview purposes, form will be copied into this
|
||||
// otherwise, actual form might get manipulated in preview mode
|
||||
$scope.previewForm = {};
|
||||
|
||||
|
||||
// accordion settings
|
||||
$scope.accordion = {};
|
||||
$scope.accordion.oneAtATime = true;
|
||||
|
||||
// create new field button click
|
||||
$scope.addNewField = function(){
|
||||
|
||||
// incr field_id counter
|
||||
$scope.addField.lastAddedID++;
|
||||
|
||||
var newField = {
|
||||
'title' : 'New field - ' + ($scope.addField.lastAddedID),
|
||||
'fieldType' : $scope.addField.new,
|
||||
'fieldValue' : '',
|
||||
'required' : true,
|
||||
'disabled' : false
|
||||
};
|
||||
|
||||
// put newField into fields array
|
||||
$scope.form.form_fields.push(newField);
|
||||
};
|
||||
|
||||
// deletes particular field on button click
|
||||
$scope.deleteField = function (field_id){
|
||||
for(var i = 0; i < $scope.form.form_fields.length; i++){
|
||||
if($scope.form.form_fields[i].field_id === field_id){
|
||||
$scope.form.form_fields.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// add new option to the field
|
||||
$scope.addOption = function (field){
|
||||
if(!field.field_options)
|
||||
field.field_options = [];
|
||||
|
||||
var lastOptionID = 0;
|
||||
|
||||
if(field.field_options[field.field_options.length-1])
|
||||
lastOptionID = field.field_options[field.field_options.length-1].option_id;
|
||||
|
||||
// new option's id
|
||||
var option_id = lastOptionID + 1;
|
||||
|
||||
var newOption = {
|
||||
'option_id' : option_id,
|
||||
'option_title' : 'Option ' + option_id,
|
||||
'option_value' : option_id
|
||||
};
|
||||
|
||||
// put new option into field_options array
|
||||
field.field_options.push(newOption);
|
||||
};
|
||||
|
||||
// delete particular option
|
||||
$scope.deleteOption = function (field, option){
|
||||
for(var i = 0; i < field.field_options.length; i++){
|
||||
if(field.field_options[i].option_id === option.option_id){
|
||||
field.field_options.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// preview form
|
||||
$scope.previewOn = function(){
|
||||
if($scope.form.form_fields === null || $scope.form.form_fields.length === 0) {
|
||||
var title = 'Error';
|
||||
var msg = 'No fields added yet, please add fields to the form before preview.';
|
||||
var btns = [{result:'ok', label: 'OK', cssClass: 'btn-primary'}];
|
||||
|
||||
// $dialog.messageBox(title, msg, btns).open();
|
||||
}
|
||||
else {
|
||||
$scope.previewMode = !$scope.previewMode;
|
||||
$scope.form.submitted = false;
|
||||
angular.copy($scope.form, $scope.previewForm);
|
||||
}
|
||||
};
|
||||
|
||||
// hide preview form, go back to create mode
|
||||
$scope.previewOff = function(){
|
||||
$scope.previewMode = !$scope.previewMode;
|
||||
$scope.form.submitted = false;
|
||||
};
|
||||
|
||||
// decides whether field options block will be shown (true for dropdown and radio fields)
|
||||
$scope.showAddOptions = function (field){
|
||||
if(field.field_type === 'radio' || field.field_type === 'dropdown')
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
};
|
||||
}
|
||||
]);
|
|
@ -28,26 +28,14 @@ angular.module('forms').controller('ViewSubmissionController', ['$scope', '$stat
|
|||
|
||||
// Remove existing submission
|
||||
$scope.remove = function(submission) {
|
||||
if (submission) {
|
||||
submission.$remove();
|
||||
|
||||
$http.delete('/forms/'+$stateParams.formId+'submissions/'+$scope.submission._id).
|
||||
success(function(data, status, headers){
|
||||
console.log('submission deleted successfully');
|
||||
alert('submission deleted..');
|
||||
});
|
||||
|
||||
} else {
|
||||
$scope.submission.$remove(function() {
|
||||
console.log('remove');
|
||||
$state.path('submissions');
|
||||
$http.delete('/forms/'+$stateParams.formId+'/submissions/'+$scope.submission._id).
|
||||
success(function(data, status, headers){
|
||||
console.log('submission deleted successfully');
|
||||
alert('submission deleted..');
|
||||
});
|
||||
});
|
||||
if (!submission) {
|
||||
submission = $scope.submission;
|
||||
}
|
||||
$http.delete('/forms/'+$stateParams.formId+'/submissions/'+submission._id).
|
||||
success(function(data, status, headers){
|
||||
console.log('submission deleted successfully');
|
||||
alert('submission deleted..');
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -34,12 +34,12 @@ angular.module('forms').controller('ViewFormController', ['$scope', '$stateParam
|
|||
});
|
||||
}
|
||||
console.log($scope.submissions);
|
||||
}
|
||||
};
|
||||
|
||||
//hide submissions of Form
|
||||
$scope.hideSubmissions = function(){
|
||||
$scope.viewSubmissions = false;
|
||||
}
|
||||
};
|
||||
|
||||
// Return all user's Forms
|
||||
$scope.findAll = function() {
|
||||
|
@ -56,11 +56,10 @@ angular.module('forms').controller('ViewFormController', ['$scope', '$stateParam
|
|||
|
||||
// Remove existing Form
|
||||
$scope.remove = function() {
|
||||
console.log('hello');
|
||||
var form = CurrentForm.getForm()
|
||||
if(!form){
|
||||
form = $scope.form
|
||||
}
|
||||
|
||||
var form = CurrentForm.getForm();
|
||||
if(!form) form = $scope.form;
|
||||
|
||||
$http.delete('/forms/'+$scope.form._id)
|
||||
.success(function(data, status, headers){
|
||||
console.log('form deleted successfully');
|
||||
|
|
31
public/modules/forms/directives/table.client.directive.js
Normal file
31
public/modules/forms/directives/table.client.directive.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('forms').directive('tableDirective', ['$http', '$timeout', 'Auth',
|
||||
function ($http, $timeout, Auth) {
|
||||
return {
|
||||
controller: function($scope){
|
||||
|
||||
$scope.toggleChecker = function(checked) {
|
||||
var rows = $scope.gridOptions.$gridScope.renderedRows,
|
||||
allChecked = true;
|
||||
|
||||
for (var r = 0; r < rows.length; r++) {
|
||||
if (rows[r].entity.checker !== true) {
|
||||
allChecked = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$scope.gridOptions.$gridScope.checker = allChecked;
|
||||
};
|
||||
|
||||
},
|
||||
templateUrl: './modules/forms/views/directiveViews/table/table.html',
|
||||
restrict: 'E',
|
||||
scope: {
|
||||
rows:'=',
|
||||
extras:'=',
|
||||
}
|
||||
};
|
||||
}
|
||||
]);
|
|
@ -23,43 +23,3 @@ angular.module('users').config(['$httpProvider',
|
|||
};
|
||||
});
|
||||
}]);
|
||||
|
||||
|
||||
// Config HTTP Error Handling
|
||||
// angular.module('users').config(['$httpProvider',
|
||||
// function($httpProvider) {
|
||||
// // Set the httpProvider "not authorized" interceptor
|
||||
// $httpProvider.interceptors.push(['$q', '$location', 'Principal',
|
||||
// function($q, $state, Principal) {
|
||||
// return {
|
||||
// responseSuccess: function(response) {
|
||||
// Principal.identity().then(function(user){
|
||||
// console.log(user);
|
||||
// // $rootScope.user = user;
|
||||
// }, function(error){
|
||||
// console.log("Coudn't get current user. \n ERROR: "+error);
|
||||
// });
|
||||
|
||||
// },
|
||||
// responseError: function(rejection) {
|
||||
// switch (rejection.status) {
|
||||
// case 401:
|
||||
// // Deauthenticate the global user
|
||||
// Principal.authenticate(null);
|
||||
|
||||
// // Redirect to signin page
|
||||
// $location.path('/signin');
|
||||
// break;
|
||||
// case 403:
|
||||
// // Add unauthorized behaviour
|
||||
// break;
|
||||
// }
|
||||
>>>>>>> dev_working
|
||||
|
||||
return $q.reject(rejection);
|
||||
}
|
||||
};
|
||||
}
|
||||
]);
|
||||
}
|
||||
]);
|
|
@ -6,8 +6,6 @@ angular.module('users').controller('AuthenticationController', ['$scope', '$loca
|
|||
$scope = $rootScope;
|
||||
$scope.credentials = {};
|
||||
|
||||
// $scope.authentication = Principal;
|
||||
|
||||
// If user is signed in then redirect back home
|
||||
if ($scope.authentication.isAuthenticated()) $state.go('home');
|
||||
|
||||
|
|
|
@ -2,14 +2,10 @@
|
|||
|
||||
angular.module('users').controller('PasswordController', ['$scope', '$stateParams', '$state', 'User',
|
||||
function($scope, $stateParams, $state, User) {
|
||||
// $scope.authentication = Principal;
|
||||
|
||||
//If user is signed in then redirect back home
|
||||
if ($scope.authentication.isAuthenticated()) $state.go('home');
|
||||
|
||||
// Principal.identity().then(function(response){
|
||||
// $scope.authentication.user = response;
|
||||
|
||||
// Submit forgotten password account id
|
||||
$scope.askForPasswordReset = function() {
|
||||
User.askForPasswordReset($scope.credentials).then(
|
||||
|
@ -41,21 +37,6 @@ angular.module('users').controller('PasswordController', ['$scope', '$stateParam
|
|||
$scope.passwordDetails = null;
|
||||
}
|
||||
);
|
||||
// $scope.success = $scope.error = null;
|
||||
|
||||
// $http.post('/auth/reset/' + $stateParams.token, $scope.passwordDetails).success(function(response) {
|
||||
// // If successful show success message and clear form
|
||||
// $scope.passwordDetails = null;
|
||||
|
||||
// // Attach user profile
|
||||
// // Principal.user() = response;
|
||||
|
||||
// // And redirect to the index page
|
||||
// $state.go('reset-success');
|
||||
// }).error(function(response) {
|
||||
// $scope.error = response.message;
|
||||
// });
|
||||
};
|
||||
// });
|
||||
}
|
||||
]);
|
|
@ -1,47 +1,47 @@
|
|||
'use strict';
|
||||
// 'use strict';
|
||||
|
||||
angular.module('users').factory('Authorization', ['$rootScope', '$http', '$q', '$state', 'Principal',
|
||||
function($rootScope, $http, $q, $state, Principal) {
|
||||
var service = {
|
||||
authorize: function(){
|
||||
var deferred = $q.defer();
|
||||
$http.get('/user/me').success(function(response) {
|
||||
// angular.module('users').factory('Authorization', ['$rootScope', '$http', '$q', '$state', 'Principal',
|
||||
// function($rootScope, $http, $q, $state, Principal) {
|
||||
// var service = {
|
||||
// authorize: function(){
|
||||
// var deferred = $q.defer();
|
||||
// $http.get('/user/me').success(function(response) {
|
||||
|
||||
|
||||
//user is logged in
|
||||
if(response.data !== null){
|
||||
deferred.resolve();
|
||||
}else {
|
||||
$rootScope.message = 'You need to log in.';
|
||||
deferred.reject();
|
||||
$state.go('/login');
|
||||
}
|
||||
// //user is logged in
|
||||
// if(response.data !== null){
|
||||
// deferred.resolve();
|
||||
// }else {
|
||||
// $rootScope.message = 'You need to log in.';
|
||||
// deferred.reject();
|
||||
// $state.go('/login');
|
||||
// }
|
||||
|
||||
});
|
||||
return deferred.promise();
|
||||
}
|
||||
};
|
||||
return service;
|
||||
// this.authorize = function() {
|
||||
// return Principal.identity().then(function(){
|
||||
// var isAuthenticated = Principal.isAuthenticated();
|
||||
// if( angular.isDefined($rootScope.toState.data) ){
|
||||
// // if ($rootScope.toState.data.roles && $rootScope.toState.data.roles.length > 0 && !principal.isInAnyRole($rootScope.toState.data.roles)) {
|
||||
// if (!isAuthenticated){ //$location.path('/access_denied'); // user is signed in but not authorized for desired state
|
||||
// // console.log('isAuthenticated: '+isAuthenticated);
|
||||
// });
|
||||
// return deferred.promise();
|
||||
// }
|
||||
// };
|
||||
// return service;
|
||||
// // this.authorize = function() {
|
||||
// // return Principal.identity().then(function(){
|
||||
// // var isAuthenticated = Principal.isAuthenticated();
|
||||
// // if( angular.isDefined($rootScope.toState.data) ){
|
||||
// // // if ($rootScope.toState.data.roles && $rootScope.toState.data.roles.length > 0 && !principal.isInAnyRole($rootScope.toState.data.roles)) {
|
||||
// // if (!isAuthenticated){ //$location.path('/access_denied'); // user is signed in but not authorized for desired state
|
||||
// // // console.log('isAuthenticated: '+isAuthenticated);
|
||||
|
||||
// // else {
|
||||
// // user is not authenticated. so the state they wanted before you
|
||||
// // send them to the signin state, so you can return them when you're done
|
||||
// $rootScope.returnToState = $rootScope.toState;
|
||||
// $rootScope.returnToStateParams = $rootScope.toStateParams;
|
||||
// // // else {
|
||||
// // // user is not authenticated. so the state they wanted before you
|
||||
// // // send them to the signin state, so you can return them when you're done
|
||||
// // $rootScope.returnToState = $rootScope.toState;
|
||||
// // $rootScope.returnToStateParams = $rootScope.toStateParams;
|
||||
|
||||
// // now, send them to the signin state so they can log in
|
||||
// $location.path('/signin');
|
||||
// }
|
||||
// // }
|
||||
// }
|
||||
// });
|
||||
// };
|
||||
}
|
||||
]);
|
||||
// // // now, send them to the signin state so they can log in
|
||||
// // $location.path('/signin');
|
||||
// // }
|
||||
// // // }
|
||||
// // }
|
||||
// // });
|
||||
// // };
|
||||
// }
|
||||
// ]);
|
|
@ -1,202 +1,202 @@
|
|||
'use strict';
|
||||
// 'use strict';
|
||||
|
||||
angular.module('users').factory('AuthenticationService', function($http, $timeout, $q) {
|
||||
var error;
|
||||
var service = {
|
||||
// Information about the current user
|
||||
currentUser: null,
|
||||
// angular.module('users').factory('AuthenticationService', function($http, $timeout, $q) {
|
||||
// var error;
|
||||
// var service = {
|
||||
// // Information about the current user
|
||||
// currentUser: null,
|
||||
|
||||
login: function(credentials) {
|
||||
var login = $http.post('/auth/signin', credentials);
|
||||
login.success(function(data) {
|
||||
service.currentUser = data.user;
|
||||
// $flash.clear();
|
||||
}).error(function(error) {
|
||||
error = error.error ? error.error : error;
|
||||
console.error(error.message || error);
|
||||
});
|
||||
return login;
|
||||
},
|
||||
|
||||
logout: function() {
|
||||
var logout = $http.get('/auth/logout');
|
||||
logout.success(function() {
|
||||
service.currentUser = null;
|
||||
console.log("You've successfully logged out");
|
||||
});
|
||||
return logout;
|
||||
},
|
||||
|
||||
signup: function(credentials) {
|
||||
var signup = $http.post('/auth/signup', credentials)
|
||||
signup.success(function(response) {
|
||||
console.log("You've successfully created an account");
|
||||
}).error(function(response) {
|
||||
error = error.error ? error.error : error;
|
||||
console.error(error.message || error);
|
||||
});
|
||||
|
||||
return signup;
|
||||
},
|
||||
|
||||
// Ask the backend to see if a user is already authenticated -
|
||||
// this may be from a previous session.
|
||||
identity: function() {
|
||||
if (service.isAuthenticated()) {
|
||||
return $q.when(service.currentUser);
|
||||
} else {
|
||||
return $http.get('/user/me').then(function(response) {
|
||||
service.currentUser = response.data.user;
|
||||
return service.currentUser;
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// Is the current user authenticated?
|
||||
isAuthenticated: function() {
|
||||
return !!service.currentUser;
|
||||
},
|
||||
|
||||
isInRole: function(role) {
|
||||
return service.isAuthenticated() (service.currentUser.roles.indexOf(role) !== -1);
|
||||
},
|
||||
|
||||
isInAnyRole: function(roles) {
|
||||
if ( !service.isAuthenticated() || !service.currentUser.roles) return false;
|
||||
var roles = service.currentUser.roles;
|
||||
|
||||
for (var i = 0; i < roles.length; i++) {
|
||||
if (this.isInRole(roles[i])) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
};
|
||||
return service;
|
||||
});
|
||||
|
||||
// .factory('Principal', ['$window', '$http', '$q', '$timeout', '$state',
|
||||
// function($window, $http, $q, $timeout, $state) {
|
||||
// var _identity,
|
||||
// _authenticated = false;
|
||||
|
||||
// return {
|
||||
// isIdentityResolved: function() {
|
||||
// return angular.isDefined(_identity);
|
||||
// },
|
||||
// isAuthenticated: function() {
|
||||
// return _authenticated;
|
||||
// },
|
||||
// isInRole: function(role) {
|
||||
// if (!_authenticated || !_identity.roles) return false;
|
||||
|
||||
// return _identity.roles.indexOf(role) !== -1;
|
||||
// },
|
||||
// isInAnyRole: function(roles) {
|
||||
// if (!_authenticated || !_identity.roles) return false;
|
||||
|
||||
// for (var i = 0; i < roles.length; i++) {
|
||||
// if (this.isInRole(roles[i])) return true;
|
||||
// }
|
||||
|
||||
// return false;
|
||||
// },
|
||||
// authenticate: function(user) {
|
||||
// _identity = user;
|
||||
// _authenticated = (user !== null);
|
||||
|
||||
// // for this demo, we'll store the identity in localStorage. For you, it could be a cookie, sessionStorage, whatever
|
||||
// if (user) $window.user = user;
|
||||
// else $window.user = null;
|
||||
// },
|
||||
// signin: function(credentials) {
|
||||
|
||||
// var deferred = $q.defer();
|
||||
// var self = this;
|
||||
// $http.post('/auth/signin', credentials).success(function(response) {
|
||||
// // If successful we assign the response to the global user model
|
||||
// self.authenticate(response);
|
||||
// deferred.resolve(response);
|
||||
// }).error(function(response) {
|
||||
// _authenticated = false;
|
||||
// deferred.reject({ error: response.message });
|
||||
// login: function(credentials) {
|
||||
// var login = $http.post('/auth/signin', credentials);
|
||||
// login.success(function(data) {
|
||||
// service.currentUser = data.user;
|
||||
// // $flash.clear();
|
||||
// }).error(function(error) {
|
||||
// error = error.error ? error.error : error;
|
||||
// console.error(error.message || error);
|
||||
// });
|
||||
// return deferred.promise;
|
||||
// return login;
|
||||
// },
|
||||
|
||||
// logout: function() {
|
||||
// var logout = $http.get('/auth/logout');
|
||||
// logout.success(function() {
|
||||
// service.currentUser = null;
|
||||
// console.log("You've successfully logged out");
|
||||
// });
|
||||
// return logout;
|
||||
// },
|
||||
|
||||
// signup: function(credentials) {
|
||||
|
||||
// var deferred = $q.defer();
|
||||
|
||||
// $http.post('/auth/signup', credentials).success(function(response) {
|
||||
// // If successful we assign the response to the global user model
|
||||
// deferred.resolve(response);
|
||||
// var signup = $http.post('/auth/signup', credentials)
|
||||
// signup.success(function(response) {
|
||||
// console.log("You've successfully created an account");
|
||||
// }).error(function(response) {
|
||||
|
||||
// deferred.reject({ error: response.message });
|
||||
// error = error.error ? error.error : error;
|
||||
// console.error(error.message || error);
|
||||
// });
|
||||
|
||||
// return deferred.promise;
|
||||
// return signup;
|
||||
// },
|
||||
// signout: function() {
|
||||
// var deferred = $q.defer();
|
||||
// $http.get('/auth/signout').success(function(response) {
|
||||
// // If successful we assign the response to the global user model
|
||||
// deferred.resolve({});
|
||||
// }).error(function(response) {
|
||||
// deferred.reject({ error: response.message });
|
||||
// });
|
||||
|
||||
// _authenticated = false;
|
||||
// _identity = undefined;
|
||||
|
||||
// return deferred.promise;
|
||||
// },
|
||||
// // Ask the backend to see if a user is already authenticated -
|
||||
// // this may be from a previous session.
|
||||
// identity: function() {
|
||||
// var self = this;
|
||||
|
||||
// var deferred = $q.defer();
|
||||
|
||||
// // check and see if we have retrieved the user data from the server. if we have, reuse it by immediately resolving
|
||||
// if (angular.isDefined(_identity)) {
|
||||
|
||||
// deferred.resolve(_identity);
|
||||
// return deferred.promise;
|
||||
// }else if($window.user){
|
||||
// // console.log($window.user);
|
||||
// // self.authenticate($window.user);
|
||||
// // var user = $window.user;
|
||||
// _identity = $window.user;
|
||||
// self.authenticate(_identity);
|
||||
// deferred.resolve(_identity);
|
||||
|
||||
// return deferred.promise;
|
||||
// }else {
|
||||
|
||||
// // otherwise, retrieve the user data from the server, update the user object, and then resolve.
|
||||
// $http.get('/users/me', { ignoreErrors: true })
|
||||
// .success(function(response) {
|
||||
// self.authenticate(response);
|
||||
// $window.user = response;
|
||||
// deferred.resolve(_identity);
|
||||
// })
|
||||
// .error(function() {
|
||||
// _identity = null;
|
||||
// _authenticated = false;
|
||||
// $window.user = null;
|
||||
// $state.path('signin');
|
||||
// deferred.resolve(_identity);
|
||||
// });
|
||||
|
||||
// return deferred.promise;
|
||||
// }
|
||||
// if (service.isAuthenticated()) {
|
||||
// return $q.when(service.currentUser);
|
||||
// } else {
|
||||
// return $http.get('/user/me').then(function(response) {
|
||||
// service.currentUser = response.data.user;
|
||||
// return service.currentUser;
|
||||
// });
|
||||
// }
|
||||
// },
|
||||
// getUser: function(){
|
||||
// this.identity(false).then( function(user){
|
||||
// return user;
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
// // Is the current user authenticated?
|
||||
// isAuthenticated: function() {
|
||||
// return !!service.currentUser;
|
||||
// },
|
||||
|
||||
// isInRole: function(role) {
|
||||
// return service.isAuthenticated() (service.currentUser.roles.indexOf(role) !== -1);
|
||||
// },
|
||||
|
||||
// isInAnyRole: function(roles) {
|
||||
// if ( !service.isAuthenticated() || !service.currentUser.roles) return false;
|
||||
// var roles = service.currentUser.roles;
|
||||
|
||||
// for (var i = 0; i < roles.length; i++) {
|
||||
// if (this.isInRole(roles[i])) return true;
|
||||
// }
|
||||
|
||||
// return false;
|
||||
// },
|
||||
|
||||
// };
|
||||
// return service;
|
||||
// });
|
||||
|
||||
// // .factory('Principal', ['$window', '$http', '$q', '$timeout', '$state',
|
||||
// // function($window, $http, $q, $timeout, $state) {
|
||||
// // var _identity,
|
||||
// // _authenticated = false;
|
||||
|
||||
// // return {
|
||||
// // isIdentityResolved: function() {
|
||||
// // return angular.isDefined(_identity);
|
||||
// // },
|
||||
// // isAuthenticated: function() {
|
||||
// // return _authenticated;
|
||||
// // },
|
||||
// // isInRole: function(role) {
|
||||
// // if (!_authenticated || !_identity.roles) return false;
|
||||
|
||||
// // return _identity.roles.indexOf(role) !== -1;
|
||||
// // },
|
||||
// // isInAnyRole: function(roles) {
|
||||
// // if (!_authenticated || !_identity.roles) return false;
|
||||
|
||||
// // for (var i = 0; i < roles.length; i++) {
|
||||
// // if (this.isInRole(roles[i])) return true;
|
||||
// // }
|
||||
|
||||
// // return false;
|
||||
// // },
|
||||
// // authenticate: function(user) {
|
||||
// // _identity = user;
|
||||
// // _authenticated = (user !== null);
|
||||
|
||||
// // // for this demo, we'll store the identity in localStorage. For you, it could be a cookie, sessionStorage, whatever
|
||||
// // if (user) $window.user = user;
|
||||
// // else $window.user = null;
|
||||
// // },
|
||||
// // signin: function(credentials) {
|
||||
|
||||
// // var deferred = $q.defer();
|
||||
// // var self = this;
|
||||
// // $http.post('/auth/signin', credentials).success(function(response) {
|
||||
// // // If successful we assign the response to the global user model
|
||||
// // self.authenticate(response);
|
||||
// // deferred.resolve(response);
|
||||
// // }).error(function(response) {
|
||||
// // _authenticated = false;
|
||||
// // deferred.reject({ error: response.message });
|
||||
// // });
|
||||
// // return deferred.promise;
|
||||
// // },
|
||||
// // signup: function(credentials) {
|
||||
|
||||
// // var deferred = $q.defer();
|
||||
|
||||
// // $http.post('/auth/signup', credentials).success(function(response) {
|
||||
// // // If successful we assign the response to the global user model
|
||||
// // deferred.resolve(response);
|
||||
// // }).error(function(response) {
|
||||
|
||||
// // deferred.reject({ error: response.message });
|
||||
// // });
|
||||
|
||||
// // return deferred.promise;
|
||||
// // },
|
||||
// // signout: function() {
|
||||
// // var deferred = $q.defer();
|
||||
// // $http.get('/auth/signout').success(function(response) {
|
||||
// // // If successful we assign the response to the global user model
|
||||
// // deferred.resolve({});
|
||||
// // }).error(function(response) {
|
||||
// // deferred.reject({ error: response.message });
|
||||
// // });
|
||||
|
||||
// // _authenticated = false;
|
||||
// // _identity = undefined;
|
||||
|
||||
// // return deferred.promise;
|
||||
// // },
|
||||
// // identity: function() {
|
||||
// // var self = this;
|
||||
|
||||
// // var deferred = $q.defer();
|
||||
|
||||
// // // check and see if we have retrieved the user data from the server. if we have, reuse it by immediately resolving
|
||||
// // if (angular.isDefined(_identity)) {
|
||||
|
||||
// // deferred.resolve(_identity);
|
||||
// // return deferred.promise;
|
||||
// // }else if($window.user){
|
||||
// // // console.log($window.user);
|
||||
// // // self.authenticate($window.user);
|
||||
// // // var user = $window.user;
|
||||
// // _identity = $window.user;
|
||||
// // self.authenticate(_identity);
|
||||
// // deferred.resolve(_identity);
|
||||
|
||||
// // return deferred.promise;
|
||||
// // }else {
|
||||
|
||||
// // // otherwise, retrieve the user data from the server, update the user object, and then resolve.
|
||||
// // $http.get('/users/me', { ignoreErrors: true })
|
||||
// // .success(function(response) {
|
||||
// // self.authenticate(response);
|
||||
// // $window.user = response;
|
||||
// // deferred.resolve(_identity);
|
||||
// // })
|
||||
// // .error(function() {
|
||||
// // _identity = null;
|
||||
// // _authenticated = false;
|
||||
// // $window.user = null;
|
||||
// // $state.path('signin');
|
||||
// // deferred.resolve(_identity);
|
||||
// // });
|
||||
|
||||
// // return deferred.promise;
|
||||
// // }
|
||||
// // },
|
||||
// // getUser: function(){
|
||||
// // this.identity(false).then( function(user){
|
||||
// // return user;
|
||||
// // });
|
||||
// // }
|
||||
// // };
|
||||
|
||||
// }
|
||||
// ]);
|
||||
// // }
|
||||
// // ]);
|
||||
|
|
Loading…
Reference in a new issue