Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var scrypt = require('scrypt');
- var Promise = require('bluebird');
- var personalRole = require('../constants/Role').personal;
- var validatePassword = require('password-strength');
- validatePassword.min = 8;
- module.exports = {
- attributes: {
- password: {
- set: function(password) {
- var params = scrypt.paramsSync(0.1);
- var crypted = scrypt.kdfSync(password, params).toString('base64');
- this.setDataValue('password', crypted);
- }
- },
- avatar: {
- set: function(avatar) {
- return this.setDataValue('avatar', JSON.stringify(avatar));
- },
- get: function() {
- var avatar = this.getDataValue('avatar');
- var structure = { large: '', medium: '', small: '', thumbnail: '' };
- return avatar ? JSON.parse(avatar) : structure;
- }
- }
- },
- options: {
- hooks: {
- afterCreate: function(user, opts, done) {
- var data = {
- name: 'My Workspace',
- owner_id: user.id
- };
- return Company.create(data).bind({}).then(function(company) {
- this.company_id = company.id;
- return user.update({ active_company: company.id });
- }).then(function() {
- personalRole.company_id = this.company_id;
- return Role.create(personalRole);
- }).then(function(role) {
- var member = {
- company_id: this.company_id,
- role_id: role.id,
- user_id: user.id
- };
- return CompanyMember.create(member);
- }).then(function() {
- // Here should be included all the new users activities
- var activity = {
- user_id: user.id,
- activities: [ 'homeSlider' ]
- };
- return PendingActivity.create(activity);
- }).then(function() {
- return done();
- });
- }
- },
- instanceMethods: {
- verifyPassword: function(testPassword) {
- if (!testPassword) {
- return false;
- }
- var passwordBuffer = new Buffer(this.getDataValue('password'), 'base64');
- return scrypt.verifyKdfSync(passwordBuffer, testPassword);
- },
- full_name: function() {
- return this.first_name + ' ' + this.last_name;
- },
- summary: function() {
- var self = this;
- return new Promise(function(resolve, reject) {
- var query = {
- include: [ {
- model: Subscription,
- required: true
- } ]
- };
- self.getCompany(query).bind({}).then(function(company) {
- this.company = company;
- return self.getRole();
- }).then(function(role) {
- this.role = role.get({ plain: true });
- return self.getSession();
- }).then(function(session) {
- var company = this.company;
- var roleOmits = [
- 'id',
- 'name',
- 'company_id',
- 'tags',
- 'created_at',
- 'updated_at'
- ];
- var response = {
- id: self.id,
- firstName: self.first_name,
- lastName: self.last_name,
- email: self.email,
- avatar: self.avatar,
- terms: self.terms,
- status: company.status,
- token: '',
- subscription: {
- no_matters: company.Subscription.no_matters,
- plan_name: company.Subscription.plan_name,
- plan_code: company.Subscription.plan_code,
- expiration_date: company.Subscription.expiration_date
- },
- company: {
- id: company.id,
- name: company.name,
- enterprise: company.enterprise
- },
- privileges: _.omit(this.role, roleOmits)
- };
- if (session) {
- response.token = session.token;
- }
- return resolve(response);
- }).catch(reject);
- });
- },
- getSubscription: function() {
- return this.getCompany().then(function(company) {
- return company.getSubscription();
- });
- },
- getBillingInformation: function() {
- return this.getCompany().then(function(company) {
- return company.getBillingInformation();
- });
- },
- getCompany: function(query) {
- var extendedQuery = _.extend({ where: { id: this.active_company } }, query);
- return Company.findOne(extendedQuery);
- },
- getRole: function(query) {
- var basicQuery = {
- where: {
- user_id: this.id,
- company_id: this.active_company
- },
- include: [ {
- model: Role
- } ]
- };
- var extendedQuery = _.extend(basicQuery, query);
- return CompanyMember.findOne(extendedQuery).then(function(instance) {
- if (!instance) {
- var error = {
- errors: [ {
- path: 'User',
- message: 'The user has no role within the company'
- } ]
- };
- return Promise.reject(error);
- }
- return Promise.resolve(instance.Role);
- });
- }
- },
- classMethods: {
- updatePassword: function(email, passwordData) {
- return new Promise(function(resolve, reject) {
- if (!passwordData.password) {
- var err = {
- errors: [ {
- path: 'password',
- message: 'Must be present'
- } ]
- };
- return reject(err);
- } else if (passwordData.password !== passwordData.confirm_password) {
- var err = {
- errors: [ {
- path: 'confirm_password',
- message: 'Passwords don\'t match'
- } ]
- };
- return reject(err);
- }
- var validation = validatePassword(passwordData.password);
- if (validation.strength === 'simple') {
- var err = {
- errors: [ {
- path: 'password',
- message: validation.hint || 'It\'s too simple'
- } ]
- };
- return reject(err);
- }
- var query = { where: { email: { $iLike: email } }, returning: true };
- return User.update(passwordData, query).then(function(result) {
- var affectedRows = result[1];
- var user = affectedRows[0];
- return resolve(user);
- }).catch(reject);
- });
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement