space_helpers.js 2.97 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
'use strict';

3
const db = require('../models/db');
mntmn's avatar
mntmn committed
4
const { Op } = require("sequelize");
mntmn's avatar
mntmn committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var config = require('config');

module.exports = (req, res, next) => {
  let spaceId = req.params.id;

  let finalizeReq = (space, role) => {
    if (role === "none") {
      res.status(403).json({
        "error": "access denied"
      });
    } else {
      req['space'] = space;
      req['spaceRole'] = role;
      res.header("x-spacedeck-space-role", req['spaceRole']);
      next();
    }
  };

  var finalizeAnonymousLogin = function(space, spaceAuth) {
    var role = "none";

    if (spaceAuth && (spaceAuth === space.edit_hash)) {
      role = "editor";
    } else {
      if (space.access_mode == "public") {
        role = "viewer";
      } else {
        role = "none";
      }
    }

    if (req.user) {
37
      db.getUserRoleInSpace(space, req.user, function(newRole) {
mntmn's avatar
mntmn committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        if (newRole == "admin" && (role == "editor" || role == "viewer")) {
          finalizeReq(space, newRole);
        } else if (newRole == "editor" && (role == "viewer")) {
          finalizeReq(space, newRole);
        } else {
          finalizeReq(space, role);
        }
      });
    } else {
      finalizeReq(space, role);
    }
  };

  var userMapping = {
    '_id': 1,
    'nickname': 1,
    'email': 1
  };

mntmn's avatar
mntmn committed
57
  // find space by id or slug
58
  db.Space.findOne({where: {
mntmn's avatar
mntmn committed
59
60
61
62
                    [Op.or]: [
                      {"_id": spaceId},
                      {"edit_slug": spaceId}
                    ]
63
  }}).then(function(space) {
mntmn's avatar
mntmn committed
64

65
66
67
68
69
70
    if (space) {
      if (space.access_mode == "public") {
        if (space.password) {
          if (req.spacePassword) {
            if (req.spacePassword === space.password) {
              finalizeAnonymousLogin(space, req["spaceAuth"]);
mntmn's avatar
mntmn committed
71
            } else {
72
73
              res.status(403).json({
                "error": "password_wrong"
mntmn's avatar
mntmn committed
74
75
76
              });
            }
          } else {
77
78
79
            res.status(401).json({
              "error": "password_required"
            });
mntmn's avatar
mntmn committed
80
81
          }
        } else {
82
83
          finalizeAnonymousLogin(space, req["spaceAuth"]);
        }
mntmn's avatar
mntmn committed
84

85
86
87
88
89
90
91
92
93
94
95
96
      } else {
        // space is private
        
        // special permission for screenshot/pdf export from backend
        if (req.query['api_token'] && req.query['api_token'] == config.get('phantom_api_secret')) {
          finalizeReq(space, "viewer");
          return;
        }

        if (req.user) {
          db.getUserRoleInSpace(space, req.user, function(role) {
            if (role == "none") {
mntmn's avatar
mntmn committed
97
98
              finalizeAnonymousLogin(space, req["spaceAuth"]);
            } else {
99
              finalizeReq(space, role);
mntmn's avatar
mntmn committed
100
            }
101
102
103
104
105
106
107
108
          });
        } else {
          if (req.spaceAuth && space.edit_hash) {
            finalizeAnonymousLogin(space, req["spaceAuth"]);
          } else {
            res.status(403).json({
              "error": "auth_required"
            });
mntmn's avatar
mntmn committed
109
110
111
          }
        }
      }
112
113
114
115
    } else {
      res.status(404).json({
        "error": "space_not_found"
      });
mntmn's avatar
mntmn committed
116
117
118
    }
  });
}