Skip to content

Commit

Permalink
Show invitation link if email server not setup (#3519)
Browse files Browse the repository at this point in the history
* return invite link to client if e-mail server is not set up

* add a couple of tests to make sure invite links are only returned when neccessary

* show invite link when e-mail is not configured

* remove "an e-mail has been sent" when there's no e-mail configured

* return invite_url in re-invites as well. Also refactor to reuse the code.
  • Loading branch information
Omer Lachish authored Mar 3, 2019
1 parent e7db5ca commit e9603f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
9 changes: 8 additions & 1 deletion client/app/pages/users/CreateUser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class CreateUser extends React.Component {
{ name: 'email', title: 'Email', type: 'email' },
].map(field => ({ required: true, readOnly: !!user, ...field }));

const message = user && user.invite_link ? (
<div>
<span>The user has been created. You can use the following link to invite them:</span>
<textarea className="form-control m-t-10" rows="3" readOnly>{user.invite_link}</textarea>
</div>
) : <span>The user has been created and should receive an invite email soon.</span>;

return (
<div className="row">
<EmailSettingsWarning featureName="invite emails" />
Expand All @@ -50,7 +57,7 @@ class CreateUser extends React.Component {
/>
{user && (
<Alert
message="The user has been created and should receive an invite email soon."
message={message}
type="success"
className="m-t-20"
/>
Expand Down
22 changes: 13 additions & 9 deletions redash/handlers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from redash.authentication.account import invite_link_for_user, send_invite_email, send_password_reset_email, send_verify_email
from redash.settings import parse_boolean
from redash import settings


# Ordering map for relationships
Expand All @@ -36,9 +37,17 @@
)


def invite_user(org, inviter, user):
def invite_user(org, inviter, user, send_email=True):
email_configured = settings.MAIL_DEFAULT_SENDER is not None
d = user.to_dict()

invite_url = invite_link_for_user(user)
send_invite_email(inviter, user, invite_url, org)
if email_configured and send_email:
send_invite_email(inviter, user, invite_url, org)
else:
d['invite_link'] = invite_url

return d


class UserListResource(BaseResource):
Expand Down Expand Up @@ -137,19 +146,14 @@ def post(self):
})

should_send_invitation = 'no_invite' not in request.args
if should_send_invitation:
invite_user(self.current_org, self.current_user, user)

return user.to_dict()
return invite_user(self.current_org, self.current_user, user, send_email=should_send_invitation)


class UserInviteResource(BaseResource):
@require_admin
def post(self, user_id):
user = models.User.get_by_id_and_org(user_id, self.current_org)
invite_url = invite_user(self.current_org, self.current_user, user)

return user.to_dict()
return invite_user(self.current_org, self.current_user, user)


class UserResetPasswordResource(BaseResource):
Expand Down
30 changes: 29 additions & 1 deletion tests/handlers/test_users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from redash import models
from redash import models, settings
from tests import BaseTestCase
from mock import patch

Expand Down Expand Up @@ -40,6 +40,34 @@ def test_creates_user(self):
self.assertEqual(rv.json['name'], test_user['name'])
self.assertEqual(rv.json['email'], test_user['email'])

def test_shows_invite_link_when_email_is_not_configured(self):
previous = settings.MAIL_DEFAULT_SENDER
settings.MAIL_DEFAULT_SENDER = None

admin = self.factory.create_admin()

test_user = {'name': 'User', 'email': '[email protected]'}
rv = self.make_request('post', '/api/users', data=test_user, user=admin)

self.assertEqual(rv.status_code, 200)
self.assertTrue('invite_link' in rv.json)

settings.MAIL_DEFAULT_SENDER = previous

def test_does_not_show_invite_link_when_email_is_configured(self):
previous = settings.MAIL_DEFAULT_SENDER
settings.MAIL_DEFAULT_SENDER = "[email protected]"

admin = self.factory.create_admin()

test_user = {'name': 'User', 'email': '[email protected]'}
rv = self.make_request('post', '/api/users', data=test_user, user=admin)

self.assertEqual(rv.status_code, 200)
self.assertFalse('invite_link' in rv.json)

settings.MAIL_DEFAULT_SENDER = previous

def test_creates_user_case_insensitive_email(self):
admin = self.factory.create_admin()

Expand Down

0 comments on commit e9603f5

Please sign in to comment.