1 from types import StringTypes
3 from PLC.Faults import *
4 from PLC.Parameter import Parameter
5 from PLC.Debug import profile
6 from PLC.Table import Row, Table
10 Representation of a row in the keys table. To use, instantiate with a
11 dict of values. Update as you would a dict. Commit to the database
15 primary_key = 'key_id'
17 'key_id': Parameter(int, "Key type"),
18 'key_type': Parameter(str, "Key type"),
19 'key': Parameter(str, "Key value"),
20 'is_blacklisted': Parameter(str, "Key has been blacklisted and is forever unusable"),
21 'person_id': Parameter(int, "Identifier of the account that created this key"),
22 'is_primary': Parameter(bool, "Is the primary key for this account")
25 def __init__(self, api, fields):
26 Row.__init__(self, fields)
30 def validate_key_type(self, key_type):
31 # 1. ssh is the only supported key type
32 if not key_type or not key_type in ['ssh']:
33 raise PLCInvalidArgument, "Invalid key type"
37 def validate_key(self, key):
38 # 1. key must not be blacklisted
40 # Remove leading and trailing spaces
42 # Make sure key is not blank
44 raise PLCInvalidArgument, "Invalid key"
46 rows = self.api.db.selectall("SELECT is_blacklisted from keys" \
47 " WHERE key = '%s'" % key)
49 raise PLCInvalidArgument, "Key is blacklisted"
52 def add_person(self, person, commit = True):
54 Associate key with person
57 assert 'key_id' in self
58 assert isinstance(person, PLC.Persons.Person)
59 assert 'person_id' in person
61 person_id = person['person_id']
62 key_id = self['key_id']
64 if not 'person_id' in self:
65 assert key_id not in person['key_ids']
67 self.api.db.do("INSERT INTO person_key (person_id, key_id)" \
68 " VALUES (%d, %d)" % (person_id, key_id) )
72 self['person_id'] = person_id
73 person['key_id'] = key_id
75 def set_primary_key(self, person, commit = True):
77 Set the primary key for a person
80 assert 'key_id' in self
81 assert isinstance(person, PLC.Persons.Person)
82 assert 'person_id' in person
84 person_id = person['person_id']
85 key_id = self['key_id']
86 assert person_id in [self['person_id']]
88 self.api.db.do("UPDATE person_key SET is_primary = False" \
89 " WHERE person_id = %d " % person_id)
90 self.api.db.do("UPDATE person_key SET is_primary = True" \
91 " WHERE person_id = %d AND key_id = %d" \
92 % (person_id, key_id) )
97 self['is_primary'] = True
99 def delete(self, commit = True):
101 Delete key from the database
103 assert 'key_id' in self
105 for table in ['person_key', 'keys']:
106 self.api.db.do("DELETE FROM %s WHERE key_id = %d" % \
107 (table, self['key_id']), self)
114 Representation of row(s) from the keys table in the
118 def __init__(self, api, key_id_list = None):
121 sql = "SELECT %s FROM keys LEFT JOIN person_key USING (%s) " % \
122 (", ".join(Key.fields), Key.primary_key)
125 sql += " WHERE key_id IN (%s)" % ", ".join(map(str, key_id_list))
127 rows = self.api.db.selectall(sql)
130 self[row['key_id']] = Key(api, row)