Пример #1
0
function signup (profile, accessToken, fn) {
  var user = new User()

  user.firstName = profile._json.first_name
  user.lastName = profile._json.last_name
  user.profiles.facebook = profile._json
  user.email = profile.emails[0].value
  user.emailValidated = true
  user.profilePictureUrl = '//graph.facebook.com/' + profile._json.id + '/picture'

  user.save(function (err) {
    return fn(err, user)
  })
}
Пример #2
0
export const validateUser = async (model) => {
  if (!model.name){
    throw new Error("name_required");
  }

  if (!model.email){
    throw new Error("email_required");
  }

  // Check email existing
  var exists = await User.findOne({email: model.email}).exec();
  if(exists && exists.email !== model.email) {
    throw new Error("email_existing");
  }
}
Пример #3
0
    function(accessToken, refreshToken, profile, done) { // eslint-disable-line no-unused-vars
      User.findByProvider(profile, function (err, user) {
        if (err) return done(err);

        if (!user) return signup(profile, accessToken, done);

        var email = profile._json.email;
        if (user.email !== email) {
          user.set('email', email);
          user.set('profiles.gitlab.email', email);
        }

        user.isModified() ? user.save(done) : done(null, user); // eslint-disable-line no-unused-expressions
      });
    }
Пример #4
0
  comment.save(function (err) {
    if (err) {
      log('Found error %s', err);
      return fn(err);
    };
    User.populate(comment, { path: 'author' }, function(err) {
      if (err) {
        log('Found error %s', err)
        return fn(err);
      };

      log('Delivering comment %j', comment.id);
      fn(null, comment);
    });
  });
Пример #5
0
exports.get = function get (id, fn) {
  log('Looking for User %s', id)

  User
    .findById(id)
    .exec(function (err, user) {
      if (err) {
        log('Found error %j', err)
        return fn(err)
      }

      log('Delivering User %j', user)
      fn(null, user)
    })
  return this
}
Пример #6
0
exports.findEmailValidated = function findEmailValidated (fn) {
  log('Searching for email validated users matching')

  User.find({ emailValidated: true })
    .exec(function (err, users) {
      if (err) {
        log('Found error: %j', err)
        return fn(err)
      }

      log('Found %d email validated users', users.length)
      fn(null, users)
    })

  return this
}
Пример #7
0
exports.all = function all(fn) {
  log('Looking for all users.');

  User
  .find()
  .sort('lastName')
  .exec(function (err, users) {
    if (err) {
      log('Found error %j', err);
      return fn(err);
    };

    log('Delivering all users %j', pluck(users, 'id'));
    fn(null, users);
  });
  return this;
}
Пример #8
0
exports.all = function all (fn) {
  log('Looking for all users.')

  User
    .find()
    .sort('-createdAt')
    .exec(function (err, users) {
      if (err) {
        log('Found error %j', err)
        return fn(err)
      }

      log('Delivering all users %j', pluck(users, 'id'))
      fn(null, users)
    })
  return this
}
Пример #9
0
  .exec(function(err, comments) {
    if (err) {
      log('Found error %j', err);
      return fn(err);
    }

    var opts = { path: 'author', select: fields };

    User.populate(comments, opts, function(_err, _comments) {
      if (_err) {
        log('Found error %j', _err);
        return fn(_err);
      }

      log('Delivering comments %j', pluck(_comments, 'id'));
      fn(null, _comments);
    });
  });
Пример #10
0
  .exec(function(err, comments) {
    if (err) {
      log('Found error %j', err);
      return fn(err);
    };

    var opts = { path: 'author', select: 'id firstName lastName fullName email avatar' };

    User.populate(comments, opts, function(err, comments) {
      if (err) {
        log('Found error %j', err);
        return fn(err);
      };

      log('Delivering comments %j', pluck(comments, 'id'));
      fn(null, comments);
    });
  });
