2 # -*- coding: iso-8859-15 -*-
4 "MediaWiki communication functions"
8 from authkit.users import UsersReadOnly, md5, AuthKitError
9 import formencode, formencode.national
12 log = logging.getLogger(__name__)
14 import wradmin.model as model
15 import wradmin.model.validators
21 class MediaWikiUsers(UsersReadOnly):
22 def __init__(self, data=None, encrypt=None):
23 UsersReadOnly.__init__(self, data, encrypt)
25 # Initialize class fields
30 self.user_ids = {} # MediaWiki user_id field of the database
31 self.real_names = {} # Real names of the users
32 self.emails = {} # E-Mail addresses of the users
35 con = model.meta.engine.connect()
36 sql = "SELECT user_id, user_name, user_real_name, user_password, user_email FROM user, user_groups WHERE ug_user=user_id AND ug_group='beauftragte'"
37 result = con.execute(sql)
39 user_id, username, real_name, password, email = row
40 username = username.lower()
44 self.usernames.append(username)
45 self.passwords[username] = password
46 self.roles[username] = role
47 self.groups[username] = group
48 self.user_ids[username] = user_id
49 self.real_names[username] = real_name
50 self.emails[username] = email
52 log.info("%d users loaded from the MediaWiki database" % len(self.usernames))
55 def user_has_password(self, username, password):
57 Passwords are case sensitive.
58 Returns ``True`` if the user has the password specified, ``False`` otherwise.
59 Raises an exception if the user doesn't exist.
61 See http://www.winterrodeln.org/trac/wiki/MediaWikiAuthorization
63 pwd = self.user_password(username)
64 # Example: pwd = ':B:d25b2886:41e46c952790b1b442aac4f24f7ea7a8'
65 pwd_parts = pwd.split(':') # password_parts = ['', 'B', 'd25b2886', '41e46c952790b1b442aac4f24f7ea7a8']
66 if len(pwd_parts) == 4 and pwd_parts[1] == 'B':
67 salt, pwd_md5 = tuple(pwd_parts[2:4]) # salt = 'd25b2886'; pwd_md5 = '41e46c952790b1b442aac4f24f7ea7a8'
69 raise AuthKitError("Password in the MediaWiki database format has an unexpected format ('%s' instead of e.g. ':B:d25b2886:41e46c952790b1b442aac4f24f7ea7a8')" % pwd)
70 # log.info("user: '%s'; md5 of salt+' '+entered_pwd: '%s'; md5-part of DB-pwd: %s" % (username, md5(salt + '-' + md5(password)), pwd_md5))
71 return md5(salt + '-' + md5(password)) == pwd_md5