Skip to content

Commit

Permalink
feat: able to disable registration now
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Nov 19, 2022
1 parent e2d02a7 commit 82a2f96
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
1 change: 1 addition & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var EmailVerificationEnabled = false
var GitHubOAuthEnabled = false
var WeChatAuthEnabled = false
var TurnstileCheckEnabled = false
var RegisterEnabled = true

var SMTPServer = ""
var SMTPAccount = ""
Expand Down
22 changes: 15 additions & 7 deletions controller/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,24 @@ func GitHubOAuth(c *gin.Context) {
if model.IsGitHubIdAlreadyTaken(user.GitHubId) {
user.FillUserByGitHubId()
} else {
user.Username = "github_" + strconv.Itoa(model.GetMaxUserId()+1)
user.DisplayName = githubUser.Name
user.Email = githubUser.Email
user.Role = common.RoleCommonUser
user.Status = common.UserStatusEnabled
if common.RegisterEnabled {
user.Username = "github_" + strconv.Itoa(model.GetMaxUserId()+1)
user.DisplayName = githubUser.Name
user.Email = githubUser.Email
user.Role = common.RoleCommonUser
user.Status = common.UserStatusEnabled

if err := user.Insert(); err != nil {
if err := user.Insert(); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
} else {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
"message": "管理员关闭了新用户注册",
})
return
}
Expand Down
9 changes: 8 additions & 1 deletion controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,20 @@ func Logout(c *gin.Context) {
}

func Register(c *gin.Context) {
if !common.PasswordRegisterEnabled {
if !common.RegisterEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "管理员关闭了新用户注册",
"success": false,
})
return
}
if !common.PasswordRegisterEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "管理员关闭了通过密码进行注册,请使用第三方账户验证的形式进行注册",
"success": false,
})
return
}
var user model.User
err := json.NewDecoder(c.Request.Body).Decode(&user)
if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions controller/wechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,23 @@ func WeChatAuth(c *gin.Context) {
if model.IsWeChatIdAlreadyTaken(wechatId) {
user.FillUserByWeChatId()
} else {
user.Username = "wechat_" + strconv.Itoa(model.GetMaxUserId()+1)
user.DisplayName = "WeChat User"
user.Role = common.RoleCommonUser
user.Status = common.UserStatusEnabled
if common.RegisterEnabled {
user.Username = "wechat_" + strconv.Itoa(model.GetMaxUserId()+1)
user.DisplayName = "WeChat User"
user.Role = common.RoleCommonUser
user.Status = common.UserStatusEnabled

if err := user.Insert(); err != nil {
if err := user.Insert(); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
} else {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
"message": "管理员关闭了新用户注册",
})
return
}
Expand Down
3 changes: 3 additions & 0 deletions model/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func InitOptionMap() {
common.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(common.GitHubOAuthEnabled)
common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled)
common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled)
common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled)
common.OptionMap["SMTPServer"] = ""
common.OptionMap["SMTPAccount"] = ""
common.OptionMap["SMTPToken"] = ""
Expand Down Expand Up @@ -105,6 +106,8 @@ func updateOptionMap(key string, value string) {
common.WeChatAuthEnabled = boolValue
case "TurnstileCheckEnabled":
common.TurnstileCheckEnabled = boolValue
case "RegisterEnabled":
common.RegisterEnabled = boolValue
}
}
switch key {
Expand Down
8 changes: 8 additions & 0 deletions web/src/components/SystemSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const SystemSetting = () => {
TurnstileCheckEnabled: '',
TurnstileSiteKey: '',
TurnstileSecretKey: '',
RegisterEnabled: '',
});
let originInputs = {};
let [loading, setLoading] = useState(false);
Expand Down Expand Up @@ -55,6 +56,7 @@ const SystemSetting = () => {
case 'GitHubOAuthEnabled':
case 'WeChatAuthEnabled':
case 'TurnstileCheckEnabled':
case 'RegisterEnabled':
value = inputs[key] === 'true' ? 'false' : 'true';
break;
default:
Expand Down Expand Up @@ -212,6 +214,12 @@ const SystemSetting = () => {
/>
</Form.Group>
<Form.Group inline>
<Form.Checkbox
checked={inputs.RegisterEnabled === 'true'}
label='允许新用户注册(此项为否时,新用户将无法以任何方式进行注册)'
name='RegisterEnabled'
onChange={handleInputChange}
/>
<Form.Checkbox
checked={inputs.TurnstileCheckEnabled === 'true'}
label='启用 Turnstile 用户校验'
Expand Down

0 comments on commit 82a2f96

Please sign in to comment.