Пример #11
0
export const isOwnerDashboard = async ({user, dashboard}, res, next) => {
  if (!dashboard.owner) {
    return res.status(403).send('This dashboard cannot be removed because it has no owner.');
  }

  if (dashboard.owner._id.toString() !== user._id.toString()) {
    return res.status(403).send('Only Owner can remove this dashboard.');
  }

  if (dashboard.projectsCount > 0) {
    return res.status(403).send('Only Dashboards with no projects can be removed.');
  }

  const count = User.count({ admin_in: dashboard.domain });
  if (count > 1){
    return res.status(403).send('Only Dashboards with ONE admin can be removed.');
  }
  next();
};
Пример #12
0
exports.search = function search (query, fn) {
  log('Searching for users matching %s', query)

  User
    .find({$text: {$search: query }}, {score: {$meta: 'textScore'}})
    .sort({score: {$meta: 'textScore'}})
    .limit(10)
    .exec(function (err, users) {
      if (err) {
        log('Found error: %j', err)
        return fn(err)
      }

      log('Found users %j for hash %s', pluck(users, 'id'), query)
      fn(null, users)
    })

  return this
}
Пример #13
0
      function (accessToken, refreshToken, profile, done) {
        User.findByProvider(profile, function (err, user) {
          if (err) return done(err)

          if (!user) return signup(profile, accessToken, done)

          var email = profile._json.email
          if (user.email !== email) {
            user.set('email', email)
            user.set('profiles.facebook.email', email)
          }

          if (user.profiles.facebook.deauthorized) {
            user.set('profiles.facebook.deauthorized')
          }

          user.isModified() ? user.save(done) : done(null, user)
        })
      })
Пример #14
0
  Comment.findById(id, function(err, comment) {
    if (err) return log('Found error %s', err), fn(err);

    User.populate(comment, { path: 'author' }, function (err, comment) {
      if (err) return log('Found error %s', err), fn(err);

      if (comment.author.id == citizen.id) {
        log('Author %s tried to vote their own comment %s', citizen.id, comment.id);
        return fn(t('comments.score.not-allowed'), comment);
      }

      log('Downvoting comment %s', comment.id);
      comment.vote(citizen, 'negative', function(err) {
        if (err) return log('Found error %s', err), fn(err);

        log('Delivering comment %s', comment.id);
        fn(null, comment);
      });
    });
  });
Пример #15
0
exports.getByEmail = function search(email, fn) {
  log('Searching for User with email %s', email);

  User.findOne({email : email})
  .select('firstName lastName fullName email profilePictureUrl notifications emailValidated')
  .exec(function(err, user) {
    if (err) {
      return log('Found error: %j', err), fn(err);
    }

    if (!user) {
      return log('User not found for email %s', email), fn(null, false);
    };

    log('Found User %j for email %s', user.id, email)
    fn(null, user);
  });

  return this;
};
Пример #16
0
  .exec(function(err, comment) {
    if (err) {
      log('Found error %j', err);
      return fn(err);
    };

    var opts = { path: 'replies.author', select: 'id firstName lastName fullName email avatar' };

    User.populate(comment, opts, function(err, comment) {
      if (err) {
        log('Found error %j', err);
        return fn(err);
      };

      var replies = comment && comment.replies ? comment.replies : [];

      log('Delivering replies %j', pluck(replies, 'id'));
      fn(null, replies);
    });
  });
Пример #17
0
  .exec(function(err, comment) {
    if (err) {
      log('Found error %j', err);
      return fn(err);
    }

    var opts = { path: 'replies.author', select: fields };

    User.populate(comment, opts, function (_err, _comment) {
      if (_err) {
        log('Found error %j', _err);
        return fn(_err);
      }

      var _replies = _comment && _comment.replies ? _comment.replies : [];

      log('Delivering replies %j', pluck(_replies, 'id'));
      fn(null, _replies);
    });
  });
