This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Denis Krienbühl
committed
Mar 17, 2015
1 parent
e0f4c9d
commit 8ebf90f
Showing
8 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div metal:use-macro="layout.base" i18n:domain="onegov.town"> | ||
<tal:block metal:fill-slot="title"> | ||
${title} | ||
</tal:block> | ||
<tal:block metal:fill-slot="content"> | ||
<div metal:use-macro="layout.macros['form']" /> | ||
</tal:block> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" The login/logout views. """ | ||
|
||
import morepath | ||
|
||
from onegov.core.security import Public, Private | ||
from onegov.form import Form | ||
from wtforms import StringField, PasswordField, validators | ||
from onegov.town import _ | ||
from onegov.town.app import TownApp | ||
from onegov.town.layout import DefaultLayout | ||
from onegov.town.model import Town | ||
from onegov.user import UserCollection | ||
|
||
|
||
class LoginForm(Form): | ||
""" Defines the login form for onegov town. """ | ||
|
||
email = StringField(_(u'Email Address'), [validators.InputRequired()]) | ||
password = PasswordField(_(u'Password'), [validators.InputRequired()]) | ||
|
||
def get_identity(self, request): | ||
""" Returns the identity if the username and password match. If they | ||
don't match, None is returned. | ||
""" | ||
users = UserCollection(request.app.session()) | ||
user = users.by_username_and_password( | ||
self.email.data, self.password.data | ||
) | ||
|
||
if user is None: | ||
return None | ||
else: | ||
return morepath.Identity( | ||
userid=user.username, | ||
role=user.role, | ||
application_id=request.app.application_id | ||
) | ||
|
||
|
||
@TownApp.html( | ||
model=Town, name='login', template='form.pt', permission=Public, | ||
request_method='GET') | ||
def view_login(self, request): | ||
return handle_login(self, request) | ||
|
||
|
||
@TownApp.html( | ||
model=Town, name='login', template='form.pt', permission=Public, | ||
request_method='POST') | ||
def view_post_login(self, request): | ||
return handle_login(self, request) | ||
|
||
|
||
def handle_login(self, request): | ||
""" Handles the GET and POST login requests. """ | ||
|
||
form = LoginForm(request.POST) | ||
form.action = request.link(self, name='login') | ||
|
||
if form.submitted(request): | ||
identity = form.get_identity(request) | ||
|
||
if identity is not None: | ||
response = morepath.redirect(request.link(self)) | ||
morepath.remember_identity(response, request, identity) | ||
return response | ||
|
||
return { | ||
'layout': DefaultLayout(self, request), | ||
'title': _(u'Login to ${town}', mapping={'town': self.name}), | ||
'form': form | ||
} | ||
|
||
|
||
@TownApp.html( | ||
model=Town, name='logout', permission=Private, | ||
request_method='GET') | ||
def view_logout(self, request): | ||
""" Handles the logout requests. """ | ||
|
||
response = morepath.redirect(request.link(self)) | ||
morepath.forget_identity(response, request) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters