Skip to content

Commit

Permalink
Merge pull request #112 from djangoliv/endpoint-to-reset-password
Browse files Browse the repository at this point in the history
Allow admin to change any password
  • Loading branch information
lambdaTotoro authored Oct 21, 2020
2 parents 5d34628 + 7c6b517 commit a367863
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
34 changes: 33 additions & 1 deletion nativeauthenticator/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,12 @@ class ChangePasswordHandler(LocalBase):

@web.authenticated
async def get(self):
user = await self.get_current_user()
self._register_template_path()
html = self.render_template('change-password.html')
html = self.render_template(
'change-password.html',
user_name=user.name,
)
self.finish(html)

@web.authenticated
Expand All @@ -145,11 +149,39 @@ async def post(self):

html = self.render_template(
'change-password.html',
user_name=user.name,
result_message='Your password has been changed successfully',
)
self.finish(html)


class ChangePasswordAdminHandler(LocalBase):
"""Render the reset password page."""

@admin_only
async def get(self, user_name):
if not self.authenticator.user_exists(user_name):
raise web.HTTPError(404)
self._register_template_path()
html = self.render_template(
'change-password.html',
user_name=user_name,
)
self.finish(html)

@admin_only
async def post(self, user_name):
new_password = self.get_body_argument('password', strip=False)
self.authenticator.change_password(user_name, new_password)

message_template = 'The password for {} has been changed successfully'
html = self.render_template(
'change-password.html',
user_name=user_name,
result_message=message_template.format(user_name),
)
self.finish(html)

class LoginHandler(LoginHandler, LocalBase):

def _render(self, login_error=None, username=None):
Expand Down
7 changes: 5 additions & 2 deletions nativeauthenticator/nativeauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from tornado import gen
from traitlets import Bool, Integer, Unicode

from .handlers import (AuthorizationHandler, ChangeAuthorizationHandler,
ChangePasswordHandler, LoginHandler, SignUpHandler)
from .handlers import (
AuthorizationHandler, ChangeAuthorizationHandler, ChangePasswordHandler,
ChangePasswordAdminHandler, LoginHandler, SignUpHandler,
)
from .orm import UserInfo


Expand Down Expand Up @@ -224,6 +226,7 @@ def get_handlers(self, app):
(r'/authorize', AuthorizationHandler),
(r'/authorize/([^/]*)', ChangeAuthorizationHandler),
(r'/change-password', ChangePasswordHandler),
(r'/change-password/([^/]+)', ChangePasswordAdminHandler),
]
return native_handlers

Expand Down
2 changes: 1 addition & 1 deletion nativeauthenticator/templates/change-password.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<div class="container">
<form action="{{post_url}}" method="post" role="form">
<h2>
Change Password
Change Password For {{user_name}}
</h2>
<div>
<div class="form-group">
Expand Down

0 comments on commit a367863

Please sign in to comment.