Пример #18
0
exports.search = function search(query, fn) {
  var hashQuery = new RegExp('.*' + query + '.*','i');

  log('Searching for users matching %s', hashQuery);


  User.find()
  .or([{firstName: hashQuery}, {lastName: hashQuery}])
  .select('firstName lastName fullName email profilePictureUrl notifications')
  .exec(function(err, users) {
    if (err) {
      log('Found error: %j', err);
      return fn(err);
    }

    log('Found users %j for hash %s', pluck(users, 'id'), hashQuery)
    fn(null, users);
  });

  return this;
};
Пример #19
0
  Comment.findById(id, function (findErr, comment) {
    if (findErr) {
      log('Found error %s', findErr);
      return fn(findErr);
    }

    User.populate(comment, { path: 'author' }, function (populateErr, populatedComment) {
      if (populateErr) {
        log('Found error %s', populateErr);
        return fn(populateErr);
      }

      if (populatedComment.author.id == user.id) {
        log('Author %s tried to vote their own comment %s', user.id, populatedComment.id);
        return fn(t('comments.score.not-allowed'), populatedComment);
      }

      log('Downvoting comment %s', populatedComment.id);
      populatedComment.vote(user, 'negative', function (voteErr) {
        if (voteErr) {
          log('Found error %s', voteErr);
          return fn(voteErr);
        }

        var eventName = 'comment-downvote';
        notifier.notify(eventName)
          .to(populatedComment.author.email)
          .withData({ comment: populatedComment, user: user })
          .send(function (notifyErr) {
            if (notifyErr) {
              log('Error when sending notification for event %s: %j', eventName, notifyErr);
              return fn(notifyErr);
            }

            log('Delivering comment %s', populatedComment.id);
            fn(null, populatedComment);
          });
      });
    });
  });
Пример #20
0
exports.decodeToken = function decodeToken(token, secret, cb) {
  try {
    log('Attempting to decode token...');
    var decoded = jwt.decode(token, secret);
    if (decoded.exp <= Date.now()) {
      log('Access token has expired');
      return cb(new Error('Access token has expired'));
    }

    var User = require('lib/models').User;
    User.findByEmail(decoded.iss, function(err, user) {
      if (err) log('Token has been decoded, but user fetching failed with the following error: %s', err);
      if (!user) return log('Token has been decoded, but user was not found'), cb(new Error('No user for token %s', token));

      log('Token decoded successfully');
      return cb(err, user);
    });
  } catch (err) {
    log('Cannot decode token: %s', err);
    return cb(err);
  }
};
Пример #21
0
  const localNewToken = async (req, res, next) => {
    try {
      let email = req.body.email.trim().toLowerCase();
      console.log('NEW TOKEN PASS FOR USER', email);
      let user = await User.findOne({email: email}).exec();
      console.log('USER', user.toString());

      if(!user) {
        req.flash('error', 'Not user found for this email!');
        console.log('ERROR RETRIEVING PASSWORD, Not user found');
        return res.redirect('/login');
      }

      // Generate change password token
      let token = await crypto.randomBytes(20);
      if(!token) {
        req.flash('error', 'Sorry, internal server error while generating token');
        res.redirect('/login');
      }
      user.resetPasswordToken = token.toString('hex');
      user.resetPasswordExpires = Date.now() + (3600000 * 12); // 12 hours
      await user.save();
      bus.emit('user_notification', {
        type: 'new_password',
        user: user,
        token: user.resetPasswordToken
      });

      req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.')
      return res.redirect('/login');

      next();

    } catch(err) {
        req.flash('error', 'Sorry, server error! ' + err);
        console.log('ERROR RETRIEVING PASSWORD, Server error', err);
        return res.redirect('/login');
    }
  };
Пример #22
0
export const createDashboard = async (req, res, next) => {
  try {
    const dashboard = await Dashboard.findOne({domain: req.body.domain}).exec();
    if(dashboard) throw new Error();
  } catch(err) {
    return res.status(409).send({ error: "subdomain_inuse" });
  }

  const newDashboard = new Dashboard({
    domain: req.body.domain,
    owner: req.user._id
  });

  try {
    const savedDashboard = await newDashboard.save();
    const admin = await User.findById(req.user.id).exec();
    admin.admin_in.push(req.body.domain);
    await admin.save();
    req.dashboard = savedDashboard;
    next();
  } catch(err) {
    next(err);
  }
};
Пример #23
0
passport.deserializeUser((_id, done) => User.findById(_id, done));