2016-03-14 13:06:57 -04:00
|
|
|
CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
|
|
|
|
Project = require("../../models/Project").Project
|
|
|
|
User = require("../../models/User").User
|
2016-03-15 10:35:01 -04:00
|
|
|
PrivilegeLevels = require("./PrivilegeLevels")
|
|
|
|
PublicAccessLevels = require("./PublicAccessLevels")
|
2016-03-18 11:59:03 -04:00
|
|
|
Errors = require("../Errors/Errors")
|
2017-03-17 10:42:07 -04:00
|
|
|
ObjectId = require("mongojs").ObjectId
|
2017-09-27 09:01:52 -04:00
|
|
|
TokenAccessHandler = require('../TokenAccess/TokenAccessHandler')
|
2017-03-17 10:42:07 -04:00
|
|
|
|
2016-03-14 13:06:57 -04:00
|
|
|
|
|
|
|
module.exports = AuthorizationManager =
|
2017-09-27 09:01:52 -04:00
|
|
|
|
2017-09-28 05:53:35 -04:00
|
|
|
getPublicAccessLevel: (project_id, callback=(err, level)->) ->
|
|
|
|
if !ObjectId.isValid(project_id)
|
|
|
|
return callback(new Error("invalid project id"))
|
|
|
|
# Note, the Project property in the DB is `publicAccesLevel`, without the second `s`
|
|
|
|
Project.findOne { _id: project_id }, { publicAccesLevel: 1 }, (error, project) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if !project?
|
|
|
|
return callback new Errors.NotFoundError("no project found with id #{project_id}")
|
|
|
|
callback null, project.publicAccesLevel
|
2017-09-27 09:01:52 -04:00
|
|
|
|
2016-03-14 13:06:57 -04:00
|
|
|
# Get the privilege level that the user has for the project
|
|
|
|
# Returns:
|
|
|
|
# * privilegeLevel: "owner", "readAndWrite", of "readOnly" if the user has
|
|
|
|
# access. false if the user does not have access
|
|
|
|
# * becausePublic: true if the access level is only because the project is public.
|
2017-10-09 05:57:23 -04:00
|
|
|
getPrivilegeLevelForProject: (
|
2017-10-13 06:20:57 -04:00
|
|
|
user_id, project_id, token,
|
2017-10-09 05:57:23 -04:00
|
|
|
callback = (error, privilegeLevel, becausePublic) ->
|
|
|
|
) ->
|
2016-03-14 13:06:57 -04:00
|
|
|
if !user_id?
|
2017-09-27 09:01:52 -04:00
|
|
|
# User is Anonymous, Try Token-based access
|
2017-09-28 05:53:35 -04:00
|
|
|
AuthorizationManager.getPublicAccessLevel project_id, (err, publicAccessLevel) ->
|
2017-09-27 09:01:52 -04:00
|
|
|
return callback(err) if err?
|
|
|
|
if publicAccessLevel == PublicAccessLevels.TOKEN_BASED
|
2017-10-09 06:13:55 -04:00
|
|
|
# Anonymous users can have read-only access to token-based projects,
|
2017-10-18 08:04:37 -04:00
|
|
|
# while read-write access must be logged in,
|
|
|
|
# unless the `enableAnonymousReadAndWriteSharing` setting is enabled
|
2017-10-19 09:54:59 -04:00
|
|
|
TokenAccessHandler.isValidToken project_id, token, (err, isValidReadAndWrite, isValidReadOnly) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
if isValidReadOnly
|
|
|
|
# Grant anonymous user read-only access
|
|
|
|
callback null, PrivilegeLevels.READ_ONLY, false
|
|
|
|
else if (
|
|
|
|
isValidReadAndWrite and
|
|
|
|
TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED
|
|
|
|
)
|
|
|
|
# Grant anonymous user read-and-write access
|
|
|
|
callback null, PrivilegeLevels.READ_AND_WRITE, false
|
|
|
|
else
|
|
|
|
# Deny anonymous access
|
|
|
|
callback null, PrivilegeLevels.NONE, false
|
2017-09-27 09:01:52 -04:00
|
|
|
else if publicAccessLevel == PublicAccessLevels.READ_ONLY
|
2017-10-09 06:13:55 -04:00
|
|
|
# Legacy public read-only access for anonymous user
|
2017-09-27 09:01:52 -04:00
|
|
|
callback null, PrivilegeLevels.READ_ONLY, true
|
|
|
|
else if publicAccessLevel == PublicAccessLevels.READ_AND_WRITE
|
2017-10-09 06:13:55 -04:00
|
|
|
# Legacy public read-write access for anonymous user
|
2017-09-27 09:01:52 -04:00
|
|
|
callback null, PrivilegeLevels.READ_AND_WRITE, true
|
|
|
|
else
|
2017-10-09 06:13:55 -04:00
|
|
|
# Deny anonymous user access
|
2017-09-27 09:01:52 -04:00
|
|
|
callback null, PrivilegeLevels.NONE, false
|
2016-03-14 13:06:57 -04:00
|
|
|
else
|
2017-09-27 09:01:52 -04:00
|
|
|
# User is present, get their privilege level from database
|
2016-03-14 13:06:57 -04:00
|
|
|
CollaboratorsHandler.getMemberIdPrivilegeLevel user_id, project_id, (error, privilegeLevel) ->
|
|
|
|
return callback(error) if error?
|
2016-03-15 10:35:01 -04:00
|
|
|
if privilegeLevel? and privilegeLevel != PrivilegeLevels.NONE
|
2016-03-14 13:06:57 -04:00
|
|
|
# The user has direct access
|
|
|
|
callback null, privilegeLevel, false
|
|
|
|
else
|
2016-03-21 13:03:31 -04:00
|
|
|
AuthorizationManager.isUserSiteAdmin user_id, (error, isAdmin) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if isAdmin
|
|
|
|
callback null, PrivilegeLevels.OWNER, false
|
|
|
|
else
|
2017-09-27 09:01:52 -04:00
|
|
|
# Legacy public-access system
|
|
|
|
# User is present (not anonymous), but does not have direct access
|
2017-09-28 05:53:35 -04:00
|
|
|
AuthorizationManager.getPublicAccessLevel project_id, (err, publicAccessLevel) ->
|
2017-09-27 09:01:52 -04:00
|
|
|
return callback(err) if err?
|
|
|
|
if publicAccessLevel == PublicAccessLevels.READ_ONLY
|
|
|
|
callback null, PrivilegeLevels.READ_ONLY, true
|
2017-09-28 05:37:57 -04:00
|
|
|
else if publicAccessLevel == PublicAccessLevels.READ_AND_WRITE
|
2017-09-27 09:01:52 -04:00
|
|
|
callback null, PrivilegeLevels.READ_AND_WRITE, true
|
|
|
|
else
|
|
|
|
callback null, PrivilegeLevels.NONE, false
|
2016-03-14 13:06:57 -04:00
|
|
|
|
2017-10-13 06:20:57 -04:00
|
|
|
canUserReadProject: (user_id, project_id, token, callback = (error, canRead) ->) ->
|
|
|
|
AuthorizationManager.getPrivilegeLevelForProject user_id, project_id, token, (error, privilegeLevel) ->
|
2016-03-14 13:06:57 -04:00
|
|
|
return callback(error) if error?
|
2016-03-15 10:35:01 -04:00
|
|
|
return callback null, (privilegeLevel in [PrivilegeLevels.OWNER, PrivilegeLevels.READ_AND_WRITE, PrivilegeLevels.READ_ONLY])
|
2016-03-14 13:06:57 -04:00
|
|
|
|
2017-10-13 06:20:57 -04:00
|
|
|
canUserWriteProjectContent: (user_id, project_id, token, callback = (error, canWriteContent) ->) ->
|
|
|
|
AuthorizationManager.getPrivilegeLevelForProject user_id, project_id, token, (error, privilegeLevel) ->
|
2016-03-14 13:06:57 -04:00
|
|
|
return callback(error) if error?
|
2016-03-15 10:35:01 -04:00
|
|
|
return callback null, (privilegeLevel in [PrivilegeLevels.OWNER, PrivilegeLevels.READ_AND_WRITE])
|
2016-03-10 12:17:26 -05:00
|
|
|
|
2017-10-13 06:20:57 -04:00
|
|
|
canUserWriteProjectSettings: (user_id, project_id, token, callback = (error, canWriteSettings) ->) ->
|
|
|
|
AuthorizationManager.getPrivilegeLevelForProject user_id, project_id, token, (error, privilegeLevel, becausePublic) ->
|
2016-03-14 13:06:57 -04:00
|
|
|
return callback(error) if error?
|
2016-03-15 10:35:01 -04:00
|
|
|
if privilegeLevel == PrivilegeLevels.OWNER
|
2016-03-14 13:06:57 -04:00
|
|
|
return callback null, true
|
2016-03-15 10:35:01 -04:00
|
|
|
else if privilegeLevel == PrivilegeLevels.READ_AND_WRITE and !becausePublic
|
2016-03-14 13:06:57 -04:00
|
|
|
return callback null, true
|
|
|
|
else
|
|
|
|
return callback null, false
|
2016-03-10 12:17:26 -05:00
|
|
|
|
2017-10-13 06:20:57 -04:00
|
|
|
canUserAdminProject: (user_id, project_id, token, callback = (error, canAdmin) ->) ->
|
|
|
|
AuthorizationManager.getPrivilegeLevelForProject user_id, project_id, token, (error, privilegeLevel) ->
|
2016-03-14 13:06:57 -04:00
|
|
|
return callback(error) if error?
|
2016-03-15 10:35:01 -04:00
|
|
|
return callback null, (privilegeLevel == PrivilegeLevels.OWNER)
|
2016-03-10 12:17:26 -05:00
|
|
|
|
|
|
|
isUserSiteAdmin: (user_id, callback = (error, isAdmin) ->) ->
|
2016-03-14 13:06:57 -04:00
|
|
|
if !user_id?
|
|
|
|
return callback null, false
|
|
|
|
User.findOne { _id: user_id }, { isAdmin: 1 }, (error, user) ->
|
|
|
|
return callback(error) if error?
|
2017-09-27 09:01:52 -04:00
|
|
|
return callback null, (user?.isAdmin == true)
|