casdoor/controllers/organization.go

151 lines
4.9 KiB
Go
Raw Normal View History

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 ...
// @Title GetOrganizations
// @Tag Organization API
// @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")
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")
if limit == "" || page == "" {
2021-11-06 21:14:53 +08:00
c.Data["json"] = object.GetMaskedOrganizations(object.GetOrganizations(owner))
c.ServeJSON()
} else {
limit := util.ParseInt(limit)
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))
c.ResponseOk(organizations, paginator.Nums())
}
2020-12-20 20:31:48 +08:00
}
2021-08-07 22:02:56 +08:00
// GetOrganization ...
// @Title GetOrganization
// @Tag Organization API
// @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 ...
// @Title UpdateOrganization
// @Tag Organization API
// @Description update organization
// @Param id query string true "The id of the organization"
// @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 {
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 ...
// @Title AddOrganization
// @Tag Organization API
// @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 {
c.ResponseError(err.Error())
return
2020-12-20 20:31:48 +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 ...
// @Title DeleteOrganization
// @Tag Organization API
// @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 {
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()
}
// 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())
return
}
2022-10-09 10:39:33 +08:00
maskedApplication := object.GetMaskedApplication(application, userId)
c.ResponseOk(maskedApplication)
}