megrok.login

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

megrok.login

Mats Nordgren
I'm trying to authenticate using megrok.login but it doesn't
authenticate my user.  It does authenticate using the session login
form against my admin username and password from the grok install.

I created a register form as so:

class Register(base.PageForm):
    grok.context(interface.Interface)

    fields = base.Fields(IRegister)
    ignoreContext = True

    @base.button.buttonAndHandler(u'Register')
    def handle_registration(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return
        username = data['username']
        password = data['password']

        auth = component.getUtility(IAuthentication)
        pf = auth['principals']
        pf[username] = InternalPrincipal(username, password, username)
        pm = IPrincipalPermissionManager(grok.getSite())
        pm.grantPermissionToPrincipal('iport.Registered', username)
        self.redirect('index')

My login form looks like this:

class Login(Page):
    def update(self, camefrom=None, SUBMIT=None):
        self.camefrom = camefrom
        if SUBMIT is not None and camefrom is not None:
            self.redirect(camefrom)
        return

The registration does seem to work correctly as it adds the principal
to site._sm['megrok_login_pau']['principals'] but authentication gives
me nothing but displaying the login form again and user staying as
'zope.anybody'.

What am I doing wrong?


Thanks,

Mats
_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: megrok.login

Danilo G B
On Mon, 23 May 2011 12:34:32 -0700 Mats <[hidden email]> wrote:

I'm trying to authenticate using megrok.login but it doesn't
authenticate my user.  It does authenticate using the session login
form against my admin username and password from the grok install.

I created a register form as so:

class Register(base.PageForm):
    grok.context(interface.Interface)

    fields = base.Fields(IRegister)
    ignoreContext = True

    @base.button.buttonAndHandler(u'Register')
    def handle_registration(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return
        username = data['username']
        password = data['password']

        auth = component.getUtility(IAuthentication)
        pf = auth['principals']
        pf[username] = InternalPrincipal(username, password, username)
        pm = IPrincipalPermissionManager(grok.getSite())
        pm.grantPermissionToPrincipal('iport.Registered', username)
        self.redirect('index')

My login form looks like this:

class Login(Page):
    def update(self, camefrom=None, SUBMIT=None):
        self.camefrom = camefrom
        if SUBMIT is not None and camefrom is not None:
            self.redirect(camefrom)
        return

The registration does seem to work correctly as it adds the principal
to site._sm['megrok_login_pau']['principals'] but authentication gives
me nothing but displaying the login form again and user staying as
'zope.anybody'.

What am I doing wrong?


Thanks,

Mats

The magic of the megrok.login authentication is performed by the code in loginform.py ( check the LoginForm class' __call__ method).
In my code I dropped the self.index() in the last line and used a grok.View.__call__(self) instead (you should use Page.__call__).

class Login(grok.View):
   
    grok.context(Interface)
    grok.require('zope.Public')
   
    def __call__(self):
        request = self.request
        principal = request.principal

        unauthenticated = IUnauthenticatedPrincipal.providedBy(principal)
        self.unauthenticated = unauthenticated
       
        camefrom = request.get('camefrom')
        if isinstance(camefrom, list):
            # this can happen on python2.6, as it changed the
            # behaviour of cgi.FieldStorage a bit.
            camefrom = camefrom[0]
        self.camefrom = camefrom
       
        if (not unauthenticated) and ('SUBMIT' in request):
            # authenticated by submitting
            request.response.redirect(camefrom or '.')
            return ''
       
        return grok.View.__call__(self)
       
    def update(self, camefrom=None, SUBMIT=None):
        self.camefrom = camefrom
        if SUBMIT is not None and camefrom is not None:
            # The credentials were entered. Go back. If the entered
            # credentials are not valid, another redirect will happen
            # to this view.
            self.redirect(camefrom)
        self.baseurl = self.url(self.context)
        return

_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: megrok.login

Mats Nordgren
That worked wonders.  Thanks a million.

Could you tell me why it doesn't work when I remove
grok.require('zope.Public')?  Isn't all views public when no require
statement is specified?

Thanks again,

Mats

> The magic of the megrok.login authentication is performed by the code in
> loginform.py ( check the LoginForm class' __call__ method).
> In my code I dropped the self.index() in the last line and used a
> grok.View.__call__(self) instead (you should use Page.__call__).
>
> class Login(grok.View):
>
>     grok.context(Interface)
>     grok.require('zope.Public')
>
>     def __call__(self):
>         request = self.request
>         principal = request.principal
>
>         unauthenticated = IUnauthenticatedPrincipal.providedBy(principal)
>         self.unauthenticated = unauthenticated
>
>         camefrom = request.get('camefrom')
>         if isinstance(camefrom, list):
>             # this can happen on python2.6, as it changed the
>             # behaviour of cgi.FieldStorage a bit.
>             camefrom = camefrom[0]
>         self.camefrom = camefrom
>
>         if (not unauthenticated) and ('SUBMIT' in request):
>             # authenticated by submitting
>             request.response.redirect(camefrom or '.')
>             return ''
>
>         return grok.View.__call__(self)
>
>     def update(self, camefrom=None, SUBMIT=None):
>         self.camefrom = camefrom
>         if SUBMIT is not None and camefrom is not None:
>             # The credentials were entered. Go back. If the entered
>             # credentials are not valid, another redirect will happen
>             # to this view.
>             self.redirect(camefrom)
>         self.baseurl = self.url(self.context)
>         return
>
> _______________________________________________
> Grok-dev mailing list
> [hidden email]
> https://mail.zope.org/mailman/listinfo/grok-dev
>
>
_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: megrok.login

Danilo G B
Permissions depend on your site.zcml (etc/site.zcml.in and later parts/etc/site.zcml). By default, paster adds the following line to those files which grants zope.View and not zope.Public to anonymous users:

<grant permission="zope.View"
             principal="zope.Everybody" />


From: Mats <[hidden email]>
To: Danilo G B <[hidden email]>
Cc: [hidden email]
Sent: Wed, May 25, 2011 9:51:17 AM
Subject: Re: [Grok-dev] megrok.login

That worked wonders.  Thanks a million.

Could you tell me why it doesn't work when I remove
grok.require('zope.Public')?  Isn't all views public when no require
statement is specified?

Thanks again,

Mats

> The magic of the megrok.login authentication is performed by the code in
> loginform.py ( check the LoginForm class' __call__ method).
> In my code I dropped the self.index() in the last line and used a
> grok.View.__call__(self) instead (you should use Page.__call__).
>
> class Login(grok.View):
>
>     grok.context(Interface)
>     grok.require('zope.Public')
>
>     def __call__(self):
>         request = self.request
>         principal = request.principal
>
>         unauthenticated = IUnauthenticatedPrincipal.providedBy(principal)
>         self.unauthenticated = unauthenticated
>
>         camefrom = request.get('camefrom')
>         if isinstance(camefrom, list):
>             # this can happen on python2.6, as it changed the
>             # behaviour of cgi.FieldStorage a bit.
>             camefrom = camefrom[0]
>         self.camefrom = camefrom
>
>         if (not unauthenticated) and ('SUBMIT' in request):
>             # authenticated by submitting
>             request.response.redirect(camefrom or '.')
>             return ''
>
>         return grok.View.__call__(self)
>
>     def update(self, camefrom=None, SUBMIT=None):
>         self.camefrom = camefrom
>         if SUBMIT is not None and camefrom is not None:
>             # The credentials were entered. Go back. If the entered
>             # credentials are not valid, another redirect will happen
>             # to this view.
>             self.redirect(camefrom)
>         self.baseurl = self.url(self.context)
>         return
>
> _______________________________________________
> Grok-dev mailing list
> [hidden email]
> https://mail.zope.org/mailman/listinfo/grok-dev
>
>

_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev