Skip to content

Commit 1f87676

Browse files
douxcGarfieldDai
andauthored
Supports display license status (#10408)
Co-authored-by: Garfield Dai <[email protected]>
1 parent c2ce2f8 commit 1f87676

File tree

11 files changed

+186
-52
lines changed

11 files changed

+186
-52
lines changed

web/app/components/header/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import EnvNav from './env-nav'
1212
import ExploreNav from './explore-nav'
1313
import ToolsNav from './tools-nav'
1414
import GithubStar from './github-star'
15+
import LicenseNav from './license-env'
1516
import { WorkspaceProvider } from '@/context/workspace-context'
1617
import { useAppContext } from '@/context/app-context'
1718
import LogoSite from '@/app/components/base/logo/logo-site'
@@ -79,6 +80,7 @@ const Header = () => {
7980
</div>
8081
)}
8182
<div className='flex items-center flex-shrink-0'>
83+
<LicenseNav />
8284
<EnvNav />
8385
{enableBilling && (
8486
<div className='mr-3 select-none'>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client'
2+
3+
import AppContext from '@/context/app-context'
4+
import { LicenseStatus } from '@/types/feature'
5+
import { useTranslation } from 'react-i18next'
6+
import { useContextSelector } from 'use-context-selector'
7+
import dayjs from 'dayjs'
8+
9+
const LicenseNav = () => {
10+
const { t } = useTranslation()
11+
const systemFeatures = useContextSelector(AppContext, s => s.systemFeatures)
12+
13+
if (systemFeatures.license?.status === LicenseStatus.EXPIRING) {
14+
const expiredAt = systemFeatures.license?.expired_at
15+
const count = dayjs(expiredAt).diff(dayjs(), 'days')
16+
return <div className='px-2 py-1 mr-4 rounded-full bg-util-colors-orange-orange-50 border-util-colors-orange-orange-100 system-xs-medium text-util-colors-orange-orange-600'>
17+
{count <= 1 && <span>{t('common.license.expiring', { count })}</span>}
18+
{count > 1 && <span>{t('common.license.expiring_plural', { count })}</span>}
19+
</div>
20+
}
21+
if (systemFeatures.license.status === LicenseStatus.ACTIVE) {
22+
return <div className='px-2 py-1 mr-4 rounded-md bg-util-colors-indigo-indigo-50 border-util-colors-indigo-indigo-100 system-xs-medium text-util-colors-indigo-indigo-600'>
23+
Enterprise
24+
</div>
25+
}
26+
return null
27+
}
28+
29+
export default LicenseNav

web/app/signin/normalForm.tsx

+44-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import React, { useCallback, useEffect, useState } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import Link from 'next/link'
44
import { useRouter, useSearchParams } from 'next/navigation'
5-
import { RiDoorLockLine } from '@remixicon/react'
5+
import { RiContractLine, RiDoorLockLine, RiErrorWarningFill } from '@remixicon/react'
66
import Loading from '../components/base/loading'
77
import MailAndCodeAuth from './components/mail-and-code-auth'
88
import MailAndPasswordAuth from './components/mail-and-password-auth'
99
import SocialAuth from './components/social-auth'
1010
import SSOAuth from './components/sso-auth'
1111
import cn from '@/utils/classnames'
1212
import { getSystemFeatures, invitationCheck } from '@/service/common'
13-
import { defaultSystemFeatures } from '@/types/feature'
13+
import { LicenseStatus, defaultSystemFeatures } from '@/types/feature'
1414
import Toast from '@/app/components/base/toast'
1515
import { IS_CE_EDITION } from '@/config'
1616

@@ -83,6 +83,48 @@ const NormalForm = () => {
8383
<Loading type='area' />
8484
</div>
8585
}
86+
if (systemFeatures.license?.status === LicenseStatus.LOST) {
87+
return <div className='w-full mx-auto mt-8'>
88+
<div className='bg-white'>
89+
<div className="p-4 rounded-lg bg-gradient-to-r from-workflow-workflow-progress-bg-1 to-workflow-workflow-progress-bg-2">
90+
<div className='flex items-center justify-center w-10 h-10 rounded-xl bg-components-card-bg shadow shadows-shadow-lg mb-2 relative'>
91+
<RiContractLine className='w-5 h-5' />
92+
<RiErrorWarningFill className='absolute w-4 h-4 text-text-warning-secondary -top-1 -right-1' />
93+
</div>
94+
<p className='system-sm-medium text-text-primary'>{t('login.licenseLost')}</p>
95+
<p className='system-xs-regular text-text-tertiary mt-1'>{t('login.licenseLostTip')}</p>
96+
</div>
97+
</div>
98+
</div>
99+
}
100+
if (systemFeatures.license?.status === LicenseStatus.EXPIRED) {
101+
return <div className='w-full mx-auto mt-8'>
102+
<div className='bg-white'>
103+
<div className="p-4 rounded-lg bg-gradient-to-r from-workflow-workflow-progress-bg-1 to-workflow-workflow-progress-bg-2">
104+
<div className='flex items-center justify-center w-10 h-10 rounded-xl bg-components-card-bg shadow shadows-shadow-lg mb-2 relative'>
105+
<RiContractLine className='w-5 h-5' />
106+
<RiErrorWarningFill className='absolute w-4 h-4 text-text-warning-secondary -top-1 -right-1' />
107+
</div>
108+
<p className='system-sm-medium text-text-primary'>{t('login.licenseExpired')}</p>
109+
<p className='system-xs-regular text-text-tertiary mt-1'>{t('login.licenseExpiredTip')}</p>
110+
</div>
111+
</div>
112+
</div>
113+
}
114+
if (systemFeatures.license?.status === LicenseStatus.INACTIVE) {
115+
return <div className='w-full mx-auto mt-8'>
116+
<div className='bg-white'>
117+
<div className="p-4 rounded-lg bg-gradient-to-r from-workflow-workflow-progress-bg-1 to-workflow-workflow-progress-bg-2">
118+
<div className='flex items-center justify-center w-10 h-10 rounded-xl bg-components-card-bg shadow shadows-shadow-lg mb-2 relative'>
119+
<RiContractLine className='w-5 h-5' />
120+
<RiErrorWarningFill className='absolute w-4 h-4 text-text-warning-secondary -top-1 -right-1' />
121+
</div>
122+
<p className='system-sm-medium text-text-primary'>{t('login.licenseInactive')}</p>
123+
<p className='system-xs-regular text-text-tertiary mt-1'>{t('login.licenseInactiveTip')}</p>
124+
</div>
125+
</div>
126+
</div>
127+
}
86128

87129
return (
88130
<>

web/context/app-context.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export const AppContextProvider: FC<AppContextProviderProps> = ({ children }) =>
144144
theme,
145145
setTheme: handleSetTheme,
146146
apps: appList.data,
147-
systemFeatures,
147+
systemFeatures: { ...defaultSystemFeatures, ...systemFeatures },
148148
mutateApps,
149149
userProfile,
150150
mutateUserProfile,

web/i18n/en-US/common.ts

+4
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@ const translation = {
591591
created: 'Tag created successfully',
592592
failed: 'Tag creation failed',
593593
},
594+
license: {
595+
expiring: 'Expiring in one day',
596+
expiring_plural: 'Expiring in {{count}} days',
597+
},
594598
}
595599

596600
export default translation

web/i18n/en-US/login.ts

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ const translation = {
9898
back: 'Back',
9999
noLoginMethod: 'Authentication method not configured',
100100
noLoginMethodTip: 'Please contact the system admin to add an authentication method.',
101+
licenseExpired: 'License Expired',
102+
licenseExpiredTip: 'The Dify Enterprise license for your workspace has expired. Please contact your administrator to continue using Dify.',
103+
licenseLost: 'License Lost',
104+
licenseLostTip: 'Failed to connect Dify license server. Please contact your administrator to continue using Dify.',
105+
licenseInactive: 'License Inactive',
106+
licenseInactiveTip: 'The Dify Enterprise license for your workspace is inactive. Please contact your administrator to continue using Dify.',
101107
}
102108

103109
export default translation

web/i18n/zh-Hans/common.ts

+4
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@ const translation = {
591591
created: '标签创建成功',
592592
failed: '标签创建失败',
593593
},
594+
license: {
595+
expiring: '许可证还有 1 天到期',
596+
expiring_plural: '许可证还有 {{count}} 天到期',
597+
},
594598
}
595599

596600
export default translation

web/i18n/zh-Hans/login.ts

+6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ const translation = {
9999
back: '返回',
100100
noLoginMethod: '未配置身份认证方式',
101101
noLoginMethodTip: '请联系系统管理员添加身份认证方式',
102+
licenseExpired: '许可证已过期',
103+
licenseExpiredTip: '您所在空间的 Dify Enterprise 许可证已过期,请联系管理员以继续使用 Dify。',
104+
licenseLost: '许可证丢失',
105+
licenseLostTip: '无法连接 Dify 许可证服务器,请联系管理员以继续使用 Dify。',
106+
licenseInactive: '许可证未激活',
107+
licenseInactiveTip: '您所在空间的 Dify Enterprise 许可证尚未激活,请联系管理员以继续使用 Dify。',
102108
}
103109

104110
export default translation

web/service/base.ts

+69-45
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
WorkflowStartedResponse,
1818
} from '@/types/workflow'
1919
import { removeAccessToken } from '@/app/components/share/utils'
20+
import { asyncRunSafe } from '@/utils'
2021
const TIME_OUT = 100000
2122

2223
const ContentType = {
@@ -550,55 +551,78 @@ export const ssePost = (
550551
}
551552

552553
// base request
553-
export const request = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
554-
return new Promise<T>((resolve, reject) => {
554+
export const request = async<T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
555+
try {
555556
const otherOptionsForBaseFetch = otherOptions || {}
556-
baseFetch<T>(url, options, otherOptionsForBaseFetch).then(resolve).catch((errResp) => {
557-
if (errResp?.status === 401) {
558-
return refreshAccessTokenOrRelogin(TIME_OUT).then(() => {
559-
baseFetch<T>(url, options, otherOptionsForBaseFetch).then(resolve).catch(reject)
560-
}).catch(() => {
561-
const {
562-
isPublicAPI = false,
563-
silent,
564-
} = otherOptionsForBaseFetch
565-
const bodyJson = errResp.json()
566-
if (isPublicAPI) {
567-
return bodyJson.then((data: ResponseError) => {
568-
if (data.code === 'web_sso_auth_required')
569-
requiredWebSSOLogin()
570-
571-
if (data.code === 'unauthorized') {
572-
removeAccessToken()
573-
globalThis.location.reload()
574-
}
557+
const [err, resp] = await asyncRunSafe<T>(baseFetch(url, options, otherOptionsForBaseFetch))
558+
if (err === null)
559+
return resp
560+
const errResp: Response = err as any
561+
if (errResp.status === 401) {
562+
const [parseErr, errRespData] = await asyncRunSafe<ResponseError>(errResp.json())
563+
const loginUrl = `${globalThis.location.origin}/signin`
564+
if (parseErr) {
565+
globalThis.location.href = loginUrl
566+
return Promise.reject(err)
567+
}
568+
// special code
569+
const { code, message } = errRespData
570+
// webapp sso
571+
if (code === 'web_sso_auth_required') {
572+
requiredWebSSOLogin()
573+
return Promise.reject(err)
574+
}
575+
if (code === 'unauthorized_and_force_logout') {
576+
localStorage.removeItem('console_token')
577+
localStorage.removeItem('refresh_token')
578+
globalThis.location.reload()
579+
return Promise.reject(err)
580+
}
581+
const {
582+
isPublicAPI = false,
583+
silent,
584+
} = otherOptionsForBaseFetch
585+
if (isPublicAPI && code === 'unauthorized') {
586+
removeAccessToken()
587+
globalThis.location.reload()
588+
return Promise.reject(err)
589+
}
590+
if (code === 'init_validate_failed' && IS_CE_EDITION && !silent) {
591+
Toast.notify({ type: 'error', message, duration: 4000 })
592+
return Promise.reject(err)
593+
}
594+
if (code === 'not_init_validated' && IS_CE_EDITION) {
595+
globalThis.location.href = `${globalThis.location.origin}/init`
596+
return Promise.reject(err)
597+
}
598+
if (code === 'not_setup' && IS_CE_EDITION) {
599+
globalThis.location.href = `${globalThis.location.origin}/install`
600+
return Promise.reject(err)
601+
}
575602

576-
return Promise.reject(data)
577-
})
578-
}
579-
const loginUrl = `${globalThis.location.origin}/signin`
580-
bodyJson.then((data: ResponseError) => {
581-
if (data.code === 'init_validate_failed' && IS_CE_EDITION && !silent)
582-
Toast.notify({ type: 'error', message: data.message, duration: 4000 })
583-
else if (data.code === 'not_init_validated' && IS_CE_EDITION)
584-
globalThis.location.href = `${globalThis.location.origin}/init`
585-
else if (data.code === 'not_setup' && IS_CE_EDITION)
586-
globalThis.location.href = `${globalThis.location.origin}/install`
587-
else if (location.pathname !== '/signin' || !IS_CE_EDITION)
588-
globalThis.location.href = loginUrl
589-
else if (!silent)
590-
Toast.notify({ type: 'error', message: data.message })
591-
}).catch(() => {
592-
// Handle any other errors
593-
globalThis.location.href = loginUrl
594-
})
595-
})
603+
// refresh token
604+
const [refreshErr] = await asyncRunSafe(refreshAccessTokenOrRelogin(TIME_OUT))
605+
if (refreshErr === null)
606+
return baseFetch<T>(url, options, otherOptionsForBaseFetch)
607+
if (location.pathname !== '/signin' || !IS_CE_EDITION) {
608+
globalThis.location.href = loginUrl
609+
return Promise.reject(err)
596610
}
597-
else {
598-
reject(errResp)
611+
if (!silent) {
612+
Toast.notify({ type: 'error', message })
613+
return Promise.reject(err)
599614
}
600-
})
601-
})
615+
globalThis.location.href = loginUrl
616+
return Promise.reject(err)
617+
}
618+
else {
619+
return Promise.reject(err)
620+
}
621+
}
622+
catch (error) {
623+
console.error(error)
624+
return Promise.reject(error)
625+
}
602626
}
603627

604628
// request methods

web/types/feature.ts

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ export enum SSOProtocol {
44
OAuth2 = 'oauth2',
55
}
66

7+
export enum LicenseStatus {
8+
NONE = 'none',
9+
INACTIVE = 'inactive',
10+
ACTIVE = 'active',
11+
EXPIRING = 'expiring',
12+
EXPIRED = 'expired',
13+
LOST = 'lost',
14+
}
15+
16+
type License = {
17+
status: LicenseStatus
18+
expired_at: string | null
19+
}
20+
721
export type SystemFeatures = {
822
sso_enforced_for_signin: boolean
923
sso_enforced_for_signin_protocol: SSOProtocol | ''
@@ -15,6 +29,7 @@ export type SystemFeatures = {
1529
enable_social_oauth_login: boolean
1630
is_allow_create_workspace: boolean
1731
is_allow_register: boolean
32+
license: License
1833
}
1934

2035
export const defaultSystemFeatures: SystemFeatures = {
@@ -28,4 +43,8 @@ export const defaultSystemFeatures: SystemFeatures = {
2843
enable_social_oauth_login: false,
2944
is_allow_create_workspace: false,
3045
is_allow_register: false,
46+
license: {
47+
status: LicenseStatus.NONE,
48+
expired_at: '',
49+
},
3150
}

web/utils/index.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ export async function asyncRunSafe<T = any>(fn: Promise<T>): Promise<[Error] | [
88
try {
99
return [null, await fn]
1010
}
11-
catch (e) {
12-
if (e instanceof Error)
13-
return [e]
14-
return [new Error('unknown error')]
11+
catch (e: any) {
12+
return [e || new Error('unknown error')]
1513
}
1614
}
1715

0 commit comments

Comments
 (0)