1window.angular && (function(angular) {
2  'use strict';
3
4  angular.module('app.common.directives').directive('certificate', [
5    'APIUtils',
6    function(APIUtils) {
7      return {
8        'restrict': 'E',
9        'template': require('./certificate.html'),
10        'scope': {'cert': '=', 'reload': '&'},
11        'controller': [
12          '$scope', 'APIUtils', 'toastService', 'Constants', '$uibModal',
13          function($scope, APIUtils, toastService, Constants, $uibModal) {
14            var certificateType = 'PEM';
15            var availableCertificateTypes = Constants.CERTIFICATE_TYPES;
16
17            /**
18             * This function is needed to map the backend Description to what
19             * should appear in the GUI. This is needed specifically for CA
20             * certificate types. The backend description for the certificate
21             * type is 'TrustStore Certificate', this function will make sure we
22             * display 'CA Certificate' on the frontend
23             * @param {string} : certificate Description property
24             * @returns {string} : certificate name that should appear on GUI
25             */
26            $scope.getCertificateName = function(certificateDescription) {
27              var matched =
28                  availableCertificateTypes.find(function(certificate) {
29                    return certificate.Description === certificateDescription;
30                  });
31              if (matched === undefined) {
32                return '';
33              } else {
34                return matched.name;
35              }
36            };
37
38            $scope.isDeletable = function(certificate) {
39              return certificate.Description == 'TrustStore Certificate';
40            };
41
42            $scope.confirmDeleteCert = function(certificate) {
43              initRemoveModal(certificate);
44            };
45
46            /**
47             * Intiate remove certificate modal
48             * @param {*} certificate
49             */
50            function initRemoveModal(certificate) {
51              const template = require('./certificate-modal-remove.html');
52              $uibModal
53                  .open({
54                    template,
55                    windowTopClass: 'uib-modal',
56                    ariaLabelledBy: 'dialog_label',
57                    controllerAs: 'modalCtrl',
58                    controller: function() {
59                      this.certificate = certificate;
60                    }
61                  })
62                  .result
63                  .then(() => {
64                    deleteCert(certificate);
65                  })
66                  .catch(
67                      () => {
68                          // do nothing
69                      })
70            };
71
72            /**
73             * Removes certificate
74             * @param {*} certificate
75             */
76            function deleteCert(certificate) {
77              $scope.confirm_delete = false;
78              APIUtils.deleteRedfishObject(certificate['@odata.id'])
79                  .then(
80                      function(data) {
81                        $scope.loading = false;
82                        toastService.success(
83                            $scope.getCertificateName(certificate.Description) +
84                            ' was deleted.');
85                        $scope.reload();
86                      },
87                      function(error) {
88                        console.log(error);
89                        $scope.loading = false;
90                        toastService.error(
91                            'Unable to delete ' +
92                            $scope.getCertificateName(certificate.Description));
93                      });
94              return;
95            };
96
97            $scope.replaceCertificate = function(certificate) {
98              $scope.loading = true;
99              if (certificate.file.name.split('.').pop() !==
100                  certificateType.toLowerCase()) {
101                toastService.error(
102                    'Certificate must be replaced with a .pem file.');
103                return;
104              }
105              var file =
106                  document
107                      .getElementById(
108                          'upload_' + certificate.Description + certificate.Id)
109                      .files[0];
110              var reader = new FileReader();
111              reader.onloadend = function(e) {
112                var data = {};
113                data.CertificateString = e.target.result;
114                data.CertificateUri = {'@odata.id': certificate['@odata.id']};
115                data.CertificateType = certificateType;
116                APIUtils.replaceCertificate(data).then(
117                    function(data) {
118                      $scope.loading = false;
119                      toastService.success(
120                          $scope.getCertificateName(certificate.Description) +
121                          ' was replaced.');
122                      $scope.reload();
123                    },
124                    function(error) {
125                      console.log(error);
126                      $scope.loading = false;
127                      toastService.error(
128                          'Unable to replace ' +
129                          $scope.getCertificateName(certificate.Description));
130                    });
131              };
132              reader.readAsBinaryString(file);
133            };
134          }
135        ]
136      };
137    }
138  ]);
139})(window.angular);
140