Ticket #131: remember-me.diff

File remember-me.diff, 3.0 kB (added by luks, 3 years ago)
  • acct_mgr/web_ui.py

    old new  
    1010# Author: Matthew Good <trac@matt-good.net> 
    1111 
    1212from __future__ import generators 
     13import time 
    1314 
    1415from trac import perm, util 
    1516from trac.core import * 
     
    178179            return 'login.cs', None 
    179180        return auth.LoginModule.process_request(self, req) 
    180181 
     182    def _get_name_for_cookie(self, req, cookie): 
     183        name = auth.LoginModule._get_name_for_cookie(self, req, cookie) 
     184        if name and not req.incookie.has_key('trac_auth_session'): 
     185            self.env.log.debug('Updating auth cookie %s for user %s' %  
     186                               (cookie.value, name)) 
     187            db = self.env.get_db_cnx() 
     188            cursor = db.cursor() 
     189            cursor.execute('UPDATE auth_cookie SET time=%s WHERE cookie=%s', 
     190                           (int(time.time()), cookie.value)) 
     191            req.outcookie['trac_auth'] = cookie.value 
     192            req.outcookie['trac_auth']['path'] = self.env.href() 
     193            req.outcookie['trac_auth']['expires'] = 86400 * 30 
     194            req.outcookie['trac_auth_session'] = '1' 
     195            req.outcookie['trac_auth_session']['path'] = self.env.href() 
     196        return name 
     197         
    181198    def _do_login(self, req): 
    182199        if not req.remote_user: 
    183200            req.redirect(self.env.abs_href()) 
    184         return auth.LoginModule._do_login(self, req) 
     201        res = auth.LoginModule._do_login(self, req) 
     202        if req.args.get('rememberme', '0') == '1': 
     203            req.outcookie['trac_auth']['expires'] = 86400 * 30 
     204        return res 
    185205 
     206    def _do_logout(self, req): 
     207        """Log the user out. 
     208 
     209        Simply deletes the corresponding record from the auth_cookie table. 
     210        """ 
     211        if req.authname == 'anonymous': 
     212            # Not logged in 
     213            return 
     214 
     215        # While deleting this cookie we also take the opportunity to delete 
     216        # cookies older than 30 days 
     217        db = self.env.get_db_cnx() 
     218        cursor = db.cursor() 
     219        cursor.execute("DELETE FROM auth_cookie WHERE name=%s OR time < %s", 
     220                       (req.authname, int(time.time()) - 86400 * 30)) 
     221        db.commit() 
     222        self._expire_cookie(req) 
     223         
    186224    def _remote_user(self, req): 
    187225        user = req.args.get('user') 
    188226        if AccountManager(self.env).check_password(user, 
  • acct_mgr/templates/login.cs

    old new  
    2424   <label for="password">Password:</label> 
    2525   <input type="password" id="password" name="password" class="textwidget" size="20" /> 
    2626  </div> 
     27  <div> 
     28    <input type="checkbox" id="rememberme" name="rememberme" value="1" /> <label for="rememberme">Remember me</label> 
     29  </div> 
    2730  <input type="submit" value="Login" /> 
    2831 </form> 
    2932