2014-02-12 10:23:40 +00:00
|
|
|
logger = require("logger-sharelatex")
|
2018-02-15 12:18:43 +00:00
|
|
|
ProjectGetter = require('../Project/ProjectGetter')
|
2017-01-27 15:26:58 +00:00
|
|
|
UserGetter = require("../User/UserGetter")
|
2014-02-12 10:23:40 +00:00
|
|
|
SubscriptionLocator = require("./SubscriptionLocator")
|
|
|
|
Settings = require("settings-sharelatex")
|
2016-03-07 15:32:04 +00:00
|
|
|
CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
|
2016-08-02 14:42:50 +00:00
|
|
|
CollaboratorsInvitesHandler = require("../Collaborators/CollaboratorsInviteHandler")
|
2018-06-04 16:55:02 +00:00
|
|
|
V1SubscriptionManager = require("./V1SubscriptionManager")
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2018-06-01 10:23:25 +00:00
|
|
|
module.exports = LimitationsManager =
|
2014-02-12 10:23:40 +00:00
|
|
|
allowedNumberOfCollaboratorsInProject: (project_id, callback) ->
|
2018-02-15 12:18:43 +00:00
|
|
|
ProjectGetter.getProject project_id, owner_ref: true, (error, project) =>
|
2017-01-27 15:26:58 +00:00
|
|
|
return callback(error) if error?
|
|
|
|
@allowedNumberOfCollaboratorsForUser project.owner_ref, callback
|
2018-02-15 12:18:43 +00:00
|
|
|
|
2017-01-27 15:26:58 +00:00
|
|
|
allowedNumberOfCollaboratorsForUser: (user_id, callback) ->
|
|
|
|
UserGetter.getUser user_id, {features: 1}, (error, user) ->
|
2014-02-12 10:23:40 +00:00
|
|
|
return callback(error) if error?
|
2017-01-27 15:26:58 +00:00
|
|
|
if user.features? and user.features.collaborators?
|
|
|
|
callback null, user.features.collaborators
|
2014-02-12 10:23:40 +00:00
|
|
|
else
|
2018-07-10 09:31:25 +00:00
|
|
|
callback null, Settings.defaultFeatures.collaborators
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2015-10-14 16:29:58 +00:00
|
|
|
canAddXCollaborators: (project_id, x_collaborators, callback = (error, allowed)->) ->
|
2014-02-12 10:23:40 +00:00
|
|
|
@allowedNumberOfCollaboratorsInProject project_id, (error, allowed_number) =>
|
|
|
|
return callback(error) if error?
|
2017-09-20 09:29:47 +00:00
|
|
|
CollaboratorsHandler.getInvitedCollaboratorCount project_id, (error, current_number) =>
|
2014-02-12 10:23:40 +00:00
|
|
|
return callback(error) if error?
|
2016-08-02 14:42:50 +00:00
|
|
|
CollaboratorsInvitesHandler.getInviteCount project_id, (error, invite_count) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
if current_number + invite_count + x_collaborators <= allowed_number or allowed_number < 0
|
|
|
|
callback null, true
|
|
|
|
else
|
|
|
|
callback null, false
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2018-09-03 14:09:57 +00:00
|
|
|
hasPaidSubscription: (user, callback = (err, hasSubscriptionOrIsMember)->) ->
|
2018-06-05 10:57:25 +00:00
|
|
|
@userHasV2Subscription user, (err, hasSubscription, subscription)=>
|
2014-08-07 14:29:06 +00:00
|
|
|
return callback(err) if err?
|
|
|
|
@userIsMemberOfGroupSubscription user, (err, isMember)=>
|
|
|
|
return callback(err) if err?
|
2018-09-03 14:09:57 +00:00
|
|
|
@userHasV1Subscription user, (err, hasV1Subscription)=>
|
2018-06-04 16:55:02 +00:00
|
|
|
return callback(err) if err?
|
|
|
|
logger.log {user_id:user._id, isMember, hasSubscription, hasV1Subscription}, "checking if user has subscription or is group member"
|
|
|
|
callback err, isMember or hasSubscription or hasV1Subscription, subscription
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2018-09-05 10:37:37 +00:00
|
|
|
|
|
|
|
# alias for backward-compatibility with modules. Use `haspaidsubscription` instead
|
|
|
|
userHasSubscriptionOrIsGroupMember: (user, callback) ->
|
|
|
|
@hasPaidSubscription(user, callback)
|
|
|
|
|
2018-06-05 10:57:25 +00:00
|
|
|
userHasV2Subscription: (user, callback = (err, hasSubscription, subscription)->) ->
|
2014-02-12 10:23:40 +00:00
|
|
|
logger.log user_id:user._id, "checking if user has subscription"
|
|
|
|
SubscriptionLocator.getUsersSubscription user._id, (err, subscription)->
|
2014-03-08 19:01:00 +00:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
2016-07-19 09:28:43 +00:00
|
|
|
hasValidSubscription = subscription? and (subscription.recurlySubscription_id? or subscription?.customAccount == true)
|
2014-03-08 18:31:07 +00:00
|
|
|
logger.log user:user, hasValidSubscription:hasValidSubscription, subscription:subscription, "checking if user has subscription"
|
2014-02-12 10:23:40 +00:00
|
|
|
callback err, hasValidSubscription, subscription
|
2016-08-02 14:42:50 +00:00
|
|
|
|
2018-06-05 10:57:25 +00:00
|
|
|
userHasV1OrV2Subscription: (user, callback = (err, hasSubscription) ->) ->
|
|
|
|
@userHasV2Subscription user, (err, hasV2Subscription) =>
|
|
|
|
return callback(err) if err?
|
|
|
|
return callback null, true if hasV2Subscription
|
|
|
|
@userHasV1Subscription user, (err, hasV1Subscription) =>
|
|
|
|
return callback(err) if err?
|
|
|
|
return callback null, true if hasV1Subscription
|
|
|
|
return callback null, false
|
|
|
|
|
2014-08-07 14:29:06 +00:00
|
|
|
userIsMemberOfGroupSubscription: (user, callback = (error, isMember, subscriptions) ->) ->
|
2016-06-15 17:34:56 +00:00
|
|
|
logger.log user_id: user._id, "checking is user is member of subscription groups"
|
2014-08-07 14:29:06 +00:00
|
|
|
SubscriptionLocator.getMemberSubscriptions user._id, (err, subscriptions = []) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback err, subscriptions.length > 0, subscriptions
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2018-06-05 10:57:25 +00:00
|
|
|
userHasV1Subscription: (user, callback = (error, hasV1Subscription) ->) ->
|
|
|
|
V1SubscriptionManager.getSubscriptionsFromV1 user._id, (err, v1Subscription) ->
|
|
|
|
logger.log {user_id: user._id, v1Subscription}, '[userHasV1Subscription]'
|
|
|
|
callback err, !!v1Subscription?.has_subscription
|
|
|
|
|
2018-05-31 10:54:50 +00:00
|
|
|
teamHasReachedMemberLimit: (subscription) ->
|
2018-06-07 15:49:17 +00:00
|
|
|
currentTotal = (subscription.member_ids or []).length +
|
|
|
|
(subscription.teamInvites or []).length +
|
|
|
|
(subscription.invited_emails or []).length
|
2018-06-07 15:22:38 +00:00
|
|
|
|
2018-05-31 10:54:50 +00:00
|
|
|
return currentTotal >= subscription.membersLimit
|
|
|
|
|
2018-07-11 08:31:57 +00:00
|
|
|
hasGroupMembersLimitReached: (subscriptionId, callback = (err, limitReached, subscription)->)->
|
|
|
|
SubscriptionLocator.getSubscription subscriptionId, (err, subscription)->
|
2016-02-22 16:50:41 +00:00
|
|
|
if err?
|
2018-07-11 08:31:57 +00:00
|
|
|
logger.err err:err, subscriptionId: subscriptionId, "error getting subscription"
|
2016-02-22 16:50:41 +00:00
|
|
|
return callback(err)
|
|
|
|
if !subscription?
|
2018-07-11 08:31:57 +00:00
|
|
|
logger.err subscriptionId: subscriptionId, "no subscription found"
|
2016-02-22 16:50:41 +00:00
|
|
|
return callback("no subscription found")
|
2018-06-01 10:23:25 +00:00
|
|
|
|
|
|
|
limitReached = LimitationsManager.teamHasReachedMemberLimit(subscription)
|
2016-02-22 16:50:41 +00:00
|
|
|
callback(err, limitReached, subscription)
|