mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-08 17:10:27 +08:00
feat: add quota limitation to organizations, users, providers and applications (#1339)
This commit is contained in:
@ -21,3 +21,4 @@ isDemoMode = false
|
|||||||
batchSize = 100
|
batchSize = 100
|
||||||
ldapServerPort = 389
|
ldapServerPort = 389
|
||||||
languages = en,zh,es,fr,de,ja,ko,ru
|
languages = en,zh,es,fr,de,ja,ko,ru
|
||||||
|
quota = {"organization": -1, "user": -1, "application": -1, "provider": -1}
|
||||||
|
25
conf/conf.go
25
conf/conf.go
@ -15,6 +15,7 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -24,6 +25,15 @@ import (
|
|||||||
"github.com/beego/beego"
|
"github.com/beego/beego"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Quota struct {
|
||||||
|
Organization int `json:"organization"`
|
||||||
|
User int `json:"user"`
|
||||||
|
Application int `json:"application"`
|
||||||
|
Provider int `json:"provider"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var quota = &Quota{-1, -1, -1, -1}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// this array contains the beego configuration items that may be modified via env
|
// this array contains the beego configuration items that may be modified via env
|
||||||
presetConfigItems := []string{"httpport", "appname"}
|
presetConfigItems := []string{"httpport", "appname"}
|
||||||
@ -35,6 +45,17 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
initQuota()
|
||||||
|
}
|
||||||
|
|
||||||
|
func initQuota() {
|
||||||
|
res := beego.AppConfig.String("quota")
|
||||||
|
if res != "" {
|
||||||
|
err := json.Unmarshal([]byte(res), quota)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfigString(key string) string {
|
func GetConfigString(key string) string {
|
||||||
@ -95,3 +116,7 @@ func GetConfigBatchSize() int {
|
|||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetConfigQuota() *Quota {
|
||||||
|
return quota
|
||||||
|
}
|
||||||
|
@ -93,3 +93,19 @@ func TestGetConfBool(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetConfigQuota(t *testing.T) {
|
||||||
|
scenarios := []struct {
|
||||||
|
description string
|
||||||
|
expected *Quota
|
||||||
|
}{
|
||||||
|
{"default", &Quota{-1, -1, -1, -1}},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := beego.LoadAppConfig("ini", "app.conf")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
for _, scenery := range scenarios {
|
||||||
|
quota := GetConfigQuota()
|
||||||
|
assert.Equal(t, scenery.expected, quota)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -167,6 +167,12 @@ func (c *ApiController) AddApplication() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count := object.GetApplicationCount("", "", "")
|
||||||
|
if err := checkQuotaForApplication(count); err != nil {
|
||||||
|
c.ResponseError(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.Data["json"] = wrapActionResponse(object.AddApplication(&application))
|
c.Data["json"] = wrapActionResponse(object.AddApplication(&application))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
@ -99,6 +99,12 @@ func (c *ApiController) AddOrganization() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count := object.GetOrganizationCount("", "", "")
|
||||||
|
if err := checkQuotaForOrganization(count); err != nil {
|
||||||
|
c.ResponseError(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.Data["json"] = wrapActionResponse(object.AddOrganization(&organization))
|
c.Data["json"] = wrapActionResponse(object.AddOrganization(&organization))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
@ -122,6 +122,12 @@ func (c *ApiController) AddProvider() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count := object.GetProviderCount("", "", "")
|
||||||
|
if err := checkQuotaForProvider(count); err != nil {
|
||||||
|
c.ResponseError(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.Data["json"] = wrapActionResponse(object.AddProvider(&provider))
|
c.Data["json"] = wrapActionResponse(object.AddProvider(&provider))
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
@ -183,6 +183,12 @@ func (c *ApiController) AddUser() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count := object.GetUserCount("", "", "")
|
||||||
|
if err := checkQuotaForUser(count); err != nil {
|
||||||
|
c.ResponseError(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
msg := object.CheckUsername(user.Name, c.GetAcceptLanguage())
|
msg := object.CheckUsername(user.Name, c.GetAcceptLanguage())
|
||||||
if msg != "" {
|
if msg != "" {
|
||||||
c.ResponseError(msg)
|
c.ResponseError(msg)
|
||||||
|
@ -153,3 +153,47 @@ func (c *ApiController) GetProviderFromContext(category string) (*object.Provide
|
|||||||
|
|
||||||
return provider, user, true
|
return provider, user, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkQuotaForApplication(count int) error {
|
||||||
|
quota := conf.GetConfigQuota().Application
|
||||||
|
if quota == -1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if count >= quota {
|
||||||
|
return fmt.Errorf("application quota is exceeded")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkQuotaForOrganization(count int) error {
|
||||||
|
quota := conf.GetConfigQuota().Organization
|
||||||
|
if quota == -1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if count >= quota {
|
||||||
|
return fmt.Errorf("organization quota is exceeded")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkQuotaForProvider(count int) error {
|
||||||
|
quota := conf.GetConfigQuota().Provider
|
||||||
|
if quota == -1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if count >= quota {
|
||||||
|
return fmt.Errorf("provider quota is exceeded")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkQuotaForUser(count int) error {
|
||||||
|
quota := conf.GetConfigQuota().User
|
||||||
|
if quota == -1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if count >= quota {
|
||||||
|
return fmt.Errorf("user quota is exceeded")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user