From e8dd5592c6f554f6d081d0393416574718eda754 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Sun, 5 Apr 2015 13:32:43 +0530 Subject: [PATCH] FEATURE: support inviting existing users to topic and message when SSO is enabled --- .../discourse/controllers/invite.js.es6 | 27 ++++++++++++++----- .../discourse/templates/modal/invite.hbs | 2 +- app/models/invite.rb | 2 +- app/models/topic.rb | 4 +-- config/locales/client.en.yml | 5 +++- lib/guardian.rb | 8 +----- 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/invite.js.es6 b/app/assets/javascripts/discourse/controllers/invite.js.es6 index 7c7c7c17754..0dcf67c6957 100644 --- a/app/assets/javascripts/discourse/controllers/invite.js.es6 +++ b/app/assets/javascripts/discourse/controllers/invite.js.es6 @@ -43,15 +43,19 @@ export default ObjectController.extend(ModalFunctionality, { // Show Groups? (add invited user to private group) showGroups: function() { - return this.get('isAdmin') && (Discourse.Utilities.emailValid(this.get('emailOrUsername')) || this.get('isPrivateTopic') || !this.get('invitingToTopic')); + return this.get('isAdmin') && (Discourse.Utilities.emailValid(this.get('emailOrUsername')) || this.get('isPrivateTopic') || !this.get('invitingToTopic')) && !Discourse.SiteSettings.enable_sso; }.property('isAdmin', 'emailOrUsername', 'isPrivateTopic', 'invitingToTopic'), // Instructional text for the modal. inviteInstructions: function() { - if (this.get('isMessage')) { + if (Discourse.SiteSettings.enable_sso) { + // inviting existing user when SSO enabled + return I18n.t('topic.invite_reply.sso_enabled'); + } else if (this.get('isMessage')) { + // inviting to a message return I18n.t('topic.invite_private.email_or_username'); } else if (this.get('invitingToTopic')) { - // display instructions based on provided entity + // when inviting to topic, display instructions based on provided entity if (this.blank('emailOrUsername')) { return I18n.t('topic.invite_reply.to_topic_blank'); } else if (Discourse.Utilities.emailValid(this.get('emailOrUsername'))) { @@ -60,6 +64,7 @@ export default ObjectController.extend(ModalFunctionality, { return I18n.t('topic.invite_reply.to_topic_username'); } } else { + // inviting to forum return I18n.t('topic.invite_reply.to_forum'); } }.property('isMessage', 'invitingToTopic', 'emailOrUsername'), @@ -76,15 +81,25 @@ export default ObjectController.extend(ModalFunctionality, { }, successMessage: function() { - return this.get('isMessage') ? - I18n.t('topic.invite_private.success') : - I18n.t('topic.invite_reply.success', { emailOrUsername: this.get('emailOrUsername') }); + if (this.get('isMessage')) { + return I18n.t('topic.invite_private.success'); + } else if ( Discourse.Utilities.emailValid(this.get('emailOrUsername')) ) { + return I18n.t('topic.invite_reply.success_email', { emailOrUsername: this.get('emailOrUsername') }); + } else { + return I18n.t('topic.invite_reply.success_username'); + } }.property('isMessage', 'emailOrUsername'), errorMessage: function() { return this.get('isMessage') ? I18n.t('topic.invite_private.error') : I18n.t('topic.invite_reply.error'); }.property('isMessage'), + placeholderKey: function() { + return Discourse.SiteSettings.enable_sso ? + 'topic.invite_reply.username_placeholder' : + 'topic.invite_private.email_or_username_placeholder'; + }.property(), + // Reset the modal to allow a new user to be invited. reset() { this.setProperties({ diff --git a/app/assets/javascripts/discourse/templates/modal/invite.hbs b/app/assets/javascripts/discourse/templates/modal/invite.hbs index 16c80290d34..4ff86a04b3a 100644 --- a/app/assets/javascripts/discourse/templates/modal/invite.hbs +++ b/app/assets/javascripts/discourse/templates/modal/invite.hbs @@ -10,7 +10,7 @@ {{else}} {{#if allowExistingMembers}} - {{user-selector single="true" allowAny=true usernames=emailOrUsername includeGroups="true" placeholderKey="topic.invite_private.email_or_username_placeholder"}} + {{user-selector single="true" allowAny=true usernames=emailOrUsername includeGroups="true" placeholderKey=placeholderKey}} {{else}} {{text-field value=emailOrUsername placeholderKey="topic.invite_reply.email_placeholder"}} {{/if}} diff --git a/app/models/invite.rb b/app/models/invite.rb index 6b84554d2b5..51af2516650 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -66,7 +66,7 @@ class Invite < ActiveRecord::Base if topic.private_message? topic.grant_permission_to_user(user.email) elsif topic.category && topic.category.groups.any? - if Guardian.new(invited_by).can_invite_to?(topic) + if Guardian.new(invited_by).can_invite_to?(topic) && !SiteSetting.enable_sso (topic.category.groups - user.groups).each { |group| group.add(user) } end end diff --git a/app/models/topic.rb b/app/models/topic.rb index d48bf6ccc53..e2149238866 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -551,12 +551,12 @@ class Topic < ActiveRecord::Base end end - if username_or_email =~ /^.+@.+$/ + if username_or_email =~ /^.+@.+$/ && !SiteSetting.enable_sso # NOTE callers expect an invite object if an invite was sent via email invite_by_email(invited_by, username_or_email, group_ids) else # invite existing member to a topic - user = User.find_by_username_or_email(username_or_email) + user = User.find_by_username(username_or_email) if user && topic_allowed_users.create!(user_id: user.id) # Notify the user they've been invited diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 66f6b6429df..35e95293635 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1073,15 +1073,18 @@ en: invite_reply: title: 'Invite' + username_placeholder: "username" action: 'Send Invite' help: 'invite others to this topic via email or notifications' to_forum: "We'll send a brief email allowing your friend to immediately join by clicking a link, no login required." + sso_enabled: "Enter the username of the person you'd like to invite to this topic." to_topic_blank: "Enter the username or email address of the person you'd like to invite to this topic." to_topic_email: "You've entered an email address. We'll email an invitation that allows your friend to immediately reply to this topic." to_topic_username: "You've entered a username. We'll send a notification to that user with a link inviting them to this topic." email_placeholder: 'name@example.com' - success: "We mailed out an invitation to {{emailOrUsername}}. We'll notify you when the invitation is redeemed. Check the invitations tab on your user page to keep track of your invites." + success_email: "We mailed out an invitation to {{emailOrUsername}}. We'll notify you when the invitation is redeemed. Check the invitations tab on your user page to keep track of your invites." + success_username: "We've invited that user to participate in this topic." error: "Sorry, we couldn't invite that person. Perhaps they have already been invited? (Invites are rate limited)" login_reply: 'Log In to Reply' diff --git a/lib/guardian.rb b/lib/guardian.rb index d3e2911ee1a..1db182aa8ed 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -197,12 +197,6 @@ class Guardian is_me?(user) end - def invitations_allowed? - !SiteSetting.enable_sso && - SiteSetting.enable_local_logins && - (!SiteSetting.must_approve_users? || is_staff?) - end - def can_invite_to_forum?(groups=nil) authenticated? && !SiteSetting.enable_sso && @@ -216,7 +210,7 @@ class Guardian def can_invite_to?(object, group_ids=nil) return false if ! authenticated? - return false if ! invitations_allowed? + return false unless ( SiteSetting.enable_local_logins && (!SiteSetting.must_approve_users? || is_staff?) ) return true if is_admin? return false if ! can_see?(object)