1
0
Fork 0
gnuviechadmin-historic/gnuviechadmin/xmlrpc/users.py

88 lines
2.6 KiB
Python
Raw Normal View History

# -*- python -*-
# -*- coding: utf-8 -*-
from authkit.users import Users, AuthKitNoSuchUserError
import logging
log = logging.getLogger(__name__)
class UserProvider(Users):
def _get_user(self, username, password, role):
return {
'username' : username,
'group' : None,
'password' : password,
'roles' : [role]
}
class ClientUserProvider(UserProvider):
def user(self, username):
print 'checking %s' % username
if username == 'dummy':
return self._get_user(username, username, 'client')
return AuthKitNoSuchUserError()
def list_roles(self):
return ['client']
class MailuserUserProvider(UserProvider):
def user(self, username):
raise AuthKitNoSuchUserError()
def list_roles(self):
return ['mailuser']
class SysuserUserProvider(UserProvider):
def user(self, username):
raise AuthKitNoSuchUserError()
def list_roles(self):
return ['sysuser']
class GVAUsers(Users):
def __init__(self, data, userproviders = [], encrypt = None):
"""Initialize the GVAXMLRPCUsers instance."""
Users.__init__(self, data, encrypt)
self.userproviders = [prov(self.data) for prov in userproviders]
def list_roles(self):
"""Returns a lowercase list of all role names ordered
alphabetically."""
roles = []
for prov in self.userproviders:
for role in prov.list_roles():
if not role in roles:
roles.append(role)
roles.sort()
return roles
def role_exists(self, role):
"""Returns ``True`` if the role exists, ``False``
otherwise. Roles are case insensitive."""
for prov in self.userproviders:
if prov.role_exists(role):
return True
return False
def user(self, username):
"""Returns a dictionary in the following format:
.. code-block :: Python
{
'username': username,
'group': group,
'password': password,
'roles': [role1,role2,role3... etc]
}
The role names are ordered alphabetically
Raises an exception if the user doesn't exist."""
for prov in self.userproviders:
try:
return prov.user(username)
except Exception, e:
print e
log.debug("Backend %s didn't find user %s" % (backend,
username))
raise AuthKitNoSuchUserError()