Commit a9b57a31 authored by Ad Schellevis's avatar Ad Schellevis

(captiveportal, new) add some db functions

parent 7ef601b2
...@@ -34,17 +34,27 @@ class DB(object): ...@@ -34,17 +34,27 @@ class DB(object):
database_filename = '/var/captiveportal/captiveportal.sqlite' database_filename = '/var/captiveportal/captiveportal.sqlite'
def __init__(self): def __init__(self):
""" construct new database connection """ construct new database connection, open and make sure the sqlite file exists
:return: :return:
""" """
self._connection = sqlite3.connect(self.database_filename) self.open()
self.create() self.create()
def __del__(self): def __del__(self):
""" destruct, close database handle """ destruct, close database handle
""" """
self.close()
def close(self):
""" close database
"""
self._connection.close() self._connection.close()
def open(self):
""" open database
"""
self._connection = sqlite3.connect(self.database_filename)
def create(self, force_recreate=False): def create(self, force_recreate=False):
""" create/initialize new database """ create/initialize new database
:param force_recreate: if database already exists, remove old one first :param force_recreate: if database already exists, remove old one first
...@@ -63,26 +73,34 @@ class DB(object): ...@@ -63,26 +73,34 @@ class DB(object):
cur.executescript(open(init_script_filename, 'rb').read()) cur.executescript(open(init_script_filename, 'rb').read())
cur.close() cur.close()
def address_active(self, zoneid, ip_address): def sessions_per_address(self, zoneid, ip_address = None, mac_address = None):
""" check if address / network is active """ fetch session(s) per (mac) address
:param zoneid: cp zone number :param zoneid: cp zone number
:param ip_address: ip address (to unlock) :param ip_address: ip address
:return: active status (boolean) :return: active status (boolean)
""" """
cur = self._connection.cursor() cur = self._connection.cursor()
request = {'zoneid':zoneid, 'ip_address': ip_address} request = {'zoneid':zoneid, 'ip_address': ip_address, 'mac_address': mac_address}
cur.execute("""select count(*) cur.execute("""select cc.sessionid sessionId
from cp_clients , cc.authenticated_via authenticated_via
where deleted = 0 and zoneid = :zoneid and ip_address = :ip_address""", request) from cp_clients cc
where cc.deleted = 0
and cc.zoneid = :zoneid
and (
cc.ip_address = :ip_address
or
cc.mac_address = :mac_address
)""", request)
if cur.fetchall()[0][0] > 0: result = []
return True for row in cur.fetchall():
else: result.append({'sessionId':row[0], 'authenticated_via': row[1]})
return False return result
def add_client(self, zoneid, authenticated_via, username, ip_address, mac_address): def add_client(self, zoneid, authenticated_via, username, ip_address, mac_address):
""" add a new client to the captive portal administration """ add a new client to the captive portal administration
:param zoneid: cp zone number :param zoneid: cp zone number
:param authenticated_via: name/id of the authenticator or ---ip--- / ---mac--- for authentication by address
:param username: username, maybe empty :param username: username, maybe empty
:param ip_address: ip address (to unlock) :param ip_address: ip address (to unlock)
:param mac_address: physical address of this ip :param mac_address: physical address of this ip
...@@ -99,6 +117,7 @@ class DB(object): ...@@ -99,6 +117,7 @@ class DB(object):
cur = self._connection.cursor() cur = self._connection.cursor()
# set cp_client as deleted in case there's already a user logged-in at this ip address. # set cp_client as deleted in case there's already a user logged-in at this ip address.
if ip_address is not None and ip_address != '':
cur.execute("""UPDATE cp_clients cur.execute("""UPDATE cp_clients
SET deleted = 1 SET deleted = 1
WHERE zoneid = :zoneid WHERE zoneid = :zoneid
...@@ -113,6 +132,19 @@ class DB(object): ...@@ -113,6 +132,19 @@ class DB(object):
self._connection.commit() self._connection.commit()
return response return response
def update_client_ip(self, zoneid, sessionid, ip_address):
""" change client ip address
"""
cur = self._connection.cursor()
cur.execute("""UPDATE cp_clients
SET ip_address = :ip_address
WHERE deleted = 0
and zoneid = :zoneid
AND sessionid = :sessionid
""", {'zoneid': zoneid, 'sessionid': sessionid, 'ip_address': ip_address})
self._connection.commit()
def del_client(self, zoneid, sessionid): def del_client(self, zoneid, sessionid):
""" mark (administrative) client for removal """ mark (administrative) client for removal
:param zoneid: zone id :param zoneid: zone id
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment