diff --git a/casdoorsdk/adapter.go b/casdoorsdk/adapter.go new file mode 100644 index 0000000..5134ab4 --- /dev/null +++ b/casdoorsdk/adapter.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" +) + +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 + } + + adapters, ok := response.Data.([]*Adapter) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + + 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..fd46704 --- /dev/null +++ b/casdoorsdk/adapter_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 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 a783266..5cba209 100644 --- a/casdoorsdk/base.go +++ b/casdoorsdk/base.go @@ -390,3 +390,302 @@ 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 +} + +// 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-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), + } + + 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 +} + +// 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/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..bd2356b --- /dev/null +++ b/casdoorsdk/cert_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 + +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) +} diff --git a/casdoorsdk/email.go b/casdoorsdk/email.go index 0df5af3..895c855 100644 --- a/casdoorsdk/email.go +++ b/casdoorsdk/email.go @@ -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 new file mode 100644 index 0000000..dd0e220 --- /dev/null +++ b/casdoorsdk/enfocer.go @@ -0,0 +1,112 @@ +// 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 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 + } + + enforcers, ok := response.Data.([]*Enforcer) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + + 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..7d9b900 --- /dev/null +++ b/casdoorsdk/enforcer_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 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..624e90f --- /dev/null +++ b/casdoorsdk/group.go @@ -0,0 +1,118 @@ +// 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 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 + } + + groups, ok := response.Data.([]*Group) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + + 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..a00f224 --- /dev/null +++ b/casdoorsdk/group_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 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/jwt.go b/casdoorsdk/jwt.go index 11496d7..c252c5c 100644 --- a/casdoorsdk/jwt.go +++ b/casdoorsdk/jwt.go @@ -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/model.go b/casdoorsdk/model.go new file mode 100644 index 0000000..be293a5 --- /dev/null +++ b/casdoorsdk/model.go @@ -0,0 +1,118 @@ +// 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 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 + } + + models, ok := response.Data.([]*Model) + if !ok { + return nil, 0, errors.New("response data format is incorrect") + } + + 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..251861a --- /dev/null +++ b/casdoorsdk/model_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 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/organization.go b/casdoorsdk/organization.go index cc1be15..feb33c6 100644 --- a/casdoorsdk/organization.go +++ b/casdoorsdk/organization.go @@ -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/payment.go b/casdoorsdk/payment.go new file mode 100644 index 0000000..00f8d28 --- /dev/null +++ b/casdoorsdk/payment.go @@ -0,0 +1,168 @@ +// 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 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 + } + + 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..a4d1ef8 --- /dev/null +++ b/casdoorsdk/payment_global.go @@ -0,0 +1,51 @@ +// 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) { + 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 441d6be..4fe15ae 100644 --- a/casdoorsdk/permission.go +++ b/casdoorsdk/permission.go @@ -66,10 +66,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), @@ -90,10 +86,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) @@ -114,10 +106,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), @@ -138,42 +126,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..cc89ca9 --- /dev/null +++ b/casdoorsdk/permission_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 + +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) +} diff --git a/casdoorsdk/plan.go b/casdoorsdk/plan.go new file mode 100644 index 0000000..19ea14a --- /dev/null +++ b/casdoorsdk/plan.go @@ -0,0 +1,114 @@ +// 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 + } + + 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..aacef5e --- /dev/null +++ b/casdoorsdk/pricing.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" +) + +// 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 + } + + 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..8074077 --- /dev/null +++ b/casdoorsdk/princing_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 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..56c6298 --- /dev/null +++ b/casdoorsdk/product.go @@ -0,0 +1,140 @@ +// 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 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 + } + + 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..3147de4 --- /dev/null +++ b/casdoorsdk/product_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 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 07faf78..1f6396d 100644 --- a/casdoorsdk/provider.go +++ b/casdoorsdk/provider.go @@ -89,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), @@ -124,46 +120,24 @@ func (c *Client) GetPaginationProviders(p int, pageSize int, queryMap map[string 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..7daacf7 --- /dev/null +++ b/casdoorsdk/provider_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 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/resource.go b/casdoorsdk/resource.go index a62a18e..5ba8c6a 100644 --- a/casdoorsdk/resource.go +++ b/casdoorsdk/resource.go @@ -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 d3138d3..d3f2602 100644 --- a/casdoorsdk/role.go +++ b/casdoorsdk/role.go @@ -1,4 +1,4 @@ -// Copyright 2022 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. @@ -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) @@ -75,14 +71,9 @@ func (c *Client) GetPaginationRoles(p int, pageSize int, queryMap map[string]str if !ok { return nil, 0, errors.New("response data format is incorrect") } - 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), @@ -103,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 91f9c8e..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. @@ -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) @@ -75,14 +71,9 @@ func (c *Client) GetPaginationSessions(p int, pageSize int, queryMap map[string] if !ok { return nil, 0, errors.New("response data format is incorrect") } - 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), @@ -103,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..0e15f64 100644 --- a/casdoorsdk/sms.go +++ b/casdoorsdk/sms.go @@ -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/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..7885932 --- /dev/null +++ b/casdoorsdk/subscription_global.go @@ -0,0 +1,36 @@ +// 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) { + 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..bf02acd --- /dev/null +++ b/casdoorsdk/syncer.go @@ -0,0 +1,134 @@ +// 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 + } + + 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/token.go b/casdoorsdk/token.go index ea0e015..a0a1561 100644 --- a/casdoorsdk/token.go +++ b/casdoorsdk/token.go @@ -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..4c092f5 100644 --- a/casdoorsdk/url.go +++ b/casdoorsdk/url.go @@ -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) +} 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. diff --git a/casdoorsdk/webhook.go b/casdoorsdk/webhook.go new file mode 100644 index 0000000..498a001 --- /dev/null +++ b/casdoorsdk/webhook.go @@ -0,0 +1,125 @@ +// 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 + } + + 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) +}