Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/hub/access.coffee
Views: 687
#########################################################################1# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2# License: MS-RSL – see LICENSE.md for details3#########################################################################45###6Access permissions related to projects for a given user (or project)7###89async = require('async')10winston = require('./logger').getLogger('access')1112misc = require('@cocalc/util/misc')13{defaults, required} = misc1415user_is_in_project_group = (opts) ->16opts = defaults opts,17project_id : required18account_id : undefined19account_groups : undefined20groups : required21database : required22cb : required # cb(err, true or false)23dbg = (m) -> winston.debug("user_is_in_project_group -- #{m}")24dbg()25if not opts.account_id?26dbg("not logged in, so for now we just say 'no' -- this may change soon.")27opts.cb(undefined, false) # do not have access28return29if opts.account_id == opts.project_id30# special case, e.g., project accessing "itself" for a project api key.31opts.cb(undefined, true)32return3334access = false35async.series([36(cb) ->37dbg("check if admin or in appropriate group -- #{misc.to_json(opts.account_groups)}")38if opts.account_groups? and 'admin' in opts.account_groups # check also done below!39access = true40cb()41else42opts.database.user_is_in_project_group43project_id : opts.project_id44account_id : opts.account_id45groups : opts.groups46cb : (err, x) ->47access = x48cb(err)49(cb) ->50if access51cb() # done52else if opts.account_groups?53# already decided above54cb()55else56# User does not have access in normal way and account_groups not provided, so57# we do an extra group check before denying user.58opts.database.get_account59columns : ['groups']60account_id : opts.account_id61cb : (err, r) ->62if err63cb(err)64else65access = 'admin' in (r['groups'] ? [])66cb()67], (err) ->68dbg("done with tests -- now access=#{access}, err=#{err}")69opts.cb(err, access)70)7172exports.user_has_write_access_to_project = (opts) ->73opts.groups = ['owner', 'collaborator']74user_is_in_project_group(opts)7576exports.user_has_read_access_to_project = (opts) ->77# Read access is granted if user is in any of the groups listed below (owner, collaborator, or *viewer*).78#dbg = (m) -> winston.debug("user_has_read_access_to_project #{opts.project_id}, #{opts.account_id}; #{m}")79opts.groups = ['owner', 'collaborator', 'viewer']80user_is_in_project_group(opts)818283