Yeah, do session based auth.:) l will send more when not on my phone.<br><br>Sent from my HTC<br><br>----- Reply message -----<br>From: &quot;Hector Blanco&quot; &lt;white.lists@gmail.com&gt;<br>Date: Mon, Dec 27, 2010 6:14 pm<br>Subject: [Grok-dev] Putting together the authentication plugin and a Session. Is that possible?<br>To: &lt;grok-dev@zope.org&gt;<br><br>Hello list:<br><br>I have set up a user authentication mechanism as explained in<br><a href="http://grok.zope.org/documentation/how-to/authentication-with-grok">http://grok.zope.org/documentation/how-to/authentication-with-grok</a><br><br>The users structure is serialized on a MySQL database.<br><br>I have setup the &quot;authenticateCredentials&quot; and &quot;getAccount&quot; methods<br>for the authenticator plugin like this<br><br>def authenticateCredentials(self, credentials):<br>        if isinstance(credentials, dict):<br>                if ((&quot;login&quot; in credentials) and (&quot;password&quot; in credentials)):<br>                        user = self.getAccount(credentials[&#39;login&#39;])<br>                        if user and (user.checkPassword(credentials[&#39;password&#39;])):<br>                                log.debug(&quot;::UserAuthenticatorPlugin &gt; authenticateCredentials &gt;<br>                                 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Credentials authenticated for user %s &quot; % (user.userName))<br>                                return MyOwnPrincipalInfo.MyOwnPrincipalInfo(user)<br>        return None<br><br>def getAccount(self, login):<br>try:<br>        return grok.getSite()[&quot;UserManager&quot;].getByName(login, allData=False)<br>except Exception, e:<br>        log.warn(&quot;::UserAuthenticatorPlugin &gt; getAccount &gt; Got exception %s &quot; % e)<br>        log.debug(&quot;::UserAuthenticatorPlugin &gt; getAccount &gt; Showing<br>traceback:\n%s&quot; % traceback.format_exc(limit=5))<br>finally:<br>        Database.session.close()<br><br>The &quot;UserManager&quot; is just a bunch of static methods that access the<br>database (using SqlAlchemy) and, in this case, tries to get the user<br>whose &quot;userName&quot; is the same in &quot;login&quot;. That means having to access<br>the database many, many times.<br><br>So here&#39;s the question:<br>Is there any way of using the ISession object so I don&#39;t have to query<br>the database so often?<br><br>The idea would be putting &quot;something&quot; in the &quot;authenticateCredentials&quot;<br>method so if userName and password are correct, a new entry for that<br>user is created in the session object, so I can get it from there,<br>instead of having to access the database that often.<br><br>Something that would allow me to modify the getAccount() method to<br>something like this:<br><br>def getAccount(self, login):<br>try:<br> &nbsp; &nbsp; &nbsp; &nbsp;if (ISession(self.request)[&#39;users&#39;][self.request.principal.id]):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return =<br>ISession(self.request)[&#39;users&#39;][self.request.principal.id]<br> &nbsp; &nbsp; &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return<br>grok.getSite()[&quot;UserManager&quot;].getByName(login, allData=False)<br>except Exception, e:<br>        log.warn(&quot;::UserAuthenticatorPlugin &gt; getAccount &gt; Got exception %s &quot; % e)<br>        log.debug(&quot;::UserAuthenticatorPlugin &gt; getAccount &gt; Showing<br>traceback:\n%s&quot; % traceback.format_exc(limit=5))<br>finally:<br>        Database.session.close()<br><br>But that&#39;s the issue... in order to use the session, I need to have<br>access to a .request, right? And in getAccount(self...), self is an<br>instance of &quot;UserAuthenticatorPlugin&quot;, which doesn&#39;t have any request<br>associated (at least, not that I have seen)<br><br>Is there any way to access the request from an instance of<br>&quot;UserAuthenticatorPlugin&quot;? &nbsp;The idea would be &quot;registering&quot; the user<br>that is authenticated in the Session object so I don&#39;t have to access<br>the database that many times...<br><br>Thank you in advance!<br>_______________________________________________<br>Grok-dev mailing list<br>Grok-dev@zope.org<br><a href="https://mail.zope.org/mailman/listinfo/grok-dev">https://mail.zope.org/mailman/listinfo/grok-dev</a><br><br><br>