Add GetMaskedApplication() and GetMaskedApplications().

This commit is contained in:
Yang Luo
2021-12-29 19:12:11 +08:00
parent 377ac05928
commit 519fd655cf
3 changed files with 38 additions and 17 deletions

View File

@@ -30,6 +30,7 @@ import (
// @Success 200 {array} object.Application The Response object // @Success 200 {array} object.Application The Response object
// @router /get-applications [get] // @router /get-applications [get]
func (c *ApiController) GetApplications() { func (c *ApiController) GetApplications() {
userId := c.GetSessionUsername()
owner := c.Input().Get("owner") owner := c.Input().Get("owner")
limit := c.Input().Get("pageSize") limit := c.Input().Get("pageSize")
page := c.Input().Get("p") page := c.Input().Get("p")
@@ -38,12 +39,12 @@ func (c *ApiController) GetApplications() {
sortField := c.Input().Get("sortField") sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder") sortOrder := c.Input().Get("sortOrder")
if limit == "" || page == "" { if limit == "" || page == "" {
c.Data["json"] = object.GetApplications(owner) c.Data["json"] = object.GetMaskedApplications(object.GetApplications(owner), userId)
c.ServeJSON() c.ServeJSON()
} else { } else {
limit := util.ParseInt(limit) limit := util.ParseInt(limit)
paginator := pagination.SetPaginator(c.Ctx, limit, int64(object.GetApplicationCount(owner, field, value))) paginator := pagination.SetPaginator(c.Ctx, limit, int64(object.GetApplicationCount(owner, field, value)))
applications := object.GetPaginationApplications(owner, paginator.Offset(), limit, field, value, sortField, sortOrder) applications := object.GetMaskedApplications(object.GetPaginationApplications(owner, paginator.Offset(), limit, field, value, sortField, sortOrder), userId)
c.ResponseOk(applications, paginator.Nums()) c.ResponseOk(applications, paginator.Nums())
} }
} }
@@ -56,9 +57,10 @@ func (c *ApiController) GetApplications() {
// @Success 200 {object} object.Application The Response object // @Success 200 {object} object.Application The Response object
// @router /get-application [get] // @router /get-application [get]
func (c *ApiController) GetApplication() { func (c *ApiController) GetApplication() {
userId := c.GetSessionUsername()
id := c.Input().Get("id") id := c.Input().Get("id")
c.Data["json"] = object.GetApplication(id) c.Data["json"] = object.GetMaskedApplication(object.GetApplication(id), userId)
c.ServeJSON() c.ServeJSON()
} }
@@ -70,6 +72,7 @@ func (c *ApiController) GetApplication() {
// @Success 200 {object} object.Application The Response object // @Success 200 {object} object.Application The Response object
// @router /get-user-application [get] // @router /get-user-application [get]
func (c *ApiController) GetUserApplication() { func (c *ApiController) GetUserApplication() {
userId := c.GetSessionUsername()
id := c.Input().Get("id") id := c.Input().Get("id")
user := object.GetUser(id) user := object.GetUser(id)
if user == nil { if user == nil {
@@ -77,7 +80,7 @@ func (c *ApiController) GetUserApplication() {
return return
} }
c.Data["json"] = object.GetApplicationByUser(user) c.Data["json"] = object.GetMaskedApplication(object.GetApplicationByUser(user), userId)
c.ServeJSON() c.ServeJSON()
} }

View File

@@ -200,24 +200,37 @@ func GetApplicationByClientId(clientId string) *Application {
} }
} }
func GetApplicationByClientIdAndSecret(clientId, clientSecret string) *Application {
if util.IsStrsEmpty(clientId, clientSecret) {
return nil
}
app := GetApplicationByClientId(clientId)
if app == nil || app.ClientSecret != clientSecret {
return nil
}
return app
}
func GetApplication(id string) *Application { func GetApplication(id string) *Application {
owner, name := util.GetOwnerAndNameFromId(id) owner, name := util.GetOwnerAndNameFromId(id)
return getApplication(owner, name) return getApplication(owner, name)
} }
func GetMaskedApplication(application *Application, userId string) *Application {
if isUserIdGlobalAdmin(userId) {
return application
}
if application == nil {
return nil
}
if application.ClientSecret != "" {
application.ClientSecret = "***"
}
return application
}
func GetMaskedApplications(applications []*Application, userId string) []*Application {
if isUserIdGlobalAdmin(userId) {
return applications
}
for _, application := range applications {
application = GetMaskedApplication(application, userId)
}
return applications
}
func UpdateApplication(id string, application *Application) bool { func UpdateApplication(id string, application *Application) bool {
owner, name := util.GetOwnerAndNameFromId(id) owner, name := util.GetOwnerAndNameFromId(id)
if getApplication(owner, name) == nil { if getApplication(owner, name) == nil {

View File

@@ -16,6 +16,7 @@ package object
import ( import (
"fmt" "fmt"
"strings"
"github.com/casbin/casdoor/util" "github.com/casbin/casdoor/util"
"xorm.io/core" "xorm.io/core"
@@ -407,3 +408,7 @@ func LinkUserAccount(user *User, field string, value string) bool {
func (user *User) GetId() string { func (user *User) GetId() string {
return fmt.Sprintf("%s/%s", user.Owner, user.Name) return fmt.Sprintf("%s/%s", user.Owner, user.Name)
} }
func isUserIdGlobalAdmin(userId string) bool {
return strings.HasPrefix(userId, "built-in/")
}