2006-04-15 22:00:23 +02:00
|
|
|
#
|
|
|
|
# Service facade for gnuviech-admin tool backend
|
|
|
|
# (c) 2006 Jan Dittberner <jan@dittberner.info>
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
from SessionManager import *
|
|
|
|
from DomainManager import *
|
2006-04-17 22:24:38 +02:00
|
|
|
import Settings
|
2006-11-04 21:24:36 +01:00
|
|
|
import psycopg2
|
2006-04-15 22:00:23 +02:00
|
|
|
|
|
|
|
class ServiceFacade:
|
|
|
|
"""
|
|
|
|
This class implements the facade to the services provided by the
|
|
|
|
gnuviech admin backend.
|
|
|
|
"""
|
2006-04-17 21:20:14 +02:00
|
|
|
def __init__(self):
|
2006-04-18 21:27:33 +02:00
|
|
|
connstr = 'host=%(dbhost)s user=%(dbuser)s ' + \
|
|
|
|
'password=%(dbpassword)s dbname=%(dbname)s'
|
2006-11-04 21:24:36 +01:00
|
|
|
dbconn = psycopg2.connect(connstr % Settings.DBSETTINGS)
|
2006-04-17 22:24:38 +02:00
|
|
|
self.sessionManager = SessionManager(dbconn)
|
|
|
|
self.domainManager = DomainManager(dbconn)
|
2006-04-17 21:20:14 +02:00
|
|
|
|
|
|
|
def _dispatch(self, method, params):
|
|
|
|
try:
|
|
|
|
func = getattr(self, method)
|
|
|
|
except AttributeError:
|
|
|
|
raise Exception('method "%s" is not supported' % method)
|
|
|
|
else:
|
|
|
|
if method != 'login' and method != 'logout':
|
|
|
|
sessionid = params[0]
|
|
|
|
try:
|
|
|
|
session = self.sessionManager.getSession(sessionid)
|
|
|
|
except InvalidSessionError:
|
|
|
|
return ""
|
|
|
|
nparams = [session]
|
|
|
|
for item in params[1:]:
|
|
|
|
nparams.append(item)
|
|
|
|
params = nparams
|
|
|
|
self.sessionManager.updateSession(sessionid)
|
|
|
|
return func(*params)
|
|
|
|
|
2006-04-15 22:00:23 +02:00
|
|
|
def login(self, login, password):
|
|
|
|
"""
|
|
|
|
Logs in the user specified by the given login and password.
|
|
|
|
The method creates a session and returns the session id which
|
|
|
|
has to be sent back by subsequent requests. If the login is
|
|
|
|
invalid the returned id is 0
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.sessionManager.newSession(login, password)
|
|
|
|
except InvalidLoginError, ile:
|
|
|
|
return 0
|
|
|
|
|
2006-04-17 21:20:14 +02:00
|
|
|
def logout(self, sessionid):
|
|
|
|
self.sessionManager.deleteSession(sessionid)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def listdomains(self, session):
|
2006-04-15 22:00:23 +02:00
|
|
|
"""
|
|
|
|
Lists the domains the given session may see.
|
|
|
|
"""
|
2006-04-17 21:20:14 +02:00
|
|
|
return self.domainManager.listDomains(session)
|