From b2b557b0391630093ab6783204755ddca9776ff9 Mon Sep 17 00:00:00 2001 From: liuqs Date: Thu, 17 Aug 2023 15:54:16 +0800 Subject: [PATCH 1/9] feat:Add apis for cert. --- casdoorsdk/base.go | 25 ++++++++++++++++++++++ casdoorsdk/cert.go | 44 ++++++++++++++++++++++++++++++++------- casdoorsdk/cert_global.go | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 casdoorsdk/cert_global.go diff --git a/casdoorsdk/base.go b/casdoorsdk/base.go index a783266..ebab206 100644 --- a/casdoorsdk/base.go +++ b/casdoorsdk/base.go @@ -390,3 +390,28 @@ func (c *Client) modifyRole(action string, role *Role, columns []string) (*Respo return resp, resp.Data == "Affected", nil } + +// modifyCert is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-cert`, `update-cert`, `delete-cert`, +func (c *Client) modifyCert(action string, cert *Cert, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", cert.Owner, cert.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + cert.Owner = c.OrganizationName + postBytes, err := json.Marshal(cert) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} diff --git a/casdoorsdk/cert.go b/casdoorsdk/cert.go index 69db297..bdc8035 100644 --- a/casdoorsdk/cert.go +++ b/casdoorsdk/cert.go @@ -14,7 +14,10 @@ package casdoorsdk -import "encoding/json" +import ( + "encoding/json" + "fmt" +) // Cert has the same definition as https://github.com/casdoor/casdoor/blob/master/object/cert.go#L24 type Cert struct { @@ -51,10 +54,6 @@ func (c *Client) GetGlobalCerts() ([]*Cert, error) { return certs, nil } -func GetGlobalCerts() ([]*Cert, error) { - return globalClient.GetGlobalCerts() -} - func (c *Client) GetCerts() ([]*Cert, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -75,6 +74,37 @@ func (c *Client) GetCerts() ([]*Cert, error) { return certs, nil } -func GetCerts() ([]*Cert, error) { - return globalClient.GetCerts() +func (c *Client) GetCert(name string) (*Cert, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-cert", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var cert *Cert + err = json.Unmarshal(bytes, &cert) + if err != nil { + return nil, err + } + return cert, nil +} + +func (c *Client) AddCert(cert *Cert) (bool, error) { + _, affected, err := c.modifyCert("add-cert", cert, nil) + return affected, err +} + +func (c *Client) UpdateCert(cert *Cert) (bool, error) { + _, affected, err := c.modifyCert("update-cert", cert, nil) + return affected, err +} + +func (c *Client) DeleteCert(cert *Cert) (bool, error) { + _, affected, err := c.modifyCert("delete-cert", cert, nil) + return affected, err } diff --git a/casdoorsdk/cert_global.go b/casdoorsdk/cert_global.go new file mode 100644 index 0000000..b330e61 --- /dev/null +++ b/casdoorsdk/cert_global.go @@ -0,0 +1,36 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetGlobalCerts() ([]*Cert, error) { + return globalClient.GetGlobalCerts() + +} + +func GetCerts() ([]*Cert, error) { + return globalClient.GetCerts() +} + +func UpdateCert(cert *Cert) (bool, error) { + return globalClient.UpdateCert(cert) +} + +func AddCert(cert *Cert) (bool, error) { + return globalClient.AddCert(cert) +} + +func DeleteCert(cert *Cert) (bool, error) { + return globalClient.DeleteCert(cert) +} From 2e495b5ddbbcfc6f3330e6a111307a5676042bfc Mon Sep 17 00:00:00 2001 From: liuqs Date: Thu, 17 Aug 2023 16:30:06 +0800 Subject: [PATCH 2/9] feat:Add apis for adapter,enforcer,group,model,permission. --- casdoorsdk/adapter.go | 111 +++++++++++++++++++++++++++++++ casdoorsdk/adapter_global.go | 25 +++++++ casdoorsdk/base.go | 99 ++++++++++++++++++++++++++++ casdoorsdk/enfocer.go | 106 ++++++++++++++++++++++++++++++ casdoorsdk/enforcer_global.go | 25 +++++++ casdoorsdk/group.go | 112 ++++++++++++++++++++++++++++++++ casdoorsdk/group_global.go | 25 +++++++ casdoorsdk/model.go | 112 ++++++++++++++++++++++++++++++++ casdoorsdk/model_global.go | 25 +++++++ casdoorsdk/permission.go | 32 --------- casdoorsdk/permission_global.go | 33 ++++++++++ 11 files changed, 673 insertions(+), 32 deletions(-) create mode 100644 casdoorsdk/adapter.go create mode 100644 casdoorsdk/adapter_global.go create mode 100644 casdoorsdk/enfocer.go create mode 100644 casdoorsdk/enforcer_global.go create mode 100644 casdoorsdk/group.go create mode 100644 casdoorsdk/group_global.go create mode 100644 casdoorsdk/model.go create mode 100644 casdoorsdk/model_global.go create mode 100644 casdoorsdk/permission_global.go diff --git a/casdoorsdk/adapter.go b/casdoorsdk/adapter.go new file mode 100644 index 0000000..49b3437 --- /dev/null +++ b/casdoorsdk/adapter.go @@ -0,0 +1,111 @@ +package casdoorsdk + +import ( + "encoding/json" + "fmt" + "strconv" +) + +type Adapter struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + + Type string `xorm:"varchar(100)" json:"type"` + DatabaseType string `xorm:"varchar(100)" json:"databaseType"` + Host string `xorm:"varchar(100)" json:"host"` + Port int `json:"port"` + User string `xorm:"varchar(100)" json:"user"` + Password string `xorm:"varchar(100)" json:"password"` + Database string `xorm:"varchar(100)" json:"database"` + Table string `xorm:"varchar(100)" json:"table"` + TableNamePrefix string `xorm:"varchar(100)" json:"tableNamePrefix"` + + IsEnabled bool `json:"isEnabled"` + + //*xormadapter.Adapter `xorm:"-" json:"-"` +} + +func (c *Client) GetAdapters() ([]*Adapter, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-adapters", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var adapters []*Adapter + err = json.Unmarshal(bytes, &adapters) + if err != nil { + return nil, err + } + return adapters, nil +} + +func (c *Client) GetPaginationAdapters(p int, pageSize int, queryMap map[string]string) ([]*Adapter, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-adapters", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + bytes, err := json.Marshal(response.Data) + if err != nil { + return nil, 0, err + } + + var adapters []*Adapter + err = json.Unmarshal(bytes, &adapters) + if err != nil { + return nil, 0, err + } + return adapters, int(response.Data2.(float64)), nil +} + +func (c *Client) GetAdapter(name string) (*Adapter, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-adapter", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var adapter *Adapter + err = json.Unmarshal(bytes, &adapter) + if err != nil { + return nil, err + } + return adapter, nil +} + +func (c *Client) UpdateAdapter(adapter *Adapter) (bool, error) { + _, affected, err := c.modifyAdapter("update-adapter", adapter, nil) + return affected, err +} + +func (c *Client) AddAdapter(adapter *Adapter) (bool, error) { + _, affected, err := c.modifyAdapter("add-adapter", adapter, nil) + return affected, err +} + +func (c *Client) DeleteAdapter(adapter *Adapter) (bool, error) { + _, affected, err := c.modifyAdapter("delete-adapter", adapter, nil) + return affected, err +} diff --git a/casdoorsdk/adapter_global.go b/casdoorsdk/adapter_global.go new file mode 100644 index 0000000..dff3222 --- /dev/null +++ b/casdoorsdk/adapter_global.go @@ -0,0 +1,25 @@ +package casdoorsdk + +func GetAdapters() ([]*Adapter, error) { + return globalClient.GetAdapters() +} + +func GetPaginationAdapters(p int, pageSize int, queryMap map[string]string) ([]*Adapter, int, error) { + return globalClient.GetPaginationAdapters(p, pageSize, queryMap) +} + +func GetAdapter(name string) (*Adapter, error) { + return globalClient.GetAdapter(name) +} + +func UpdateAdapter(adapter *Adapter) (bool, error) { + return globalClient.UpdateAdapter(adapter) +} + +func AddAdapter(adapter *Adapter) (bool, error) { + return globalClient.AddAdapter(adapter) +} + +func DeleteAdapter(adapter *Adapter) (bool, error) { + return globalClient.DeleteAdapter(adapter) +} diff --git a/casdoorsdk/base.go b/casdoorsdk/base.go index ebab206..7ece959 100644 --- a/casdoorsdk/base.go +++ b/casdoorsdk/base.go @@ -415,3 +415,102 @@ func (c *Client) modifyCert(action string, cert *Cert, columns []string) (*Respo return resp, resp.Data == "Affected", nil } + +// modifyEnforcer is an encapsulation of cert CUD(Create, Update, Delete) operations. +func (c *Client) modifyEnforcer(action string, enforcer *Enforcer, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", enforcer.Owner, enforcer.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + enforcer.Owner = c.OrganizationName + postBytes, err := json.Marshal(enforcer) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + +// modifyEnforcer is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-group`, `update-group`, `delete-group`, +func (c *Client) modifyGroup(action string, group *Group, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", group.Owner, group.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + group.Owner = c.OrganizationName + postBytes, err := json.Marshal(group) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + +// modifyAdapter is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-adapter`, `update-adapter`, `delete-adapter`, +func (c *Client) modifyAdapter(action string, adapter *Adapter, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", adapter.Owner, adapter.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + adapter.Owner = c.OrganizationName + postBytes, err := json.Marshal(adapter) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + +// modifyModel is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-adapter`, `update-adapter`, `delete-adapter`, +func (c *Client) modifyModel(action string, model *Model, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", model.Owner, model.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + model.Owner = c.OrganizationName + postBytes, err := json.Marshal(model) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} diff --git a/casdoorsdk/enfocer.go b/casdoorsdk/enfocer.go new file mode 100644 index 0000000..a6584e3 --- /dev/null +++ b/casdoorsdk/enfocer.go @@ -0,0 +1,106 @@ +package casdoorsdk + +import ( + "encoding/json" + "fmt" + "strconv" +) + +type Enforcer struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + UpdatedTime string `xorm:"varchar(100) updated" json:"updatedTime"` + DisplayName string `xorm:"varchar(100)" json:"displayName"` + Description string `xorm:"varchar(100)" json:"description"` + + Model string `xorm:"varchar(100)" json:"model"` + Adapter string `xorm:"varchar(100)" json:"adapter"` + IsEnabled bool `json:"isEnabled"` + + //*casbin.Enforcer +} + +func (c *Client) GetEnforcers() ([]*Enforcer, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-enforcers", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var enforcers []*Enforcer + err = json.Unmarshal(bytes, &enforcers) + if err != nil { + return nil, err + } + return enforcers, nil +} + +func (c *Client) GetPaginationEnforcers(p int, pageSize int, queryMap map[string]string) ([]*Enforcer, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-enforcers", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + bytes, err := json.Marshal(response.Data) + if err != nil { + return nil, 0, err + } + + var enforcers []*Enforcer + err = json.Unmarshal(bytes, &enforcers) + if err != nil { + return nil, 0, err + } + return enforcers, int(response.Data2.(float64)), nil +} + +func (c *Client) GetEnforcer(name string) (*Enforcer, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-enforcer", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var enforcer *Enforcer + err = json.Unmarshal(bytes, &enforcer) + if err != nil { + return nil, err + } + return enforcer, nil +} + +func (c *Client) UpdateEnforcer(enforcer *Enforcer) (bool, error) { + _, affected, err := c.modifyEnforcer("update-enforcer", enforcer, nil) + return affected, err +} + +func (c *Client) AddEnforcer(enforcer *Enforcer) (bool, error) { + _, affected, err := c.modifyEnforcer("add-enforcer", enforcer, nil) + return affected, err +} + +func (c *Client) DeleteEnforcer(enforcer *Enforcer) (bool, error) { + _, affected, err := c.modifyEnforcer("delete-enforcer", enforcer, nil) + return affected, err +} diff --git a/casdoorsdk/enforcer_global.go b/casdoorsdk/enforcer_global.go new file mode 100644 index 0000000..87019f0 --- /dev/null +++ b/casdoorsdk/enforcer_global.go @@ -0,0 +1,25 @@ +package casdoorsdk + +func GetEnforcers() ([]*Enforcer, error) { + return globalClient.GetEnforcers() +} + +func GetPaginationEnforcers(p int, pageSize int, queryMap map[string]string) ([]*Enforcer, int, error) { + return globalClient.GetPaginationEnforcers(p, pageSize, queryMap) +} + +func GetEnforcer(name string) (*Enforcer, error) { + return globalClient.GetEnforcer(name) +} + +func UpdateEnforcer(enforcer *Enforcer) (bool, error) { + return globalClient.UpdateEnforcer(enforcer) +} + +func AddEnforcer(enforcer *Enforcer) (bool, error) { + return globalClient.AddEnforcer(enforcer) +} + +func DeleteEnforcer(enforcer *Enforcer) (bool, error) { + return globalClient.DeleteEnforcer(enforcer) +} diff --git a/casdoorsdk/group.go b/casdoorsdk/group.go new file mode 100644 index 0000000..728590b --- /dev/null +++ b/casdoorsdk/group.go @@ -0,0 +1,112 @@ +package casdoorsdk + +import ( + "encoding/json" + "fmt" + "strconv" +) + +type Group struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk unique index" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"` + + DisplayName string `xorm:"varchar(100)" json:"displayName"` + Manager string `xorm:"varchar(100)" json:"manager"` + ContactEmail string `xorm:"varchar(100)" json:"contactEmail"` + Type string `xorm:"varchar(100)" json:"type"` + ParentId string `xorm:"varchar(100)" json:"parentId"` + IsTopGroup bool `xorm:"bool" json:"isTopGroup"` + Users []*User `xorm:"-" json:"users"` + + Title string `json:"title,omitempty"` + Key string `json:"key,omitempty"` + Children []*Group `json:"children,omitempty"` + + IsEnabled bool `json:"isEnabled"` +} + +func (c *Client) GetGroups() ([]*Group, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-groups", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var groups []*Group + err = json.Unmarshal(bytes, &groups) + if err != nil { + return nil, err + } + return groups, nil +} + +func (c *Client) GetPaginationGroups(p int, pageSize int, queryMap map[string]string) ([]*Group, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-groups", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + bytes, err := json.Marshal(response.Data) + if err != nil { + return nil, 0, err + } + + var groups []*Group + err = json.Unmarshal(bytes, &groups) + if err != nil { + return nil, 0, err + } + return groups, int(response.Data2.(float64)), nil +} + +func (c *Client) GetGroup(name string) (*Group, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-group", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var group *Group + err = json.Unmarshal(bytes, &group) + if err != nil { + return nil, err + } + return group, nil +} + +func (c *Client) UpdateGroup(group *Group) (bool, error) { + _, affected, err := c.modifyGroup("update-group", group, nil) + return affected, err +} + +func (c *Client) AddGroup(group *Group) (bool, error) { + _, affected, err := c.modifyGroup("add-group", group, nil) + return affected, err +} + +func (c *Client) DeleteGroup(group *Group) (bool, error) { + _, affected, err := c.modifyGroup("delete-group", group, nil) + return affected, err +} diff --git a/casdoorsdk/group_global.go b/casdoorsdk/group_global.go new file mode 100644 index 0000000..aa110ff --- /dev/null +++ b/casdoorsdk/group_global.go @@ -0,0 +1,25 @@ +package casdoorsdk + +func GetGroups() ([]*Group, error) { + return globalClient.GetGroups() +} + +func GetPaginationGroups(p int, pageSize int, queryMap map[string]string) ([]*Group, int, error) { + return globalClient.GetPaginationGroups(p, pageSize, queryMap) +} + +func GetGroup(name string) (*Group, error) { + return globalClient.GetGroup(name) +} + +func UpdateGroup(group *Group) (bool, error) { + return globalClient.UpdateGroup(group) +} + +func AddGroup(group *Group) (bool, error) { + return globalClient.AddGroup(group) +} + +func DeleteGroup(group *Group) (bool, error) { + return globalClient.DeleteGroup(group) +} diff --git a/casdoorsdk/model.go b/casdoorsdk/model.go new file mode 100644 index 0000000..29305f3 --- /dev/null +++ b/casdoorsdk/model.go @@ -0,0 +1,112 @@ +package casdoorsdk + +import ( + "encoding/json" + "fmt" + "strconv" +) + +type Model struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk unique index" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"` + + DisplayName string `xorm:"varchar(100)" json:"displayName"` + Manager string `xorm:"varchar(100)" json:"manager"` + ContactEmail string `xorm:"varchar(100)" json:"contactEmail"` + Type string `xorm:"varchar(100)" json:"type"` + ParentId string `xorm:"varchar(100)" json:"parentId"` + IsTopModel bool `xorm:"bool" json:"isTopModel"` + Users []*User `xorm:"-" json:"users"` + + Title string `json:"title,omitempty"` + Key string `json:"key,omitempty"` + Children []*Model `json:"children,omitempty"` + + IsEnabled bool `json:"isEnabled"` +} + +func (c *Client) GetModels() ([]*Model, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-models", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var models []*Model + err = json.Unmarshal(bytes, &models) + if err != nil { + return nil, err + } + return models, nil +} + +func (c *Client) GetPaginationModels(p int, pageSize int, queryMap map[string]string) ([]*Model, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-models", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + bytes, err := json.Marshal(response.Data) + if err != nil { + return nil, 0, err + } + + var models []*Model + err = json.Unmarshal(bytes, &models) + if err != nil { + return nil, 0, err + } + return models, int(response.Data2.(float64)), nil +} + +func (c *Client) GetModel(name string) (*Model, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-model", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var model *Model + err = json.Unmarshal(bytes, &model) + if err != nil { + return nil, err + } + return model, nil +} + +func (c *Client) UpdateModel(model *Model) (bool, error) { + _, affected, err := c.modifyModel("update-model", model, nil) + return affected, err +} + +func (c *Client) AddModel(model *Model) (bool, error) { + _, affected, err := c.modifyModel("add-model", model, nil) + return affected, err +} + +func (c *Client) DeleteModel(model *Model) (bool, error) { + _, affected, err := c.modifyModel("delete-model", model, nil) + return affected, err +} diff --git a/casdoorsdk/model_global.go b/casdoorsdk/model_global.go new file mode 100644 index 0000000..913082c --- /dev/null +++ b/casdoorsdk/model_global.go @@ -0,0 +1,25 @@ +package casdoorsdk + +func GetModels() ([]*Model, error) { + return globalClient.GetModels() +} + +func GetPaginationModels(p int, pageSize int, queryMap map[string]string) ([]*Model, int, error) { + return globalClient.GetPaginationModels(p, pageSize, queryMap) +} + +func GetModel(name string) (*Model, error) { + return globalClient.GetModel(name) +} + +func UpdateModel(model *Model) (bool, error) { + return globalClient.UpdateModel(model) +} + +func AddModel(model *Model) (bool, error) { + return globalClient.AddModel(model) +} + +func DeleteModel(model *Model) (bool, error) { + return globalClient.DeleteModel(model) +} diff --git a/casdoorsdk/permission.go b/casdoorsdk/permission.go index f55b33c..84e0e3b 100644 --- a/casdoorsdk/permission.go +++ b/casdoorsdk/permission.go @@ -65,10 +65,6 @@ func (c *Client) GetPermissions() ([]*Permission, error) { return permissions, nil } -func GetPermissions() ([]*Permission, error) { - return globalClient.GetPermissions() -} - func (c *Client) GetPermissionsByRole(name string) ([]*Permission, error) { queryMap := map[string]string{ "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), @@ -89,10 +85,6 @@ func (c *Client) GetPermissionsByRole(name string) ([]*Permission, error) { return permissions, nil } -func GetPermissionsByRole(name string) ([]*Permission, error) { - return globalClient.GetPermissionsByRole(name) -} - func (c *Client) GetPaginationPermissions(p int, pageSize int, queryMap map[string]string) ([]*Permission, int, error) { queryMap["owner"] = c.OrganizationName queryMap["p"] = strconv.Itoa(p) @@ -122,10 +114,6 @@ func (c *Client) GetPaginationPermissions(p int, pageSize int, queryMap map[stri return permissions, int(response.Data2.(float64)), nil } -func GetPaginationPermissions(p int, pageSize int, queryMap map[string]string) ([]*Permission, int, error) { - return globalClient.GetPaginationPermissions(p, pageSize, queryMap) -} - func (c *Client) GetPermission(name string) (*Permission, error) { queryMap := map[string]string{ "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), @@ -146,42 +134,22 @@ func (c *Client) GetPermission(name string) (*Permission, error) { return permission, nil } -func GetPermission(name string) (*Permission, error) { - return globalClient.GetPermission(name) -} - func (c *Client) UpdatePermission(permission *Permission) (bool, error) { _, affected, err := c.modifyPermission("update-permission", permission, nil) return affected, err } -func UpdatePermission(permission *Permission) (bool, error) { - return globalClient.UpdatePermission(permission) -} - func (c *Client) UpdatePermissionForColumns(permission *Permission, columns []string) (bool, error) { _, affected, err := c.modifyPermission("update-permission", permission, columns) return affected, err } -func UpdatePermissionForColumns(permission *Permission, columns []string) (bool, error) { - return globalClient.UpdatePermissionForColumns(permission, columns) -} - func (c *Client) AddPermission(permission *Permission) (bool, error) { _, affected, err := c.modifyPermission("add-permission", permission, nil) return affected, err } -func AddPermission(permission *Permission) (bool, error) { - return globalClient.AddPermission(permission) -} - func (c *Client) DeletePermission(permission *Permission) (bool, error) { _, affected, err := c.modifyPermission("delete-permission", permission, nil) return affected, err } - -func DeletePermission(permission *Permission) (bool, error) { - return globalClient.DeletePermission(permission) -} diff --git a/casdoorsdk/permission_global.go b/casdoorsdk/permission_global.go new file mode 100644 index 0000000..a604faf --- /dev/null +++ b/casdoorsdk/permission_global.go @@ -0,0 +1,33 @@ +package casdoorsdk + +func GetPermissions() ([]*Permission, error) { + return globalClient.GetPermissions() +} + +func GetPermissionsByRole(name string) ([]*Permission, error) { + return globalClient.GetPermissionsByRole(name) +} + +func GetPaginationPermissions(p int, pageSize int, queryMap map[string]string) ([]*Permission, int, error) { + return globalClient.GetPaginationPermissions(p, pageSize, queryMap) +} + +func GetPermission(name string) (*Permission, error) { + return globalClient.GetPermission(name) +} + +func UpdatePermission(permission *Permission) (bool, error) { + return globalClient.UpdatePermission(permission) +} + +func UpdatePermissionForColumns(permission *Permission, columns []string) (bool, error) { + return globalClient.UpdatePermissionForColumns(permission, columns) +} + +func AddPermission(permission *Permission) (bool, error) { + return globalClient.AddPermission(permission) +} + +func DeletePermission(permission *Permission) (bool, error) { + return globalClient.DeletePermission(permission) +} From df9a2c4cc681e66ba783a948a10ae00c1cb40613 Mon Sep 17 00:00:00 2001 From: liuqs Date: Sat, 19 Aug 2023 15:47:05 +0800 Subject: [PATCH 3/9] feat:Add apis. --- casdoorsdk/adapter.go | 13 +-- casdoorsdk/base.go | 177 +++++++++++++++++++++++++++++- casdoorsdk/enfocer.go | 13 +-- casdoorsdk/model.go | 13 +-- casdoorsdk/payment.go | 157 ++++++++++++++++++++++++++ casdoorsdk/payment_global.go | 35 ++++++ casdoorsdk/permission.go | 13 +-- casdoorsdk/plan.go | 117 ++++++++++++++++++++ casdoorsdk/plan_global.go | 39 +++++++ casdoorsdk/pricing.go | 120 ++++++++++++++++++++ casdoorsdk/princing_global.go | 38 +++++++ casdoorsdk/product.go | 129 ++++++++++++++++++++++ casdoorsdk/product_global.go | 29 +++++ casdoorsdk/provider.go | 38 +------ casdoorsdk/provider_global.go | 25 +++++ casdoorsdk/subscription.go | 119 ++++++++++++++++++++ casdoorsdk/subscription_global.go | 23 ++++ casdoorsdk/syncer.go | 137 +++++++++++++++++++++++ casdoorsdk/syncer_global.go | 39 +++++++ casdoorsdk/webhook.go | 128 +++++++++++++++++++++ casdoorsdk/webhook_global.go | 39 +++++++ 21 files changed, 1370 insertions(+), 71 deletions(-) create mode 100644 casdoorsdk/payment.go create mode 100644 casdoorsdk/payment_global.go create mode 100644 casdoorsdk/plan.go create mode 100644 casdoorsdk/plan_global.go create mode 100644 casdoorsdk/pricing.go create mode 100644 casdoorsdk/princing_global.go create mode 100644 casdoorsdk/product.go create mode 100644 casdoorsdk/product_global.go create mode 100644 casdoorsdk/provider_global.go create mode 100644 casdoorsdk/subscription.go create mode 100644 casdoorsdk/subscription_global.go create mode 100644 casdoorsdk/syncer.go create mode 100644 casdoorsdk/syncer_global.go create mode 100644 casdoorsdk/webhook.go create mode 100644 casdoorsdk/webhook_global.go diff --git a/casdoorsdk/adapter.go b/casdoorsdk/adapter.go index 49b3437..6ebb407 100644 --- a/casdoorsdk/adapter.go +++ b/casdoorsdk/adapter.go @@ -2,6 +2,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -62,15 +63,9 @@ func (c *Client) GetPaginationAdapters(p int, pageSize int, queryMap map[string] return nil, 0, fmt.Errorf(response.Msg) } - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err - } - - var adapters []*Adapter - err = json.Unmarshal(bytes, &adapters) - if err != nil { - return nil, 0, err + adapters, ok := response.Data.([]*Adapter) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } return adapters, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/base.go b/casdoorsdk/base.go index 7ece959..5cba209 100644 --- a/casdoorsdk/base.go +++ b/casdoorsdk/base.go @@ -491,7 +491,7 @@ func (c *Client) modifyAdapter(action string, adapter *Adapter, columns []string } // modifyModel is an encapsulation of cert CUD(Create, Update, Delete) operations. -// possible actions are `add-adapter`, `update-adapter`, `delete-adapter`, +// possible actions are `add-model`, `update-model`, `delete-model`, func (c *Client) modifyModel(action string, model *Model, columns []string) (*Response, bool, error) { queryMap := map[string]string{ "id": fmt.Sprintf("%s/%s", model.Owner, model.Name), @@ -514,3 +514,178 @@ func (c *Client) modifyModel(action string, model *Model, columns []string) (*Re return resp, resp.Data == "Affected", nil } + +// modifyProduct is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-product`, `update-product`, `delete-product`, +func (c *Client) modifyProduct(action string, product *Product, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", product.Owner, product.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + product.Owner = c.OrganizationName + postBytes, err := json.Marshal(product) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + +// modifyPayment is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-payment`, `update-payment`, `delete-payment`, +func (c *Client) modifyPayment(action string, payment *Payment, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", payment.Owner, payment.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + payment.Owner = c.OrganizationName + postBytes, err := json.Marshal(payment) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + +// modifyPlan is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-plan`, `update-plan`, `delete-plan`, +func (c *Client) modifyPlan(action string, plan *Plan, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", plan.Owner, plan.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + plan.Owner = c.OrganizationName + postBytes, err := json.Marshal(plan) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + +// modifyPricing is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-pricing`, `update-pricing`, `delete-pricing`, +func (c *Client) modifyPricing(action string, pricing *Pricing, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", pricing.Owner, pricing.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + pricing.Owner = c.OrganizationName + postBytes, err := json.Marshal(pricing) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + +// modifySubscription is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-subscription`, `update-subscription`, `delete-subscription`, +func (c *Client) modifySubscription(action string, subscription *Subscription, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", subscription.Owner, subscription.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + subscription.Owner = c.OrganizationName + postBytes, err := json.Marshal(subscription) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + +// modifySyner is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-syncer`, `update-syncer`, `delete-syncer`, +func (c *Client) modifySyncer(action string, syncer *Syncer, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", syncer.Owner, syncer.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + syncer.Owner = c.OrganizationName + postBytes, err := json.Marshal(syncer) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + +// modifyWebhook is an encapsulation of cert CUD(Create, Update, Delete) operations. +// possible actions are `add-webhook`, `update-webhook`, `delete-webhook`, +func (c *Client) modifyWebhook(action string, webhook *Webhook, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", webhook.Owner, webhook.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + webhook.Owner = c.OrganizationName + postBytes, err := json.Marshal(webhook) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} diff --git a/casdoorsdk/enfocer.go b/casdoorsdk/enfocer.go index a6584e3..db00b8e 100644 --- a/casdoorsdk/enfocer.go +++ b/casdoorsdk/enfocer.go @@ -2,6 +2,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -57,15 +58,9 @@ func (c *Client) GetPaginationEnforcers(p int, pageSize int, queryMap map[string return nil, 0, fmt.Errorf(response.Msg) } - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err - } - - var enforcers []*Enforcer - err = json.Unmarshal(bytes, &enforcers) - if err != nil { - return nil, 0, err + enforcers, ok := response.Data.([]*Enforcer) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } return enforcers, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/model.go b/casdoorsdk/model.go index 29305f3..18419a7 100644 --- a/casdoorsdk/model.go +++ b/casdoorsdk/model.go @@ -2,6 +2,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -63,15 +64,9 @@ func (c *Client) GetPaginationModels(p int, pageSize int, queryMap map[string]st return nil, 0, fmt.Errorf(response.Msg) } - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err - } - - var models []*Model - err = json.Unmarshal(bytes, &models) - if err != nil { - return nil, 0, err + models, ok := response.Data.([]*Model) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } return models, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/payment.go b/casdoorsdk/payment.go new file mode 100644 index 0000000..7e8d636 --- /dev/null +++ b/casdoorsdk/payment.go @@ -0,0 +1,157 @@ +package casdoorsdk + +import ( + "encoding/json" + "errors" + "fmt" + "strconv" +) + +type Payment struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + DisplayName string `xorm:"varchar(100)" json:"displayName"` + // Payment Provider Info + Provider string `xorm:"varchar(100)" json:"provider"` + Type string `xorm:"varchar(100)" json:"type"` + // Product Info + ProductName string `xorm:"varchar(100)" json:"productName"` + ProductDisplayName string `xorm:"varchar(100)" json:"productDisplayName"` + Detail string `xorm:"varchar(255)" json:"detail"` + Tag string `xorm:"varchar(100)" json:"tag"` + Currency string `xorm:"varchar(100)" json:"currency"` + Price float64 `json:"price"` + ReturnUrl string `xorm:"varchar(1000)" json:"returnUrl"` + // Payer Info + User string `xorm:"varchar(100)" json:"user"` + PersonName string `xorm:"varchar(100)" json:"personName"` + PersonIdCard string `xorm:"varchar(100)" json:"personIdCard"` + PersonEmail string `xorm:"varchar(100)" json:"personEmail"` + PersonPhone string `xorm:"varchar(100)" json:"personPhone"` + // Invoice Info + InvoiceType string `xorm:"varchar(100)" json:"invoiceType"` + InvoiceTitle string `xorm:"varchar(100)" json:"invoiceTitle"` + InvoiceTaxId string `xorm:"varchar(100)" json:"invoiceTaxId"` + InvoiceRemark string `xorm:"varchar(100)" json:"invoiceRemark"` + InvoiceUrl string `xorm:"varchar(255)" json:"invoiceUrl"` + // Order Info + OutOrderId string `xorm:"varchar(100)" json:"outOrderId"` + PayUrl string `xorm:"varchar(2000)" json:"payUrl"` + //State pp.PaymentState `xorm:"varchaFr(100)" json:"state"` + State string `xorm:"varchar(100)" json:"state"` + Message string `xorm:"varchar(2000)" json:"message"` +} + +func (c *Client) GetPayments() ([]*Payment, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-payments", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var payments []*Payment + err = json.Unmarshal(bytes, &payments) + if err != nil { + return nil, err + } + return payments, nil +} + +func (c *Client) GetPaginationPayments(p int, pageSize int, queryMap map[string]string) ([]*Payment, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-payments", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + payments, ok := response.Data.([]*Payment) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + return payments, int(response.Data2.(float64)), nil +} + +func (c *Client) GetPayment(name string) (*Payment, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-payment", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var payment *Payment + err = json.Unmarshal(bytes, &payment) + if err != nil { + return nil, err + } + return payment, nil +} + +func (c *Client) GetUserPayments() ([]*Payment, error) { + return nil, errors.New("Not implemented") + queryMap := map[string]string{ + "owner": c.OrganizationName, + "orgnization": c.OrganizationName, + // TODO: get user name + //"user": c. + + } + + url := c.GetUrl("get-user-payments", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var payments []*Payment + err = json.Unmarshal(bytes, &payments) + if err != nil { + return nil, err + } + return payments, nil +} + +func (c *Client) UpdatePayment(payment *Payment) (bool, error) { + _, affected, err := c.modifyPayment("update-payment", payment, nil) + return affected, err +} + +func (c *Client) AddPayment(payment *Payment) (bool, error) { + _, affected, err := c.modifyPayment("add-payment", payment, nil) + return affected, err +} + +func (c *Client) DeletePayment(payment *Payment) (bool, error) { + _, affected, err := c.modifyPayment("delete-payment", payment, nil) + return affected, err +} + +func (c *Client) NotifyPayment(payment *Payment) (bool, error) { + _, affected, err := c.modifyPayment("notify-payment", payment, nil) + return affected, err +} + +func (c *Client) InvoicePayment(payment *Payment) (bool, error) { + _, affected, err := c.modifyPayment("invoice-payment", payment, nil) + return affected, err +} diff --git a/casdoorsdk/payment_global.go b/casdoorsdk/payment_global.go new file mode 100644 index 0000000..e050d27 --- /dev/null +++ b/casdoorsdk/payment_global.go @@ -0,0 +1,35 @@ +package casdoorsdk + +func GetPayments() ([]*Payment, error) { + return globalClient.GetPayments() +} + +func GetPaginationPayments(p int, pageSize int, queryMap map[string]string) ([]*Payment, int, error) { + return globalClient.GetPaginationPayments(p, pageSize, queryMap) +} + +func GetPayment(name string) (*Payment, error) { + return globalClient.GetPayment(name) +} +func GetUserPayments() ([]*Payment, error) { + return globalClient.GetUserPayments() +} + +func UpdatePayment(payment *Payment) (bool, error) { + return globalClient.UpdatePayment(payment) +} + +func AddPayment(payment *Payment) (bool, error) { + return globalClient.AddPayment(payment) +} + +func DeletePayment(payment *Payment) (bool, error) { + return globalClient.DeletePayment(payment) +} +func NotifyPayment(payment *Payment) (bool, error) { + return globalClient.NotifyPayment(payment) +} + +func InvoicePayment(payment *Payment) (bool, error) { + return globalClient.NotifyPayment(payment) +} diff --git a/casdoorsdk/permission.go b/casdoorsdk/permission.go index 84e0e3b..78d7b2d 100644 --- a/casdoorsdk/permission.go +++ b/casdoorsdk/permission.go @@ -16,6 +16,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -101,15 +102,9 @@ func (c *Client) GetPaginationPermissions(p int, pageSize int, queryMap map[stri return nil, 0, fmt.Errorf(response.Msg) } - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err - } - - var permissions []*Permission - err = json.Unmarshal(bytes, &permissions) - if err != nil { - return nil, 0, err + permissions, ok := response.Data.([]*Permission) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } return permissions, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/plan.go b/casdoorsdk/plan.go new file mode 100644 index 0000000..24286c6 --- /dev/null +++ b/casdoorsdk/plan.go @@ -0,0 +1,117 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +import ( + "encoding/json" + "errors" + "fmt" + "strconv" +) + +// Plan has the same definition as https://github.com/casdoor/casdoor/blob/master/object/plan.go#L24 +type Plan struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + DisplayName string `xorm:"varchar(100)" json:"displayName"` + Description string `xorm:"varchar(100)" json:"description"` + + PricePerMonth float64 `json:"pricePerMonth"` + PricePerYear float64 `json:"pricePerYear"` + Currency string `xorm:"varchar(100)" json:"currency"` + IsEnabled bool `json:"isEnabled"` + + Role string `xorm:"varchar(100)" json:"role"` + Options []string `xorm:"-" json:"options"` +} + +func (c *Client) GetPlans() ([]*Plan, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-plans", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var plans []*Plan + err = json.Unmarshal(bytes, &plans) + if err != nil { + return nil, err + } + return plans, nil +} + +func (c *Client) GetPaginationPlans(p int, pageSize int, queryMap map[string]string) ([]*Plan, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-payments", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + plans, ok := response.Data.([]*Plan) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + return plans, int(response.Data2.(float64)), nil +} + +func (c *Client) GetPlan(name string) (*Plan, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-plan", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var plan *Plan + err = json.Unmarshal(bytes, &plan) + if err != nil { + return nil, err + } + return plan, nil +} + +func (c *Client) AddPlan(plan *Plan) (bool, error) { + _, affected, err := c.modifyPlan("add-plan", plan, nil) + return affected, err +} + +func (c *Client) UpdatePlan(plan *Plan) (bool, error) { + _, affected, err := c.modifyPlan("update-plan", plan, nil) + return affected, err +} + +func (c *Client) DeletePlan(plan *Plan) (bool, error) { + _, affected, err := c.modifyPlan("delete-plan", plan, nil) + return affected, err +} diff --git a/casdoorsdk/plan_global.go b/casdoorsdk/plan_global.go new file mode 100644 index 0000000..c060204 --- /dev/null +++ b/casdoorsdk/plan_global.go @@ -0,0 +1,39 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetPlans() ([]*Plan, error) { + return globalClient.GetPlans() +} + +func GetPaginationPlans(p int, pageSize int, queryMap map[string]string) ([]*Plan, int, error) { + return globalClient.GetPaginationPlans(p, pageSize, queryMap) +} + +func GetPlan(name string) (*Plan, error) { + return globalClient.GetPlan(name) +} + +func UpdatePlan(plan *Plan) (bool, error) { + return globalClient.UpdatePlan(plan) +} + +func AddPlan(plan *Plan) (bool, error) { + return globalClient.AddPlan(plan) +} + +func DeletePlan(plan *Plan) (bool, error) { + return globalClient.DeletePlan(plan) +} diff --git a/casdoorsdk/pricing.go b/casdoorsdk/pricing.go new file mode 100644 index 0000000..3f08f3c --- /dev/null +++ b/casdoorsdk/pricing.go @@ -0,0 +1,120 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +import ( + "encoding/json" + "errors" + "fmt" + "strconv" +) + +// Pricing has the same definition as https://github.com/casdoor/casdoor/blob/master/object/pricing.go#L24 +type Pricing struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + DisplayName string `xorm:"varchar(100)" json:"displayName"` + Description string `xorm:"varchar(100)" json:"description"` + + Plans []string `xorm:"mediumtext" json:"plans"` + IsEnabled bool `json:"isEnabled"` + TrialDuration int `json:"trialDuration"` + Application string `xorm:"varchar(100)" json:"application"` + + Submitter string `xorm:"varchar(100)" json:"submitter"` + Approver string `xorm:"varchar(100)" json:"approver"` + ApproveTime string `xorm:"varchar(100)" json:"approveTime"` + + State string `xorm:"varchar(100)" json:"state"` +} + +func (c *Client) GetPricings() ([]*Pricing, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-pricings", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var pricings []*Pricing + err = json.Unmarshal(bytes, &pricings) + if err != nil { + return nil, err + } + return pricings, nil +} + +func (c *Client) GetPaginationPricings(p int, pageSize int, queryMap map[string]string) ([]*Pricing, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-payments", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + pricings, ok := response.Data.([]*Pricing) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + return pricings, int(response.Data2.(float64)), nil +} + +func (c *Client) GetPricing(name string) (*Pricing, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-pricing", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var pricing *Pricing + err = json.Unmarshal(bytes, &pricing) + if err != nil { + return nil, err + } + return pricing, nil +} + +func (c *Client) AddPricing(pricing *Pricing) (bool, error) { + _, affected, err := c.modifyPricing("add-pricing", pricing, nil) + return affected, err +} + +func (c *Client) UpdatePricing(pricing *Pricing) (bool, error) { + _, affected, err := c.modifyPricing("update-pricing", pricing, nil) + return affected, err +} + +func (c *Client) DeletePricing(pricing *Pricing) (bool, error) { + _, affected, err := c.modifyPricing("delete-pricing", pricing, nil) + return affected, err +} diff --git a/casdoorsdk/princing_global.go b/casdoorsdk/princing_global.go new file mode 100644 index 0000000..f74a84c --- /dev/null +++ b/casdoorsdk/princing_global.go @@ -0,0 +1,38 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetPricings() ([]*Pricing, error) { + return globalClient.GetPricings() +} +func GetPaginationPricings(p int, pageSize int, queryMap map[string]string) ([]*Pricing, int, error) { + return globalClient.GetPaginationPricings(p, pageSize, queryMap) +} + +func GetPricing(name string) (*Pricing, error) { + return globalClient.GetPricing(name) +} + +func UpdatePricing(pricing *Pricing) (bool, error) { + return globalClient.UpdatePricing(pricing) +} + +func AddPricing(pricing *Pricing) (bool, error) { + return globalClient.AddPricing(pricing) +} + +func DeletePricing(pricing *Pricing) (bool, error) { + return globalClient.DeletePricing(pricing) +} diff --git a/casdoorsdk/product.go b/casdoorsdk/product.go new file mode 100644 index 0000000..47d0ed8 --- /dev/null +++ b/casdoorsdk/product.go @@ -0,0 +1,129 @@ +package casdoorsdk + +import ( + "encoding/json" + "errors" + "fmt" + "strconv" +) + +type Product struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + DisplayName string `xorm:"varchar(100)" json:"displayName"` + + Image string `xorm:"varchar(100)" json:"image"` + Detail string `xorm:"varchar(255)" json:"detail"` + Description string `xorm:"varchar(100)" json:"description"` + Tag string `xorm:"varchar(100)" json:"tag"` + Currency string `xorm:"varchar(100)" json:"currency"` + Price float64 `json:"price"` + Quantity int `json:"quantity"` + Sold int `json:"sold"` + Providers []string `xorm:"varchar(100)" json:"providers"` + ReturnUrl string `xorm:"varchar(1000)" json:"returnUrl"` + + State string `xorm:"varchar(100)" json:"state"` + + ProviderObjs []*Provider `xorm:"-" json:"providerObjs"` +} + +func (c *Client) GetProducts() ([]*Product, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-products", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var products []*Product + err = json.Unmarshal(bytes, &products) + if err != nil { + return nil, err + } + return products, nil +} + +func (c *Client) GetPaginationProducts(p int, pageSize int, queryMap map[string]string) ([]*Product, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-products", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + products, ok := response.Data.([]*Product) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + return products, int(response.Data2.(float64)), nil +} + +func (c *Client) GetProduct(name string) (*Product, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-product", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var product *Product + err = json.Unmarshal(bytes, &product) + if err != nil { + return nil, err + } + return product, nil +} + +func (c *Client) UpdateProduct(product *Product) (bool, error) { + _, affected, err := c.modifyProduct("update-product", product, nil) + return affected, err +} + +func (c *Client) AddProduct(product *Product) (bool, error) { + _, affected, err := c.modifyProduct("add-product", product, nil) + return affected, err +} + +func (c *Client) DeleteProduct(product *Product) (bool, error) { + _, affected, err := c.modifyProduct("delete-product", product, nil) + return affected, err +} + +func (c *Client) BuyProduct(name string, providerName string) (*Product, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + "providerName": providerName, + } + + url := c.GetUrl("buy-product", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var product *Product + err = json.Unmarshal(bytes, &product) + if err != nil { + return nil, err + } + return product, nil +} diff --git a/casdoorsdk/product_global.go b/casdoorsdk/product_global.go new file mode 100644 index 0000000..05370cb --- /dev/null +++ b/casdoorsdk/product_global.go @@ -0,0 +1,29 @@ +package casdoorsdk + +func GetProducts() ([]*Product, error) { + return globalClient.GetProducts() +} + +func GetPaginationProducts(p int, pageSize int, queryMap map[string]string) ([]*Product, int, error) { + return globalClient.GetPaginationProducts(p, pageSize, queryMap) +} + +func GetProduct(name string) (*Product, error) { + return globalClient.GetProduct(name) +} + +func UpdateProduct(product *Product) (bool, error) { + return globalClient.UpdateProduct(product) +} + +func AddProduct(product *Product) (bool, error) { + return globalClient.AddProduct(product) +} + +func DeleteProduct(product *Product) (bool, error) { + return globalClient.DeleteProduct(product) +} + +func BuyProduct(name string, providerName string) (*Product, error) { + return globalClient.BuyProduct(name, providerName) +} diff --git a/casdoorsdk/provider.go b/casdoorsdk/provider.go index cc1afdc..1f6396d 100644 --- a/casdoorsdk/provider.go +++ b/casdoorsdk/provider.go @@ -16,6 +16,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -88,10 +89,6 @@ func (c *Client) GetProviders() ([]*Provider, error) { return providers, nil } -func GetProviders() ([]*Provider, error) { - return globalClient.GetProviders() -} - func (c *Client) GetProvider(name string) (*Provider, error) { queryMap := map[string]string{ "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), @@ -123,51 +120,24 @@ func (c *Client) GetPaginationProviders(p int, pageSize int, queryMap map[string if err != nil { return nil, 0, err } - - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err - } - - var providers []*Provider - err = json.Unmarshal(bytes, &providers) - if err != nil { - return nil, 0, err + providers, ok := response.Data.([]*Provider) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } return providers, int(response.Data2.(float64)), nil } -func GetPaginationProviders(p int, pageSize int, queryMap map[string]string) ([]*Provider, int, error) { - return globalClient.GetPaginationProviders(p, pageSize, queryMap) -} - -func GetProvider(name string) (*Provider, error) { - return globalClient.GetProvider(name) -} - func (c *Client) UpdateProvider(provider *Provider) (bool, error) { _, affected, err := c.modifyProvider("update-provider", provider, nil) return affected, err } -func UpdateProvider(provider *Provider) (bool, error) { - return globalClient.UpdateProvider(provider) -} - func (c *Client) AddProvider(provider *Provider) (bool, error) { _, affected, err := c.modifyProvider("add-provider", provider, nil) return affected, err } -func AddProvider(provider *Provider) (bool, error) { - return globalClient.AddProvider(provider) -} - func (c *Client) DeleteProvider(provider *Provider) (bool, error) { _, affected, err := c.modifyProvider("delete-provider", provider, nil) return affected, err } - -func DeleteProvider(provider *Provider) (bool, error) { - return globalClient.DeleteProvider(provider) -} diff --git a/casdoorsdk/provider_global.go b/casdoorsdk/provider_global.go new file mode 100644 index 0000000..2c61d93 --- /dev/null +++ b/casdoorsdk/provider_global.go @@ -0,0 +1,25 @@ +package casdoorsdk + +func GetProviders() ([]*Provider, error) { + return globalClient.GetProviders() +} + +func GetPaginationProviders(p int, pageSize int, queryMap map[string]string) ([]*Provider, int, error) { + return globalClient.GetPaginationProviders(p, pageSize, queryMap) +} + +func GetProvider(name string) (*Provider, error) { + return globalClient.GetProvider(name) +} + +func UpdateProvider(provider *Provider) (bool, error) { + return globalClient.UpdateProvider(provider) +} + +func AddProvider(provider *Provider) (bool, error) { + return globalClient.AddProvider(provider) +} + +func DeleteProvider(provider *Provider) (bool, error) { + return globalClient.DeleteProvider(provider) +} diff --git a/casdoorsdk/subscription.go b/casdoorsdk/subscription.go new file mode 100644 index 0000000..9967674 --- /dev/null +++ b/casdoorsdk/subscription.go @@ -0,0 +1,119 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +import ( + "encoding/json" + "errors" + "fmt" + "strconv" + "time" +) + +// Subscription has the same definition as https://github.com/casdoor/casdoor/blob/master/object/subscription.go#L24 +type Subscription struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + DisplayName string `xorm:"varchar(100)" json:"displayName"` + + StartDate time.Time `json:"startDate"` + EndDate time.Time `json:"endDate"` + Duration int `json:"duration"` + Description string `xorm:"varchar(100)" json:"description"` + + User string `xorm:"mediumtext" json:"user"` + Plan string `xorm:"varchar(100)" json:"plan"` + + IsEnabled bool `json:"isEnabled"` + Submitter string `xorm:"varchar(100)" json:"submitter"` + Approver string `xorm:"varchar(100)" json:"approver"` + ApproveTime string `xorm:"varchar(100)" json:"approveTime"` + + State string `xorm:"varchar(100)" json:"state"` +} + +func (c *Client) GetSubscriptions() ([]*Subscription, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-subscriptions", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var subscriptions []*Subscription + err = json.Unmarshal(bytes, &subscriptions) + if err != nil { + return nil, err + } + return subscriptions, nil +} + +func (c *Client) GetPaginationSubscriptions(p int, pageSize int, queryMap map[string]string) ([]*Subscription, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-providers", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + subscriptions, ok := response.Data.([]*Subscription) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + return subscriptions, int(response.Data2.(float64)), nil +} + +func (c *Client) GetSubscription(name string) (*Subscription, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-subscription", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var subscription *Subscription + err = json.Unmarshal(bytes, &subscription) + if err != nil { + return nil, err + } + return subscription, nil +} + +func (c *Client) AddSubscription(subscription *Subscription) (bool, error) { + _, affected, err := c.modifySubscription("add-subscription", subscription, nil) + return affected, err +} + +func (c *Client) UpdateSubscription(subscription *Subscription) (bool, error) { + _, affected, err := c.modifySubscription("update-subscription", subscription, nil) + return affected, err +} + +func (c *Client) DeleteSubscription(subscription *Subscription) (bool, error) { + _, affected, err := c.modifySubscription("delete-subscription", subscription, nil) + return affected, err +} diff --git a/casdoorsdk/subscription_global.go b/casdoorsdk/subscription_global.go new file mode 100644 index 0000000..a4f85d3 --- /dev/null +++ b/casdoorsdk/subscription_global.go @@ -0,0 +1,23 @@ +package casdoorsdk + +func GetSubscriptions() ([]*Subscription, error) { + return globalClient.GetSubscriptions() +} +func GetPaginationSubscriptions(p int, pageSize int, queryMap map[string]string) ([]*Subscription, int, error) { + return globalClient.GetPaginationSubscriptions(p, pageSize, queryMap) +} +func GetSubscription(name string) (*Subscription, error) { + return globalClient.GetSubscription(name) +} + +func UpdateSubscription(subscription *Subscription) (bool, error) { + return globalClient.UpdateSubscription(subscription) +} + +func AddSubscription(subscription *Subscription) (bool, error) { + return globalClient.AddSubscription(subscription) +} + +func DeleteSubscription(subscription *Subscription) (bool, error) { + return globalClient.DeleteSubscription(subscription) +} diff --git a/casdoorsdk/syncer.go b/casdoorsdk/syncer.go new file mode 100644 index 0000000..3028b6c --- /dev/null +++ b/casdoorsdk/syncer.go @@ -0,0 +1,137 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +import ( + "encoding/json" + "errors" + "fmt" + "strconv" +) + +type TableColumn struct { + Name string `json:"name"` + Type string `json:"type"` + CasdoorName string `json:"casdoorName"` + IsKey bool `json:"isKey"` + IsHashed bool `json:"isHashed"` + Values []string `json:"values"` +} + +// Syncer has the same definition as https://github.com/casdoor/casdoor/blob/master/object/syncer.go#L24 +type Syncer struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + + Organization string `xorm:"varchar(100)" json:"organization"` + Type string `xorm:"varchar(100)" json:"type"` + + Host string `xorm:"varchar(100)" json:"host"` + Port int `json:"port"` + User string `xorm:"varchar(100)" json:"user"` + Password string `xorm:"varchar(100)" json:"password"` + DatabaseType string `xorm:"varchar(100)" json:"databaseType"` + Database string `xorm:"varchar(100)" json:"database"` + Table string `xorm:"varchar(100)" json:"table"` + TablePrimaryKey string `xorm:"varchar(100)" json:"tablePrimaryKey"` + TableColumns []*TableColumn `xorm:"mediumtext" json:"tableColumns"` + AffiliationTable string `xorm:"varchar(100)" json:"affiliationTable"` + AvatarBaseUrl string `xorm:"varchar(100)" json:"avatarBaseUrl"` + ErrorText string `xorm:"mediumtext" json:"errorText"` + SyncInterval int `json:"syncInterval"` + IsReadOnly bool `json:"isReadOnly"` + IsEnabled bool `json:"isEnabled"` + + //Ormer *Ormer `xorm:"-" json:"-"` +} + +func (c *Client) GetSyncers() ([]*Syncer, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-syncers", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var syncers []*Syncer + err = json.Unmarshal(bytes, &syncers) + if err != nil { + return nil, err + } + return syncers, nil +} + +func (c *Client) GetPaginationSyncers(p int, pageSize int, queryMap map[string]string) ([]*Syncer, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-models", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + syncers, ok := response.Data.([]*Syncer) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + return syncers, int(response.Data2.(float64)), nil +} + +func (c *Client) GetSyncer(name string) (*Syncer, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-syncer", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var syncer *Syncer + err = json.Unmarshal(bytes, &syncer) + if err != nil { + return nil, err + } + return syncer, nil +} + +func (c *Client) AddSyncer(syncer *Syncer) (bool, error) { + _, affected, err := c.modifySyncer("add-syncer", syncer, nil) + return affected, err +} + +func (c *Client) UpdateSyncer(syncer *Syncer) (bool, error) { + _, affected, err := c.modifySyncer("update-syncer", syncer, nil) + return affected, err +} + +func (c *Client) DeleteSyncer(syncer *Syncer) (bool, error) { + _, affected, err := c.modifySyncer("delete-syncer", syncer, nil) + return affected, err +} diff --git a/casdoorsdk/syncer_global.go b/casdoorsdk/syncer_global.go new file mode 100644 index 0000000..3df230f --- /dev/null +++ b/casdoorsdk/syncer_global.go @@ -0,0 +1,39 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetSyncers() ([]*Syncer, error) { + return globalClient.GetSyncers() +} + +func GetPaginationSyncers(p int, pageSize int, queryMap map[string]string) ([]*Syncer, int, error) { + return globalClient.GetPaginationSyncers(p, pageSize, queryMap) +} + +func GetSyncer(name string) (*Syncer, error) { + return globalClient.GetSyncer(name) +} + +func UpdateSyncer(syncer *Syncer) (bool, error) { + return globalClient.UpdateSyncer(syncer) +} + +func AddSyncer(syncer *Syncer) (bool, error) { + return globalClient.AddSyncer(syncer) +} + +func DeleteSyncer(syncer *Syncer) (bool, error) { + return globalClient.DeleteSyncer(syncer) +} diff --git a/casdoorsdk/webhook.go b/casdoorsdk/webhook.go new file mode 100644 index 0000000..c599e1e --- /dev/null +++ b/casdoorsdk/webhook.go @@ -0,0 +1,128 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +import ( + "encoding/json" + "errors" + "fmt" + "strconv" +) + +// Webhook has the same definition as https://github.com/casdoor/casdoor/blob/master/object/webhook.go#L24 +type Webhook struct { + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` + Name string `xorm:"varchar(100) notnull pk" json:"name"` + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` + + Organization string `xorm:"varchar(100)" json:"organization"` + Type string `xorm:"varchar(100)" json:"type"` + + Host string `xorm:"varchar(100)" json:"host"` + Port int `json:"port"` + User string `xorm:"varchar(100)" json:"user"` + Password string `xorm:"varchar(100)" json:"password"` + DatabaseType string `xorm:"varchar(100)" json:"databaseType"` + Database string `xorm:"varchar(100)" json:"database"` + Table string `xorm:"varchar(100)" json:"table"` + TablePrimaryKey string `xorm:"varchar(100)" json:"tablePrimaryKey"` + TableColumns []*TableColumn `xorm:"mediumtext" json:"tableColumns"` + AffiliationTable string `xorm:"varchar(100)" json:"affiliationTable"` + AvatarBaseUrl string `xorm:"varchar(100)" json:"avatarBaseUrl"` + ErrorText string `xorm:"mediumtext" json:"errorText"` + SyncInterval int `json:"syncInterval"` + IsReadOnly bool `json:"isReadOnly"` + IsEnabled bool `json:"isEnabled"` + + //Ormer *Ormer `xorm:"-" json:"-"` +} + +func (c *Client) GetWebhooks() ([]*Webhook, error) { + queryMap := map[string]string{ + "owner": c.OrganizationName, + } + + url := c.GetUrl("get-webhooks", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var webhooks []*Webhook + err = json.Unmarshal(bytes, &webhooks) + if err != nil { + return nil, err + } + return webhooks, nil +} + +func (c *Client) GetPaginationWebhooks(p int, pageSize int, queryMap map[string]string) ([]*Webhook, int, error) { + queryMap["owner"] = c.OrganizationName + queryMap["p"] = strconv.Itoa(p) + queryMap["pageSize"] = strconv.Itoa(pageSize) + + url := c.GetUrl("get-models", queryMap) + + response, err := c.DoGetResponse(url) + if err != nil { + return nil, 0, err + } + + if response.Status != "ok" { + return nil, 0, fmt.Errorf(response.Msg) + } + + webhooks, ok := response.Data.([]*Webhook) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + return webhooks, int(response.Data2.(float64)), nil +} + +func (c *Client) GetWebhook(name string) (*Webhook, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + } + + url := c.GetUrl("get-webhook", queryMap) + + bytes, err := c.DoGetBytes(url) + if err != nil { + return nil, err + } + + var webhook *Webhook + err = json.Unmarshal(bytes, &webhook) + if err != nil { + return nil, err + } + return webhook, nil +} + +func (c *Client) AddWebhook(webhook *Webhook) (bool, error) { + _, affected, err := c.modifyWebhook("add-webhook", webhook, nil) + return affected, err +} + +func (c *Client) UpdateWebhook(webhook *Webhook) (bool, error) { + _, affected, err := c.modifyWebhook("update-webhook", webhook, nil) + return affected, err +} + +func (c *Client) DeleteWebhook(webhook *Webhook) (bool, error) { + _, affected, err := c.modifyWebhook("delete-webhook", webhook, nil) + return affected, err +} diff --git a/casdoorsdk/webhook_global.go b/casdoorsdk/webhook_global.go new file mode 100644 index 0000000..c7dbf4f --- /dev/null +++ b/casdoorsdk/webhook_global.go @@ -0,0 +1,39 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetWebhooks() ([]*Webhook, error) { + return globalClient.GetWebhooks() +} + +func GetPaginationWebhooks(p int, pageSize int, queryMap map[string]string) ([]*Webhook, int, error) { + return globalClient.GetPaginationWebhooks(p, pageSize, queryMap) +} + +func GetWebhook(name string) (*Webhook, error) { + return globalClient.GetWebhook(name) +} + +func UpdateWebhook(webhook *Webhook) (bool, error) { + return globalClient.UpdateWebhook(webhook) +} + +func AddWebhook(webhook *Webhook) (bool, error) { + return globalClient.AddWebhook(webhook) +} + +func DeleteWebhook(webhook *Webhook) (bool, error) { + return globalClient.DeleteWebhook(webhook) +} From 1261fae8e2d7c93a18b7b07f3ee669a5d26c06f6 Mon Sep 17 00:00:00 2001 From: liuqs Date: Sat, 19 Aug 2023 15:54:44 +0800 Subject: [PATCH 4/9] fix:remove json. --- casdoorsdk/group.go | 13 ++++--------- casdoorsdk/record.go | 12 ++++-------- casdoorsdk/role.go | 13 ++++--------- casdoorsdk/session.go | 13 ++++--------- casdoorsdk/user.go | 13 ++++--------- 5 files changed, 20 insertions(+), 44 deletions(-) diff --git a/casdoorsdk/group.go b/casdoorsdk/group.go index 728590b..8be1bbf 100644 --- a/casdoorsdk/group.go +++ b/casdoorsdk/group.go @@ -2,6 +2,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -63,15 +64,9 @@ func (c *Client) GetPaginationGroups(p int, pageSize int, queryMap map[string]st return nil, 0, fmt.Errorf(response.Msg) } - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err - } - - var groups []*Group - err = json.Unmarshal(bytes, &groups) - if err != nil { - return nil, 0, err + groups, ok := response.Data.([]*Group) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } return groups, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/record.go b/casdoorsdk/record.go index d8935e1..03e2c62 100644 --- a/casdoorsdk/record.go +++ b/casdoorsdk/record.go @@ -16,6 +16,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -101,16 +102,11 @@ func (c *Client) GetPaginationRecords(p int, pageSize int, queryMap map[string]s return nil, 0, err } - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err + records, ok := response.Data.([]*Record) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } - var records []*Record - err = json.Unmarshal(bytes, &records) - if err != nil { - return nil, 0, err - } return records, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/role.go b/casdoorsdk/role.go index b3de636..5a80d40 100644 --- a/casdoorsdk/role.go +++ b/casdoorsdk/role.go @@ -16,6 +16,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -70,15 +71,9 @@ func (c *Client) GetPaginationRoles(p int, pageSize int, queryMap map[string]str return nil, 0, err } - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err - } - - var roles []*Role - err = json.Unmarshal(bytes, &roles) - if err != nil { - return nil, 0, err + roles, ok := response.Data.([]*Role) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } return roles, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/session.go b/casdoorsdk/session.go index a6f082a..3a534ef 100644 --- a/casdoorsdk/session.go +++ b/casdoorsdk/session.go @@ -16,6 +16,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -70,15 +71,9 @@ func (c *Client) GetPaginationSessions(p int, pageSize int, queryMap map[string] return nil, 0, err } - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err - } - - var sessions []*Session - err = json.Unmarshal(bytes, &sessions) - if err != nil { - return nil, 0, err + sessions, ok := response.Data.([]*Session) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } return sessions, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/user.go b/casdoorsdk/user.go index abab01d..a5e1bfb 100644 --- a/casdoorsdk/user.go +++ b/casdoorsdk/user.go @@ -16,6 +16,7 @@ package casdoorsdk import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -291,15 +292,9 @@ func (c *Client) GetPaginationUsers(p int, pageSize int, queryMap map[string]str return nil, 0, fmt.Errorf(response.Msg) } - bytes, err := json.Marshal(response.Data) - if err != nil { - return nil, 0, err - } - - var users []*User - err = json.Unmarshal(bytes, &users) - if err != nil { - return nil, 0, err + users, ok := response.Data.([]*User) + if !ok { + return nil, 0, errors.New("response data format is incorrect") } return users, int(response.Data2.(float64)), nil } From eb9f99600af2d9a3ccb092237b195afd19a066e8 Mon Sep 17 00:00:00 2001 From: liuqs Date: Sat, 19 Aug 2023 15:56:05 +0800 Subject: [PATCH 5/9] fix:code format. --- casdoorsdk/cert_global.go | 1 - casdoorsdk/payment.go | 2 +- casdoorsdk/payment_global.go | 2 ++ casdoorsdk/princing_global.go | 1 + casdoorsdk/subscription_global.go | 2 ++ casdoorsdk/syncer.go | 2 +- casdoorsdk/webhook.go | 2 +- 7 files changed, 8 insertions(+), 4 deletions(-) diff --git a/casdoorsdk/cert_global.go b/casdoorsdk/cert_global.go index b330e61..bd2356b 100644 --- a/casdoorsdk/cert_global.go +++ b/casdoorsdk/cert_global.go @@ -16,7 +16,6 @@ package casdoorsdk func GetGlobalCerts() ([]*Cert, error) { return globalClient.GetGlobalCerts() - } func GetCerts() ([]*Cert, error) { diff --git a/casdoorsdk/payment.go b/casdoorsdk/payment.go index 7e8d636..bb29c3d 100644 --- a/casdoorsdk/payment.go +++ b/casdoorsdk/payment.go @@ -38,7 +38,7 @@ type Payment struct { // Order Info OutOrderId string `xorm:"varchar(100)" json:"outOrderId"` PayUrl string `xorm:"varchar(2000)" json:"payUrl"` - //State pp.PaymentState `xorm:"varchaFr(100)" json:"state"` + // State pp.PaymentState `xorm:"varchaFr(100)" json:"state"` State string `xorm:"varchar(100)" json:"state"` Message string `xorm:"varchar(2000)" json:"message"` } diff --git a/casdoorsdk/payment_global.go b/casdoorsdk/payment_global.go index e050d27..6adec38 100644 --- a/casdoorsdk/payment_global.go +++ b/casdoorsdk/payment_global.go @@ -11,6 +11,7 @@ func GetPaginationPayments(p int, pageSize int, queryMap map[string]string) ([]* func GetPayment(name string) (*Payment, error) { return globalClient.GetPayment(name) } + func GetUserPayments() ([]*Payment, error) { return globalClient.GetUserPayments() } @@ -26,6 +27,7 @@ func AddPayment(payment *Payment) (bool, error) { func DeletePayment(payment *Payment) (bool, error) { return globalClient.DeletePayment(payment) } + func NotifyPayment(payment *Payment) (bool, error) { return globalClient.NotifyPayment(payment) } diff --git a/casdoorsdk/princing_global.go b/casdoorsdk/princing_global.go index f74a84c..8074077 100644 --- a/casdoorsdk/princing_global.go +++ b/casdoorsdk/princing_global.go @@ -17,6 +17,7 @@ package casdoorsdk func GetPricings() ([]*Pricing, error) { return globalClient.GetPricings() } + func GetPaginationPricings(p int, pageSize int, queryMap map[string]string) ([]*Pricing, int, error) { return globalClient.GetPaginationPricings(p, pageSize, queryMap) } diff --git a/casdoorsdk/subscription_global.go b/casdoorsdk/subscription_global.go index a4f85d3..ffb9f5d 100644 --- a/casdoorsdk/subscription_global.go +++ b/casdoorsdk/subscription_global.go @@ -3,9 +3,11 @@ package casdoorsdk func GetSubscriptions() ([]*Subscription, error) { return globalClient.GetSubscriptions() } + func GetPaginationSubscriptions(p int, pageSize int, queryMap map[string]string) ([]*Subscription, int, error) { return globalClient.GetPaginationSubscriptions(p, pageSize, queryMap) } + func GetSubscription(name string) (*Subscription, error) { return globalClient.GetSubscription(name) } diff --git a/casdoorsdk/syncer.go b/casdoorsdk/syncer.go index 3028b6c..b7be356 100644 --- a/casdoorsdk/syncer.go +++ b/casdoorsdk/syncer.go @@ -55,7 +55,7 @@ type Syncer struct { IsReadOnly bool `json:"isReadOnly"` IsEnabled bool `json:"isEnabled"` - //Ormer *Ormer `xorm:"-" json:"-"` + // Ormer *Ormer `xorm:"-" json:"-"` } func (c *Client) GetSyncers() ([]*Syncer, error) { diff --git a/casdoorsdk/webhook.go b/casdoorsdk/webhook.go index c599e1e..52416e4 100644 --- a/casdoorsdk/webhook.go +++ b/casdoorsdk/webhook.go @@ -46,7 +46,7 @@ type Webhook struct { IsReadOnly bool `json:"isReadOnly"` IsEnabled bool `json:"isEnabled"` - //Ormer *Ormer `xorm:"-" json:"-"` + // Ormer *Ormer `xorm:"-" json:"-"` } func (c *Client) GetWebhooks() ([]*Webhook, error) { From 34b255c9d2dccb5c62dabd4f09fe237baa9d9ee9 Mon Sep 17 00:00:00 2001 From: liuqs Date: Sat, 19 Aug 2023 16:49:55 +0800 Subject: [PATCH 6/9] fix:update license. --- casdoorsdk/adapter.go | 14 ++++++++++++++ casdoorsdk/adapter_global.go | 14 ++++++++++++++ casdoorsdk/application.go | 2 +- casdoorsdk/base.go | 2 +- casdoorsdk/group.go | 14 ++++++++++++++ casdoorsdk/group_global.go | 14 ++++++++++++++ casdoorsdk/model.go | 14 ++++++++++++++ casdoorsdk/model_global.go | 14 ++++++++++++++ casdoorsdk/payment.go | 14 ++++++++++++++ casdoorsdk/payment_global.go | 14 ++++++++++++++ casdoorsdk/permission.go | 2 +- casdoorsdk/permission_global.go | 14 ++++++++++++++ casdoorsdk/product.go | 14 ++++++++++++++ casdoorsdk/product_global.go | 14 ++++++++++++++ casdoorsdk/provider.go | 2 +- casdoorsdk/provider_global.go | 14 ++++++++++++++ casdoorsdk/subscription_global.go | 11 +++++++++++ 17 files changed, 183 insertions(+), 4 deletions(-) diff --git a/casdoorsdk/adapter.go b/casdoorsdk/adapter.go index 6ebb407..b2f55f4 100644 --- a/casdoorsdk/adapter.go +++ b/casdoorsdk/adapter.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk import ( diff --git a/casdoorsdk/adapter_global.go b/casdoorsdk/adapter_global.go index dff3222..fd46704 100644 --- a/casdoorsdk/adapter_global.go +++ b/casdoorsdk/adapter_global.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk func GetAdapters() ([]*Adapter, error) { diff --git a/casdoorsdk/application.go b/casdoorsdk/application.go index 55c4256..e3ed15d 100644 --- a/casdoorsdk/application.go +++ b/casdoorsdk/application.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/base.go b/casdoorsdk/base.go index 5cba209..c283810 100644 --- a/casdoorsdk/base.go +++ b/casdoorsdk/base.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/group.go b/casdoorsdk/group.go index 8be1bbf..02093ad 100644 --- a/casdoorsdk/group.go +++ b/casdoorsdk/group.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk import ( diff --git a/casdoorsdk/group_global.go b/casdoorsdk/group_global.go index aa110ff..a00f224 100644 --- a/casdoorsdk/group_global.go +++ b/casdoorsdk/group_global.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk func GetGroups() ([]*Group, error) { diff --git a/casdoorsdk/model.go b/casdoorsdk/model.go index 18419a7..99491a2 100644 --- a/casdoorsdk/model.go +++ b/casdoorsdk/model.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk import ( diff --git a/casdoorsdk/model_global.go b/casdoorsdk/model_global.go index 913082c..251861a 100644 --- a/casdoorsdk/model_global.go +++ b/casdoorsdk/model_global.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk func GetModels() ([]*Model, error) { diff --git a/casdoorsdk/payment.go b/casdoorsdk/payment.go index bb29c3d..cabd92d 100644 --- a/casdoorsdk/payment.go +++ b/casdoorsdk/payment.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk import ( diff --git a/casdoorsdk/payment_global.go b/casdoorsdk/payment_global.go index 6adec38..a4d1ef8 100644 --- a/casdoorsdk/payment_global.go +++ b/casdoorsdk/payment_global.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk func GetPayments() ([]*Payment, error) { diff --git a/casdoorsdk/permission.go b/casdoorsdk/permission.go index 4fe15ae..e35b9b7 100644 --- a/casdoorsdk/permission.go +++ b/casdoorsdk/permission.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/permission_global.go b/casdoorsdk/permission_global.go index a604faf..cc89ca9 100644 --- a/casdoorsdk/permission_global.go +++ b/casdoorsdk/permission_global.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk func GetPermissions() ([]*Permission, error) { diff --git a/casdoorsdk/product.go b/casdoorsdk/product.go index 47d0ed8..effc807 100644 --- a/casdoorsdk/product.go +++ b/casdoorsdk/product.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk import ( diff --git a/casdoorsdk/product_global.go b/casdoorsdk/product_global.go index 05370cb..3147de4 100644 --- a/casdoorsdk/product_global.go +++ b/casdoorsdk/product_global.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk func GetProducts() ([]*Product, error) { diff --git a/casdoorsdk/provider.go b/casdoorsdk/provider.go index 1f6396d..2585f53 100644 --- a/casdoorsdk/provider.go +++ b/casdoorsdk/provider.go @@ -1,4 +1,4 @@ -// Copyright 2022 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/provider_global.go b/casdoorsdk/provider_global.go index 2c61d93..7daacf7 100644 --- a/casdoorsdk/provider_global.go +++ b/casdoorsdk/provider_global.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk func GetProviders() ([]*Provider, error) { diff --git a/casdoorsdk/subscription_global.go b/casdoorsdk/subscription_global.go index ffb9f5d..7885932 100644 --- a/casdoorsdk/subscription_global.go +++ b/casdoorsdk/subscription_global.go @@ -1,3 +1,14 @@ +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk func GetSubscriptions() ([]*Subscription, error) { From 6b27d779ffc5eecdc5ec64cd449f71326a26df25 Mon Sep 17 00:00:00 2001 From: liuqs Date: Sat, 19 Aug 2023 17:03:48 +0800 Subject: [PATCH 7/9] feat: Add global object. --- casdoorsdk/email.go | 6 +-- casdoorsdk/email_global.go | 19 ++++++++ casdoorsdk/enfocer.go | 14 ++++++ casdoorsdk/enforcer_global.go | 14 ++++++ casdoorsdk/jwt.go | 6 +-- casdoorsdk/jwt_global.go | 19 ++++++++ casdoorsdk/organization.go | 26 +--------- casdoorsdk/organization_global.go | 39 +++++++++++++++ casdoorsdk/resource.go | 30 +----------- casdoorsdk/resource_global.go | 43 +++++++++++++++++ casdoorsdk/role.go | 30 +----------- casdoorsdk/role_global.go | 43 +++++++++++++++++ casdoorsdk/session.go | 28 ----------- casdoorsdk/session_global.go | 43 +++++++++++++++++ casdoorsdk/sms.go | 34 +------------ casdoorsdk/sms_global.go | 47 ++++++++++++++++++ casdoorsdk/token.go | 18 +------ casdoorsdk/token_global.go | 35 ++++++++++++++ casdoorsdk/url.go | 18 +------ casdoorsdk/url_global.go | 31 ++++++++++++ casdoorsdk/user.go | 65 ------------------------- casdoorsdk/user_global.go | 80 +++++++++++++++++++++++++++++++ 22 files changed, 435 insertions(+), 253 deletions(-) create mode 100644 casdoorsdk/email_global.go create mode 100644 casdoorsdk/jwt_global.go create mode 100644 casdoorsdk/organization_global.go create mode 100644 casdoorsdk/resource_global.go create mode 100644 casdoorsdk/role_global.go create mode 100644 casdoorsdk/session_global.go create mode 100644 casdoorsdk/sms_global.go create mode 100644 casdoorsdk/token_global.go create mode 100644 casdoorsdk/url_global.go create mode 100644 casdoorsdk/user_global.go diff --git a/casdoorsdk/email.go b/casdoorsdk/email.go index 0df5af3..f8f8841 100644 --- a/casdoorsdk/email.go +++ b/casdoorsdk/email.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -49,7 +49,3 @@ func (c *Client) SendEmail(title string, content string, sender string, receiver return nil } - -func SendEmail(title string, content string, sender string, receivers ...string) error { - return globalClient.SendEmail(title, content, sender, receivers...) -} diff --git a/casdoorsdk/email_global.go b/casdoorsdk/email_global.go new file mode 100644 index 0000000..c5f0cad --- /dev/null +++ b/casdoorsdk/email_global.go @@ -0,0 +1,19 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func SendEmail(title string, content string, sender string, receivers ...string) error { + return globalClient.SendEmail(title, content, sender, receivers...) +} diff --git a/casdoorsdk/enfocer.go b/casdoorsdk/enfocer.go index db00b8e..8a3c161 100644 --- a/casdoorsdk/enfocer.go +++ b/casdoorsdk/enfocer.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk import ( diff --git a/casdoorsdk/enforcer_global.go b/casdoorsdk/enforcer_global.go index 87019f0..7d9b900 100644 --- a/casdoorsdk/enforcer_global.go +++ b/casdoorsdk/enforcer_global.go @@ -1,3 +1,17 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package casdoorsdk func GetEnforcers() ([]*Enforcer, error) { diff --git a/casdoorsdk/jwt.go b/casdoorsdk/jwt.go index 11496d7..ab321ca 100644 --- a/casdoorsdk/jwt.go +++ b/casdoorsdk/jwt.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -48,7 +48,3 @@ func (c *Client) ParseJwtToken(token string) (*Claims, error) { return nil, err } - -func ParseJwtToken(token string) (*Claims, error) { - return globalClient.ParseJwtToken(token) -} diff --git a/casdoorsdk/jwt_global.go b/casdoorsdk/jwt_global.go new file mode 100644 index 0000000..5cc47a1 --- /dev/null +++ b/casdoorsdk/jwt_global.go @@ -0,0 +1,19 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func ParseJwtToken(token string) (*Claims, error) { + return globalClient.ParseJwtToken(token) +} diff --git a/casdoorsdk/organization.go b/casdoorsdk/organization.go index cc1be15..e16ca73 100644 --- a/casdoorsdk/organization.go +++ b/casdoorsdk/organization.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -86,10 +86,6 @@ func (c *Client) GetOrganization(name string) (*Organization, error) { return organization, nil } -func GetOrganization(name string) ([]*Organization, error) { - return globalClient.GetOrganizations() -} - func (c *Client) GetOrganizations() ([]*Organization, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -110,10 +106,6 @@ func (c *Client) GetOrganizations() ([]*Organization, error) { return organizations, nil } -func GetOrganizations() ([]*Organization, error) { - return globalClient.GetOrganizations() -} - func (c *Client) GetOrganizationNames() ([]*Organization, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -134,10 +126,6 @@ func (c *Client) GetOrganizationNames() ([]*Organization, error) { return organizationNames, nil } -func GetOrganizationNames() ([]*Organization, error) { - return globalClient.GetOrganizationNames() -} - func (c *Client) AddOrganization(organization *Organization) (bool, error) { if organization.Owner == "" { organization.Owner = "admin" @@ -146,10 +134,6 @@ func (c *Client) AddOrganization(organization *Organization) (bool, error) { return affected, err } -func AddOrganization(organization *Organization) (bool, error) { - return globalClient.AddOrganization(organization) -} - func (c *Client) DeleteOrganization(name string) (bool, error) { organization := Organization{ Owner: "admin", @@ -160,15 +144,7 @@ func (c *Client) DeleteOrganization(name string) (bool, error) { return affected, err } -func DeleteOrganization(name string) (bool, error) { - return globalClient.DeleteOrganization(name) -} - func (c *Client) UpdateOrganization(organization *Organization) (bool, error) { _, affected, err := c.modifyOrganization("update-organization", organization, nil) return affected, err } - -func UpdateOrganization(organization *Organization) (bool, error) { - return globalClient.UpdateOrganization(organization) -} diff --git a/casdoorsdk/organization_global.go b/casdoorsdk/organization_global.go new file mode 100644 index 0000000..dd6e4a5 --- /dev/null +++ b/casdoorsdk/organization_global.go @@ -0,0 +1,39 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetOrganization(name string) ([]*Organization, error) { + return globalClient.GetOrganizations() +} + +func GetOrganizations() ([]*Organization, error) { + return globalClient.GetOrganizations() +} + +func GetOrganizationNames() ([]*Organization, error) { + return globalClient.GetOrganizationNames() +} + +func AddOrganization(organization *Organization) (bool, error) { + return globalClient.AddOrganization(organization) +} + +func DeleteOrganization(name string) (bool, error) { + return globalClient.DeleteOrganization(name) +} + +func UpdateOrganization(organization *Organization) (bool, error) { + return globalClient.UpdateOrganization(organization) +} diff --git a/casdoorsdk/resource.go b/casdoorsdk/resource.go index a62a18e..7e05e3a 100644 --- a/casdoorsdk/resource.go +++ b/casdoorsdk/resource.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -61,18 +61,10 @@ func (c *Client) GetResource(id string) (*Resource, error) { return resource, nil } -func GetResource(id string) (*Resource, error) { - return globalClient.GetResource(id) -} - func (c *Client) GetResourceEx(owner, name string) (*Resource, error) { return c.GetResource(fmt.Sprintf("%s/%s", owner, name)) } -func GetResourceEx(owner, name string) (*Resource, error) { - return globalClient.GetResourceEx(owner, name) -} - func (c *Client) GetResources(owner, user, field, value, sortField, sortOrder string) ([]*Resource, error) { queryMap := map[string]string{ "owner": owner, @@ -98,10 +90,6 @@ func (c *Client) GetResources(owner, user, field, value, sortField, sortOrder st return resources, nil } -func GetResources(owner, user, field, value, sortField, sortOrder string) ([]*Resource, error) { - return globalClient.GetResources(owner, user, field, value, sortField, sortOrder) -} - func (c *Client) GetPaginationResources(owner, user, field, value string, pageSize, page int, sortField, sortOrder string) ([]*Resource, error) { queryMap := map[string]string{ "owner": owner, @@ -129,10 +117,6 @@ func (c *Client) GetPaginationResources(owner, user, field, value string, pageSi return resources, nil } -func GetPaginationResources(owner, user, field, value string, pageSize, page int, sortField, sortOrder string) ([]*Resource, error) { - return globalClient.GetPaginationResources(owner, user, field, value, pageSize, page, sortField, sortOrder) -} - func (c *Client) UploadResource(user string, tag string, parent string, fullFilePath string, fileBytes []byte) (string, string, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -157,10 +141,6 @@ func (c *Client) UploadResource(user string, tag string, parent string, fullFile return fileUrl, name, nil } -func UploadResource(user string, tag string, parent string, fullFilePath string, fileBytes []byte) (string, string, error) { - return globalClient.UploadResource(user, tag, parent, fullFilePath, fileBytes) -} - func (c *Client) UploadResourceEx(user string, tag string, parent string, fullFilePath string, fileBytes []byte, createdTime string, description string) (string, string, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -187,10 +167,6 @@ func (c *Client) UploadResourceEx(user string, tag string, parent string, fullFi return fileUrl, name, nil } -func UploadResourceEx(user string, tag string, parent string, fullFilePath string, fileBytes []byte, createdTime string, description string) (string, string, error) { - return globalClient.UploadResourceEx(user, tag, parent, fullFilePath, fileBytes, createdTime, description) -} - func (c *Client) DeleteResource(name string) (bool, error) { resource := Resource{ Owner: c.OrganizationName, @@ -208,7 +184,3 @@ func (c *Client) DeleteResource(name string) (bool, error) { return resp.Data == "Affected", nil } - -func DeleteResource(name string) (bool, error) { - return globalClient.DeleteResource(name) -} diff --git a/casdoorsdk/resource_global.go b/casdoorsdk/resource_global.go new file mode 100644 index 0000000..272661d --- /dev/null +++ b/casdoorsdk/resource_global.go @@ -0,0 +1,43 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetResource(id string) (*Resource, error) { + return globalClient.GetResource(id) +} + +func GetResourceEx(owner, name string) (*Resource, error) { + return globalClient.GetResourceEx(owner, name) +} + +func GetResources(owner, user, field, value, sortField, sortOrder string) ([]*Resource, error) { + return globalClient.GetResources(owner, user, field, value, sortField, sortOrder) +} + +func GetPaginationResources(owner, user, field, value string, pageSize, page int, sortField, sortOrder string) ([]*Resource, error) { + return globalClient.GetPaginationResources(owner, user, field, value, pageSize, page, sortField, sortOrder) +} + +func UploadResource(user string, tag string, parent string, fullFilePath string, fileBytes []byte) (string, string, error) { + return globalClient.UploadResource(user, tag, parent, fullFilePath, fileBytes) +} + +func UploadResourceEx(user string, tag string, parent string, fullFilePath string, fileBytes []byte, createdTime string, description string) (string, string, error) { + return globalClient.UploadResourceEx(user, tag, parent, fullFilePath, fileBytes, createdTime, description) +} + +func DeleteResource(name string) (bool, error) { + return globalClient.DeleteResource(name) +} diff --git a/casdoorsdk/role.go b/casdoorsdk/role.go index 5a80d40..487287e 100644 --- a/casdoorsdk/role.go +++ b/casdoorsdk/role.go @@ -1,4 +1,4 @@ -// Copyright 2022 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -55,10 +55,6 @@ func (c *Client) GetRoles() ([]*Role, error) { return roles, nil } -func GetRoles() ([]*Role, error) { - return globalClient.GetRoles() -} - func (c *Client) GetPaginationRoles(p int, pageSize int, queryMap map[string]string) ([]*Role, int, error) { queryMap["owner"] = c.OrganizationName queryMap["p"] = strconv.Itoa(p) @@ -78,10 +74,6 @@ func (c *Client) GetPaginationRoles(p int, pageSize int, queryMap map[string]str return roles, int(response.Data2.(float64)), nil } -func GetPaginationRoles(p int, pageSize int, queryMap map[string]string) ([]*Role, int, error) { - return globalClient.GetPaginationRoles(p, pageSize, queryMap) -} - func (c *Client) GetRole(name string) (*Role, error) { queryMap := map[string]string{ "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), @@ -102,42 +94,22 @@ func (c *Client) GetRole(name string) (*Role, error) { return role, nil } -func GetRole(name string) (*Role, error) { - return globalClient.GetRole(name) -} - func (c *Client) UpdateRole(role *Role) (bool, error) { _, affected, err := c.modifyRole("update-role", role, nil) return affected, err } -func UpdateRole(role *Role) (bool, error) { - return globalClient.UpdateRole(role) -} - func (c *Client) UpdateRoleForColumns(role *Role, columns []string) (bool, error) { _, affected, err := c.modifyRole("update-role", role, columns) return affected, err } -func UpdateRoleForColumns(role *Role, columns []string) (bool, error) { - return globalClient.UpdateRoleForColumns(role, columns) -} - func (c *Client) AddRole(role *Role) (bool, error) { _, affected, err := c.modifyRole("add-role", role, nil) return affected, err } -func AddRole(role *Role) (bool, error) { - return globalClient.AddRole(role) -} - func (c *Client) DeleteRole(role *Role) (bool, error) { _, affected, err := c.modifyRole("delete-role", role, nil) return affected, err } - -func DeleteRole(role *Role) (bool, error) { - return globalClient.DeleteRole(role) -} diff --git a/casdoorsdk/role_global.go b/casdoorsdk/role_global.go new file mode 100644 index 0000000..ff799d2 --- /dev/null +++ b/casdoorsdk/role_global.go @@ -0,0 +1,43 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetRoles() ([]*Role, error) { + return globalClient.GetRoles() +} + +func GetPaginationRoles(p int, pageSize int, queryMap map[string]string) ([]*Role, int, error) { + return globalClient.GetPaginationRoles(p, pageSize, queryMap) +} + +func GetRole(name string) (*Role, error) { + return globalClient.GetRole(name) +} + +func UpdateRole(role *Role) (bool, error) { + return globalClient.UpdateRole(role) +} + +func UpdateRoleForColumns(role *Role, columns []string) (bool, error) { + return globalClient.UpdateRoleForColumns(role, columns) +} + +func AddRole(role *Role) (bool, error) { + return globalClient.AddRole(role) +} + +func DeleteRole(role *Role) (bool, error) { + return globalClient.DeleteRole(role) +} diff --git a/casdoorsdk/session.go b/casdoorsdk/session.go index 3a534ef..5cdcd13 100644 --- a/casdoorsdk/session.go +++ b/casdoorsdk/session.go @@ -55,10 +55,6 @@ func (c *Client) GetSessions() ([]*Session, error) { return sessions, nil } -func GetSessions() ([]*Session, error) { - return globalClient.GetSessions() -} - func (c *Client) GetPaginationSessions(p int, pageSize int, queryMap map[string]string) ([]*Session, int, error) { queryMap["owner"] = c.OrganizationName queryMap["p"] = strconv.Itoa(p) @@ -78,10 +74,6 @@ func (c *Client) GetPaginationSessions(p int, pageSize int, queryMap map[string] return sessions, int(response.Data2.(float64)), nil } -func GetPaginationSessions(p int, pageSize int, queryMap map[string]string) ([]*Session, int, error) { - return globalClient.GetPaginationSessions(p, pageSize, queryMap) -} - func (c *Client) GetSession(name string) (*Session, error) { queryMap := map[string]string{ "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), @@ -102,42 +94,22 @@ func (c *Client) GetSession(name string) (*Session, error) { return session, nil } -func GetSession(name string) (*Session, error) { - return globalClient.GetSession(name) -} - func (c *Client) UpdateSession(session *Session) (bool, error) { _, affected, err := c.modifySession("update-session", session, nil) return affected, err } -func UpdateSession(session *Session) (bool, error) { - return globalClient.UpdateSession(session) -} - func (c *Client) UpdateSessionForColumns(session *Session, columns []string) (bool, error) { _, affected, err := c.modifySession("update-session", session, columns) return affected, err } -func UpdateSessionForColumns(session *Session, columns []string) (bool, error) { - return globalClient.UpdateSessionForColumns(session, columns) -} - func (c *Client) AddSession(session *Session) (bool, error) { _, affected, err := c.modifySession("add-session", session, nil) return affected, err } -func AddSession(session *Session) (bool, error) { - return globalClient.AddSession(session) -} - func (c *Client) DeleteSession(session *Session) (bool, error) { _, affected, err := c.modifySession("delete-session", session, nil) return affected, err } - -func DeleteSession(session *Session) (bool, error) { - return globalClient.DeleteSession(session) -} diff --git a/casdoorsdk/session_global.go b/casdoorsdk/session_global.go new file mode 100644 index 0000000..e0d2e0f --- /dev/null +++ b/casdoorsdk/session_global.go @@ -0,0 +1,43 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing records and +// limitations under the License. + +package casdoorsdk + +func GetSessions() ([]*Session, error) { + return globalClient.GetSessions() +} + +func GetPaginationSessions(p int, pageSize int, queryMap map[string]string) ([]*Session, int, error) { + return globalClient.GetPaginationSessions(p, pageSize, queryMap) +} + +func GetSession(name string) (*Session, error) { + return globalClient.GetSession(name) +} + +func UpdateSession(session *Session) (bool, error) { + return globalClient.UpdateSession(session) +} + +func UpdateSessionForColumns(session *Session, columns []string) (bool, error) { + return globalClient.UpdateSessionForColumns(session, columns) +} + +func AddSession(session *Session) (bool, error) { + return globalClient.AddSession(session) +} + +func DeleteSession(session *Session) (bool, error) { + return globalClient.DeleteSession(session) +} diff --git a/casdoorsdk/sms.go b/casdoorsdk/sms.go index 9293260..435034c 100644 --- a/casdoorsdk/sms.go +++ b/casdoorsdk/sms.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,38 +14,6 @@ package casdoorsdk -import ( - "encoding/json" - "fmt" -) - -type smsForm struct { - Content string `json:"content"` - Receivers []string `json:"receivers"` -} - -func (c *Client) SendSms(content string, receivers ...string) error { - form := smsForm{ - Content: content, - Receivers: receivers, - } - postBytes, err := json.Marshal(form) - if err != nil { - return err - } - - resp, err := c.DoPost("send-sms", nil, postBytes, false, false) - if err != nil { - return err - } - - if resp.Status != "ok" { - return fmt.Errorf(resp.Msg) - } - - return nil -} - func SendSms(content string, receivers ...string) error { return globalClient.SendSms(content, receivers...) } diff --git a/casdoorsdk/sms_global.go b/casdoorsdk/sms_global.go new file mode 100644 index 0000000..8d837cc --- /dev/null +++ b/casdoorsdk/sms_global.go @@ -0,0 +1,47 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +import ( + "encoding/json" + "fmt" +) + +type smsForm struct { + Content string `json:"content"` + Receivers []string `json:"receivers"` +} + +func (c *Client) SendSms(content string, receivers ...string) error { + form := smsForm{ + Content: content, + Receivers: receivers, + } + postBytes, err := json.Marshal(form) + if err != nil { + return err + } + + resp, err := c.DoPost("send-sms", nil, postBytes, false, false) + if err != nil { + return err + } + + if resp.Status != "ok" { + return fmt.Errorf(resp.Msg) + } + + return nil +} diff --git a/casdoorsdk/token.go b/casdoorsdk/token.go index ea0e015..1dac238 100644 --- a/casdoorsdk/token.go +++ b/casdoorsdk/token.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -72,10 +72,6 @@ func (c *Client) GetOAuthToken(code string, state string) (*oauth2.Token, error) return token, err } -func GetOAuthToken(code string, state string) (*oauth2.Token, error) { - return globalClient.GetOAuthToken(code, state) -} - // RefreshOAuthToken refreshes the OAuth token func (c *Client) RefreshOAuthToken(refreshToken string) (*oauth2.Token, error) { config := oauth2.Config{ @@ -102,10 +98,6 @@ func (c *Client) RefreshOAuthToken(refreshToken string) (*oauth2.Token, error) { return token, err } -func RefreshOAuthToken(refreshToken string) (*oauth2.Token, error) { - return globalClient.RefreshOAuthToken(refreshToken) -} - func (c *Client) GetTokens(p int, pageSize int) ([]*Token, int, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -128,10 +120,6 @@ func (c *Client) GetTokens(p int, pageSize int) ([]*Token, int, error) { return tokens, int(response.Data2.(float64)), nil } -func GetTokens(p int, pageSize int) ([]*Token, int, error) { - return globalClient.GetTokens(p, pageSize) -} - func (c *Client) DeleteToken(name string) (bool, error) { organization := Organization{ Owner: "admin", @@ -149,7 +137,3 @@ func (c *Client) DeleteToken(name string) (bool, error) { return resp.Data == "Affected", nil } - -func DeleteToken(name string) (bool, error) { - return globalClient.DeleteToken(name) -} diff --git a/casdoorsdk/token_global.go b/casdoorsdk/token_global.go new file mode 100644 index 0000000..07512b2 --- /dev/null +++ b/casdoorsdk/token_global.go @@ -0,0 +1,35 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +import ( + "golang.org/x/oauth2" +) + +func GetOAuthToken(code string, state string) (*oauth2.Token, error) { + return globalClient.GetOAuthToken(code, state) +} + +func RefreshOAuthToken(refreshToken string) (*oauth2.Token, error) { + return globalClient.RefreshOAuthToken(refreshToken) +} + +func GetTokens(p int, pageSize int) ([]*Token, int, error) { + return globalClient.GetTokens(p, pageSize) +} + +func DeleteToken(name string) (bool, error) { + return globalClient.DeleteToken(name) +} diff --git a/casdoorsdk/url.go b/casdoorsdk/url.go index 0969379..37ba366 100644 --- a/casdoorsdk/url.go +++ b/casdoorsdk/url.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -29,10 +29,6 @@ func (c *Client) GetSignupUrl(enablePassword bool, redirectUri string) string { } } -func GetSignupUrl(enablePassword bool, redirectUri string) string { - return globalClient.GetSignupUrl(enablePassword, redirectUri) -} - func (c *Client) GetSigninUrl(redirectUri string) string { // origin := "https://door.casbin.com" // redirectUri := fmt.Sprintf("%s/callback", origin) @@ -42,10 +38,6 @@ func (c *Client) GetSigninUrl(redirectUri string) string { c.Endpoint, c.ClientId, url.QueryEscape(redirectUri), scope, state) } -func GetSigninUrl(redirectUri string) string { - return globalClient.GetSigninUrl(redirectUri) -} - func (c *Client) GetUserProfileUrl(userName string, accessToken string) string { param := "" if accessToken != "" { @@ -54,10 +46,6 @@ func (c *Client) GetUserProfileUrl(userName string, accessToken string) string { return fmt.Sprintf("%s/users/%s/%s%s", c.Endpoint, c.OrganizationName, userName, param) } -func GetUserProfileUrl(userName string, accessToken string) string { - return globalClient.GetUserProfileUrl(userName, accessToken) -} - func (c *Client) GetMyProfileUrl(accessToken string) string { param := "" if accessToken != "" { @@ -65,7 +53,3 @@ func (c *Client) GetMyProfileUrl(accessToken string) string { } return fmt.Sprintf("%s/account%s", c.Endpoint, param) } - -func GetMyProfileUrl(accessToken string) string { - return globalClient.GetMyProfileUrl(accessToken) -} diff --git a/casdoorsdk/url_global.go b/casdoorsdk/url_global.go new file mode 100644 index 0000000..da9b879 --- /dev/null +++ b/casdoorsdk/url_global.go @@ -0,0 +1,31 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetSignupUrl(enablePassword bool, redirectUri string) string { + return globalClient.GetSignupUrl(enablePassword, redirectUri) +} + +func GetSigninUrl(redirectUri string) string { + return globalClient.GetSigninUrl(redirectUri) +} + +func GetUserProfileUrl(userName string, accessToken string) string { + return globalClient.GetUserProfileUrl(userName, accessToken) +} + +func GetMyProfileUrl(accessToken string) string { + return globalClient.GetMyProfileUrl(accessToken) +} diff --git a/casdoorsdk/user.go b/casdoorsdk/user.go index b11aa46..62baf8c 100644 --- a/casdoorsdk/user.go +++ b/casdoorsdk/user.go @@ -222,10 +222,6 @@ func (c *Client) GetGlobalUsers() ([]*User, error) { return users, nil } -func GetGlobalUsers() ([]*User, error) { - return globalClient.GetGlobalUsers() -} - func (c *Client) GetUsers() ([]*User, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -246,10 +242,6 @@ func (c *Client) GetUsers() ([]*User, error) { return users, nil } -func GetUsers() ([]*User, error) { - return globalClient.GetUsers() -} - func (c *Client) GetSortedUsers(sorter string, limit int) ([]*User, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -272,10 +264,6 @@ func (c *Client) GetSortedUsers(sorter string, limit int) ([]*User, error) { return users, nil } -func GetSortedUsers(sorter string, limit int) ([]*User, error) { - return globalClient.GetSortedUsers(sorter, limit) -} - func (c *Client) GetPaginationUsers(p int, pageSize int, queryMap map[string]string) ([]*User, int, error) { queryMap["owner"] = c.OrganizationName queryMap["p"] = strconv.Itoa(p) @@ -296,10 +284,6 @@ func (c *Client) GetPaginationUsers(p int, pageSize int, queryMap map[string]str return users, int(response.Data2.(float64)), nil } -func GetPaginationUsers(p int, pageSize int, queryMap map[string]string) ([]*User, int, error) { - return globalClient.GetPaginationUsers(p, pageSize, queryMap) -} - func (c *Client) GetUserCount(isOnline string) (int, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -321,10 +305,6 @@ func (c *Client) GetUserCount(isOnline string) (int, error) { return count, nil } -func GetUserCount(isOnline string) (int, error) { - return globalClient.GetUserCount(isOnline) -} - func (c *Client) GetUser(name string) (*User, error) { queryMap := map[string]string{ "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), @@ -345,10 +325,6 @@ func (c *Client) GetUser(name string) (*User, error) { return user, nil } -func GetUser(name string) (*User, error) { - return globalClient.GetUser(name) -} - func (c *Client) GetUserByEmail(email string) (*User, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -370,10 +346,6 @@ func (c *Client) GetUserByEmail(email string) (*User, error) { return user, nil } -func GetUserByEmail(email string) (*User, error) { - return globalClient.GetUserByEmail(email) -} - func (c *Client) GetUserByPhone(phone string) (*User, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -395,10 +367,6 @@ func (c *Client) GetUserByPhone(phone string) (*User, error) { return user, nil } -func GetUserByPhone(phone string) (*User, error) { - return globalClient.GetUserByPhone(phone) -} - func (c *Client) GetUserByUserId(userId string) (*User, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -420,10 +388,6 @@ func (c *Client) GetUserByUserId(userId string) (*User, error) { return user, nil } -func GetUserByUserId(userId string) (*User, error) { - return globalClient.GetUserByUserId(userId) -} - // note: oldPassword is not required, if you don't need, just pass a empty string func (c *Client) SetPassword(owner, name, oldPassword, newPassword string) (bool, error) { param := map[string]string{ @@ -446,65 +410,36 @@ func (c *Client) SetPassword(owner, name, oldPassword, newPassword string) (bool return resp.Status == "ok", nil } -// note: oldPassword is not required, if you don't need, just pass a empty string -func SetPassword(owner, name, oldPassword, newPassword string) (bool, error) { - return globalClient.SetPassword(owner, name, oldPassword, newPassword) -} - func (c *Client) UpdateUserById(id string, user *User) (bool, error) { _, affected, err := c.modifyUserById("update-user", id, user, nil) return affected, err } -func UpdateUserById(id string, user *User) (bool, error) { - return globalClient.UpdateUserById(id, user) -} - func (c *Client) UpdateUser(user *User) (bool, error) { _, affected, err := c.modifyUser("update-user", user, nil) return affected, err } -func UpdateUser(user *User) (bool, error) { - return globalClient.UpdateUser(user) -} - func (c *Client) UpdateUserForColumns(user *User, columns []string) (bool, error) { _, affected, err := c.modifyUser("update-user", user, columns) return affected, err } -func UpdateUserForColumns(user *User, columns []string) (bool, error) { - return globalClient.UpdateUserForColumns(user, columns) -} - func (c *Client) AddUser(user *User) (bool, error) { _, affected, err := c.modifyUser("add-user", user, nil) return affected, err } -func AddUser(user *User) (bool, error) { - return globalClient.AddUser(user) -} - func (c *Client) DeleteUser(user *User) (bool, error) { _, affected, err := c.modifyUser("delete-user", user, nil) return affected, err } -func DeleteUser(user *User) (bool, error) { - return globalClient.DeleteUser(user) -} - func (c *Client) CheckUserPassword(user *User) (bool, error) { response, _, err := c.modifyUser("check-user-password", user, nil) return response.Status == "ok", err } -func CheckUserPassword(user *User) (bool, error) { - return globalClient.CheckUserPassword(user) -} - func (u User) GetId() string { return fmt.Sprintf("%s/%s", u.Owner, u.Name) } diff --git a/casdoorsdk/user_global.go b/casdoorsdk/user_global.go new file mode 100644 index 0000000..f1980ff --- /dev/null +++ b/casdoorsdk/user_global.go @@ -0,0 +1,80 @@ +// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package casdoorsdk + +func GetGlobalUsers() ([]*User, error) { + return globalClient.GetGlobalUsers() +} + +func GetUsers() ([]*User, error) { + return globalClient.GetUsers() +} + +func GetSortedUsers(sorter string, limit int) ([]*User, error) { + return globalClient.GetSortedUsers(sorter, limit) +} + +func GetPaginationUsers(p int, pageSize int, queryMap map[string]string) ([]*User, int, error) { + return globalClient.GetPaginationUsers(p, pageSize, queryMap) +} + +func GetUserCount(isOnline string) (int, error) { + return globalClient.GetUserCount(isOnline) +} + +func GetUser(name string) (*User, error) { + return globalClient.GetUser(name) +} + +func GetUserByEmail(email string) (*User, error) { + return globalClient.GetUserByEmail(email) +} + +func GetUserByPhone(phone string) (*User, error) { + return globalClient.GetUserByPhone(phone) +} + +func GetUserByUserId(userId string) (*User, error) { + return globalClient.GetUserByUserId(userId) +} + +// note: oldPassword is not required, if you don't need, just pass a empty string +func SetPassword(owner, name, oldPassword, newPassword string) (bool, error) { + return globalClient.SetPassword(owner, name, oldPassword, newPassword) +} + +func UpdateUserById(id string, user *User) (bool, error) { + return globalClient.UpdateUserById(id, user) +} + +func UpdateUser(user *User) (bool, error) { + return globalClient.UpdateUser(user) +} + +func UpdateUserForColumns(user *User, columns []string) (bool, error) { + return globalClient.UpdateUserForColumns(user, columns) +} + +func AddUser(user *User) (bool, error) { + return globalClient.AddUser(user) +} + +func DeleteUser(user *User) (bool, error) { + return globalClient.DeleteUser(user) +} + +func CheckUserPassword(user *User) (bool, error) { + return globalClient.CheckUserPassword(user) +} From 723dd1bfe244f9e5f27b5699be6092b2ed1e71d9 Mon Sep 17 00:00:00 2001 From: liuqs Date: Sat, 19 Aug 2023 17:55:31 +0800 Subject: [PATCH 8/9] fix: remove duplicate code. --- casdoorsdk/adapter.go | 5 +---- casdoorsdk/enfocer.go | 5 +---- casdoorsdk/group.go | 5 +---- casdoorsdk/model.go | 5 +---- casdoorsdk/payment.go | 5 +---- casdoorsdk/plan.go | 5 +---- casdoorsdk/pricing.go | 5 +---- casdoorsdk/product.go | 5 +---- casdoorsdk/syncer.go | 5 +---- casdoorsdk/webhook.go | 5 +---- 10 files changed, 10 insertions(+), 40 deletions(-) diff --git a/casdoorsdk/adapter.go b/casdoorsdk/adapter.go index b2f55f4..5134ab4 100644 --- a/casdoorsdk/adapter.go +++ b/casdoorsdk/adapter.go @@ -73,14 +73,11 @@ func (c *Client) GetPaginationAdapters(p int, pageSize int, queryMap map[string] return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - adapters, ok := response.Data.([]*Adapter) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return adapters, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/enfocer.go b/casdoorsdk/enfocer.go index 8a3c161..dd0e220 100644 --- a/casdoorsdk/enfocer.go +++ b/casdoorsdk/enfocer.go @@ -68,14 +68,11 @@ func (c *Client) GetPaginationEnforcers(p int, pageSize int, queryMap map[string return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - enforcers, ok := response.Data.([]*Enforcer) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return enforcers, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/group.go b/casdoorsdk/group.go index 02093ad..624e90f 100644 --- a/casdoorsdk/group.go +++ b/casdoorsdk/group.go @@ -74,14 +74,11 @@ func (c *Client) GetPaginationGroups(p int, pageSize int, queryMap map[string]st return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - groups, ok := response.Data.([]*Group) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return groups, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/model.go b/casdoorsdk/model.go index 99491a2..be293a5 100644 --- a/casdoorsdk/model.go +++ b/casdoorsdk/model.go @@ -74,14 +74,11 @@ func (c *Client) GetPaginationModels(p int, pageSize int, queryMap map[string]st return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - models, ok := response.Data.([]*Model) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return models, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/payment.go b/casdoorsdk/payment.go index cabd92d..00f8d28 100644 --- a/casdoorsdk/payment.go +++ b/casdoorsdk/payment.go @@ -89,14 +89,11 @@ func (c *Client) GetPaginationPayments(p int, pageSize int, queryMap map[string] return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - payments, ok := response.Data.([]*Payment) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return payments, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/plan.go b/casdoorsdk/plan.go index 24286c6..19ea14a 100644 --- a/casdoorsdk/plan.go +++ b/casdoorsdk/plan.go @@ -70,14 +70,11 @@ func (c *Client) GetPaginationPlans(p int, pageSize int, queryMap map[string]str return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - plans, ok := response.Data.([]*Plan) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return plans, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/pricing.go b/casdoorsdk/pricing.go index 3f08f3c..aacef5e 100644 --- a/casdoorsdk/pricing.go +++ b/casdoorsdk/pricing.go @@ -73,14 +73,11 @@ func (c *Client) GetPaginationPricings(p int, pageSize int, queryMap map[string] return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - pricings, ok := response.Data.([]*Pricing) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return pricings, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/product.go b/casdoorsdk/product.go index effc807..56c6298 100644 --- a/casdoorsdk/product.go +++ b/casdoorsdk/product.go @@ -75,14 +75,11 @@ func (c *Client) GetPaginationProducts(p int, pageSize int, queryMap map[string] return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - products, ok := response.Data.([]*Product) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return products, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/syncer.go b/casdoorsdk/syncer.go index b7be356..bf02acd 100644 --- a/casdoorsdk/syncer.go +++ b/casdoorsdk/syncer.go @@ -90,14 +90,11 @@ func (c *Client) GetPaginationSyncers(p int, pageSize int, queryMap map[string]s return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - syncers, ok := response.Data.([]*Syncer) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return syncers, int(response.Data2.(float64)), nil } diff --git a/casdoorsdk/webhook.go b/casdoorsdk/webhook.go index 52416e4..498a001 100644 --- a/casdoorsdk/webhook.go +++ b/casdoorsdk/webhook.go @@ -81,14 +81,11 @@ func (c *Client) GetPaginationWebhooks(p int, pageSize int, queryMap map[string] return nil, 0, err } - if response.Status != "ok" { - return nil, 0, fmt.Errorf(response.Msg) - } - webhooks, ok := response.Data.([]*Webhook) if !ok { return nil, 0, errors.New("response data format is incorrect") } + return webhooks, int(response.Data2.(float64)), nil } From 91f63d226e395c48a29653b6291a2575b59204f3 Mon Sep 17 00:00:00 2001 From: liuqs Date: Sat, 19 Aug 2023 17:56:35 +0800 Subject: [PATCH 9/9] fix: correct copyright year in license. --- casdoorsdk/application.go | 2 +- casdoorsdk/base.go | 2 +- casdoorsdk/email.go | 2 +- casdoorsdk/jwt.go | 2 +- casdoorsdk/organization.go | 2 +- casdoorsdk/permission.go | 2 +- casdoorsdk/provider.go | 2 +- casdoorsdk/resource.go | 2 +- casdoorsdk/role.go | 2 +- casdoorsdk/session.go | 2 +- casdoorsdk/sms.go | 2 +- casdoorsdk/token.go | 2 +- casdoorsdk/url.go | 2 +- casdoorsdk/util_global.go | 2 +- 14 files changed, 14 insertions(+), 14 deletions(-) diff --git a/casdoorsdk/application.go b/casdoorsdk/application.go index e3ed15d..55c4256 100644 --- a/casdoorsdk/application.go +++ b/casdoorsdk/application.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/base.go b/casdoorsdk/base.go index c283810..5cba209 100644 --- a/casdoorsdk/base.go +++ b/casdoorsdk/base.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/email.go b/casdoorsdk/email.go index f8f8841..895c855 100644 --- a/casdoorsdk/email.go +++ b/casdoorsdk/email.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/jwt.go b/casdoorsdk/jwt.go index ab321ca..c252c5c 100644 --- a/casdoorsdk/jwt.go +++ b/casdoorsdk/jwt.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/organization.go b/casdoorsdk/organization.go index e16ca73..feb33c6 100644 --- a/casdoorsdk/organization.go +++ b/casdoorsdk/organization.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/permission.go b/casdoorsdk/permission.go index e35b9b7..4fe15ae 100644 --- a/casdoorsdk/permission.go +++ b/casdoorsdk/permission.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/provider.go b/casdoorsdk/provider.go index 2585f53..1f6396d 100644 --- a/casdoorsdk/provider.go +++ b/casdoorsdk/provider.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2022 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/resource.go b/casdoorsdk/resource.go index 7e05e3a..5ba8c6a 100644 --- a/casdoorsdk/resource.go +++ b/casdoorsdk/resource.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/role.go b/casdoorsdk/role.go index 487287e..d3f2602 100644 --- a/casdoorsdk/role.go +++ b/casdoorsdk/role.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/session.go b/casdoorsdk/session.go index 5cdcd13..2da3e28 100644 --- a/casdoorsdk/session.go +++ b/casdoorsdk/session.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/sms.go b/casdoorsdk/sms.go index 435034c..0e15f64 100644 --- a/casdoorsdk/sms.go +++ b/casdoorsdk/sms.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/token.go b/casdoorsdk/token.go index 1dac238..a0a1561 100644 --- a/casdoorsdk/token.go +++ b/casdoorsdk/token.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/url.go b/casdoorsdk/url.go index 37ba366..4c092f5 100644 --- a/casdoorsdk/url.go +++ b/casdoorsdk/url.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/casdoorsdk/util_global.go b/casdoorsdk/util_global.go index e7ed823..e63f6b9 100644 --- a/casdoorsdk/util_global.go +++ b/casdoorsdk/util_global.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License.