mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 04:40:59 +00:00
feat: 针对 discord 登录配置使用新版设置方案
This commit is contained in:
@@ -44,7 +44,6 @@ var PasswordLoginEnabled = true
|
|||||||
var PasswordRegisterEnabled = true
|
var PasswordRegisterEnabled = true
|
||||||
var EmailVerificationEnabled = false
|
var EmailVerificationEnabled = false
|
||||||
var GitHubOAuthEnabled = false
|
var GitHubOAuthEnabled = false
|
||||||
var DiscordOAuthEnabled = false
|
|
||||||
var LinuxDOOAuthEnabled = false
|
var LinuxDOOAuthEnabled = false
|
||||||
var WeChatAuthEnabled = false
|
var WeChatAuthEnabled = false
|
||||||
var TelegramOAuthEnabled = false
|
var TelegramOAuthEnabled = false
|
||||||
@@ -83,8 +82,6 @@ var SMTPToken = ""
|
|||||||
|
|
||||||
var GitHubClientId = ""
|
var GitHubClientId = ""
|
||||||
var GitHubClientSecret = ""
|
var GitHubClientSecret = ""
|
||||||
var DiscordClientId = ""
|
|
||||||
var DiscordClientSecret = ""
|
|
||||||
var LinuxDOClientId = ""
|
var LinuxDOClientId = ""
|
||||||
var LinuxDOClientSecret = ""
|
var LinuxDOClientSecret = ""
|
||||||
var LinuxDOMinimumTrustLevel = 0
|
var LinuxDOMinimumTrustLevel = 0
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ func getDiscordUserInfoByCode(code string) (*DiscordUser, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
values := url.Values{}
|
values := url.Values{}
|
||||||
values.Set("client_id", common.DiscordClientId)
|
values.Set("client_id", system_setting.GetDiscordSettings().ClientId)
|
||||||
values.Set("client_secret", common.DiscordClientSecret)
|
values.Set("client_secret", system_setting.GetDiscordSettings().ClientSecret)
|
||||||
values.Set("code", code)
|
values.Set("code", code)
|
||||||
values.Set("grant_type", "authorization_code")
|
values.Set("grant_type", "authorization_code")
|
||||||
values.Set("redirect_uri", fmt.Sprintf("%s/oauth/discord", system_setting.ServerAddress))
|
values.Set("redirect_uri", fmt.Sprintf("%s/oauth/discord", system_setting.ServerAddress))
|
||||||
@@ -114,10 +114,10 @@ func DiscordOAuth(c *gin.Context) {
|
|||||||
DiscordBind(c)
|
DiscordBind(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !common.DiscordOAuthEnabled {
|
if !system_setting.GetDiscordSettings().Enabled {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "管理员未开启通过 discord 登录以及注册",
|
"message": "管理员未开启通过 Discord 登录以及注册",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -179,7 +179,7 @@ func DiscordOAuth(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DiscordBind(c *gin.Context) {
|
func DiscordBind(c *gin.Context) {
|
||||||
if !common.DiscordOAuthEnabled {
|
if !system_setting.GetDiscordSettings().Enabled {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "管理员未开启通过 Discord 登录以及注册",
|
"message": "管理员未开启通过 Discord 登录以及注册",
|
||||||
@@ -204,7 +204,6 @@ func DiscordBind(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
session := sessions.Default(c)
|
session := sessions.Default(c)
|
||||||
id := session.Get("id")
|
id := session.Get("id")
|
||||||
// id := c.GetInt("id") // critical bug!
|
|
||||||
user.Id = id.(int)
|
user.Id = id.(int)
|
||||||
err = user.FillUserById()
|
err = user.FillUserById()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ func GetStatus(c *gin.Context) {
|
|||||||
"email_verification": common.EmailVerificationEnabled,
|
"email_verification": common.EmailVerificationEnabled,
|
||||||
"github_oauth": common.GitHubOAuthEnabled,
|
"github_oauth": common.GitHubOAuthEnabled,
|
||||||
"github_client_id": common.GitHubClientId,
|
"github_client_id": common.GitHubClientId,
|
||||||
"discord_oauth": common.DiscordOAuthEnabled,
|
"discord_oauth": system_setting.GetDiscordSettings().Enabled,
|
||||||
"discord_client_id": common.DiscordClientId,
|
"discord_client_id": system_setting.GetDiscordSettings().ClientId,
|
||||||
"linuxdo_oauth": common.LinuxDOOAuthEnabled,
|
"linuxdo_oauth": common.LinuxDOOAuthEnabled,
|
||||||
"linuxdo_client_id": common.LinuxDOClientId,
|
"linuxdo_client_id": common.LinuxDOClientId,
|
||||||
"linuxdo_minimum_trust_level": common.LinuxDOMinimumTrustLevel,
|
"linuxdo_minimum_trust_level": common.LinuxDOMinimumTrustLevel,
|
||||||
|
|||||||
@@ -71,8 +71,8 @@ func UpdateOption(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case "DiscordOAuthEnabled":
|
case "discord.enabled":
|
||||||
if option.Value == "true" && common.DiscordClientId == "" {
|
if option.Value == "true" && system_setting.GetDiscordSettings().ClientId == "" {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "无法启用 Discord OAuth,请先填入 Discord Client Id 以及 Discord Client Secret!",
|
"message": "无法启用 Discord OAuth,请先填入 Discord Client Id 以及 Discord Client Secret!",
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ func InitOptionMap() {
|
|||||||
common.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(common.PasswordRegisterEnabled)
|
common.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(common.PasswordRegisterEnabled)
|
||||||
common.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(common.EmailVerificationEnabled)
|
common.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(common.EmailVerificationEnabled)
|
||||||
common.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(common.GitHubOAuthEnabled)
|
common.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(common.GitHubOAuthEnabled)
|
||||||
common.OptionMap["DiscordOAuthEnabled"] = strconv.FormatBool(common.DiscordOAuthEnabled)
|
|
||||||
common.OptionMap["LinuxDOOAuthEnabled"] = strconv.FormatBool(common.LinuxDOOAuthEnabled)
|
common.OptionMap["LinuxDOOAuthEnabled"] = strconv.FormatBool(common.LinuxDOOAuthEnabled)
|
||||||
common.OptionMap["TelegramOAuthEnabled"] = strconv.FormatBool(common.TelegramOAuthEnabled)
|
common.OptionMap["TelegramOAuthEnabled"] = strconv.FormatBool(common.TelegramOAuthEnabled)
|
||||||
common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled)
|
common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled)
|
||||||
@@ -96,8 +95,6 @@ func InitOptionMap() {
|
|||||||
common.OptionMap["PayMethods"] = operation_setting.PayMethods2JsonString()
|
common.OptionMap["PayMethods"] = operation_setting.PayMethods2JsonString()
|
||||||
common.OptionMap["GitHubClientId"] = ""
|
common.OptionMap["GitHubClientId"] = ""
|
||||||
common.OptionMap["GitHubClientSecret"] = ""
|
common.OptionMap["GitHubClientSecret"] = ""
|
||||||
common.OptionMap["DiscordClientId"] = ""
|
|
||||||
common.OptionMap["DiscordClientSecret"] = ""
|
|
||||||
common.OptionMap["TelegramBotToken"] = ""
|
common.OptionMap["TelegramBotToken"] = ""
|
||||||
common.OptionMap["TelegramBotName"] = ""
|
common.OptionMap["TelegramBotName"] = ""
|
||||||
common.OptionMap["WeChatServerAddress"] = ""
|
common.OptionMap["WeChatServerAddress"] = ""
|
||||||
@@ -227,8 +224,6 @@ func updateOptionMap(key string, value string) (err error) {
|
|||||||
common.EmailVerificationEnabled = boolValue
|
common.EmailVerificationEnabled = boolValue
|
||||||
case "GitHubOAuthEnabled":
|
case "GitHubOAuthEnabled":
|
||||||
common.GitHubOAuthEnabled = boolValue
|
common.GitHubOAuthEnabled = boolValue
|
||||||
case "DiscordOAuthEnabled":
|
|
||||||
common.DiscordOAuthEnabled = boolValue
|
|
||||||
case "LinuxDOOAuthEnabled":
|
case "LinuxDOOAuthEnabled":
|
||||||
common.LinuxDOOAuthEnabled = boolValue
|
common.LinuxDOOAuthEnabled = boolValue
|
||||||
case "WeChatAuthEnabled":
|
case "WeChatAuthEnabled":
|
||||||
@@ -365,10 +360,6 @@ func updateOptionMap(key string, value string) (err error) {
|
|||||||
common.GitHubClientId = value
|
common.GitHubClientId = value
|
||||||
case "GitHubClientSecret":
|
case "GitHubClientSecret":
|
||||||
common.GitHubClientSecret = value
|
common.GitHubClientSecret = value
|
||||||
case "DiscordClientId":
|
|
||||||
common.DiscordClientId = value
|
|
||||||
case "DiscordClientSecret":
|
|
||||||
common.DiscordClientSecret = value
|
|
||||||
case "LinuxDOClientId":
|
case "LinuxDOClientId":
|
||||||
common.LinuxDOClientId = value
|
common.LinuxDOClientId = value
|
||||||
case "LinuxDOClientSecret":
|
case "LinuxDOClientSecret":
|
||||||
|
|||||||
21
setting/system_setting/discord.go
Normal file
21
setting/system_setting/discord.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package system_setting
|
||||||
|
|
||||||
|
import "github.com/QuantumNous/new-api/setting/config"
|
||||||
|
|
||||||
|
type DiscordSettings struct {
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
ClientId string `json:"client_id"`
|
||||||
|
ClientSecret string `json:"client_secret"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认配置
|
||||||
|
var defaultDiscordSettings = DiscordSettings{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// 注册到全局配置管理器
|
||||||
|
config.GlobalConfig.Register("discord", &defaultDiscordSettings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDiscordSettings() *DiscordSettings {
|
||||||
|
return &defaultDiscordSettings
|
||||||
|
}
|
||||||
@@ -52,9 +52,9 @@ const SystemSetting = () => {
|
|||||||
GitHubOAuthEnabled: '',
|
GitHubOAuthEnabled: '',
|
||||||
GitHubClientId: '',
|
GitHubClientId: '',
|
||||||
GitHubClientSecret: '',
|
GitHubClientSecret: '',
|
||||||
DiscordOAuthEnabled: '',
|
'discord.enabled': '',
|
||||||
DiscordClientId: '',
|
'discord.client_id': '',
|
||||||
DiscordClientSecret: '',
|
'discord.client_secret': '',
|
||||||
'oidc.enabled': '',
|
'oidc.enabled': '',
|
||||||
'oidc.client_id': '',
|
'oidc.client_id': '',
|
||||||
'oidc.client_secret': '',
|
'oidc.client_secret': '',
|
||||||
@@ -182,7 +182,7 @@ const SystemSetting = () => {
|
|||||||
case 'EmailAliasRestrictionEnabled':
|
case 'EmailAliasRestrictionEnabled':
|
||||||
case 'SMTPSSLEnabled':
|
case 'SMTPSSLEnabled':
|
||||||
case 'LinuxDOOAuthEnabled':
|
case 'LinuxDOOAuthEnabled':
|
||||||
case 'DiscordOAuthEnabled':
|
case 'discord.enabled':
|
||||||
case 'oidc.enabled':
|
case 'oidc.enabled':
|
||||||
case 'passkey.enabled':
|
case 'passkey.enabled':
|
||||||
case 'passkey.allow_insecure_origin':
|
case 'passkey.allow_insecure_origin':
|
||||||
@@ -480,16 +480,16 @@ const SystemSetting = () => {
|
|||||||
const submitDiscordOAuth = async () => {
|
const submitDiscordOAuth = async () => {
|
||||||
const options = [];
|
const options = [];
|
||||||
|
|
||||||
if (originInputs['DiscordClientId'] !== inputs.DiscordClientId) {
|
if (originInputs['discord.client_id'] !== inputs['discord.client_id']) {
|
||||||
options.push({ key: 'DiscordClientId', value: inputs.DiscordClientId });
|
options.push({ key: 'discord.client_id', value: inputs['discord.client_id'] });
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
originInputs['DiscordClientSecret'] !== inputs.DiscordClientSecret &&
|
originInputs['discord.client_secret'] !== inputs['discord.client_secret'] &&
|
||||||
inputs.DiscordClientSecret !== ''
|
inputs['discord.client_secret'] !== ''
|
||||||
) {
|
) {
|
||||||
options.push({
|
options.push({
|
||||||
key: 'DiscordClientSecret',
|
key: 'discord.client_secret',
|
||||||
value: inputs.DiscordClientSecret,
|
value: inputs['discord.client_secret'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1040,10 +1040,10 @@ const SystemSetting = () => {
|
|||||||
{t('允许通过 GitHub 账户登录 & 注册')}
|
{t('允许通过 GitHub 账户登录 & 注册')}
|
||||||
</Form.Checkbox>
|
</Form.Checkbox>
|
||||||
<Form.Checkbox
|
<Form.Checkbox
|
||||||
field='DiscordOAuthEnabled'
|
field='discord.enabled'
|
||||||
noLabel
|
noLabel
|
||||||
onChange={(e) =>
|
onChange={(e) =>
|
||||||
handleCheckboxChange('DiscordOAuthEnabled', e)
|
handleCheckboxChange('discord.enabled', e)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{t('允许通过 Discord 账户登录 & 注册')}
|
{t('允许通过 Discord 账户登录 & 注册')}
|
||||||
@@ -1457,13 +1457,13 @@ const SystemSetting = () => {
|
|||||||
>
|
>
|
||||||
<Col xs={24} sm={24} md={12} lg={12} xl={12}>
|
<Col xs={24} sm={24} md={12} lg={12} xl={12}>
|
||||||
<Form.Input
|
<Form.Input
|
||||||
field='DiscordClientId'
|
field="['discord.client_id']"
|
||||||
label={t('Discord Client ID')}
|
label={t('Discord Client ID')}
|
||||||
/>
|
/>
|
||||||
</Col>
|
</Col>
|
||||||
<Col xs={24} sm={24} md={12} lg={12} xl={12}>
|
<Col xs={24} sm={24} md={12} lg={12} xl={12}>
|
||||||
<Form.Input
|
<Form.Input
|
||||||
field='DiscordClientSecret'
|
field="['discord.client_secret']"
|
||||||
label={t('Discord Client Secret')}
|
label={t('Discord Client Secret')}
|
||||||
type='password'
|
type='password'
|
||||||
placeholder={t('敏感信息不会发送到前端显示')}
|
placeholder={t('敏感信息不会发送到前端显示')}
|
||||||
|
|||||||
Reference in New Issue
Block a user