You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@paimon.apache.org by ni...@apache.org on 2024/06/17 03:03:02 UTC

(paimon-webui) branch main updated: [Feature] Introduce changing user password (#412)

This is an automated email from the ASF dual-hosted git repository.

nicholasjiang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-webui.git


The following commit(s) were added to refs/heads/main by this push:
     new 750518d4 [Feature] Introduce changing user password (#412)
750518d4 is described below

commit 750518d43a774f2af6732ebc72231dd2b0905853
Author: s7monk <34...@users.noreply.github.com>
AuthorDate: Mon Jun 17 11:02:57 2024 +0800

    [Feature] Introduce changing user password (#412)
---
 paimon-web-ui/src/api/models/user/index.ts         |  7 ++
 .../layouts/content/components/toolbar/index.tsx   | 91 ++++++++++++++++++++--
 paimon-web-ui/src/locales/en/modules/login.ts      |  2 +
 paimon-web-ui/src/locales/zh/modules/login.ts      |  2 +
 paimon-web-ui/src/store/user/index.ts              |  8 ++
 paimon-web-ui/src/views/login/use-form.ts          |  1 +
 6 files changed, 104 insertions(+), 7 deletions(-)

diff --git a/paimon-web-ui/src/api/models/user/index.ts b/paimon-web-ui/src/api/models/user/index.ts
index a855187e..e7f4522a 100644
--- a/paimon-web-ui/src/api/models/user/index.ts
+++ b/paimon-web-ui/src/api/models/user/index.ts
@@ -55,3 +55,10 @@ export function updateUser() {
 export function deleteUser(userId: number) {
   return httpRequest.delete!<unknown, UserDTO>(`/user/${userId}`)
 }
+
+/**
+ * # Change password
+ */
+export function changePassword(user: UserDTO) {
+  return httpRequest.post('/user/change/password', user)
+}
diff --git a/paimon-web-ui/src/layouts/content/components/toolbar/index.tsx b/paimon-web-ui/src/layouts/content/components/toolbar/index.tsx
index 863e0b66..79118ee4 100644
--- a/paimon-web-ui/src/layouts/content/components/toolbar/index.tsx
+++ b/paimon-web-ui/src/layouts/content/components/toolbar/index.tsx
@@ -17,10 +17,13 @@ under the License. */
 
 import { Language, LogoGithub, Moon, SunnyOutline } from '@vicons/ionicons5'
 import type { Router } from 'vue-router'
+import { NInput } from 'naive-ui'
 import { LANGUAGES } from '@/locales'
 import { useConfigStore } from '@/store/config'
 import { onLogout } from '@/api/models/login'
 import { useUserStore } from '@/store/user'
+import { changePassword } from '@/api/models/user'
+import type { UserDTO } from '@/api/models/user/types'
 
 // ts-ignore
 export default defineComponent({
@@ -28,6 +31,9 @@ export default defineComponent({
   setup() {
     const router: Router = useRouter()
     const { t, setLanguage } = useLocaleHooks()
+    const dialog = useDialog()
+    const message = useMessage()
+    const newPassword = ref('')
 
     const handleLink = () => {
       window.open('https://github.com/apache/paimon-webui')
@@ -55,6 +61,49 @@ export default defineComponent({
       })
     }
 
+    const handleChangePassword = () => {
+      const _dialogInst = dialog.create({
+        title: 'Change Password',
+        content: () => h(
+          NInput,
+          {
+            placeholder: 'Input you new password',
+            modelValue: newPassword.value,
+            onInput: (e: string) => {
+              newPassword.value = e
+            },
+          },
+        ),
+        positiveText: t('login.modify'),
+        onPositiveClick: async () => {
+          if (!newPassword.value || !newPassword.value.trim())
+            return message.error('new password is required')
+
+          const user: UserDTO = {
+            id: userStore.getUserId,
+            username: userStore.getUsername || '',
+            password: newPassword.value,
+            enabled: true,
+          }
+
+          _dialogInst.loading = true
+          try {
+            await changePassword(user)
+              .then(() => {
+                message.success('Change password successfully')
+                handleLogout()
+              })
+          }
+          catch (error) {
+            console.error('Failed to change password:', error)
+          }
+          finally {
+            _dialogInst.loading = false
+          }
+        },
+      })
+    }
+
     const avatarMenuOptions = [
       {
         label: t('login.logout'),
@@ -65,6 +114,15 @@ export default defineComponent({
           },
         },
       },
+      {
+        label: t('login.change_password'),
+        key: 'change-password',
+        props: {
+          onClick: () => {
+            handleChangePassword()
+          },
+        },
+      },
     ]
 
     return {
@@ -97,19 +155,38 @@ export default defineComponent({
         >
           <span>{this.t(`layout.${String(this.configStore.getCurrentTheme === 'light' ? 'dark' : 'light')}`)}</span>
         </n-popover>
-        <n-icon size="24" onClick={this.handleLink}>
-          <LogoGithub />
-        </n-icon>
-        <n-icon size="24" onClick={this.handleLanguage}>
-          <Language />
-        </n-icon>
+        <n-popover
+          trigger="hover"
+          placement="bottom"
+          v-slots={{
+            trigger: () => (
+              <n-icon size="24" onClick={this.handleLink}>
+                <LogoGithub />
+              </n-icon>
+            ),
+          }}
+        >
+          <span>GitHub</span>
+        </n-popover>
+        <n-popover
+          trigger="hover"
+          placement="bottom"
+          v-slots={{
+            trigger: () => (
+              <n-icon size="24" onClick={this.handleLanguage}>
+                <Language />
+              </n-icon>
+            ),
+          }}
+        >
+          <span>{this.configStore.getCurrentLocale === LANGUAGES.ZH ? 'Switch to English' : '切换到中文'}</span>
+        </n-popover>
         <n-dropdown trigger="click" options={this.avatarMenuOptions}>
           <n-avatar round>
             {' '}
             {this.nickname?.slice(0, 1)}
           </n-avatar>
         </n-dropdown>
-
       </n-space>
     )
   },
diff --git a/paimon-web-ui/src/locales/en/modules/login.ts b/paimon-web-ui/src/locales/en/modules/login.ts
index 8e808687..b2992d47 100644
--- a/paimon-web-ui/src/locales/en/modules/login.ts
+++ b/paimon-web-ui/src/locales/en/modules/login.ts
@@ -24,4 +24,6 @@ export default {
   light: 'Light',
   dark: 'Dark',
   logout: 'Logout',
+  change_password: 'Change Password',
+  modify: 'Modify',
 }
diff --git a/paimon-web-ui/src/locales/zh/modules/login.ts b/paimon-web-ui/src/locales/zh/modules/login.ts
index 3e1911b3..b5844d7b 100644
--- a/paimon-web-ui/src/locales/zh/modules/login.ts
+++ b/paimon-web-ui/src/locales/zh/modules/login.ts
@@ -24,4 +24,6 @@ export default {
   light: '浅色',
   dark: '暗色',
   logout: '退出登录',
+  change_password: '修改密码',
+  modify: '修改',
 }
diff --git a/paimon-web-ui/src/store/user/index.ts b/paimon-web-ui/src/store/user/index.ts
index ec1f1646..22441f6e 100644
--- a/paimon-web-ui/src/store/user/index.ts
+++ b/paimon-web-ui/src/store/user/index.ts
@@ -16,6 +16,7 @@ specific language governing permissions and limitations
 under the License. */
 
 export interface UserState {
+  userId: number
   username: string | null
   nickname: string | null
   directories: Array<string>
@@ -26,6 +27,7 @@ export interface UserState {
 export const useUserStore = defineStore({
   id: 'user',
   state: (): UserState => ({
+    userId: 1,
     username: '',
     nickname: '',
     directories: [],
@@ -34,6 +36,9 @@ export const useUserStore = defineStore({
   }),
   persist: true,
   getters: {
+    getUserId(): number {
+      return this.userId
+    },
     getUsername(): string | null {
       return this.username
     },
@@ -51,6 +56,9 @@ export const useUserStore = defineStore({
     },
   },
   actions: {
+    setUserId(userId: number): void {
+      this.userId = userId
+    },
     setUsername(username: string): void {
       this.username = username
     },
diff --git a/paimon-web-ui/src/views/login/use-form.ts b/paimon-web-ui/src/views/login/use-form.ts
index 9750a1a9..68a07304 100644
--- a/paimon-web-ui/src/views/login/use-form.ts
+++ b/paimon-web-ui/src/views/login/use-form.ts
@@ -44,6 +44,7 @@ export function useForm() {
           ldapLogin: false,
           rememberMe: true,
         }).then((res: ResponseOptions<any>) => {
+          userStore.setUserId(res.data.user.id)
           userStore.setUsername(res.data.user.username)
           userStore.setNickname(res.data.user.nickname)
           configStore.setCurrentNavActive(null)