xref: /openbmc/webui-vue/src/views/ProfileSettings/ProfileSettings.vue (revision bcb0ab4f1e933795e53da7c28ca75382c94f9af9)
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
27*bcb0ab4fSDamian Celico              id="input-group-0"
28*bcb0ab4fSDamian Celico              :label="$t('pageProfileSettings.currentPassword')"
29*bcb0ab4fSDamian Celico              label-for="input-0"
30*bcb0ab4fSDamian Celico            >
31*bcb0ab4fSDamian Celico              <input-password-toggle>
32*bcb0ab4fSDamian Celico                <b-form-input
33*bcb0ab4fSDamian Celico                  id="old-password"
34*bcb0ab4fSDamian Celico                  v-model="form.currentPassword"
35*bcb0ab4fSDamian Celico                  type="password"
36*bcb0ab4fSDamian Celico                  data-test-id="profileSettings-input-ocurrentPassword"
37*bcb0ab4fSDamian Celico                  class="form-control-with-button"
38*bcb0ab4fSDamian Celico                />
39*bcb0ab4fSDamian Celico              </input-password-toggle>
40*bcb0ab4fSDamian Celico            </b-form-group>
41*bcb0ab4fSDamian 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"
60b1f559f0SSukanya Pandey                  :state="getValidationState($v.form.newPassword)"
614334d260SSukanya Pandey                  data-test-id="profileSettings-input-newPassword"
625ea16782SDixsie Wolmers                  class="form-control-with-button"
63b1f559f0SSukanya Pandey                  @input="$v.form.newPassword.$touch()"
64b1f559f0SSukanya Pandey                />
65b1f559f0SSukanya Pandey                <b-form-invalid-feedback role="alert">
66b1f559f0SSukanya Pandey                  <template
67b1f559f0SSukanya Pandey                    v-if="
68b1f559f0SSukanya Pandey                      !$v.form.newPassword.minLength ||
69b1f559f0SSukanya Pandey                      !$v.form.newPassword.maxLength
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"
92b1f559f0SSukanya Pandey                  :state="getValidationState($v.form.confirmPassword)"
934334d260SSukanya Pandey                  data-test-id="profileSettings-input-confirmPassword"
945ea16782SDixsie Wolmers                  class="form-control-with-button"
95b1f559f0SSukanya Pandey                  @input="$v.form.confirmPassword.$touch()"
96b1f559f0SSukanya Pandey                />
97b1f559f0SSukanya Pandey                <b-form-invalid-feedback role="alert">
98fc16f3c2SSukanya Pandey                  <template v-if="!$v.form.confirmPassword.sameAsPassword">
99fc16f3c2SSukanya Pandey                    {{ $t('pageProfileSettings.passwordsDoNotMatch') }}
100b1f559f0SSukanya Pandey                  </template>
101b1f559f0SSukanya Pandey                </b-form-invalid-feedback>
102b1f559f0SSukanya Pandey              </input-password-toggle>
103b1f559f0SSukanya Pandey            </b-form-group>
104b1f559f0SSukanya Pandey          </page-section>
105b1f559f0SSukanya Pandey        </b-col>
106b1f559f0SSukanya Pandey      </b-row>
107fc16f3c2SSukanya Pandey      <page-section :section-title="$t('pageProfileSettings.timezoneDisplay')">
108fc16f3c2SSukanya Pandey        <p>{{ $t('pageProfileSettings.timezoneDisplayDesc') }}</p>
109fc16f3c2SSukanya Pandey        <b-row>
110fc16f3c2SSukanya Pandey          <b-col md="9" lg="8" xl="9">
111fc16f3c2SSukanya Pandey            <b-form-group :label="$t('pageProfileSettings.timezone')">
112fc16f3c2SSukanya Pandey              <b-form-radio
113fc16f3c2SSukanya Pandey                v-model="form.isUtcDisplay"
114fc16f3c2SSukanya Pandey                :value="true"
1154334d260SSukanya Pandey                data-test-id="profileSettings-radio-defaultUTC"
116fc16f3c2SSukanya Pandey              >
117fc16f3c2SSukanya Pandey                {{ $t('pageProfileSettings.defaultUTC') }}
118fc16f3c2SSukanya Pandey              </b-form-radio>
119fc16f3c2SSukanya Pandey              <b-form-radio
120fc16f3c2SSukanya Pandey                v-model="form.isUtcDisplay"
121fc16f3c2SSukanya Pandey                :value="false"
1224334d260SSukanya Pandey                data-test-id="profileSettings-radio-browserOffset"
123fc16f3c2SSukanya Pandey              >
124fc16f3c2SSukanya Pandey                {{
125fc16f3c2SSukanya Pandey                  $t('pageProfileSettings.browserOffset', {
126602e98aaSDerick Montague                    timezone,
127fc16f3c2SSukanya Pandey                  })
128fc16f3c2SSukanya Pandey                }}
129fc16f3c2SSukanya Pandey              </b-form-radio>
130fc16f3c2SSukanya Pandey            </b-form-group>
131fc16f3c2SSukanya Pandey          </b-col>
132fc16f3c2SSukanya Pandey        </b-row>
133fc16f3c2SSukanya Pandey      </page-section>
1344334d260SSukanya Pandey      <b-button
1354334d260SSukanya Pandey        variant="primary"
1364334d260SSukanya Pandey        type="submit"
1374334d260SSukanya Pandey        data-test-id="profileSettings-button-saveSettings"
1384334d260SSukanya Pandey      >
139fc16f3c2SSukanya Pandey        {{ $t('global.action.saveSettings') }}
140b1f559f0SSukanya Pandey      </b-button>
141b1f559f0SSukanya Pandey    </b-form>
142b1f559f0SSukanya Pandey  </b-container>
143b1f559f0SSukanya Pandey</template>
144b1f559f0SSukanya Pandey
145b1f559f0SSukanya Pandey<script>
146b1f559f0SSukanya Pandeyimport BVToastMixin from '@/components/Mixins/BVToastMixin';
147b1f559f0SSukanya Pandeyimport InputPasswordToggle from '@/components/Global/InputPasswordToggle';
148570a74f6SSukanya Pandeyimport { maxLength, minLength, sameAs } from 'vuelidate/lib/validators';
149b93608dbSSukanya Pandeyimport LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
150fcda2001SDixsie Wolmersimport LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin';
151b93608dbSSukanya Pandeyimport PageTitle from '@/components/Global/PageTitle';
152b93608dbSSukanya Pandeyimport PageSection from '@/components/Global/PageSection';
153b93608dbSSukanya Pandeyimport VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
154b1f559f0SSukanya Pandey
155b1f559f0SSukanya Pandeyexport default {
156b1f559f0SSukanya Pandey  name: 'ProfileSettings',
157b93608dbSSukanya Pandey  components: { InputPasswordToggle, PageSection, PageTitle },
158fcda2001SDixsie Wolmers  mixins: [
159fcda2001SDixsie Wolmers    BVToastMixin,
160fcda2001SDixsie Wolmers    LocalTimezoneLabelMixin,
161fcda2001SDixsie Wolmers    LoadingBarMixin,
162602e98aaSDerick Montague    VuelidateMixin,
163fcda2001SDixsie Wolmers  ],
164b1f559f0SSukanya Pandey  data() {
165b1f559f0SSukanya Pandey    return {
166b1f559f0SSukanya Pandey      form: {
167b1f559f0SSukanya Pandey        newPassword: '',
168fc16f3c2SSukanya Pandey        confirmPassword: '',
169*bcb0ab4fSDamian Celico        currentPassword: '',
170602e98aaSDerick Montague        isUtcDisplay: this.$store.getters['global/isUtcDisplay'],
171602e98aaSDerick Montague      },
172b1f559f0SSukanya Pandey    };
173b1f559f0SSukanya Pandey  },
174b93608dbSSukanya Pandey  computed: {
175b93608dbSSukanya Pandey    username() {
176b93608dbSSukanya Pandey      return this.$store.getters['global/username'];
177b93608dbSSukanya Pandey    },
178b93608dbSSukanya Pandey    passwordRequirements() {
179b440616cSSandeepa Singh      return this.$store.getters['userManagement/accountPasswordRequirements'];
180fc16f3c2SSukanya Pandey    },
181fc16f3c2SSukanya Pandey    timezone() {
182fcda2001SDixsie Wolmers      return this.localOffset();
183602e98aaSDerick Montague    },
184b93608dbSSukanya Pandey  },
185b93608dbSSukanya Pandey  created() {
186b93608dbSSukanya Pandey    this.startLoader();
187b93608dbSSukanya Pandey    this.$store
188b440616cSSandeepa Singh      .dispatch('userManagement/getAccountSettings')
189b93608dbSSukanya Pandey      .finally(() => this.endLoader());
190b93608dbSSukanya Pandey  },
191b1f559f0SSukanya Pandey  validations() {
192b1f559f0SSukanya Pandey    return {
193b1f559f0SSukanya Pandey      form: {
194b1f559f0SSukanya Pandey        newPassword: {
195b1f559f0SSukanya Pandey          minLength: minLength(this.passwordRequirements.minLength),
196602e98aaSDerick Montague          maxLength: maxLength(this.passwordRequirements.maxLength),
197b1f559f0SSukanya Pandey        },
198b1f559f0SSukanya Pandey        confirmPassword: {
199602e98aaSDerick Montague          sameAsPassword: sameAs('newPassword'),
200602e98aaSDerick Montague        },
201602e98aaSDerick Montague      },
202b1f559f0SSukanya Pandey    };
203b1f559f0SSukanya Pandey  },
204b1f559f0SSukanya Pandey  methods: {
205fc16f3c2SSukanya Pandey    saveNewPasswordInputData() {
206fc16f3c2SSukanya Pandey      this.$v.form.confirmPassword.$touch();
207fc16f3c2SSukanya Pandey      this.$v.form.newPassword.$touch();
208b1f559f0SSukanya Pandey      if (this.$v.$invalid) return;
209b1f559f0SSukanya Pandey      let userData = {
210b1f559f0SSukanya Pandey        originalUsername: this.username,
211602e98aaSDerick Montague        password: this.form.newPassword,
212b1f559f0SSukanya Pandey      };
213b1f559f0SSukanya Pandey
214b1f559f0SSukanya Pandey      this.$store
215b440616cSSandeepa Singh        .dispatch('userManagement/updateUser', userData)
216602e98aaSDerick Montague        .then((message) => {
217*bcb0ab4fSDamian Celico          (this.form.newPassword = ''),
218*bcb0ab4fSDamian Celico            (this.form.confirmPassword = ''),
219*bcb0ab4fSDamian Celico            (this.form.currentPassword = '');
2209422e1a6SSukanya Pandey          this.$v.$reset();
2219422e1a6SSukanya Pandey          this.successToast(message);
222*bcb0ab4fSDamian Celico          this.$store.dispatch('authentication/logout');
2239422e1a6SSukanya Pandey        })
224b1f559f0SSukanya Pandey        .catch(({ message }) => this.errorToast(message));
225fc16f3c2SSukanya Pandey    },
226fc16f3c2SSukanya Pandey    saveTimeZonePrefrenceData() {
227fc16f3c2SSukanya Pandey      localStorage.setItem('storedUtcDisplay', this.form.isUtcDisplay);
228fc16f3c2SSukanya Pandey      this.$store.commit('global/setUtcTime', this.form.isUtcDisplay);
229fc16f3c2SSukanya Pandey      this.successToast(
23077e64901SSandeepa Singh        this.$t('pageProfileSettings.toast.successUpdatingTimeZone')
231fc16f3c2SSukanya Pandey      );
232fc16f3c2SSukanya Pandey    },
233fc16f3c2SSukanya Pandey    submitForm() {
234*bcb0ab4fSDamian Celico      if (
235*bcb0ab4fSDamian Celico        this.form.confirmPassword &&
236*bcb0ab4fSDamian Celico        this.form.newPassword &&
237*bcb0ab4fSDamian Celico        this.form.currentPassword
238*bcb0ab4fSDamian Celico      ) {
239*bcb0ab4fSDamian Celico        this.confirmAuthenticate();
240fc16f3c2SSukanya Pandey      }
241*bcb0ab4fSDamian Celico      if (
242*bcb0ab4fSDamian Celico        this.$store.getters['global/isUtcDisplay'] != this.form.isUtcDisplay
243*bcb0ab4fSDamian Celico      ) {
244fc16f3c2SSukanya Pandey        this.saveTimeZonePrefrenceData();
245*bcb0ab4fSDamian Celico      }
246*bcb0ab4fSDamian Celico    },
247*bcb0ab4fSDamian Celico    confirmAuthenticate() {
248*bcb0ab4fSDamian Celico      this.$v.form.newPassword.$touch();
249*bcb0ab4fSDamian Celico      if (this.$v.$invalid) return;
250*bcb0ab4fSDamian Celico
251*bcb0ab4fSDamian Celico      const username = this.username;
252*bcb0ab4fSDamian Celico      const password = this.form.currentPassword;
253*bcb0ab4fSDamian Celico
254*bcb0ab4fSDamian Celico      this.$store
255*bcb0ab4fSDamian Celico        .dispatch('authentication/login', { username, password })
256*bcb0ab4fSDamian Celico        .then(() => {
257*bcb0ab4fSDamian Celico          this.saveNewPasswordInputData();
258*bcb0ab4fSDamian Celico        })
259*bcb0ab4fSDamian Celico        .catch(() => {
260*bcb0ab4fSDamian Celico          this.$v.$reset();
261*bcb0ab4fSDamian Celico          this.errorToast(
262*bcb0ab4fSDamian Celico            this.$t('pageProfileSettings.toast.wrongCredentials')
263*bcb0ab4fSDamian Celico          );
264*bcb0ab4fSDamian Celico        });
265602e98aaSDerick Montague    },
266602e98aaSDerick Montague  },
267b1f559f0SSukanya Pandey};
268b1f559f0SSukanya Pandey</script>
269