feat: replace GetOwnerAndNameFromId with GetOwnerAndNameFromIdWithError everywhere (#4383)

This commit is contained in:
Copilot
2025-11-03 11:38:54 +08:00
committed by GitHub
parent af5a9c805d
commit 8ee8767882
39 changed files with 323 additions and 90 deletions

View File

@@ -343,8 +343,12 @@ func (c *ApiController) Logout() {
c.ClearUserSession()
c.ClearTokenSession()
owner, username := util.GetOwnerAndNameFromId(user)
_, err := object.DeleteSessionId(util.GetSessionId(owner, username, object.CasdoorApplication), c.Ctx.Input.CruSession.SessionID())
owner, username, err := util.GetOwnerAndNameFromIdWithError(user)
if err != nil {
c.ResponseError(err.Error())
return
}
_, err = object.DeleteSessionId(util.GetSessionId(owner, username, object.CasdoorApplication), c.Ctx.Input.CruSession.SessionID())
if err != nil {
c.ResponseError(err.Error())
return
@@ -391,7 +395,11 @@ func (c *ApiController) Logout() {
c.ClearUserSession()
c.ClearTokenSession()
// TODO https://github.com/casdoor/casdoor/pull/1494#discussion_r1095675265
owner, username := util.GetOwnerAndNameFromId(user)
owner, username, err := util.GetOwnerAndNameFromIdWithError(user)
if err != nil {
c.ResponseError(err.Error())
return
}
_, err = object.DeleteSessionId(util.GetSessionId(owner, username, object.CasdoorApplication), c.Ctx.Input.CruSession.SessionID())
if err != nil {

View File

@@ -119,7 +119,11 @@ func (c *ApiController) Enforce() {
permissions := []*object.Permission{}
if modelId != "" {
owner, modelName := util.GetOwnerAndNameFromId(modelId)
owner, modelName, err := util.GetOwnerAndNameFromIdWithError(modelId)
if err != nil {
c.ResponseError(err.Error())
return
}
permissions, err = object.GetPermissionsByModel(owner, modelName)
if err != nil {
c.ResponseError(err.Error())
@@ -255,7 +259,11 @@ func (c *ApiController) BatchEnforce() {
permissions := []*object.Permission{}
if modelId != "" {
owner, modelName := util.GetOwnerAndNameFromId(modelId)
owner, modelName, err := util.GetOwnerAndNameFromIdWithError(modelId)
if err != nil {
c.ResponseError(err.Error())
return
}
permissions, err = object.GetPermissionsByModel(owner, modelName)
if err != nil {
c.ResponseError(err.Error())

View File

@@ -24,7 +24,11 @@ import (
func (c *ApiController) UploadGroups() {
userId := c.GetSessionUsername()
owner, user := util.GetOwnerAndNameFromId(userId)
owner, user, err := util.GetOwnerAndNameFromIdWithError(userId)
if err != nil {
c.ResponseError(err.Error())
return
}
file, header, err := c.Ctx.Request.FormFile("file")
if err != nil {

View File

@@ -47,7 +47,11 @@ type LdapSyncResp struct {
func (c *ApiController) GetLdapUsers() {
id := c.Input().Get("id")
_, ldapId := util.GetOwnerAndNameFromId(id)
_, ldapId, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
c.ResponseError(err.Error())
return
}
ldapServer, err := object.GetLdap(ldapId)
if err != nil {
c.ResponseError(err.Error())
@@ -125,7 +129,11 @@ func (c *ApiController) GetLdap() {
return
}
_, name := util.GetOwnerAndNameFromId(id)
_, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
c.ResponseError(err.Error())
return
}
ldap, err := object.GetLdap(name)
if err != nil {
c.ResponseError(err.Error())
@@ -255,9 +263,13 @@ func (c *ApiController) DeleteLdap() {
func (c *ApiController) SyncLdapUsers() {
id := c.Input().Get("id")
owner, ldapId := util.GetOwnerAndNameFromId(id)
owner, ldapId, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
c.ResponseError(err.Error())
return
}
var users []object.LdapUser
err := json.Unmarshal(c.Ctx.Input.RequestBody, &users)
err = json.Unmarshal(c.Ctx.Input.RequestBody, &users)
if err != nil {
c.ResponseError(err.Error())
return

View File

@@ -24,7 +24,11 @@ import (
func (c *ApiController) UploadPermissions() {
userId := c.GetSessionUsername()
owner, user := util.GetOwnerAndNameFromId(userId)
owner, user, err := util.GetOwnerAndNameFromIdWithError(userId)
if err != nil {
c.ResponseError(err.Error())
return
}
file, header, err := c.Ctx.Request.FormFile("file")
if err != nil {

View File

@@ -180,7 +180,11 @@ func (c *ApiController) BuyProduct() {
pricingName := c.Input().Get("pricingName")
planName := c.Input().Get("planName")
paidUserName := c.Input().Get("userName")
owner, _ := util.GetOwnerAndNameFromId(id)
owner, _, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
c.ResponseError(err.Error())
return
}
userId := util.GetId(owner, paidUserName)
if paidUserName != "" && paidUserName != c.GetSessionUsername() && !c.IsAdmin() {
c.ResponseError(c.T("general:Only admin user can specify user"))

View File

@@ -24,7 +24,11 @@ import (
func (c *ApiController) UploadRoles() {
userId := c.GetSessionUsername()
owner, user := util.GetOwnerAndNameFromId(userId)
owner, user, err := util.GetOwnerAndNameFromIdWithError(userId)
if err != nil {
c.ResponseError(err.Error())
return
}
file, header, err := c.Ctx.Request.FormFile("file")
if err != nil {

View File

@@ -52,7 +52,11 @@ func (c *ApiController) UploadUsers() {
}
userId := c.GetSessionUsername()
owner, user := util.GetOwnerAndNameFromId(userId)
owner, user, err := util.GetOwnerAndNameFromIdWithError(userId)
if err != nil {
c.ResponseError(err.Error())
return
}
file, header, err := c.Ctx.Request.FormFile("file")
if err != nil {

View File

@@ -88,12 +88,18 @@ func getAdapter(owner, name string) (*Adapter, error) {
}
func GetAdapter(id string) (*Adapter, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getAdapter(owner, name)
}
func UpdateAdapter(id string, adapter *Adapter) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if adapter, err := getAdapter(owner, name); adapter == nil {
return false, err
}

View File

@@ -449,7 +449,10 @@ func GetApplicationByUser(user *User) (*Application, error) {
}
func GetApplicationByUserId(userId string) (application *Application, err error) {
_, name := util.GetOwnerAndNameFromId(userId)
_, name, err := util.GetOwnerAndNameFromIdWithError(userId)
if err != nil {
return nil, err
}
if IsAppUser(userId) {
application, err = getApplication("admin", name)
return
@@ -646,7 +649,10 @@ func GetAllowedApplications(applications []*Application, userId string, lang str
}
func UpdateApplication(id string, application *Application, isGlobalAdmin bool, lang string) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
oldApplication, err := getApplication(owner, name)
if oldApplication == nil {
return false, err

View File

@@ -149,7 +149,10 @@ func getCertByName(name string) (*Cert, error) {
}
func GetCert(id string) (*Cert, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
cert, err := getCert(owner, name)
if cert == nil && owner != "admin" {
return getCert("admin", name)
@@ -159,7 +162,10 @@ func GetCert(id string) (*Cert, error) {
}
func UpdateCert(id string, cert *Cert) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if c, err := getCert(owner, name); err != nil {
return false, err
} else if c == nil {
@@ -167,13 +173,13 @@ func UpdateCert(id string, cert *Cert) (bool, error) {
}
if name != cert.Name {
err := certChangeTrigger(name, cert.Name)
err = certChangeTrigger(name, cert.Name)
if err != nil {
return false, err
}
}
err := cert.populateContent()
err = cert.populateContent()
if err != nil {
return false, err
}

View File

@@ -568,7 +568,10 @@ func CheckApiPermission(userId string, organization string, path string, method
}
func CheckLoginPermission(userId string, application *Application) (bool, error) {
owner, _ := util.GetOwnerAndNameFromId(userId)
owner, _, err := util.GetOwnerAndNameFromIdWithError(userId)
if err != nil {
return false, err
}
if owner == "built-in" {
return true, nil
}

View File

@@ -84,12 +84,18 @@ func getEnforcer(owner string, name string) (*Enforcer, error) {
}
func GetEnforcer(id string) (*Enforcer, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getEnforcer(owner, name)
}
func UpdateEnforcer(id string, enforcer *Enforcer) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if oldEnforcer, err := getEnforcer(owner, name); err != nil {
return false, err
} else if oldEnforcer == nil {

View File

@@ -96,12 +96,18 @@ func getForm(owner string, name string) (*Form, error) {
}
func GetForm(id string) (*Form, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getForm(owner, name)
}
func UpdateForm(id string, form *Form) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
existingForm, err := getForm(owner, name)
if existingForm == nil {
return false, fmt.Errorf("the form: %s is not found", id)

View File

@@ -135,12 +135,18 @@ func getGroup(owner string, name string) (*Group, error) {
}
func GetGroup(id string) (*Group, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getGroup(owner, name)
}
func UpdateGroup(id string, group *Group) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
oldGroup, err := getGroup(owner, name)
if oldGroup == nil {
return false, err
@@ -299,7 +305,10 @@ func ConvertToTreeData(groups []*Group, parentId string) []*Group {
}
func GetGroupUserCount(groupId string, field, value string) (int64, error) {
owner, _ := util.GetOwnerAndNameFromId(groupId)
owner, _, err := util.GetOwnerAndNameFromIdWithError(groupId)
if err != nil {
return 0, err
}
names, err := userEnforcer.GetUserNamesByGroupName(groupId)
if err != nil {
return 0, err
@@ -318,7 +327,10 @@ func GetGroupUserCount(groupId string, field, value string) (int64, error) {
func GetPaginationGroupUsers(groupId string, offset, limit int, field, value, sortField, sortOrder string) ([]*User, error) {
users := []*User{}
owner, _ := util.GetOwnerAndNameFromId(groupId)
owner, _, err := util.GetOwnerAndNameFromIdWithError(groupId)
if err != nil {
return nil, err
}
names, err := userEnforcer.GetUserNamesByGroupName(groupId)
if err != nil {
return nil, err

View File

@@ -90,7 +90,10 @@ func getInvitation(owner string, name string) (*Invitation, error) {
}
func GetInvitation(id string) (*Invitation, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getInvitation(owner, name)
}
@@ -133,7 +136,10 @@ func GetMaskedInvitation(invitation *Invitation) *Invitation {
}
func UpdateInvitation(id string, invitation *Invitation, lang string) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if p, err := getInvitation(owner, name); err != nil {
return false, err
} else if p == nil {
@@ -146,7 +152,7 @@ func UpdateInvitation(id string, invitation *Invitation, lang string) (bool, err
invitation.IsRegexp = isRegexp
}
err := CheckInvitationDefaultCode(invitation.Code, invitation.DefaultCode, lang)
err = CheckInvitationDefaultCode(invitation.Code, invitation.DefaultCode, lang)
if err != nil {
return false, err
}

View File

@@ -80,12 +80,18 @@ func getModel(owner string, name string) (*Model, error) {
}
func GetModel(id string) (*Model, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getModel(owner, name)
}
func GetModelEx(id string) (*Model, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
model, err := getModel(owner, name)
if err != nil {
return nil, err
@@ -112,7 +118,10 @@ func UpdateModelWithCheck(id string, modelObj *Model) error {
}
func UpdateModel(id string, modelObj *Model) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if m, err := getModel(owner, name); err != nil {
return false, err
} else if m == nil {

View File

@@ -205,7 +205,10 @@ func GetMaskedOrganizations(organizations []*Organization, errs ...error) ([]*Or
}
func UpdateOrganization(id string, organization *Organization, isGlobalAdmin bool) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
org, err := getOrganization(owner, name)
if err != nil {
return false, err
@@ -425,14 +428,20 @@ func organizationChangeTrigger(oldName string, newName string) error {
}
for i, u := range role.Users {
// u = organization/username
owner, name := util.GetOwnerAndNameFromId(u)
owner, name, err := util.GetOwnerAndNameFromIdWithError(u)
if err != nil {
return err
}
if name == oldName {
role.Users[i] = util.GetId(owner, newName)
}
}
for i, u := range role.Roles {
// u = organization/username
owner, name := util.GetOwnerAndNameFromId(u)
owner, name, err := util.GetOwnerAndNameFromIdWithError(u)
if err != nil {
return err
}
if name == oldName {
role.Roles[i] = util.GetId(owner, newName)
}
@@ -450,14 +459,20 @@ func organizationChangeTrigger(oldName string, newName string) error {
}
for i, u := range permission.Users {
// u = organization/username
owner, name := util.GetOwnerAndNameFromId(u)
owner, name, err := util.GetOwnerAndNameFromIdWithError(u)
if err != nil {
return err
}
if name == oldName {
permission.Users[i] = util.GetId(owner, newName)
}
}
for i, u := range permission.Roles {
// u = organization/username
owner, name := util.GetOwnerAndNameFromId(u)
owner, name, err := util.GetOwnerAndNameFromIdWithError(u)
if err != nil {
return err
}
if name == oldName {
permission.Roles[i] = util.GetId(owner, newName)
}

View File

@@ -116,12 +116,18 @@ func getPayment(owner string, name string) (*Payment, error) {
}
func GetPayment(id string) (*Payment, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getPayment(owner, name)
}
func UpdatePayment(id string, payment *Payment) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if p, err := getPayment(owner, name); err != nil {
return false, err
} else if p == nil {

View File

@@ -477,13 +477,19 @@ func (p *Permission) GetModelAndAdapter() string {
}
func (p *Permission) isUserHit(name string) bool {
targetOrg, targetName := util.GetOwnerAndNameFromId(name)
targetOrg, targetName, err := util.GetOwnerAndNameFromIdWithError(name)
if err != nil {
return false
}
for _, user := range p.Users {
if user == "*" {
return true
}
userOrg, userName := util.GetOwnerAndNameFromId(user)
userOrg, userName, err := util.GetOwnerAndNameFromIdWithError(user)
if err != nil {
continue
}
if userOrg == targetOrg && (userName == "*" || userName == targetName) {
return true
}

View File

@@ -138,7 +138,10 @@ func getPolicies(permission *Permission) [][]string {
}
func getRolesInRole(roleId string, visited map[string]struct{}) ([]*Role, error) {
roleOwner, roleName := util.GetOwnerAndNameFromId(roleId)
roleOwner, roleName, err := util.GetOwnerAndNameFromIdWithError(roleId)
if err != nil {
return []*Role{}, err
}
if roleName == "*" {
roles, err := GetRoles(roleOwner)
if err != nil {

View File

@@ -108,12 +108,18 @@ func getPlan(owner, name string) (*Plan, error) {
}
func GetPlan(id string) (*Plan, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getPlan(owner, name)
}
func UpdatePlan(id string, plan *Plan) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if p, err := getPlan(owner, name); err != nil {
return false, err
} else if p == nil {

View File

@@ -98,7 +98,10 @@ func getPricing(owner, name string) (*Pricing, error) {
}
func GetPricing(id string) (*Pricing, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getPricing(owner, name)
}
@@ -117,7 +120,10 @@ func GetApplicationDefaultPricing(owner, appName string) (*Pricing, error) {
}
func UpdatePricing(id string, pricing *Pricing) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if p, err := getPricing(owner, name); err != nil {
return false, err
} else if p == nil {

View File

@@ -94,12 +94,18 @@ func getProduct(owner string, name string) (*Product, error) {
}
func GetProduct(id string) (*Product, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getProduct(owner, name)
}
func UpdateProduct(id string, product *Product) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if p, err := getProduct(owner, name); err != nil {
return false, err
} else if p == nil {

View File

@@ -182,7 +182,10 @@ func getProvider(owner string, name string) (*Provider, error) {
}
func GetProvider(id string) (*Provider, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getProvider(owner, name)
}
@@ -197,7 +200,10 @@ func GetWechatMiniProgramProvider(application *Application) *Provider {
}
func UpdateProvider(id string, provider *Provider) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if p, err := getProvider(owner, name); err != nil {
return false, err
} else if p == nil {
@@ -349,7 +355,10 @@ func (p *Provider) GetId() string {
}
func GetCaptchaProviderByOwnerName(applicationId, lang string) (*Provider, error) {
owner, name := util.GetOwnerAndNameFromId(applicationId)
owner, name, err := util.GetOwnerAndNameFromIdWithError(applicationId)
if err != nil {
return nil, err
}
provider := Provider{Owner: owner, Name: name, Category: "Captcha"}
existed, err := ormer.Engine.Get(&provider)
if err != nil {
@@ -387,7 +396,10 @@ func GetCaptchaProviderByApplication(applicationId, isCurrentProvider, lang stri
}
func GetFaceIdProviderByOwnerName(applicationId, lang string) (*Provider, error) {
owner, name := util.GetOwnerAndNameFromId(applicationId)
owner, name, err := util.GetOwnerAndNameFromIdWithError(applicationId)
if err != nil {
return nil, err
}
provider := Provider{Owner: owner, Name: name, Category: "Face ID"}
existed, err := ormer.Engine.Get(&provider)
if err != nil {

View File

@@ -69,7 +69,10 @@ func getPaginationRadiusAccounting(owner, field, value, sortField, sortOrder str
}
func GetRadiusAccounting(id string) (*RadiusAccounting, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getRadiusAccounting(owner, name)
}
@@ -95,8 +98,11 @@ func DeleteRadiusAccounting(ra *RadiusAccounting) error {
}
func UpdateRadiusAccounting(id string, ra *RadiusAccounting) error {
owner, name := util.GetOwnerAndNameFromId(id)
_, err := ormer.Engine.ID(core.PK{owner, name}).Update(ra)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return err
}
_, err = ormer.Engine.ID(core.PK{owner, name}).Update(ra)
return err
}

View File

@@ -342,7 +342,10 @@ func roleChangeTrigger(oldName string, newName string) error {
continue
}
owner, name := util.GetOwnerAndNameFromId(u)
owner, name, err := util.GetOwnerAndNameFromIdWithError(u)
if err != nil {
return err
}
if name == oldName {
role.Roles[j] = util.GetId(owner, newName)
}
@@ -366,7 +369,10 @@ func roleChangeTrigger(oldName string, newName string) error {
continue
}
owner, name := util.GetOwnerAndNameFromId(u)
owner, name, err := util.GetOwnerAndNameFromIdWithError(u)
if err != nil {
return err
}
if name == oldName {
permission.Roles[j] = util.GetId(owner, newName)
}

View File

@@ -210,12 +210,18 @@ func getSubscription(owner string, name string) (*Subscription, error) {
}
func GetSubscription(id string) (*Subscription, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getSubscription(owner, name)
}
func UpdateSubscription(id string, subscription *Subscription) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if s, err := getSubscription(owner, name); err != nil {
return false, err
} else if s == nil {

View File

@@ -119,7 +119,10 @@ func getSyncer(owner string, name string) (*Syncer, error) {
}
func GetSyncer(id string) (*Syncer, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getSyncer(owner, name)
}
@@ -155,7 +158,10 @@ func GetMaskedSyncers(syncers []*Syncer, errs ...error) ([]*Syncer, error) {
}
func UpdateSyncer(id string, syncer *Syncer, isGlobalAdmin bool, lang string) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
s, err := getSyncer(owner, name)
if err != nil {
return false, err

View File

@@ -154,7 +154,10 @@ func updateUsedByCode(token *Token) (bool, error) {
}
func GetToken(id string) (*Token, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getToken(owner, name)
}
@@ -181,7 +184,10 @@ func (token *Token) popularHashes() {
}
func UpdateToken(id string, token *Token, isGlobalAdmin bool) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if t, err := getToken(owner, name); err != nil {
return false, err
} else if t == nil {

View File

@@ -103,12 +103,18 @@ func getTransaction(owner string, name string) (*Transaction, error) {
}
func GetTransaction(id string) (*Transaction, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getTransaction(owner, name)
}
func UpdateTransaction(id string, transaction *Transaction, lang string) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
oldTransaction, err := getTransaction(owner, name)
if err != nil {
return false, err

View File

@@ -634,7 +634,10 @@ func GetUserByAccessKey(accessKey string) (*User, error) {
}
func GetUser(id string) (*User, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getUser(owner, name)
}
@@ -859,7 +862,10 @@ func updateUser(id string, user *User, columns []string) (int64, error) {
func UpdateUserForAllFields(id string, user *User) (bool, error) {
var err error
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
oldUser, err := getUser(owner, name)
if err != nil {
return false, err
@@ -1233,7 +1239,10 @@ func userChangeTrigger(oldName string, newName string) error {
for _, role := range roles {
for j, u := range role.Users {
// u = organization/username
owner, name := util.GetOwnerAndNameFromId(u)
owner, name, err := util.GetOwnerAndNameFromIdWithError(u)
if err != nil {
return err
}
if name == oldName {
role.Users[j] = util.GetId(owner, newName)
}
@@ -1256,7 +1265,10 @@ func userChangeTrigger(oldName string, newName string) error {
}
// u = organization/username
owner, name := util.GetOwnerAndNameFromId(u)
owner, name, err := util.GetOwnerAndNameFromIdWithError(u)
if err != nil {
return err
}
if name == oldName {
permission.Users[j] = util.GetId(owner, newName)
}

View File

@@ -426,6 +426,9 @@ func getVerification(owner string, name string) (*VerificationRecord, error) {
}
func GetVerification(id string) (*VerificationRecord, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getVerification(owner, name)
}

View File

@@ -101,12 +101,18 @@ func getWebhook(owner string, name string) (*Webhook, error) {
}
func GetWebhook(id string) (*Webhook, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getWebhook(owner, name)
}
func UpdateWebhook(id string, webhook *Webhook, isGlobalAdmin bool, lang string) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if w, err := getWebhook(owner, name); err != nil {
return false, err
} else if w == nil {

View File

@@ -28,7 +28,10 @@ func NewBalancePaymentProvider() (*BalancePaymentProvider, error) {
}
func (pp *BalancePaymentProvider) Pay(r *PayReq) (*PayResp, error) {
owner, _ := util.GetOwnerAndNameFromId(r.PayerId)
owner, _, err := util.GetOwnerAndNameFromIdWithError(r.PayerId)
if err != nil {
return nil, err
}
return &PayResp{
PayUrl: r.ReturnUrl,
OrderId: fmt.Sprintf("%s/%s", owner, r.PaymentName),

View File

@@ -152,7 +152,13 @@ func handleAccountingRequest(w radius.ResponseWriter, r *radius.Request) {
organization := rfc2865.Class_GetString(r.Packet)
if strings.Contains(username, "/") {
organization, username = util.GetOwnerAndNameFromId(username)
var err error
organization, username, err = util.GetOwnerAndNameFromIdWithError(username)
if err != nil {
log.Printf("handleAccountingRequest() failed to parse username, err = %v", err)
w.Write(r.Response(radius.CodeAccessReject))
return
}
}
log.Printf("handleAccountingRequest() username=%v, org=%v, statusType=%v", username, organization, statusType)

View File

@@ -58,7 +58,11 @@ func getSubject(ctx *context.Context) (string, string) {
}
// username == "built-in/admin"
return util.GetOwnerAndNameFromId(username)
owner, name, err := util.GetOwnerAndNameFromIdWithError(username)
if err != nil {
panic(err)
}
return owner, name
}
func getObject(ctx *context.Context) (string, string, error) {

View File

@@ -77,7 +77,11 @@ func AfterRecordMessage(ctx *context.Context) {
userId := ctx.Input.Params()["recordUserId"]
if userId != "" {
record.Organization, record.User = util.GetOwnerAndNameFromId(userId)
owner, user, err := util.GetOwnerAndNameFromIdWithError(userId)
if err != nil {
panic(err)
}
record.Organization, record.User = owner, user
}
var record2 *casvisorsdk.Record

View File

@@ -123,15 +123,6 @@ func SpaceToCamel(name string) string {
return strings.Join(words, "")
}
func GetOwnerAndNameFromId(id string) (string, string) {
tokens := strings.Split(id, "/")
if len(tokens) != 2 {
panic(errors.New("GetOwnerAndNameFromId() error, wrong token count for ID: " + id))
}
return tokens[0], tokens[1]
}
func GetOwnerAndNameFromIdWithError(id string) (string, string, error) {
tokens := strings.Split(id, "/")
if len(tokens) != 2 {