LimitationsManager.coffee 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. logger = require("logger-sharelatex")
  2. Project = require("../../models/Project").Project
  3. User = require("../../models/User").User
  4. SubscriptionLocator = require("./SubscriptionLocator")
  5. Settings = require("settings-sharelatex")
  6. module.exports =
  7. allowedNumberOfCollaboratorsInProject: (project_id, callback) ->
  8. getOwnerOfProject project_id, (error, owner)->
  9. return callback(error) if error?
  10. if owner.features? and owner.features.collaborators?
  11. callback null, owner.features.collaborators
  12. else
  13. callback null, Settings.defaultPlanCode.collaborators
  14. currentNumberOfCollaboratorsInProject: (project_id, callback) ->
  15. Project.findById project_id, 'collaberator_refs readOnly_refs', (error, project) ->
  16. return callback(error) if error?
  17. callback null, (project.collaberator_refs.length + project.readOnly_refs.length)
  18. isCollaboratorLimitReached: (project_id, callback = (error, limit_reached)->) ->
  19. @allowedNumberOfCollaboratorsInProject project_id, (error, allowed_number) =>
  20. return callback(error) if error?
  21. @currentNumberOfCollaboratorsInProject project_id, (error, current_number) =>
  22. return callback(error) if error?
  23. if current_number < allowed_number or allowed_number < 0
  24. callback null, false
  25. else
  26. callback null, true
  27. userHasSubscriptionOrFreeTrial: (user, callback = (err, hasSubscriptionOrTrial, subscription)->) ->
  28. @userHasSubscription user, (err, hasSubscription, subscription)=>
  29. @userHasFreeTrial user, (err, hasFreeTrial)=>
  30. logger.log user_id:user._id, subscription:subscription, hasFreeTrial:hasFreeTrial, hasSubscription:hasSubscription, "checking if user has subscription or free trial"
  31. callback null, hasFreeTrial or hasSubscription, subscription
  32. userHasSubscription: (user, callback = (err, hasSubscription, subscription)->) ->
  33. logger.log user_id:user._id, "checking if user has subscription"
  34. SubscriptionLocator.getUsersSubscription user._id, (err, subscription)->
  35. logger.log user:user, subscription:subscription, "checking if user has subscription"
  36. hasValidSubscription = subscription? and subscription.recurlySubscription_id? and subscription?.state != "expired"
  37. callback err, hasValidSubscription, subscription
  38. userHasFreeTrial: (user, callback = (err, hasFreeTrial, subscription)->) ->
  39. logger.log user_id:user._id, "checking if user has free trial"
  40. SubscriptionLocator.getUsersSubscription user, (err, subscription)->
  41. callback err, subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt?, subscription
  42. hasGroupMembersLimitReached: (user_id, callback)->
  43. SubscriptionLocator.getUsersSubscription user_id, (err, subscription)->
  44. limitReached = subscription.member_ids.length >= subscription.membersLimit
  45. logger.log user_id:user_id, limitReached:limitReached, currentTotal: subscription.member_ids.length, membersLimit: subscription.membersLimit, "checking if subscription members limit has been reached"
  46. callback(null, limitReached)
  47. getOwnerOfProject = (project_id, callback)->
  48. Project.findById project_id, 'owner_ref', (error, project) ->
  49. return callback(error) if error?
  50. User.findById project.owner_ref, (error, owner) ->
  51. callback(error, owner)