Просмотр исходного кода

⏱️ fix(token): disallow selecting expiration date earlier than now

Add custom validator to the `expired_time` DatePicker in `EditToken.js`
to ensure that selected expiration timestamps are strictly in the future.

- Introduce Promise-based validator that:
  • Rejects invalid date formats with a clear error message
  • Prevents past or current dates and shows “Expiration time cannot be earlier than the current time!”
- Keeps support for the special “never expires” value (-1)
- Blocks form submission until a valid future datetime is provided

This change prevents accidental creation of already expired tokens and
improves overall robustness of token management.
t0ng7u 7 месяцев назад
Родитель
Сommit
0a434d3b3a
2 измененных файлов с 18 добавлено и 1 удалено
  1. 1 0
      web/src/i18n/locales/en.json
  2. 17 1
      web/src/pages/Token/EditToken.js

+ 1 - 0
web/src/i18n/locales/en.json

@@ -512,6 +512,7 @@
   "创建新的兑换码": "Create a new redemption code",
   "创建新的兑换码": "Create a new redemption code",
   "未找到所请求的页面": "The requested page was not found",
   "未找到所请求的页面": "The requested page was not found",
   "过期时间格式错误!": "Expiration time format error!",
   "过期时间格式错误!": "Expiration time format error!",
+  "过期时间不能早于当前时间!": "Expiration time cannot be earlier than the current time!",
   "请输入过期时间,格式为 yyyy-MM-dd HH:mm:ss,-1 表示无限制": "Please enter the expiration time, the format is yyyy-MM-dd HH:mm:ss, -1 means no limit",
   "请输入过期时间,格式为 yyyy-MM-dd HH:mm:ss,-1 表示无限制": "Please enter the expiration time, the format is yyyy-MM-dd HH:mm:ss, -1 means no limit",
   "此项可选,为一个 JSON 文本,键为用户请求的模型名称,值为要替换的模型名称,例如:": "This is optional, it's a JSON text, the key is the model name requested by the user, and the value is the model name to be replaced, for example:",
   "此项可选,为一个 JSON 文本,键为用户请求的模型名称,值为要替换的模型名称,例如:": "This is optional, it's a JSON text, the key is the model name requested by the user, and the value is the model name to be replaced, for example:",
   "此项可选,输入镜像站地址,格式为:": "This is optional, enter the mirror site address, the format is:",
   "此项可选,输入镜像站地址,格式为:": "This is optional, enter the mirror site address, the format is:",

+ 17 - 1
web/src/pages/Token/EditToken.js

@@ -345,7 +345,23 @@ const EditToken = (props) => {
                       label={t('过期时间')}
                       label={t('过期时间')}
                       type='dateTime'
                       type='dateTime'
                       placeholder={t('请选择过期时间')}
                       placeholder={t('请选择过期时间')}
-                      rules={[{ required: true, message: t('请选择过期时间') }]}
+                      rules={[
+                        { required: true, message: t('请选择过期时间') },
+                        {
+                          validator: (rule, value) => {
+                            // 允许 -1 表示永不过期,也允许空值在必填校验时被拦截
+                            if (value === -1 || !value) return Promise.resolve();
+                            const time = Date.parse(value);
+                            if (isNaN(time)) {
+                              return Promise.reject(t('过期时间格式错误!'));
+                            }
+                            if (time <= Date.now()) {
+                              return Promise.reject(t('过期时间不能早于当前时间!'));
+                            }
+                            return Promise.resolve();
+                          },
+                        },
+                      ]}
                       showClear
                       showClear
                       style={{ width: '100%' }}
                       style={{ width: '100%' }}
                     />
                     />