Attachment 'moinoodle.py'

Download

   1 # Copyright D J Moore 2007
   2 
   3 # This program is free software; you can redistribute it and/or modify
   4 # it under the terms of the GNU General Public License as published by
   5 # the Free Software Foundation; either version 2 of the License, or
   6 # (at your option) any later version.
   7 #
   8 # This program is distributed in the hope that it will be useful,
   9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11 # GNU General Public License for more details.
  12 #
  13 # You should have received a copy of the GNU General Public License
  14 # along with this program; if not, write to the Free Software
  15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  16 
  17 
  18 import re
  19 import Cookie
  20 from MoinMoin.user import User
  21 
  22 def moinoodle(request, **kw):
  23     """ authenticate via the Moodle session cookie """
  24     user_obj   = kw.get('user_obj')
  25     cookiename = "MoodleSession"
  26 
  27     #log = file("/tmp/moinoodle.out", "w")
  28     log = None
  29     if log: log.write("moinoodle authentication\n")
  30     try:
  31         cookie = Cookie.SimpleCookie(request.saved_cookie)
  32     except Cookie.CookieError:
  33         cookie = None
  34 
  35     if not cookie or not cookie.has_key(cookiename):
  36         # no cookie, no login
  37         return user_obj, True
  38 
  39     if log: log.write("got %s cookie\n" % cookiename)
  40     sessId = cookie[cookiename].value
  41     sessPath = request.cfg.moodle_session_dir + "/sess_" + sessId
  42     sessIn = file(sessPath)
  43 
  44     if not sessIn:
  45         # no session, no login
  46         return user_obj, True
  47     if log: log.write("got %s session\n" % sessPath)
  48 
  49     data = sessIn.read()
  50     #pre-Moodle 1.8 used: match = re.search(r'"loggedin";b:1;', data)
  51     match = re.search(r'"currentlogin"', data)
  52     if not match:
  53         if log: log.write("not logged in!\n")
  54         return user_obj, True
  55     if log: log.write("got loggedin flag\n")
  56 
  57     match = re.search(r'"username";s:\d+:"([^"]*)"', data)
  58     if not match:
  59         return None, True
  60     username = match.group(1)
  61     if log: log.write("username=%s\n" % username)
  62 
  63     match = re.search(r'"firstname";s:\d+:"([^"]*)"', data)
  64     fullname = ""
  65     if match:
  66         fullname = match.group(1)
  67         match = re.search(r'"lastname";s:\d+:"([^"]*)"', data)
  68         if match:
  69             fullname += " " + match.group(1)
  70 
  71     email = ""
  72     match = re.search(r'"email";s:\d+:"([^"]+@[^"]+)"', data)
  73     if match:
  74         email = match.group(1)
  75 
  76     tz = ""
  77     match = re.search(r'"timezone";s:\d+:"([^"]*)"', data)
  78     if match:
  79         tz = match.group(1)
  80     if log: log.write("fullname=%s email=%s tz=%s\n" % (fullname, email, tz))
  81 
  82     user = User(request, auth_username=username)
  83 
  84     if fullname:
  85         user.aliasname = fullname
  86     if email:
  87         user.email = email
  88     if tz and tz != "99":
  89         user.tz_offset = int(float(tz) * 3600)
  90 
  91     user.create_or_update(True)
  92 
  93     if user.valid:
  94         if log: log.write("Logged in OK!\n")
  95         return user, False
  96     else:
  97         if log: log.write("user not valid?!\n")
  98         return user_obj, True

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-05-21 09:30:29, 3.0 KB) [[attachment:moinoodle.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.