xref: /openbmc/webui-vue/src/views/ProfileSettings/ProfileSettings.vue (revision 69be824a756a42efc64ae05b7cc5ca0d83a1dee3)
1b1f559f0SSukanya Pandey<template>
2b1f559f0SSukanya Pandey  <b-container fluid="xl">
3b1f559f0SSukanya Pandey    <page-title />
4b1f559f0SSukanya Pandey
5b1f559f0SSukanya Pandey    <b-row>
6b1f559f0SSukanya Pandey      <b-col md="8" lg="8" xl="6">
7fc16f3c2SSukanya Pandey        <page-section
8fc16f3c2SSukanya Pandey          :section-title="$t('pageProfileSettings.profileInfoTitle')"
9fc16f3c2SSukanya Pandey        >
10b1f559f0SSukanya Pandey          <dl>
11fc16f3c2SSukanya Pandey            <dt>{{ $t('pageProfileSettings.username') }}</dt>
12b1f559f0SSukanya Pandey            <dd>
13b1f559f0SSukanya Pandey              {{ username }}
14b1f559f0SSukanya Pandey            </dd>
15b1f559f0SSukanya Pandey          </dl>
16b1f559f0SSukanya Pandey        </page-section>
17b1f559f0SSukanya Pandey      </b-col>
18b1f559f0SSukanya Pandey    </b-row>
19b1f559f0SSukanya Pandey
20b1f559f0SSukanya Pandey    <b-form @submit.prevent="submitForm">
21b1f559f0SSukanya Pandey      <b-row>
22b1f559f0SSukanya Pandey        <b-col sm="8" md="6" xl="3">
23fc16f3c2SSukanya Pandey          <page-section
24fc16f3c2SSukanya Pandey            :section-title="$t('pageProfileSettings.changePassword')"
25fc16f3c2SSukanya Pandey          >
26b1f559f0SSukanya Pandey            <b-form-group
27bcb0ab4fSDamian Celico              id="input-group-0"
28bcb0ab4fSDamian Celico              :label="$t('pageProfileSettings.currentPassword')"
29bcb0ab4fSDamian Celico              label-for="input-0"
30bcb0ab4fSDamian Celico            >
31bcb0ab4fSDamian Celico              <input-password-toggle>
32bcb0ab4fSDamian Celico                <b-form-input
33bcb0ab4fSDamian Celico                  id="old-password"
34bcb0ab4fSDamian Celico                  v-model="form.currentPassword"
35bcb0ab4fSDamian Celico                  type="password"
36bcb0ab4fSDamian Celico                  data-test-id="profileSettings-input-ocurrentPassword"
37bcb0ab4fSDamian Celico                  class="form-control-with-button"
38bcb0ab4fSDamian Celico                />
39bcb0ab4fSDamian Celico              </input-password-toggle>
40bcb0ab4fSDamian Celico            </b-form-group>
41bcb0ab4fSDamian Celico            <b-form-group
42b1f559f0SSukanya Pandey              id="input-group-1"
43fc16f3c2SSukanya Pandey              :label="$t('pageProfileSettings.newPassword')"
44b1f559f0SSukanya Pandey              label-for="input-1"
45b1f559f0SSukanya Pandey            >
46b1f559f0SSukanya Pandey              <b-form-text id="password-help-block">
47b1f559f0SSukanya Pandey                {{
48b440616cSSandeepa Singh                  $t('pageUserManagement.modal.passwordMustBeBetween', {
49b1f559f0SSukanya Pandey                    min: passwordRequirements.minLength,
50602e98aaSDerick Montague                    max: passwordRequirements.maxLength,
51b1f559f0SSukanya Pandey                  })
52b1f559f0SSukanya Pandey                }}
53b1f559f0SSukanya Pandey              </b-form-text>
54b1f559f0SSukanya Pandey              <input-password-toggle>
55b1f559f0SSukanya Pandey                <b-form-input
56b1f559f0SSukanya Pandey                  id="password"
57b1f559f0SSukanya Pandey                  v-model="form.newPassword"
58b1f559f0SSukanya Pandey                  type="password"
59b1f559f0SSukanya Pandey                  aria-describedby="password-help-block"
60de23ea23SSurya V                  :state="getValidationState(v$.form.newPassword)"
614334d260SSukanya Pandey                  data-test-id="profileSettings-input-newPassword"
625ea16782SDixsie Wolmers                  class="form-control-with-button"
63de23ea23SSurya V                  @input="v$.form.newPassword.$touch()"
64b1f559f0SSukanya Pandey                />
65b1f559f0SSukanya Pandey                <b-form-invalid-feedback role="alert">
66b1f559f0SSukanya Pandey                  <template
67b1f559f0SSukanya Pandey                    v-if="
68*69be824aSSurya Venkatesan                      v$.form.newPassword.minLength.$invalid ||
69*69be824aSSurya Venkatesan                      v$.form.newPassword.maxLength.$invalid
70b1f559f0SSukanya Pandey                    "
71b1f559f0SSukanya Pandey                  >
72b1f559f0SSukanya Pandey                    {{
73fc16f3c2SSukanya Pandey                      $t('pageProfileSettings.newPassLabelTextInfo', {
74b1f559f0SSukanya Pandey                        min: passwordRequirements.minLength,
75602e98aaSDerick Montague                        max: passwordRequirements.maxLength,
76b1f559f0SSukanya Pandey                      })
77b1f559f0SSukanya Pandey                    }}
78b1f559f0SSukanya Pandey                  </template>
79b1f559f0SSukanya Pandey                </b-form-invalid-feedback>
80b1f559f0SSukanya Pandey              </input-password-toggle>
81b1f559f0SSukanya Pandey            </b-form-group>
82b1f559f0SSukanya Pandey            <b-form-group
83b1f559f0SSukanya Pandey              id="input-group-2"
84fc16f3c2SSukanya Pandey              :label="$t('pageProfileSettings.confirmPassword')"
85b1f559f0SSukanya Pandey              label-for="input-2"
86b1f559f0SSukanya Pandey            >
87b1f559f0SSukanya Pandey              <input-password-toggle>
88b1f559f0SSukanya Pandey                <b-form-input
89b1f559f0SSukanya Pandey                  id="password-confirmation"
90b1f559f0SSukanya Pandey                  v-model="form.confirmPassword"
91b1f559f0SSukanya Pandey                  type="password"
92de23ea23SSurya V                  :state="getValidationState(v$.form.confirmPassword)"
934334d260SSukanya Pandey                  data-test-id="profileSettings-input-confirmPassword"
945ea16782SDixsie Wolmers                  class="form-control-with-button"
95de23ea23SSurya V                  @input="v$.form.confirmPassword.$touch()"
96b1f559f0SSukanya Pandey                />
97b1f559f0SSukanya Pandey                <b-form-invalid-feedback role="alert">
98*69be824aSSurya Venkatesan                  <template
99*69be824aSSurya Venkatesan                    v-if="v$.form.confirmPassword.sameAsPassword.$invalid"
100*69be824aSSurya Venkatesan                  >
101fc16f3c2SSukanya Pandey                    {{ $t('pageProfileSettings.passwordsDoNotMatch') }}
102b1f559f0SSukanya Pandey                  </template>
103b1f559f0SSukanya Pandey                </b-form-invalid-feedback>
104b1f559f0SSukanya Pandey              </input-password-toggle>
105b1f559f0SSukanya Pandey            </b-form-group>
106b1f559f0SSukanya Pandey          </page-section>
107b1f559f0SSukanya Pandey        </b-col>
108b1f559f0SSukanya Pandey      </b-row>
109fc16f3c2SSukanya Pandey      <page-section :section-title="$t('pageProfileSettings.timezoneDisplay')">
110fc16f3c2SSukanya Pandey        <p>{{ $t('pageProfileSettings.timezoneDisplayDesc') }}</p>
111fc16f3c2SSukanya Pandey        <b-row>
112fc16f3c2SSukanya Pandey          <b-col md="9" lg="8" xl="9">
113fc16f3c2SSukanya Pandey            <b-form-group :label="$t('pageProfileSettings.timezone')">
114fc16f3c2SSukanya Pandey              <b-form-radio
115fc16f3c2SSukanya Pandey                v-model="form.isUtcDisplay"
116fc16f3c2SSukanya Pandey                :value="true"
1174334d260SSukanya Pandey                data-test-id="profileSettings-radio-defaultUTC"
118fc16f3c2SSukanya Pandey              >
119fc16f3c2SSukanya Pandey                {{ $t('pageProfileSettings.defaultUTC') }}
120fc16f3c2SSukanya Pandey              </b-form-radio>
121fc16f3c2SSukanya Pandey              <b-form-radio
122fc16f3c2SSukanya Pandey                v-model="form.isUtcDisplay"
123fc16f3c2SSukanya Pandey                :value="false"
1244334d260SSukanya Pandey                data-test-id="profileSettings-radio-browserOffset"
125fc16f3c2SSukanya Pandey              >
126fc16f3c2SSukanya Pandey                {{
127fc16f3c2SSukanya Pandey                  $t('pageProfileSettings.browserOffset', {
128602e98aaSDerick Montague                    timezone,
129fc16f3c2SSukanya Pandey                  })
130fc16f3c2SSukanya Pandey                }}
131fc16f3c2SSukanya Pandey              </b-form-radio>
132fc16f3c2SSukanya Pandey            </b-form-group>
133fc16f3c2SSukanya Pandey          </b-col>
134fc16f3c2SSukanya Pandey        </b-row>
135fc16f3c2SSukanya Pandey      </page-section>
1364334d260SSukanya Pandey      <b-button
1374334d260SSukanya Pandey        variant="primary"
1384334d260SSukanya Pandey        type="submit"
1394334d260SSukanya Pandey        data-test-id="profileSettings-button-saveSettings"
1404334d260SSukanya Pandey      >
141fc16f3c2SSukanya Pandey        {{ $t('global.action.saveSettings') }}
142b1f559f0SSukanya Pandey      </b-button>
143b1f559f0SSukanya Pandey    </b-form>
144b1f559f0SSukanya Pandey  </b-container>
145b1f559f0SSukanya Pandey</template>
146b1f559f0SSukanya Pandey
147b1f559f0SSukanya Pandey<script>
148b1f559f0SSukanya Pandeyimport BVToastMixin from '@/components/Mixins/BVToastMixin';
149b1f559f0SSukanya Pandeyimport InputPasswordToggle from '@/components/Global/InputPasswordToggle';
1507d6b44cbSEd Tanousimport { maxLength, minLength, sameAs } from '@vuelidate/validators';
151b93608dbSSukanya Pandeyimport LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
152fcda2001SDixsie Wolmersimport LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin';
153b93608dbSSukanya Pandeyimport PageTitle from '@/components/Global/PageTitle';
154b93608dbSSukanya Pandeyimport PageSection from '@/components/Global/PageSection';
155b93608dbSSukanya Pandeyimport VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
1567d6b44cbSEd Tanousimport { useVuelidate } from '@vuelidate/core';
157de23ea23SSurya Vimport { useI18n } from 'vue-i18n';
158de23ea23SSurya Vimport i18n from '@/i18n';
159b1f559f0SSukanya Pandey
160b1f559f0SSukanya Pandeyexport default {
161b1f559f0SSukanya Pandey  name: 'ProfileSettings',
162b93608dbSSukanya Pandey  components: { InputPasswordToggle, PageSection, PageTitle },
163fcda2001SDixsie Wolmers  mixins: [
164fcda2001SDixsie Wolmers    BVToastMixin,
165fcda2001SDixsie Wolmers    LocalTimezoneLabelMixin,
166fcda2001SDixsie Wolmers    LoadingBarMixin,
167602e98aaSDerick Montague    VuelidateMixin,
168fcda2001SDixsie Wolmers  ],
1697d6b44cbSEd Tanous  setup() {
1707d6b44cbSEd Tanous    return {
1717d6b44cbSEd Tanous      v$: useVuelidate(),
1727d6b44cbSEd Tanous    };
1737d6b44cbSEd Tanous  },
174b1f559f0SSukanya Pandey  data() {
175b1f559f0SSukanya Pandey    return {
176de23ea23SSurya V      $t: useI18n().t,
177b1f559f0SSukanya Pandey      form: {
178b1f559f0SSukanya Pandey        newPassword: '',
179fc16f3c2SSukanya Pandey        confirmPassword: '',
180bcb0ab4fSDamian Celico        currentPassword: '',
181602e98aaSDerick Montague        isUtcDisplay: this.$store.getters['global/isUtcDisplay'],
182602e98aaSDerick Montague      },
183b1f559f0SSukanya Pandey    };
184b1f559f0SSukanya Pandey  },
185b93608dbSSukanya Pandey  computed: {
186b93608dbSSukanya Pandey    username() {
187b93608dbSSukanya Pandey      return this.$store.getters['global/username'];
188b93608dbSSukanya Pandey    },
189b93608dbSSukanya Pandey    passwordRequirements() {
190b440616cSSandeepa Singh      return this.$store.getters['userManagement/accountPasswordRequirements'];
191fc16f3c2SSukanya Pandey    },
192fc16f3c2SSukanya Pandey    timezone() {
193fcda2001SDixsie Wolmers      return this.localOffset();
194602e98aaSDerick Montague    },
195b93608dbSSukanya Pandey  },
196b93608dbSSukanya Pandey  created() {
197b93608dbSSukanya Pandey    this.startLoader();
198b93608dbSSukanya Pandey    this.$store
199b440616cSSandeepa Singh      .dispatch('userManagement/getAccountSettings')
200b93608dbSSukanya Pandey      .finally(() => this.endLoader());
201b93608dbSSukanya Pandey  },
202b1f559f0SSukanya Pandey  validations() {
203b1f559f0SSukanya Pandey    return {
204b1f559f0SSukanya Pandey      form: {
205b1f559f0SSukanya Pandey        newPassword: {
206b1f559f0SSukanya Pandey          minLength: minLength(this.passwordRequirements.minLength),
207602e98aaSDerick Montague          maxLength: maxLength(this.passwordRequirements.maxLength),
208b1f559f0SSukanya Pandey        },
209b1f559f0SSukanya Pandey        confirmPassword: {
210602e98aaSDerick Montague          sameAsPassword: sameAs('newPassword'),
211602e98aaSDerick Montague        },
212602e98aaSDerick Montague      },
213b1f559f0SSukanya Pandey    };
214b1f559f0SSukanya Pandey  },
215b1f559f0SSukanya Pandey  methods: {
216fc16f3c2SSukanya Pandey    saveNewPasswordInputData() {
217de23ea23SSurya V      this.v$.form.confirmPassword.$touch();
218de23ea23SSurya V      this.v$.form.newPassword.$touch();
219de23ea23SSurya V      if (this.v$.$invalid) return;
220b1f559f0SSukanya Pandey      let userData = {
221b1f559f0SSukanya Pandey        originalUsername: this.username,
222602e98aaSDerick Montague        password: this.form.newPassword,
223b1f559f0SSukanya Pandey      };
224b1f559f0SSukanya Pandey
225b1f559f0SSukanya Pandey      this.$store
226b440616cSSandeepa Singh        .dispatch('userManagement/updateUser', userData)
227602e98aaSDerick Montague        .then((message) => {
228bcb0ab4fSDamian Celico          (this.form.newPassword = ''),
229bcb0ab4fSDamian Celico            (this.form.confirmPassword = ''),
230bcb0ab4fSDamian Celico            (this.form.currentPassword = '');
231de23ea23SSurya V          this.v$.$reset();
2329422e1a6SSukanya Pandey          this.successToast(message);
233bcb0ab4fSDamian Celico          this.$store.dispatch('authentication/logout');
2349422e1a6SSukanya Pandey        })
235b1f559f0SSukanya Pandey        .catch(({ message }) => this.errorToast(message));
236fc16f3c2SSukanya Pandey    },
237fc16f3c2SSukanya Pandey    saveTimeZonePrefrenceData() {
238fc16f3c2SSukanya Pandey      localStorage.setItem('storedUtcDisplay', this.form.isUtcDisplay);
239fc16f3c2SSukanya Pandey      this.$store.commit('global/setUtcTime', this.form.isUtcDisplay);
240fc16f3c2SSukanya Pandey      this.successToast(
241de23ea23SSurya V        i18n.global.t('pageProfileSettings.toast.successUpdatingTimeZone'),
242fc16f3c2SSukanya Pandey      );
243fc16f3c2SSukanya Pandey    },
244fc16f3c2SSukanya Pandey    submitForm() {
245bcb0ab4fSDamian Celico      if (
246bcb0ab4fSDamian Celico        this.form.confirmPassword &&
247bcb0ab4fSDamian Celico        this.form.newPassword &&
248bcb0ab4fSDamian Celico        this.form.currentPassword
249bcb0ab4fSDamian Celico      ) {
250bcb0ab4fSDamian Celico        this.confirmAuthenticate();
251fc16f3c2SSukanya Pandey      }
252bcb0ab4fSDamian Celico      if (
253bcb0ab4fSDamian Celico        this.$store.getters['global/isUtcDisplay'] != this.form.isUtcDisplay
254bcb0ab4fSDamian Celico      ) {
255fc16f3c2SSukanya Pandey        this.saveTimeZonePrefrenceData();
256bcb0ab4fSDamian Celico      }
257bcb0ab4fSDamian Celico    },
258bcb0ab4fSDamian Celico    confirmAuthenticate() {
259de23ea23SSurya V      this.v$.form.newPassword.$touch();
260de23ea23SSurya V      if (this.v$.$invalid) return;
261bcb0ab4fSDamian Celico
262bcb0ab4fSDamian Celico      const username = this.username;
263bcb0ab4fSDamian Celico      const password = this.form.currentPassword;
264bcb0ab4fSDamian Celico
265bcb0ab4fSDamian Celico      this.$store
266bcb0ab4fSDamian Celico        .dispatch('authentication/login', { username, password })
267bcb0ab4fSDamian Celico        .then(() => {
268bcb0ab4fSDamian Celico          this.saveNewPasswordInputData();
269bcb0ab4fSDamian Celico        })
270bcb0ab4fSDamian Celico        .catch(() => {
271de23ea23SSurya V          this.v$.$reset();
272bcb0ab4fSDamian Celico          this.errorToast(
273de23ea23SSurya V            i18n.global.t('pageProfileSettings.toast.wrongCredentials'),
274bcb0ab4fSDamian Celico          );
275bcb0ab4fSDamian Celico        });
276602e98aaSDerick Montague    },
277602e98aaSDerick Montague  },
278b1f559f0SSukanya Pandey};
279b1f559f0SSukanya Pandey</script>
280