<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt">On Mon, 23 May 2011 12:34:32 -0700 Mats &lt;<a ymailto="mailto:mats@ronin-group.org" href="mailto:mats@ronin-group.org">mats@ronin-group.org</a>&gt; wrote:<br><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><div style="font-family:arial, helvetica, sans-serif;font-size:10pt"><br><div style="margin-left: 40px;">I'm trying to authenticate using megrok.login but it doesn't<br>authenticate my user.&nbsp; It does authenticate using the session login<br>form against my admin username and password from the grok install.<br><br>I created a register form as so:<br><br>class Register(base.PageForm):<br>&nbsp; &nbsp; grok.context(interface.Interface)<br><br>&nbsp; &nbsp; fields = base.Fields(IRegister)<br>&nbsp; &nbsp; ignoreContext = True<br><br>&nbsp; &nbsp;
 @base.button.buttonAndHandler(u'Register')<br>&nbsp; &nbsp; def handle_registration(self, action):<br>&nbsp; &nbsp; &nbsp; &nbsp; data, errors = self.extractData()<br>&nbsp; &nbsp; &nbsp; &nbsp; if errors:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.status = self.formErrorsMessage<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return<br>&nbsp; &nbsp; &nbsp; &nbsp; username = data['username']<br>&nbsp; &nbsp; &nbsp; &nbsp; password = data['password']<br><br>&nbsp; &nbsp; &nbsp; &nbsp; auth = component.getUtility(IAuthentication)<br>&nbsp; &nbsp; &nbsp; &nbsp; pf = auth['principals']<br>&nbsp; &nbsp; &nbsp; &nbsp; pf[username] = InternalPrincipal(username, password, username)<br>&nbsp; &nbsp; &nbsp; &nbsp; pm = IPrincipalPermissionManager(grok.getSite())<br>&nbsp; &nbsp; &nbsp; &nbsp; pm.grantPermissionToPrincipal('iport.Registered', username)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.redirect('index')<br><br>My login form looks like this:<br><br>class
 Login(Page):<br>&nbsp; &nbsp; def update(self, camefrom=None, SUBMIT=None):<br>&nbsp; &nbsp; &nbsp; &nbsp; self.camefrom = camefrom<br>&nbsp; &nbsp; &nbsp; &nbsp; if SUBMIT is not None and camefrom is not None:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.redirect(camefrom)<br>&nbsp; &nbsp; &nbsp; &nbsp; return<br><br>The registration does seem to work correctly as it adds the principal<br>to site._sm['megrok_login_pau']['principals'] but authentication gives<br>me nothing but displaying the login form again and user staying as<br>'zope.anybody'.<br><br>What am I doing wrong?<br><br><br>Thanks,<br><br>Mats<br></div><br>The magic of the megrok.login authentication is performed by the code in loginform.py ( check the LoginForm class' __call__ method).<br>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__).<br><br>class Login(grok.View):<br>&nbsp;&nbsp;&nbsp;
 <br>&nbsp;&nbsp;&nbsp; grok.context(Interface)<br>&nbsp;&nbsp;&nbsp; grok.require('zope.Public')<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; def __call__(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request = self.request<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; principal = request.principal<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unauthenticated = IUnauthenticatedPrincipal.providedBy(principal)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.unauthenticated = unauthenticated<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; camefrom = request.get('camefrom')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if isinstance(camefrom, list):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # this can happen on python2.6, as it changed the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # behaviour of cgi.FieldStorage a
 bit.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; camefrom = camefrom[0]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.camefrom = camefrom<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (not unauthenticated) and ('SUBMIT' in request):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # authenticated by submitting<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.response.redirect(camefrom or '.')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ''<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return grok.View.__call__(self)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; def update(self, camefrom=None, SUBMIT=None):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.camefrom = camefrom<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
 SUBMIT is not None and camefrom is not None:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # The credentials were entered. Go back. If the entered<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # credentials are not valid, another redirect will happen<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # to this view.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.redirect(camefrom)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.baseurl = self.url(self.context)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return<br></div></div>



</div></body></html>