<commit_before>app = require '../config/app'
[Person, Team, Vote] = (app.db.model m for m in ['Person', 'Team', 'Vote'])

ensureAuth = (req, res, next) ->
  return res.redirect "/login?returnTo=#{encodeURIComponent(req.url)}" unless req.loggedIn
  next()

module.exports =
  ensureAuth: ensureAuth

  ensureAccess: (req, res, next) ->
    ensureAuth req, res, ->
      unless req.user.admin
        return next 401 if req.person? and req.person.id isnt req.user.id
        return next 401 if req.team? and not req.team.includes(req.user, req.session.team)
        return next 401 if req.vote? and req.vote.personId.toString() isnt req.user.id
      next()

  ensureAdmin: (req, res, next) ->
    ensureAuth req, res, ->
      return next 401 unless req.user.admin
      next()

  loadPerson: (req, res, next) ->
    if id = req.params.personId or req.params.id
      Person.findById id, (err, person) ->
        return next err if err
        return next 404 unless person
        req.person = person<commit_msg>you can be logged out and edit a team still<commit_after>app = require '../config/app'
[Person, Team, Vote] = (app.db.model m for m in ['Person', 'Team', 'Vote'])

ensureAuth = (req, res, next) ->
  return res.redirect "/login?returnTo=#{encodeURIComponent(req.url)}" unless req.loggedIn
  next()

module.exports =
  ensureAuth: ensureAuth

  ensureAccess: (req, res, next) ->
    return next() if req.team? and req.team.includes(null, req.session.team)
    ensureAuth req, res, ->
      unless req.user.admin
        return next 401 if req.person? and req.person.id isnt req.user.id
        return next 401 if req.team? and not req.team.includes(req.user, req.session.team)
        return next 401 if req.vote? and req.vote.personId.toString() isnt req.user.id
      next()

  ensureAdmin: (req, res, next) ->
    ensureAuth req, res, ->
      return next 401 unless req.user.admin
      next()

  loadPerson: (req, res, next) ->
    if id = req.params.personId or req.params.id
      Person.findById id, (err, person) ->
        return next err if err
        return next 404 unless person
        req.person = person
{'fraction_non_alphanumeric': 0.12717034256217738, 'fraction_numerical': 0.014077897700610043, 'mean_word_length': 2.8207885304659497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4201312', 'n_tokens_mistral': 707, 'n_tokens_neox': 672, 'n_words': 219}