Projekt

Obecné

Profil

Stáhnout (3.44 KB) Statistiky
| Větev: | Tag: | Revize:
1
from typing import List, Dict
2

    
3
from ..db_objects.certificate import Certificate
4
from ..dao.repository import IRepository
5
from db_manager import DBManager
6
from ..constants import *
7

    
8

    
9
class CertificateRepositoryImpl(IRepository):
10

    
11
    def create(self, common_name: str, valid_from: str, valid_to: str, pem_data: str,
12
               private_key_id: int, type_id: int, usages: Dict[int, bool]) -> bool:
13
        sql = (f"INSERT INTO {TAB_CERTIFICATES} ({COL_COMMON_NAME},{COL_VALID_FROM},{COL_VALID_TO},{COL_PEM_DATA},"
14
               f"{COL_PRIVATE_KEY_ID},{COL_TYPE_ID})"
15
               f"VALUES(?,?,?,?,?,?,?)")
16
        result = DBManager.create(sql, [common_name, valid_from, valid_to, pem_data, private_key_id, type_id], usages)
17
        return result
18

    
19
    def read(self, certificate_id: int = None):
20
        sql_extend = ""
21

    
22
        if certificate_id is not None:
23
            sql_extend = f" WHERE {COL_ID} = ?"
24

    
25
        sql = f"SELECT * FROM {TAB_CERTIFICATES}{sql_extend}"
26
        certificate_rows = DBManager.read(sql, certificate_id)
27

    
28
        certificates: list = []
29

    
30
        for certificate_row in certificate_rows:
31
            sql = f"SELECT * FROM {TAB_CERTIFICATE_USAGES} WHERE {COL_CERTIFICATE_ID} = ?"
32
            usage_rows = DBManager.read(sql, certificate_row[0])
33

    
34
            usage_dict: Dict[int, bool] = {}
35
            for usage_row in usage_rows:
36
                usage_dict[usage_row[1]] = True
37

    
38
            certificates.append(Certificate(certificate_row[0],
39
                                            certificate_row[1],
40
                                            certificate_row[2],
41
                                            certificate_row[3],
42
                                            certificate_row[4],
43
                                            certificate_row[5],
44
                                            certificate_row[6],
45
                                            usage_dict))
46

    
47
        if certificate_id is not None:
48
            return certificates[0]
49

    
50
        return certificates
51

    
52
    def update(self, certificate_id: int, common_name: str = None, valid_from: str = None, valid_to: str = None,
53
               pem_data: str = None, private_key_id: int = None, type_id: int = None,
54
               usages: Dict[int, bool] = None) -> bool:
55
        updated_list = []
56
        values = []
57
        if common_name is not None:
58
            updated_list.append(f"{COL_COMMON_NAME} = ?")
59
            values.append(common_name)
60
        if valid_from is not None:
61
            updated_list.append(f"{COL_VALID_FROM} = ?")
62
            values.append(valid_from)
63
        if valid_to is not None:
64
            updated_list.append(f"{COL_VALID_TO} = ?")
65
            values.append(valid_to)
66
        if pem_data is not None:
67
            updated_list.append(f"{COL_PEM_DATA} = ?")
68
            values.append(pem_data)
69
        if private_key_id is not None:
70
            updated_list.append(f"{COL_PRIVATE_KEY_ID} = ?")
71
            values.append(private_key_id)
72
        if type_id is not None:
73
            updated_list.append(f"{COL_TYPE_ID} = ?")
74
            values.append(type_id)
75

    
76
        updated_str = ", ".join(updated_list)
77
        sql = f"UPDATE {TAB_CERTIFICATES} SET {updated_str} WHERE {COL_ID} = ?"
78
        result = DBManager.update(sql, certificate_id, values, usages)
79
        return result
80

    
81
    def delete(self, certificate_id: int) -> bool:
82
        sql = f"DELETE FROM {TAB_CERTIFICATES} WHERE {COL_ID} = ?"
83
        result = DBManager.delete(sql, certificate_id, True)
84
        return result
(1-1/3)