Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show invitation link if email server not setup #3519

Merged
merged 5 commits into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
rauchy marked this conversation as resolved.
Show resolved Hide resolved
</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:
rauchy marked this conversation as resolved.
Show resolved Hide resolved
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': 'user@example.com'}
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 = "john@doe.com"

admin = self.factory.create_admin()

test_user = {'name': 'User', 'email': 'user@example.com'}
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