X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FKeys.py;h=ebabd190d555e7e02207700598d0e0db217ad603;hb=19d4a01ccf66af9e00914351b3eacd5fc880f988;hp=88c712054469ddd0da8d3b889005554e8f15b75c;hpb=6a15f46cfce003d21ee8ba0bee213df66250e60e;p=plcapi.git diff --git a/PLC/Keys.py b/PLC/Keys.py index 88c7120..ebabd19 100644 --- a/PLC/Keys.py +++ b/PLC/Keys.py @@ -2,46 +2,46 @@ import re from PLC.Faults import * from PLC.Parameter import Parameter +from PLC.Filter import Filter from PLC.Debug import profile from PLC.Table import Row, Table from PLC.KeyTypes import KeyType, KeyTypes class Key(Row): """ - Representation of a row in the keys table. To use, instantiate with a - dict of values. Update as you would a dict. Commit to the database + Representation of a row in the keys table. To use, instantiate with a + dict of values. Update as you would a dict. Commit to the database with sync(). """ table_name = 'keys' primary_key = 'key_id' + join_tables = ['person_key', 'peer_key'] fields = { 'key_id': Parameter(int, "Key identifier"), 'key_type': Parameter(str, "Key type"), 'key': Parameter(str, "Key value", max = 4096), + 'person_id': Parameter(int, "User to which this key belongs", nullok = True), + 'peer_id': Parameter(int, "Peer to which this key belongs", nullok = True), + 'peer_key_id': Parameter(int, "Foreign key identifier at peer", nullok = True), } - def __init__(self, api, fields = {}): - Row.__init__(self, fields) - self.api = api - def validate_key_type(self, key_type): - if key_type not in KeyTypes(self.api): + key_types = [row['key_type'] for row in KeyTypes(self.api)] + if key_type not in key_types: raise PLCInvalidArgument, "Invalid key type" - return key_type + return key_type def validate_key(self, key): - key = key.strip() - - # Key must not be blacklisted - rows = self.api.db.selectall("SELECT 1 from keys" \ - " WHERE key = %(key)s" \ + # Key must not be blacklisted + rows = self.api.db.selectall("SELECT 1 from keys" \ + " WHERE key = %(key)s" \ " AND is_blacklisted IS True", locals()) - if rows: + if rows: raise PLCInvalidArgument, "Key is blacklisted and cannot be used" - return key + return key def validate(self): # Basic validation @@ -67,13 +67,13 @@ class Key(Row): def blacklist(self, commit = True): """ - Permanently blacklist key (and all other identical keys), - preventing it from ever being added again. Because this could - affect multiple keys associated with multiple accounts, it - should be admin only. - """ + Permanently blacklist key (and all other identical keys), + preventing it from ever being added again. Because this could + affect multiple keys associated with multiple accounts, it + should be admin only. + """ - assert 'key_id' in self + assert 'key_id' in self assert 'key' in self # Get all matching keys @@ -87,47 +87,33 @@ class Key(Row): self.api.db.do("UPDATE keys SET is_blacklisted = True" \ " WHERE key_id IN (%s)" % ", ".join(map(str, key_ids))) - # But disassociate them from all join tables - for table in ['person_key']: + # But disassociate them from all join tables + for table in self.join_tables: self.api.db.do("DELETE FROM %s WHERE key_id IN (%s)" % \ (table, ", ".join(map(str, key_ids)))) if commit: self.api.db.commit() - def delete(self, commit = True): - """ - Delete key from the database. - """ - - assert 'key_id' in self - - for table in ['person_key', 'keys']: - self.api.db.do("DELETE FROM %s WHERE key_id = %d" % \ - (table, self['key_id'])) - - if commit: - self.api.db.commit() - class Keys(Table): """ Representation of row(s) from the keys table in the database. """ - def __init__(self, api, key_id_list = None, is_blacklisted = False): - self.api = api - - sql = "SELECT %s FROM keys WHERE True" % \ - ", ".join(Key.fields) + def __init__(self, api, key_filter = None, columns = None): + Table.__init__(self, api, Key, columns) - if is_blacklisted is not None: - sql += " AND is_blacklisted IS %(is_blacklisted)s" + sql = "SELECT %s FROM view_keys WHERE is_blacklisted IS False" % \ + ", ".join(self.columns) - if key_id_list: - sql += " AND key_id IN (%s)" % ", ".join(map(str, key_id_list)) + if key_filter is not None: + if isinstance(key_filter, (list, tuple, set, int, long)): + key_filter = Filter(Key.fields, {'key_id': key_filter}) + elif isinstance(key_filter, dict): + key_filter = Filter(Key.fields, key_filter) + else: + raise PLCInvalidArgument, "Wrong key filter %r"%key_filter + sql += " AND (%s) %s" % key_filter.sql(api) - rows = self.api.db.selectall(sql, locals()) - - for row in rows: - self[row['key_id']] = Key(api, row) + self.selectall(sql)