|
| 1 | +/** |
| 2 | + * Copyright IBM Corp. 2016, 2025 |
| 3 | + * SPDX-License-Identifier: BUSL-1.1 |
| 4 | + */ |
| 5 | + |
| 6 | +import Component from '@glimmer/component'; |
| 7 | +import { action } from '@ember/object'; |
| 8 | +import { tracked } from '@glimmer/tracking'; |
| 9 | +import { service } from '@ember/service'; |
| 10 | +import isAfter from 'date-fns/isAfter'; |
| 11 | +import differenceInDays from 'date-fns/differenceInDays'; |
| 12 | +import localStorage from 'vault/lib/local-storage'; |
| 13 | +import timestamp from 'core/utils/timestamp'; |
| 14 | +import type VersionService from 'vault/services/version'; |
| 15 | + |
| 16 | +enum Banners { |
| 17 | + EXPIRED = 'expired', |
| 18 | + WARNING = 'warning', |
| 19 | + PKI = 'pki-only-info', |
| 20 | +} |
| 21 | + |
| 22 | +interface Args { |
| 23 | + expiry: string; |
| 24 | + autoloaded: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @module LicenseBanners |
| 29 | + * LicenseBanners components are used to display Vault-specific license messages |
| 30 | + * |
| 31 | + * @example |
| 32 | + * ```js |
| 33 | + * <LicenseBanners @expiry={expiryDate} /> |
| 34 | + * ``` |
| 35 | + * @param {string} expiry - RFC3339 date timestamp |
| 36 | + */ |
| 37 | + |
| 38 | +export default class LicenseBanners extends Component<Args> { |
| 39 | + @service declare readonly version: VersionService; |
| 40 | + |
| 41 | + @tracked warningDismissed = false; |
| 42 | + @tracked expiredDismissed = false; |
| 43 | + @tracked infoDismissed = false; |
| 44 | + |
| 45 | + banners = Banners; |
| 46 | + |
| 47 | + constructor(owner: unknown, args: Args) { |
| 48 | + super(owner, args); |
| 49 | + |
| 50 | + // reset and show a previously dismissed license banner if: |
| 51 | + // the version has been updated or the license has been updated (indicated by a change in the expiry date). |
| 52 | + const item = localStorage.getItem(this.dismissedBannerKey) ?? []; // returns warning, expired and/or pki-only-info |
| 53 | + // older entries will not be an array as it was either "expired" OR "warning" |
| 54 | + // with the addition of "pki-only-info", it can hold all values |
| 55 | + // this check maintains backwards compatibility with the previous format |
| 56 | + const bannerTypes = Array.isArray(item) ? item : [item]; |
| 57 | + |
| 58 | + bannerTypes.forEach((type) => { |
| 59 | + this.updateDismissType(type); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + get currentVersion() { |
| 64 | + return this.version.version; |
| 65 | + } |
| 66 | + |
| 67 | + get dismissedBannerKey() { |
| 68 | + return `dismiss-license-banner-${this.currentVersion}-${this.args.expiry}`; |
| 69 | + } |
| 70 | + |
| 71 | + get licenseExpired() { |
| 72 | + if (!this.args.expiry) return false; |
| 73 | + return isAfter(timestamp.now(), new Date(this.args.expiry)); |
| 74 | + } |
| 75 | + |
| 76 | + get licenseExpiringInDays() { |
| 77 | + // Anything more than 30 does not render a warning |
| 78 | + if (!this.args.expiry) return 99; |
| 79 | + return differenceInDays(new Date(this.args.expiry), timestamp.now()); |
| 80 | + } |
| 81 | + |
| 82 | + get isPKIOnly() { |
| 83 | + return this.version.hasPKIOnly; |
| 84 | + } |
| 85 | + |
| 86 | + @action |
| 87 | + dismissBanner(dismissAction: Banners) { |
| 88 | + // if a client's version changed their old localStorage key will still exists. |
| 89 | + localStorage.cleanupStorage('dismiss-license-banner', this.dismissedBannerKey); |
| 90 | + |
| 91 | + // updates localStorage and then updates the template by calling updateDismissType |
| 92 | + const item = localStorage.getItem(this.dismissedBannerKey) ?? []; |
| 93 | + // older entries will not be an array as it was either "expired" OR "warning" |
| 94 | + // with the addition of "pki-only-info", it can hold all values |
| 95 | + // this check maintains backwards compatibility with the previous format |
| 96 | + const bannerTypes = Array.isArray(item) ? item : [item]; |
| 97 | + localStorage.setItem(this.dismissedBannerKey, [...bannerTypes, dismissAction]); |
| 98 | + |
| 99 | + this.updateDismissType(dismissAction); |
| 100 | + } |
| 101 | + |
| 102 | + updateDismissType(dismissType?: Banners) { |
| 103 | + // updates tracked properties to update template |
| 104 | + switch (dismissType) { |
| 105 | + case this.banners.WARNING: |
| 106 | + this.warningDismissed = true; |
| 107 | + break; |
| 108 | + case this.banners.EXPIRED: |
| 109 | + this.expiredDismissed = true; |
| 110 | + break; |
| 111 | + case this.banners.PKI: |
| 112 | + this.infoDismissed = true; |
| 113 | + break; |
| 114 | + default: |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments