Skip to content

Commit

Permalink
feat: Add functionality to disable new sign up in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
FUjr committed Nov 21, 2024
1 parent c1b5be5 commit 0556a41
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
9 changes: 9 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ func apiStart(br *broker) {
return
}

if cfg.DisableSignUp {
c.Status(http.StatusForbidden)
return
}

db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
Expand Down Expand Up @@ -398,6 +403,10 @@ func apiStart(br *broker) {
c.JSON(http.StatusOK, gin.H{"admin": isAdmin})
})

r.GET("/allowsignup", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"allow": !cfg.DisableSignUp})
})

r.GET("/users", func(c *gin.Context) {
loginUsername := getLoginUsername(c)
isAdmin := isAdminUsername(cfg, loginUsername)
Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
WhiteList map[string]bool
DB string
LocalAuth bool
DisableSignUp bool
}

func getConfigOpt(yamlCfg *yaml.File, name string, opt interface{}) {
Expand All @@ -37,6 +38,8 @@ func getConfigOpt(yamlCfg *yaml.File, name string, opt interface{}) {
*opt = val
case *int:
*opt, _ = strconv.Atoi(val)
case *bool:
*opt, _ = strconv.ParseBool(val)
}
}

Expand All @@ -53,6 +56,7 @@ func Parse(c *cli.Context) *Config {
Token: c.String("token"),
DB: c.String("db"),
LocalAuth: c.Bool("local-auth"),
DisableSignUp: c.Bool("disable-sign-up"),
}

cfg.WhiteList = make(map[string]bool)
Expand All @@ -79,6 +83,7 @@ func Parse(c *cli.Context) *Config {
getConfigOpt(yamlCfg, "token", &cfg.Token)
getConfigOpt(yamlCfg, "db", &cfg.DB)
getConfigOpt(yamlCfg, "local-auth", &cfg.LocalAuth)
getConfigOpt(yamlCfg, "disable-sign-up", &cfg.DisableSignUp)

val, err := yamlCfg.Get("white-list")
if err == nil {
Expand Down
3 changes: 3 additions & 0 deletions rttys.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
# database source
db: sqlite://rttys.db
#db: mysql://rttys:rttys@tcp(localhost)/rttys

#disable new user sign up
#disable-sign-up: True
10 changes: 8 additions & 2 deletions ui/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</FormItem>
</Form>
<p v-if="signup" style="text-align: center">{{ $t('Already have an account?') }}<a href="/login" style="text-decoration: none; color: #1c7cd6">{{ $t('Sign in') }}</a></p>
<p v-else style="text-align: center">{{ $t('New to Rttys?') }}<a href="/login?signup=1" style="text-decoration: none; color: #1c7cd6">{{ $t('Sign up') }}</a></p>
<p v-else-if="allowSignup" style="text-align: center">{{ $t('New to Rttys?') }}<a href="/login?signup=1" style="text-decoration: none; color: #1c7cd6">{{ $t('Sign up') }}</a></p>
</Card>
</template>

Expand All @@ -22,7 +22,8 @@ export default {
data() {
return {
signup: false,
formData: {
allowSignup: false,
formData: {
username: '',
password: ''
},
Expand Down Expand Up @@ -65,6 +66,11 @@ export default {
created() {
this.signup = this.$route.query.signup === '1';
sessionStorage.removeItem('rttys-sid');
this.axios.get('/allowsignup').then(response => {
this.allowSignup = response.data.allow;
}).catch(() => {
this.allowSignup = false;
});
}
}
</script>
Expand Down

0 comments on commit 0556a41

Please sign in to comment.