Blame model/sslcerts.py

Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
import exception
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
from model.base import ModelItemBase, ModelManagerBase
Ivan Mahonin 9e1462
from model.users import User
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
class Sslcert(ModelItemBase):
Ivan Mahonin 9e1462
  def __init__(self, sslcerts, row, user = None):
Ivan Mahonin 9e1462
    super().__init__(sslcerts, row)
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
    self.id = int(row['id'])
Ivan Mahonin 9e1462
    self.user_id = int(row['user_id'])
Ivan Mahonin 9e1462
    self.data = str(row['data'])
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
    assert(not user or (type(user) is User and user.id == self.user_id))
Ivan Mahonin 9e1462
    self.user = user
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def get_user(self):
Ivan Mahonin 9e1462
    if self.user == None:
Ivan Mahonin 9e1462
      self.user = self.model.users.get_by_id(self.user_id)
Ivan Mahonin 9e1462
    assert(self.user)
Ivan Mahonin 9e1462
    assert(self.user.id == self.user_id)
Ivan Mahonin 9e1462
    return self.user
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def reset_cache(self):
Ivan Mahonin 9e1462
    self.manager.reset_cache(self.id, self.data)
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def can_delete(self):
Ivan Mahonin 9e1462
    return self.user_id == self.rights.user_id or self.rights.issuperuser()
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def delete(self):
Ivan Mahonin 9e1462
    if self.can_delete():
Ivan Mahonin 9e1462
      self.connection.execute(
Ivan Mahonin 9e1462
        'DELETE FROM %T WHERE `id`=%d',
Ivan Mahonin 9e1462
        self.table(), self.id )
Ivan Mahonin 9e1462
      self.reset_cache()
Ivan Mahonin 9e1462
    else:
Ivan Mahonin 9e1462
      raise exception.ModelDeny()
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
class Sslcerts(ModelManagerBase):
Ivan Mahonin 9e1462
  def table(self):
Ivan Mahonin 9e1462
    return 'sslcerts'
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def itemtype(self):
Ivan Mahonin 9e1462
    return Sslcert
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def reset_cache(self, id, data):
Ivan Mahonin 9e1462
    super().reset_cache(id)
Ivan Mahonin 9e1462
    self.connection.cache.reset(self.table(), {'data': data})
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def extract_data(self, data):
Ivan Mahonin 9e1462
    prefix = '-----BEGIN CERTIFICATE-----'
Ivan Mahonin 9e1462
    suffix = '-----END CERTIFICATE-----'
Ivan Mahonin 9e1462
    data = str(data)
Ivan Mahonin 9e1462
    i0 = data.find(prefix)
Ivan Mahonin 9e1462
    i1 = data.find(suffix)
Ivan Mahonin 9e1462
    if i0 >= 0 and i1 >= 0 and i0 + len(prefix) <= i1:
Ivan Mahonin 9e1462
      data = data[i0 + len(prefix):i1]
Ivan Mahonin 9e1462
    data = ''.join(data.split())
Ivan Mahonin 9e1462
    return data
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def verify_data(self, data):
Ivan Mahonin 9e1462
    b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
Ivan Mahonin 9e1462
    if not data or not type(data) is str:
Ivan Mahonin 9e1462
      return False
Ivan Mahonin 9e1462
    for c in data:
Ivan Mahonin 9e1462
      if c not in b64chars:
Ivan Mahonin 9e1462
        return False
Ivan Mahonin 9e1462
    return True
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def can_read(self, user):
Ivan Mahonin 9e1462
    assert(type(user) is User)
Ivan Mahonin 9e1462
    return user.id == self.rights.user_id \
Ivan Mahonin 9e1462
        or self.rights.issuperuser()
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def can_create(self, user):
Ivan Mahonin 9e1462
    assert(type(user) is User)
Ivan Mahonin 9e1462
    return user.id == self.rights.user_id or self.rights.issuperuser()
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def create(self, user, data):
Ivan Mahonin 9e1462
    if not self.can_create(user):
Ivan Mahonin 9e1462
      raise exception.ModelDeny()
Ivan Mahonin 9e1462
    if not self.verify_data(data):
Ivan Mahonin 9e1462
      raise exception.ModelWrongData(self.t('Bad SSL certificate data'))
Ivan Mahonin 9e1462
    if self.get_by_data(data):
Ivan Mahonin 9e1462
      raise exception.ModelWrongData(self.t('SSL Certificate is not unique'))
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
    self.connection.execute(
Ivan Mahonin 9e1462
      'INSERT INTO %T SET `user_id`=%d, `data`=%s',
Ivan Mahonin 9e1462
      self.table(), user.id, data )
Ivan Mahonin 9e1462
    id = self.connection.insert_id()
Ivan Mahonin 9e1462
    self.reset_cache(id, data)
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
    return self.get_by_id(id, user)
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def get_by_id(self, id, user = None):
Ivan Mahonin 9e1462
    assert(type(id) is int)
Ivan Mahonin 9e1462
    row = self.connection.cache.row(self.table(), id)
Ivan Mahonin 9e1462
    if not row:
Ivan Mahonin 9e1462
      return None
Ivan Mahonin 9e1462
    if not user:
Ivan Mahonin 9e1462
      user = self.model.users.get_by_id(row['user_id'])
Ivan Mahonin 9e1462
      if not user:
Ivan Mahonin 9e1462
        return None
Ivan Mahonin 9e1462
    if not self.can_read(user):
Ivan Mahonin 9e1462
      return None
Ivan Mahonin 9e1462
    return Sslcert(self, row, user)
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def get_by_data(self, data):
Ivan Mahonin 9e1462
    assert(type(data) is str)
Ivan Mahonin 9e1462
    rows = self.connection.cache.select(self.table(), {'data': data})
Ivan Mahonin 9e1462
    if not rows or len(rows) > 1:
Ivan Mahonin 9e1462
      return None
Ivan Mahonin 9e1462
    return Sslcert(self, rows[0])
Ivan Mahonin 9e1462
Ivan Mahonin 9e1462
  def get_list(self, user):
Ivan Mahonin 9e1462
    assert(type(user) is User)
Ivan Mahonin 9e1462
    result = list()
Ivan Mahonin 9e1462
    if not self.can_read(user):
Ivan Mahonin 9e1462
      return result
Ivan Mahonin 9e1462
    rows = self.connection.query_dict('SELECT * FROM %T WHERE `user_id`=%d ORDER BY `data`', self.table(), user.id)
Ivan Mahonin 9e1462
    for row in rows:
Ivan Mahonin 9e1462
      result.append(Sslcert(self, row, user))
Ivan Mahonin 9e1462
    return result
Ivan Mahonin 9e1462