Skip to content

Commit

Permalink
Use skygear settings for reading configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Lei committed Oct 20, 2016
1 parent fea53b0 commit 1e7c9e7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 84 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SMTP settings are required for the plugin to send outgoing email.

### Other settings

* `FORGOT_PASSWORD_APPNAME` - the app name that will appear in the built-in
* `FORGOT_PASSWORD_APP_NAME` - the app name that will appear in the built-in
template. If you use a different template, you do not need to supply an
app name. The default app name is the Skygear Server app name.
* `FORGOT_PASSWORD_URL_PREFIX` - the URL prefix for accessing the Skygear
Expand Down Expand Up @@ -53,7 +53,7 @@ Here are a list of templates you can override:

* `templates/forgot_password/reset_password.html` - HTML form for user
to enter a new password.

* `templates/forgot_password/reset_password_error.html` - HTML page
to show when there is an error with the code and User ID of the request.

Expand Down
13 changes: 8 additions & 5 deletions forgot_password/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

from .options import options as forgot_password_options
from .handlers import register_handlers

from skygear.settings import add_parser as add_setting_parser

logger = logging.getLogger(__name__)
from .settings import get_settings_parser, get_smtp_settings_parser
from .handlers import register_handlers


def includeme(settings):
register_handlers(forgot_password_options)
register_handlers(settings.forgot_password, settings.forgot_password_smtp)


add_setting_parser('forgot_password', get_settings_parser())
add_setting_parser('forgot_password_smtp', get_smtp_settings_parser())
31 changes: 16 additions & 15 deletions forgot_password/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
logger = logging.getLogger(__name__)


class InvalidConfiguration(Exception):
pass


def reset_password_response(**kwargs):
"""
A shorthand for returning the reset password form as a response.
Expand All @@ -38,16 +34,19 @@ def reset_password_response(**kwargs):
return skygear.Response(body, content_type='text/html')


def register_handlers(settings):
if not settings.smtp_host:
logger.error('Mail server is not configured. Configure SMTP_HOST.')
raise InvalidConfiguration('mail server is not configured')

def register_handlers(settings, smtp_settings):
@skygear.op('user:forgot-password')
def forgot_password(email):
"""
Lambda function to handle forgot password request.
"""
if smtp_settings.host is None:
logger.error('Mail server is not configured. Configure SMTP_HOST.')
raise SkygearException(
'mail server is not configured',
skyerror.UnexpectedError
)

if email is None:
raise SkygearException('email is not found',
skyerror.ResourceNotFound)
Expand All @@ -64,11 +63,13 @@ def forgot_password(email):
user_record = userutil.get_user_record(c, user.id)
code = userutil.generate_code(user)
url_prefix = settings.url_prefix
if url_prefix.endswith('/'):
url_prefix = url_prefix[:-1]
link = '{0}/reset-password?code={1}&user_id={2}'.format(
url_prefix, code, user.id)

template_params = {
'appname': settings.appname,
'appname': settings.app_name,
'link': link,
'url_prefix': url_prefix,
'email': user.email,
Expand All @@ -86,11 +87,11 @@ def forgot_password(email):

try:
mailer = emailutil.Mailer(
smtp_host=settings.smtp_host,
smtp_port=settings.smtp_port,
smtp_mode=settings.smtp_mode,
smtp_login=settings.smtp_login,
smtp_password=settings.smtp_password,
smtp_host=smtp_settings.host,
smtp_port=smtp_settings.port,
smtp_mode=smtp_settings.mode,
smtp_login=smtp_settings.login,
smtp_password=smtp_settings.password,
)
mailer.send_mail(sender, email, subject, text, html=html)
logger.info('Successfully sent reset password email to user.')
Expand Down
62 changes: 0 additions & 62 deletions forgot_password/options.py

This file was deleted.

48 changes: 48 additions & 0 deletions forgot_password/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2016 Oursky Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from skygear.options import options as skyoptions
from skygear.settings import SettingsParser


def get_settings_parser():
parser = SettingsParser('FORGOT_PASSWORD')

parser.add_setting('app_name', default=skyoptions.appname)
parser.add_setting('url_prefix', default=skyoptions.skygear_endpoint)
parser.add_setting(
'sender',
resolve=False,
default='[email protected]'
)
parser.add_setting(
'subject',
resolve=False,
default='Reset password instructions'
)

return parser


def get_smtp_settings_parser():
parser = SettingsParser('SMTP')

parser.add_setting('host', resolve=False, required=False)
parser.add_setting('port', resolve=False, default=25, atype=int)
parser.add_setting('mode', resolve=False, default='normal')
parser.add_setting('login', resolve=False, default='')
parser.add_setting('password', resolve=False, default='')

return parser

0 comments on commit 1e7c9e7

Please sign in to comment.