Projekt

Obecné

Profil

Stáhnout (6.02 KB) Statistiky
| Větev: | Tag: | Revize:
1 dd56c333 Jan Pašek
Vue.use(VueLoading);
2
Vue.component('loading', VueLoading)
3 e75db9cd Jan Pašek
4 dd56c333 Jan Pašek
// certificate listing app VUE instance
5
var certificateListingApp = new Vue({
6
    el: "#certificateListingPage",
7
    data: {
8 a13b1c2d Jan Pašek
        certificatesPerPage: 20,
9 6db8a3cb Jan Pašek
        hasNextPage: false,
10
        hasPreviousPage: false,
11
        currentPage: 1,
12 6b69049b Jan Pašek
        displayFilters: false,
13 dd56c333 Jan Pašek
        loading: true,
14
        // list of all certificates to be displayed in the list
15
        certificates: [],
16 a13b1c2d Jan Pašek
        successMessage: '',
17
        filtering: {
18
            type: {
19
                RootCA: false,
20
                IntermediateCA: false,
21
                EndCertificate: false,
22
            },
23
            usage: {
24
                CA: false,
25
                authentication: false,
26
                digitalSignature: false,
27
                SSL: false,
28
            },
29
            CN: "",
30
        }
31 dd56c333 Jan Pašek
    },
32 6db8a3cb Jan Pašek
    methods: {
33
        onNextPage: async function () {
34
            this.currentPage++;
35
            await this.loadPage(this.currentPage, this.certificatesPerPage);
36
        },
37 a13b1c2d Jan Pašek
        reloadData: async function() {
38
            await this.loadPage(this.currentPage, this.certificatesPerPage);
39
        },
40 6db8a3cb Jan Pašek
        onPreviousPage: async function () {
41
            this.currentPage--;
42
            await this.loadPage(this.currentPage, this.certificatesPerPage);
43
        },
44
        loadPage: async function (page, perPage) {
45
            // download a list of all available certificates and display them in the table
46 dde3db30 Jan Pašek
            this.errorMessage = "";
47 6db8a3cb Jan Pašek
            try {
48 a13b1c2d Jan Pašek
                var params = {
49
                    page: page - 1,
50
                    per_page: perPage
51
                };
52
                var filtering = this.getFilterParameter();
53
                if (filtering != null) params.filtering = filtering;
54 6db8a3cb Jan Pašek
                const response = await axios.get(API_URL + "certificates", {
55 a13b1c2d Jan Pašek
                    params: params
56 6db8a3cb Jan Pašek
                });
57
                if (response.data["success"]) {
58
                    this.certificates = [];
59
                    response.data["data"].forEach(item => certificateListingApp.certificates.push(item));
60
                    this.hasPreviousPage = page > 1;
61
                    this.hasNextPage = await this.checkIfNextPage(page, this.certificatesPerPage);
62
                }
63 dde3db30 Jan Pašek
                else {
64
                    this.errorMessage = "Error occurred while loading certificate page.";
65
                    console.error(response.data["data"]);
66
                }
67 6db8a3cb Jan Pašek
            } catch (error) {
68 dde3db30 Jan Pašek
                this.errorMessage = "Error occurred while loading certificate page.";
69 6db8a3cb Jan Pašek
                console.log(error);
70
            }
71
        },
72 a13b1c2d Jan Pašek
        getFilterParameter: function () {
73
            var type = [];
74
            if (this.filtering.type.EndCertificate) type.push("end");
75
            if (this.filtering.type.RootCA) type.push("root");
76
            if (this.filtering.type.IntermediateCA) type.push("inter");
77
78
            var usage = [];
79
            if (this.filtering.usage.CA) usage.push("CA");
80
            if (this.filtering.usage.SSL) usage.push("SSL");
81
            if (this.filtering.usage.digitalSignature) usage.push("digitalSignature");
82
            if (this.filtering.usage.authentication) usage.push("authentication");
83
84
            var filtering = {};
85
            if (usage.length > 0) filtering.usage = usage;
86
            if (type.length > 0) filtering.type = type;
87
88
            if (this.filtering.CN.length > 0) filtering.CN = this.filtering.CN;
89
90
            if (this.filtering.hasOwnProperty("type") ||
91
                this.filtering.hasOwnProperty("usage") ||
92
                this.filtering.hasOwnProperty("CN"))
93
                return filtering;
94
            else
95
                return null;
96
        },
97 6b69049b Jan Pašek
        toggleFilters: function (){
98
            this.displayFilters = ~this.displayFilters;
99
        },
100 a13b1c2d Jan Pašek
        clearFilters: async function () {
101
            this.filtering = {
102
                type: {
103
                    RootCA: false,
104
                    IntermediateCA: false,
105
                    EndCertificate: false,
106
                },
107
                usage: {
108
                    CA: false,
109
                    authentication: false,
110
                    digitalSignature: false,
111
                    SSL: false,
112
                },
113
                CN: "",
114
            }
115
            await this.reloadData();
116
        },
117 6db8a3cb Jan Pašek
        checkIfNextPage: async function(page, perPage) {
118
            try {
119
                const response = await axios.get(API_URL + "certificates", {
120
                    params: {
121
                        page: page,
122
                        per_page: perPage
123
                    }
124
                });
125
                if (response.data["success"]) {
126
                    return response.data["data"].length > 0;
127
                }
128 dde3db30 Jan Pašek
                this.errorMessage = "Error occurred while loading certificate page.";
129 6db8a3cb Jan Pašek
                return false;
130
            } catch (error) {
131 dde3db30 Jan Pašek
                this.errorMessage = "Error occurred while loading certificate page.";
132
                console.error(error);
133 6db8a3cb Jan Pašek
                return false;
134
            }
135
        }
136
    },
137 37a8e8ec Jan Pašek
    mounted: async function () {
138 dd56c333 Jan Pašek
        const params = window.location.search;
139
        if (params !== "") {
140
            const urlParams = new URLSearchParams(params);
141
            if (urlParams.get("success") != null) this.successMessage = urlParams.get("success");
142 81e73700 Jan Pašek
143 dd56c333 Jan Pašek
            // the following code is necessary to dismiss the alert when the page is reloaded
144
            const nextURL = '/static/index.html';
145
            const nextTitle = 'X.509 Certificate Management';
146
            const nextState = {additionalInformation: 'Updated the URL with JS'};
147 81e73700 Jan Pašek
148 dd56c333 Jan Pašek
            // This will create a new entry in the browser's history, without reloading
149
            window.history.pushState(nextState, nextTitle, nextURL);
150 81e73700 Jan Pašek
151 dd56c333 Jan Pašek
            // This will replace the current entry in the browser's history, without reloading
152
            window.history.replaceState(nextState, nextTitle, nextURL);
153 e75db9cd Jan Pašek
        }
154
155 dd56c333 Jan Pašek
        // download a list of all available certificates and display them in the table
156 6db8a3cb Jan Pašek
        await this.loadPage(1, this.certificatesPerPage);
157
        this.loading = false;
158 dd56c333 Jan Pašek
    }
159
});