LimitationsManager.coffee 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 err, 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. if err?
  36. return callback(err)
  37. hasValidSubscription = subscription? and subscription.recurlySubscription_id?
  38. logger.log user:user, hasValidSubscription:hasValidSubscription, subscription:subscription, "checking if user has subscription"
  39. callback err, hasValidSubscription, subscription
  40. userHasFreeTrial: (user, callback = (err, hasFreeTrial, subscription)->) ->
  41. logger.log user_id:user._id, "checking if user has free trial"
  42. SubscriptionLocator.getUsersSubscription user, (err, subscription)->
  43. callback err, subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt?, subscription
  44. hasGroupMembersLimitReached: (user_id, callback)->
  45. SubscriptionLocator.getUsersSubscription user_id, (err, subscription)->
  46. limitReached = subscription.member_ids.length >= subscription.membersLimit
  47. logger.log user_id:user_id, limitReached:limitReached, currentTotal: subscription.member_ids.length, membersLimit: subscription.membersLimit, "checking if subscription members limit has been reached"
  48. callback(err, limitReached)
  49. getOwnerOfProject = (project_id, callback)->
  50. Project.findById project_id, 'owner_ref', (error, project) ->
  51. return callback(error) if error?
  52. User.findById project.owner_ref, (error, owner) ->
  53. callback(error, owner)