mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-08 00:50:28 +08:00
Refactor adapter's owner and organization
This commit is contained in:
@ -23,6 +23,13 @@ import (
|
|||||||
xormadapter "github.com/casdoor/xorm-adapter/v3"
|
xormadapter "github.com/casdoor/xorm-adapter/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetCasbinAdapters
|
||||||
|
// @Title GetCasbinAdapters
|
||||||
|
// @Tag Adapter API
|
||||||
|
// @Description get adapters
|
||||||
|
// @Param owner query string true "The owner of adapters"
|
||||||
|
// @Success 200 {array} object.Adapter The Response object
|
||||||
|
// @router /get-adapters [get]
|
||||||
func (c *ApiController) GetCasbinAdapters() {
|
func (c *ApiController) GetCasbinAdapters() {
|
||||||
owner := c.Input().Get("owner")
|
owner := c.Input().Get("owner")
|
||||||
limit := c.Input().Get("pageSize")
|
limit := c.Input().Get("pageSize")
|
||||||
@ -31,9 +38,9 @@ func (c *ApiController) GetCasbinAdapters() {
|
|||||||
value := c.Input().Get("value")
|
value := c.Input().Get("value")
|
||||||
sortField := c.Input().Get("sortField")
|
sortField := c.Input().Get("sortField")
|
||||||
sortOrder := c.Input().Get("sortOrder")
|
sortOrder := c.Input().Get("sortOrder")
|
||||||
organization := c.Input().Get("organization")
|
|
||||||
if limit == "" || page == "" {
|
if limit == "" || page == "" {
|
||||||
adapters, err := object.GetCasbinAdapters(owner, organization)
|
adapters, err := object.GetCasbinAdapters(owner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ResponseError(err.Error())
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
@ -42,14 +49,14 @@ func (c *ApiController) GetCasbinAdapters() {
|
|||||||
c.ResponseOk(adapters)
|
c.ResponseOk(adapters)
|
||||||
} else {
|
} else {
|
||||||
limit := util.ParseInt(limit)
|
limit := util.ParseInt(limit)
|
||||||
count, err := object.GetCasbinAdapterCount(owner, organization, field, value)
|
count, err := object.GetCasbinAdapterCount(owner, field, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ResponseError(err.Error())
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
paginator := pagination.SetPaginator(c.Ctx, limit, count)
|
paginator := pagination.SetPaginator(c.Ctx, limit, count)
|
||||||
adapters, err := object.GetPaginationCasbinAdapters(owner, organization, paginator.Offset(), limit, field, value, sortField, sortOrder)
|
adapters, err := object.GetPaginationCasbinAdapters(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ResponseError(err.Error())
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
@ -59,8 +66,16 @@ func (c *ApiController) GetCasbinAdapters() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCasbinAdapter
|
||||||
|
// @Title GetCasbinAdapter
|
||||||
|
// @Tag Adapter API
|
||||||
|
// @Description get adapter
|
||||||
|
// @Param id query string true "The id ( owner/name ) of the adapter"
|
||||||
|
// @Success 200 {object} object.Adapter The Response object
|
||||||
|
// @router /get-adapter [get]
|
||||||
func (c *ApiController) GetCasbinAdapter() {
|
func (c *ApiController) GetCasbinAdapter() {
|
||||||
id := c.Input().Get("id")
|
id := c.Input().Get("id")
|
||||||
|
|
||||||
adapter, err := object.GetCasbinAdapter(id)
|
adapter, err := object.GetCasbinAdapter(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ResponseError(err.Error())
|
c.ResponseError(err.Error())
|
||||||
@ -70,6 +85,14 @@ func (c *ApiController) GetCasbinAdapter() {
|
|||||||
c.ResponseOk(adapter)
|
c.ResponseOk(adapter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateCasbinAdapter
|
||||||
|
// @Title UpdateCasbinAdapter
|
||||||
|
// @Tag Adapter API
|
||||||
|
// @Description update adapter
|
||||||
|
// @Param id query string true "The id ( owner/name ) of the adapter"
|
||||||
|
// @Param body body object.Adapter true "The details of the adapter"
|
||||||
|
// @Success 200 {object} controllers.Response The Response object
|
||||||
|
// @router /update-adapter [post]
|
||||||
func (c *ApiController) UpdateCasbinAdapter() {
|
func (c *ApiController) UpdateCasbinAdapter() {
|
||||||
id := c.Input().Get("id")
|
id := c.Input().Get("id")
|
||||||
|
|
||||||
@ -84,6 +107,13 @@ func (c *ApiController) UpdateCasbinAdapter() {
|
|||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddCasbinAdapter
|
||||||
|
// @Title AddCasbinAdapter
|
||||||
|
// @Tag Adapter API
|
||||||
|
// @Description add adapter
|
||||||
|
// @Param body body object.Adapter true "The details of the adapter"
|
||||||
|
// @Success 200 {object} controllers.Response The Response object
|
||||||
|
// @router /add-adapter [post]
|
||||||
func (c *ApiController) AddCasbinAdapter() {
|
func (c *ApiController) AddCasbinAdapter() {
|
||||||
var casbinAdapter object.CasbinAdapter
|
var casbinAdapter object.CasbinAdapter
|
||||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
|
||||||
@ -96,6 +126,13 @@ func (c *ApiController) AddCasbinAdapter() {
|
|||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteCasbinAdapter
|
||||||
|
// @Title DeleteCasbinAdapter
|
||||||
|
// @Tag Adapter API
|
||||||
|
// @Description delete adapter
|
||||||
|
// @Param body body object.Adapter true "The details of the adapter"
|
||||||
|
// @Success 200 {object} controllers.Response The Response object
|
||||||
|
// @router /delete-adapter [post]
|
||||||
func (c *ApiController) DeleteCasbinAdapter() {
|
func (c *ApiController) DeleteCasbinAdapter() {
|
||||||
var casbinAdapter object.CasbinAdapter
|
var casbinAdapter object.CasbinAdapter
|
||||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
|
||||||
|
@ -30,9 +30,8 @@ type CasbinAdapter struct {
|
|||||||
Name string `xorm:"varchar(100) notnull pk" json:"name"`
|
Name string `xorm:"varchar(100) notnull pk" json:"name"`
|
||||||
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
||||||
|
|
||||||
Organization string `xorm:"varchar(100)" json:"organization"`
|
Type string `xorm:"varchar(100)" json:"type"`
|
||||||
Type string `xorm:"varchar(100)" json:"type"`
|
Model string `xorm:"varchar(100)" json:"model"`
|
||||||
Model string `xorm:"varchar(100)" json:"model"`
|
|
||||||
|
|
||||||
Host string `xorm:"varchar(100)" json:"host"`
|
Host string `xorm:"varchar(100)" json:"host"`
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
@ -46,14 +45,14 @@ type CasbinAdapter struct {
|
|||||||
Adapter *xormadapter.Adapter `xorm:"-" json:"-"`
|
Adapter *xormadapter.Adapter `xorm:"-" json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCasbinAdapterCount(owner, organization, field, value string) (int64, error) {
|
func GetCasbinAdapterCount(owner, field, value string) (int64, error) {
|
||||||
session := GetSession(owner, -1, -1, field, value, "", "")
|
session := GetSession(owner, -1, -1, field, value, "", "")
|
||||||
return session.Count(&CasbinAdapter{Organization: organization})
|
return session.Count(&CasbinAdapter{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCasbinAdapters(owner string, organization string) ([]*CasbinAdapter, error) {
|
func GetCasbinAdapters(owner string) ([]*CasbinAdapter, error) {
|
||||||
adapters := []*CasbinAdapter{}
|
adapters := []*CasbinAdapter{}
|
||||||
err := adapter.Engine.Where("owner = ? and organization = ?", owner, organization).Find(&adapters)
|
err := adapter.Engine.Desc("created_time").Find(&adapters, &CasbinAdapter{Owner: owner})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return adapters, err
|
return adapters, err
|
||||||
}
|
}
|
||||||
@ -61,10 +60,10 @@ func GetCasbinAdapters(owner string, organization string) ([]*CasbinAdapter, err
|
|||||||
return adapters, nil
|
return adapters, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPaginationCasbinAdapters(owner, organization string, page, limit int, field, value, sort, order string) ([]*CasbinAdapter, error) {
|
func GetPaginationCasbinAdapters(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*CasbinAdapter, error) {
|
||||||
session := GetSession(owner, page, limit, field, value, sort, order)
|
|
||||||
adapters := []*CasbinAdapter{}
|
adapters := []*CasbinAdapter{}
|
||||||
err := session.Find(&adapters, &CasbinAdapter{Organization: organization})
|
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
|
||||||
|
err := session.Find(&adapters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return adapters, err
|
return adapters, err
|
||||||
}
|
}
|
||||||
@ -214,6 +213,10 @@ func SyncPolicies(casbinAdapter *CasbinAdapter) ([]*xormadapter.CasbinRule, erro
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if modelObj == nil {
|
||||||
|
return nil, fmt.Errorf("The model: %s does not exist", util.GetId(casbinAdapter.Owner, casbinAdapter.Model))
|
||||||
|
}
|
||||||
|
|
||||||
enforcer, err := initEnforcer(modelObj, casbinAdapter)
|
enforcer, err := initEnforcer(modelObj, casbinAdapter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -402,7 +402,6 @@ func organizationChangeTrigger(oldName string, newName string) error {
|
|||||||
|
|
||||||
casbinAdapter := new(CasbinAdapter)
|
casbinAdapter := new(CasbinAdapter)
|
||||||
casbinAdapter.Owner = newName
|
casbinAdapter.Owner = newName
|
||||||
casbinAdapter.Organization = newName
|
|
||||||
_, err = session.Where("owner=?", oldName).Update(casbinAdapter)
|
_, err = session.Where("owner=?", oldName).Update(casbinAdapter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -32,7 +32,7 @@ class AdapterEditPage extends React.Component {
|
|||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
classes: props,
|
classes: props,
|
||||||
owner: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName,
|
organizationName: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName,
|
||||||
adapterName: props.match.params.adapterName,
|
adapterName: props.match.params.adapterName,
|
||||||
adapter: null,
|
adapter: null,
|
||||||
organizations: [],
|
organizations: [],
|
||||||
@ -47,7 +47,7 @@ class AdapterEditPage extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getAdapter() {
|
getAdapter() {
|
||||||
AdapterBackend.getAdapter("admin", this.state.adapterName)
|
AdapterBackend.getAdapter(this.state.organizationName, this.state.adapterName)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res.status === "ok") {
|
if (res.status === "ok") {
|
||||||
if (res.data === null) {
|
if (res.data === null) {
|
||||||
@ -59,13 +59,13 @@ class AdapterEditPage extends React.Component {
|
|||||||
adapter: res.data,
|
adapter: res.data,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.getModels(this.state.owner);
|
this.getModels(this.state.organizationName);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getOrganizations() {
|
getOrganizations() {
|
||||||
OrganizationBackend.getOrganizations(this.state.organizationName)
|
OrganizationBackend.getOrganizations("admin")
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
this.setState({
|
this.setState({
|
||||||
organizations: (res.msg === undefined) ? res : [],
|
organizations: (res.msg === undefined) ? res : [],
|
||||||
@ -114,9 +114,8 @@ class AdapterEditPage extends React.Component {
|
|||||||
{Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} :
|
{Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} :
|
||||||
</Col>
|
</Col>
|
||||||
<Col span={22} >
|
<Col span={22} >
|
||||||
<Select virtual={false} style={{width: "100%"}} disabled={!Setting.isAdminUser(this.props.account)} value={this.state.adapter.organization} onChange={(value => {
|
<Select virtual={false} style={{width: "100%"}} disabled={!Setting.isAdminUser(this.props.account)} value={this.state.adapter.owner} onChange={(value => {
|
||||||
this.getModels(value);
|
this.getModels(value);
|
||||||
this.updateAdapterField("organization", value);
|
|
||||||
this.updateAdapterField("owner", value);
|
this.updateAdapterField("owner", value);
|
||||||
})}>
|
})}>
|
||||||
{
|
{
|
||||||
@ -253,7 +252,7 @@ class AdapterEditPage extends React.Component {
|
|||||||
{Setting.getLabel(i18next.t("adapter:Policies"), i18next.t("adapter:Policies - Tooltip"))} :
|
{Setting.getLabel(i18next.t("adapter:Policies"), i18next.t("adapter:Policies - Tooltip"))} :
|
||||||
</Col>
|
</Col>
|
||||||
<Col span={22}>
|
<Col span={22}>
|
||||||
<PolicyTable owner={this.state.owner} name={this.state.adapterName} mode={this.state.mode} />
|
<PolicyTable owner={this.state.organizationName} name={this.state.adapterName} mode={this.state.mode} />
|
||||||
</Col>
|
</Col>
|
||||||
</Row>
|
</Row>
|
||||||
<Row style={{marginTop: "20px"}} >
|
<Row style={{marginTop: "20px"}} >
|
||||||
@ -272,7 +271,7 @@ class AdapterEditPage extends React.Component {
|
|||||||
|
|
||||||
submitAdapterEdit(willExist) {
|
submitAdapterEdit(willExist) {
|
||||||
const adapter = Setting.deepCopy(this.state.adapter);
|
const adapter = Setting.deepCopy(this.state.adapter);
|
||||||
AdapterBackend.updateAdapter(this.state.owner, this.state.adapterName, adapter)
|
AdapterBackend.updateAdapter(this.state.organizationName, this.state.adapterName, adapter)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res.status === "ok") {
|
if (res.status === "ok") {
|
||||||
Setting.showMessage("success", i18next.t("general:Successfully saved"));
|
Setting.showMessage("success", i18next.t("general:Successfully saved"));
|
||||||
@ -283,7 +282,7 @@ class AdapterEditPage extends React.Component {
|
|||||||
if (willExist) {
|
if (willExist) {
|
||||||
this.props.history.push("/adapters");
|
this.props.history.push("/adapters");
|
||||||
} else {
|
} else {
|
||||||
this.props.history.push(`/adapters/${this.state.owner}/${this.state.adapter.name}`);
|
this.props.history.push(`/adapters/${this.state.organizationName}/${this.state.adapter.name}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`);
|
Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`);
|
||||||
|
@ -26,10 +26,9 @@ class AdapterListPage extends BaseListPage {
|
|||||||
newAdapter() {
|
newAdapter() {
|
||||||
const randomName = Setting.getRandomName();
|
const randomName = Setting.getRandomName();
|
||||||
return {
|
return {
|
||||||
owner: "admin",
|
owner: this.props.account.owner,
|
||||||
name: `adapter_${randomName}`,
|
name: `adapter_${randomName}`,
|
||||||
createdTime: moment().format(),
|
createdTime: moment().format(),
|
||||||
organization: this.props.account.owner,
|
|
||||||
type: "Database",
|
type: "Database",
|
||||||
host: "localhost",
|
host: "localhost",
|
||||||
port: 3306,
|
port: 3306,
|
||||||
@ -47,7 +46,7 @@ class AdapterListPage extends BaseListPage {
|
|||||||
AdapterBackend.addAdapter(newAdapter)
|
AdapterBackend.addAdapter(newAdapter)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res.status === "ok") {
|
if (res.status === "ok") {
|
||||||
this.props.history.push({pathname: `/adapters/${newAdapter.organization}/${newAdapter.name}`, mode: "add"});
|
this.props.history.push({pathname: `/adapters/${newAdapter.owner}/${newAdapter.name}`, mode: "add"});
|
||||||
Setting.showMessage("success", i18next.t("general:Successfully added"));
|
Setting.showMessage("success", i18next.t("general:Successfully added"));
|
||||||
} else {
|
} else {
|
||||||
Setting.showMessage("error", `${i18next.t("general:Failed to add")}: ${res.msg}`);
|
Setting.showMessage("error", `${i18next.t("general:Failed to add")}: ${res.msg}`);
|
||||||
@ -96,11 +95,11 @@ class AdapterListPage extends BaseListPage {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: i18next.t("general:Organization"),
|
title: i18next.t("general:Organization"),
|
||||||
dataIndex: "organization",
|
dataIndex: "owner",
|
||||||
key: "organization",
|
key: "owner",
|
||||||
width: "120px",
|
width: "120px",
|
||||||
sorter: true,
|
sorter: true,
|
||||||
...this.getColumnSearchProps("organization"),
|
...this.getColumnSearchProps("owner"),
|
||||||
render: (text, record, index) => {
|
render: (text, record, index) => {
|
||||||
return (
|
return (
|
||||||
<Link to={`/organizations/${text}`}>
|
<Link to={`/organizations/${text}`}>
|
||||||
@ -247,7 +246,7 @@ class AdapterListPage extends BaseListPage {
|
|||||||
value = params.type;
|
value = params.type;
|
||||||
}
|
}
|
||||||
this.setState({loading: true});
|
this.setState({loading: true});
|
||||||
AdapterBackend.getAdapters("admin", Setting.isAdminUser(this.props.account) ? "" : this.props.account.owner, params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder)
|
AdapterBackend.getAdapters(Setting.isAdminUser(this.props.account) ? "" : this.props.account.owner, params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
this.setState({
|
this.setState({
|
||||||
loading: false,
|
loading: false,
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
|
|
||||||
import * as Setting from "../Setting";
|
import * as Setting from "../Setting";
|
||||||
|
|
||||||
export function getAdapters(owner, organization, page = "", pageSize = "", field = "", value = "", sortField = "", sortOrder = "") {
|
export function getAdapters(owner, page = "", pageSize = "", field = "", value = "", sortField = "", sortOrder = "") {
|
||||||
return fetch(`${Setting.ServerUrl}/api/get-adapters?owner=${owner}&organization=${organization}&p=${page}&pageSize=${pageSize}&field=${field}&value=${value}&sortField=${sortField}&sortOrder=${sortOrder}`, {
|
return fetch(`${Setting.ServerUrl}/api/get-adapters?owner=${owner}&p=${page}&pageSize=${pageSize}&field=${field}&value=${value}&sortField=${sortField}&sortOrder=${sortOrder}`, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
headers: {
|
headers: {
|
||||||
|
Reference in New Issue
Block a user