[Zope3-Users] login/logout with z3c.pagelet?

Hermann Himmelbauer dusty at qwer.tk
Tue Feb 12 06:09:32 EST 2008

> Hi,
> Are there any login/logout/redirect templates for z3c.pagelet?
> I could reuse the templates from zope.app.authentication, but they
> obviously
> contain some macros that need some registrations I don't have, such as
> @@standard_macros. And my layout template does not contain any macro.
> Maybe there is some package with a layer that does this job?
> How are you doing?

I simply created a simple z3c.form based form that looks like this:

class LoginPage(ActionForm):
    """ Login logic and page """

    template = ViewPageTemplateFile('pt/login.pt')
    fields = form.field.Fields(IFYMUser).select('login', 'password')
    prefix = "login_data"
    unsuccessfulLogin = False

    def __call__(self):
        request = self.request
        if (not IUnauthenticatedPrincipal.providedBy(request.principal)
            and 'login_data.widgets.login' in request):
            # Login succeeded!
            camefrom = request.get('camefrom', '.')
            if 'login_data.widgets.login' in request:
                # Something went wrong with the authentication
                self.unsuccessfulLogin = True
            return super(LoginPage, self).__call__()

    @form.button.buttonAndHandler(_('Anmelden'), name='login')
    def handleLogin(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
        if self.unsuccessfulLogin:
             raise ActionExecutionError(Invalid(
                _(u"Ungültige Login/Passwort-Kombination!")))

    @form.button.buttonAndHandler(_(u'Abbrechen'), name='abbrechen')
    def handle_abbrechen(self, action):

class LogoutPage(BrowserPagelet):
    """ Logout logic and page """

    template = ViewPageTemplateFile('pt/logout.pt')

    def update(self, nextURL=None):
        if not IUnauthenticatedPrincipal.providedBy(self.request.principal):
            auth = getUtility(IAuthentication)
            site_path = absoluteURL(getSite(), self.request)
            return self.request.response.redirect(site_path + '/logout.html')

My templates looks like this:

--------- login ------------

<h2 i18n:translate=""> Anmelden </h2>


<span tal:replace="structure view/errorstatus" />

<input tal:replace="structure view/widgets/login/snippets/div" />
<input tal:replace="structure view/widgets/password/snippets/div" />

<div class="buttons">
 <input tal:repeat="action view/actions/values"
        tal:replace="structure action/render"

------------ logout ------------

<h2 i18n:translate="">Abmelden</h2>

<p i18n:translate="">
Sie sind nun vom System abgemeldet.

Moreover, I have a link in my menu that points to the login/logout page,
(only logout if logged in and reverse).

However, I still have the problem that I don't know how to make Zope3
point to my login page in case unauthorized content is requested. Zope3
displays some "Unauthorized - a sever error has occured" page I don't know
where it comes from.

Best Regards,

P.S.: Ah yes, the "ActionForm" is a simple form inheriting from form.Form
that ignores the context. And the input fields in the template rely on my
z3c.formsnippets package, which you can easily replace with your TAL code.

More information about the Zope3-users mailing list