2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2020-12-20 20:31:48 +08:00
|
|
|
//
|
|
|
|
// 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 controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-09-29 19:44:08 +08:00
|
|
|
"github.com/beego/beego/utils/pagination"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
2020-12-20 20:31:48 +08:00
|
|
|
)
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetOrganizations ...
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title GetOrganizations
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Organization API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description get organizations
|
|
|
|
// @Param owner query string true "owner"
|
|
|
|
// @Success 200 {array} object.Organization The Response object
|
|
|
|
// @router /get-organizations [get]
|
2020-12-20 20:31:48 +08:00
|
|
|
func (c *ApiController) GetOrganizations() {
|
|
|
|
owner := c.Input().Get("owner")
|
2021-11-06 11:32:22 +08:00
|
|
|
limit := c.Input().Get("pageSize")
|
|
|
|
page := c.Input().Get("p")
|
2021-12-25 10:55:10 +08:00
|
|
|
field := c.Input().Get("field")
|
|
|
|
value := c.Input().Get("value")
|
|
|
|
sortField := c.Input().Get("sortField")
|
|
|
|
sortOrder := c.Input().Get("sortOrder")
|
2021-11-06 11:32:22 +08:00
|
|
|
if limit == "" || page == "" {
|
2021-11-06 21:14:53 +08:00
|
|
|
c.Data["json"] = object.GetMaskedOrganizations(object.GetOrganizations(owner))
|
2021-11-06 11:32:22 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
} else {
|
|
|
|
limit := util.ParseInt(limit)
|
2021-12-25 10:55:10 +08:00
|
|
|
paginator := pagination.SetPaginator(c.Ctx, limit, int64(object.GetOrganizationCount(owner, field, value)))
|
|
|
|
organizations := object.GetMaskedOrganizations(object.GetPaginationOrganizations(owner, paginator.Offset(), limit, field, value, sortField, sortOrder))
|
2021-11-06 11:32:22 +08:00
|
|
|
c.ResponseOk(organizations, paginator.Nums())
|
|
|
|
}
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetOrganization ...
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title GetOrganization
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Organization API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description get organization
|
|
|
|
// @Param id query string true "organization id"
|
|
|
|
// @Success 200 {object} object.Organization The Response object
|
|
|
|
// @router /get-organization [get]
|
2020-12-20 20:31:48 +08:00
|
|
|
func (c *ApiController) GetOrganization() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
2021-11-06 21:14:53 +08:00
|
|
|
c.Data["json"] = object.GetMaskedOrganization(object.GetOrganization(id))
|
2020-12-20 20:31:48 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// UpdateOrganization ...
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title UpdateOrganization
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Organization API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description update organization
|
2023-02-10 10:42:16 +08:00
|
|
|
// @Param id query string true "The id ( owner/name ) of the organization"
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Param body body object.Organization true "The details of the organization"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /update-organization [post]
|
2020-12-20 20:31:48 +08:00
|
|
|
func (c *ApiController) UpdateOrganization() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
|
|
|
var organization object.Organization
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &organization)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-28 00:48:34 +08:00
|
|
|
c.Data["json"] = wrapActionResponse(object.UpdateOrganization(id, &organization))
|
2020-12-20 20:31:48 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// AddOrganization ...
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title AddOrganization
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Organization API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description add organization
|
|
|
|
// @Param body body object.Organization true "The details of the organization"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /add-organization [post]
|
2020-12-20 20:31:48 +08:00
|
|
|
func (c *ApiController) AddOrganization() {
|
|
|
|
var organization object.Organization
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &organization)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2022-11-29 11:01:41 +08:00
|
|
|
count := object.GetOrganizationCount("", "", "")
|
|
|
|
if err := checkQuotaForOrganization(count); err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-28 00:48:34 +08:00
|
|
|
c.Data["json"] = wrapActionResponse(object.AddOrganization(&organization))
|
2020-12-20 20:31:48 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// DeleteOrganization ...
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title DeleteOrganization
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Organization API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description delete organization
|
|
|
|
// @Param body body object.Organization true "The details of the organization"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /delete-organization [post]
|
2020-12-20 20:31:48 +08:00
|
|
|
func (c *ApiController) DeleteOrganization() {
|
|
|
|
var organization object.Organization
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &organization)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2020-12-20 20:31:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-28 00:48:34 +08:00
|
|
|
c.Data["json"] = wrapActionResponse(object.DeleteOrganization(&organization))
|
2020-12-20 20:31:48 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
2022-09-10 20:41:45 +08:00
|
|
|
|
|
|
|
// GetDefaultApplication ...
|
|
|
|
// @Title GetDefaultApplication
|
|
|
|
// @Tag Organization API
|
|
|
|
// @Description get default application
|
|
|
|
// @Param id query string true "organization id"
|
|
|
|
// @Success 200 {object} Response The Response object
|
|
|
|
// @router /get-default-application [get]
|
|
|
|
func (c *ApiController) GetDefaultApplication() {
|
|
|
|
userId := c.GetSessionUsername()
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
2022-10-09 10:39:33 +08:00
|
|
|
application, err := object.GetDefaultApplication(id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
2022-09-10 20:41:45 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-09 10:39:33 +08:00
|
|
|
maskedApplication := object.GetMaskedApplication(application, userId)
|
|
|
|
c.ResponseOk(maskedApplication)
|
2022-09-10 20:41:45 +08:00
|
|
|
}
|