diff --git a/services/web/app/coffee/Features/Email/EmailBuilder.coffee b/services/web/app/coffee/Features/Email/EmailBuilder.coffee index 064b59f7a8..d3500c9276 100644 --- a/services/web/app/coffee/Features/Email/EmailBuilder.coffee +++ b/services/web/app/coffee/Features/Email/EmailBuilder.coffee @@ -88,6 +88,28 @@ templates.projectSharedWithYou =
""" + +templates.completeJoinGroupAccount = + subject: _.template "Verify Email to join <%= group_name %> group" + layout: NotificationEmailLayout + type:"notification" + compiledTemplate: _.template """ +Hi, please verify your email to join the <%= group_name %> and get your free premium account
+Thank you
+ +""" + module.exports = templates: templates diff --git a/services/web/app/coffee/Features/Subscription/SubscriptionDomainAllocator.coffee b/services/web/app/coffee/Features/Subscription/SubscriptionDomainAllocator.coffee index 5b89abf2ed..90401b0de5 100644 --- a/services/web/app/coffee/Features/Subscription/SubscriptionDomainAllocator.coffee +++ b/services/web/app/coffee/Features/Subscription/SubscriptionDomainAllocator.coffee @@ -6,8 +6,36 @@ _s = require("underscore.string") module.exports = SubscriptionDomainAllocator = + + getLicenceUserCanJoin: (user, callback)-> + licence = SubscriptionDomainAllocator._findDomainLicence(user.email) + if licence? + callback null, licence + else + callback() + + attemptToJoinGroup: (user, callback)-> + licence = SubscriptionDomainAllocator._findDomainLicence(user.email) + if licence? and user.emailVerified + SubscriptionGroupHandler.addUserToGroup licence.adminUser_id, user.email, callback + else + callback "user not verified" + + rejectInvitationToGroup: (user, subscription, callback)-> + removeUserFromGroup(subscription.admin_id, user._id, callback) + + + getDomainLicencePage: (user)-> + licence = SubscriptionDomainAllocator._findDomainLicence(user.email) + if licence?.verifyEmail + return "/user/subscription/#{licence.subscription_id}/group/invited" + else + return undefined + + autoAllocate: (user, callback = ->)-> licence = SubscriptionDomainAllocator._findDomainLicence(user.email) + # if licence? SubscriptionGroupHandler.addUserToGroup licence.adminUser_id, user.email, callback else @@ -21,4 +49,8 @@ module.exports = SubscriptionDomainAllocator = return licence + findDomainLicenceBySubscriptionId: (subscription_id)-> + licence = _.find settings.domainLicences, (licence)-> + licence?.subscription_id == subscription_id + return licence diff --git a/services/web/app/coffee/Features/Subscription/SubscriptionGroupController.coffee b/services/web/app/coffee/Features/Subscription/SubscriptionGroupController.coffee index 674c5affbe..fc8885563f 100644 --- a/services/web/app/coffee/Features/Subscription/SubscriptionGroupController.coffee +++ b/services/web/app/coffee/Features/Subscription/SubscriptionGroupController.coffee @@ -2,6 +2,11 @@ SubscriptionGroupHandler = require("./SubscriptionGroupHandler") logger = require("logger-sharelatex") SubscriptionLocator = require("./SubscriptionLocator") +settings = require("settings-sharelatex") +PasswordResetTokenHandler = require("../PasswordReset/PasswordResetTokenHandler") +EmailHandler = require("../Email/EmailHandler") +SubscriptionDomainAllocator = require("./SubscriptionDomainAllocator") + module.exports = addUserToGroup: (req, res)-> @@ -32,3 +37,38 @@ module.exports = title: 'group_admin' users: users subscription: subscription + + renderGroupInvitePage: (req, res)-> + subscription_id = req.params.subscription_id + licence = SubscriptionDomainAllocator.findDomainLicenceBySubscriptionId(subscription_id) + + res.render "subscriptions/group/invite", + title: "Group Invitation" + subscription_id:subscription_id + licenceName:licence.name + + beginJoinGroup: (req, res)-> + subscription_id = req.params.subscription_id + user_id = req.session.user._id + licence = SubscriptionDomainAllocator.findDomainLicenceBySubscriptionId(subscription_id) + if !licence? + res.send 500 + PasswordResetTokenHandler.getNewToken subscription_id, (err, token)-> + opts = + to : req.session.user.email + group_name: licence.name + completeJoinUrl: "#{settings.siteUrl}/user/subscription/#{subscription_id}/group/complete_join?token=#{token}" + EmailHandler.sendEmail "completeJoinGroupAccount", opts, -> + res.send 200 + + completeJoin: (req, res)-> + subscription_id = req.params.subscription_id + PasswordResetTokenHandler.getUserIdFromTokenAndExpire req.query.token, (err, token_subscription_id)-> + console.log token_subscription_id + if subscription_id != token_subscription_id + return res.send 403 + SubscriptionLocator.getSubscription subscription_id, (err, subscription)-> + SubscriptionGroupHandler.addUserToGroup subscription.admin_id, req.user.email, (err, user)-> + res.send "joined" + + diff --git a/services/web/app/coffee/Features/Subscription/SubscriptionLocator.coffee b/services/web/app/coffee/Features/Subscription/SubscriptionLocator.coffee index a4521bd8f4..19e701443b 100644 --- a/services/web/app/coffee/Features/Subscription/SubscriptionLocator.coffee +++ b/services/web/app/coffee/Features/Subscription/SubscriptionLocator.coffee @@ -14,4 +14,7 @@ module.exports = getMemberSubscriptions: (user_id, callback) -> logger.log user_id: user_id, "getting users group subscriptions" - Subscription.find(member_ids: user_id).populate("admin_id").exec callback \ No newline at end of file + Subscription.find(member_ids: user_id).populate("admin_id").exec callback + + getSubscription: (subscription_id, callback)-> + Subscription.findOne _id:subscription_id, callback diff --git a/services/web/app/coffee/Features/Subscription/SubscriptionRouter.coffee b/services/web/app/coffee/Features/Subscription/SubscriptionRouter.coffee index 963ff3dfcf..8a6732e3b0 100644 --- a/services/web/app/coffee/Features/Subscription/SubscriptionRouter.coffee +++ b/services/web/app/coffee/Features/Subscription/SubscriptionRouter.coffee @@ -24,6 +24,9 @@ module.exports = app.post '/subscription/group/user', AuthenticationController.requireLogin(), SubscriptionGroupController.addUserToGroup app.del '/subscription/group/user/:user_id', AuthenticationController.requireLogin(), SubscriptionGroupController.removeUserFromGroup + app.get '/user/subscription/:subscription_id/group/invited', AuthenticationController.requireLogin(), SubscriptionGroupController.renderGroupInvitePage + app.post '/user/subscription/:subscription_id/group/begin_join', AuthenticationController.requireLogin(), SubscriptionGroupController.beginJoinGroup + app.get '/user/subscription/:subscription_id/group/complete_join', AuthenticationController.requireLogin(), SubscriptionGroupController.completeJoin #recurly callback app.post '/user/subscription/callback', SubscriptionController.recurlyNotificationParser, SubscriptionController.recurlyCallback diff --git a/services/web/app/coffee/models/User.coffee b/services/web/app/coffee/models/User.coffee index 783389cb6a..af83318b49 100644 --- a/services/web/app/coffee/models/User.coffee +++ b/services/web/app/coffee/models/User.coffee @@ -53,7 +53,6 @@ UserSchema = new Schema # has this set to true, despite never having had a free trial hadFreeTrial: {type: Boolean, default: false} - UserSchema.statics.getAllIds = (callback)-> this.find {}, ["first_name"], callback diff --git a/services/web/app/views/subscriptions/group/invite.jade b/services/web/app/views/subscriptions/group/invite.jade new file mode 100644 index 0000000000..06089e3d4b --- /dev/null +++ b/services/web/app/views/subscriptions/group/invite.jade @@ -0,0 +1,25 @@ +extends ../../layout + +block scripts + script(type='text/javascript'). + window.subscription_id = '#{subscription_id}' + +block content + .content.content-alt + .container + .row + .col-md-8.col-md-offset-2(ng-cloak) + .card(ng-controller="GroupSubscriptionInviteController") + .page-header + h1 You are invited to Join #{licenceName} + span(ng-show="!requestSent") + div You can claim a free premium account provided by #{licenceName} by verifying your email + + a.btn.btn-default(href="/project") Skip + + a.btn.btn.btn-primary(ng-click="joinGroup()") Verify email and join Group + + span(ng-show="requestSent") + div Check your email to complete joinging the group + + a.btn(href="/project") Continue to projects diff --git a/services/web/config/settings.defaults.coffee b/services/web/config/settings.defaults.coffee index 6d923bea61..6876d48f75 100644 --- a/services/web/config/settings.defaults.coffee +++ b/services/web/config/settings.defaults.coffee @@ -349,6 +349,10 @@ module.exports = reloadModuleViewsOnEachRequest: true + domainLicences: [ + {"domains":["gmail.com"], "adminUser_id":"555208e6ce4812e7d2319daa", "subscription_id":"555c6d1eb844bfb3964e0629", "name":"HENRYS AMAZING LICENCE"} + ] + # ShareLaTeX Server Pro options (https://www.sharelatex.com/university/onsite.html) # ---------- diff --git a/services/web/public/coffee/main.coffee b/services/web/public/coffee/main.coffee index 614c29f44c..88e33694e7 100644 --- a/services/web/public/coffee/main.coffee +++ b/services/web/public/coffee/main.coffee @@ -15,6 +15,7 @@ define [ "main/new-subscription" "main/annual-upgrade" "main/register-users" + "main/subscription/group-subscription-invite-controller" "analytics/AbTestingManager" "directives/asyncForm" "directives/stopPropagation" diff --git a/services/web/public/coffee/main/subscription/group-subscription-invite-controller.coffee b/services/web/public/coffee/main/subscription/group-subscription-invite-controller.coffee new file mode 100644 index 0000000000..f123985a70 --- /dev/null +++ b/services/web/public/coffee/main/subscription/group-subscription-invite-controller.coffee @@ -0,0 +1,14 @@ +define [ + "base" +], (App) -> + App.controller "GroupSubscriptionInviteController", ($scope, $http) -> + + $scope.requestSent = false + + $scope.joinGroup = -> + console.log "joingin group" + request = $http.post "/user/subscription/#{subscription_id}/group/begin_join", {_csrf:window.csrfToken} + request.success (data, status)-> + $scope.requestSent = true + request.error (data, status)-> + console.log "the request failed" \ No newline at end of file