casdoor/controllers/application.go

268 lines
7.9 KiB
Go
Raw Permalink Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2020-12-20 23:24:09 +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-03-07 15:15:59 +08:00
"fmt"
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 23:24:09 +08:00
)
2021-08-07 22:02:56 +08:00
// GetApplications
// @Title GetApplications
// @Tag Application API
// @Description get all applications
// @Param owner query string true "The owner of applications."
// @Success 200 {array} object.Application The Response object
// @router /get-applications [get]
2020-12-20 23:24:09 +08:00
func (c *ApiController) GetApplications() {
userId := c.GetSessionUsername()
2020-12-20 23:24:09 +08:00
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")
organization := c.Input().Get("organization")
var err error
if limit == "" || page == "" {
var applications []*object.Application
if organization == "" {
applications, err = object.GetApplications(owner)
} else {
applications, err = object.GetOrganizationApplications(owner, organization)
}
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(object.GetMaskedApplications(applications, userId))
} else {
limit := util.ParseInt(limit)
count, err := object.GetApplicationCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}
paginator := pagination.SetPaginator(c.Ctx, limit, count)
2023-09-01 21:16:51 +08:00
application, err := object.GetPaginationApplications(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}
2023-09-01 21:16:51 +08:00
applications := object.GetMaskedApplications(application, userId)
c.ResponseOk(applications, paginator.Nums())
}
2020-12-20 23:24:09 +08:00
}
2021-08-07 22:02:56 +08:00
// GetApplication
// @Title GetApplication
// @Tag Application API
// @Description get the detail of an application
// @Param id query string true "The id ( owner/name ) of the application."
// @Success 200 {object} object.Application The Response object
// @router /get-application [get]
2020-12-20 23:24:09 +08:00
func (c *ApiController) GetApplication() {
userId := c.GetSessionUsername()
2020-12-20 23:24:09 +08:00
id := c.Input().Get("id")
2023-09-01 21:16:51 +08:00
application, err := object.GetApplication(id)
if err != nil {
c.ResponseError(err.Error())
return
}
2020-12-20 23:24:09 +08:00
if c.Input().Get("withKey") != "" && application != nil && application.Cert != "" {
2023-09-01 21:16:51 +08:00
cert, err := object.GetCert(util.GetId(application.Owner, application.Cert))
if err != nil {
c.ResponseError(err.Error())
return
}
if cert == nil {
cert, err = object.GetCert(util.GetId(application.Organization, application.Cert))
if err != nil {
c.ResponseError(err.Error())
return
}
}
if cert != nil {
application.CertPublicKey = cert.Certificate
}
2023-09-01 21:16:51 +08:00
}
c.ResponseOk(object.GetMaskedApplication(application, userId))
2020-12-20 23:24:09 +08:00
}
2021-08-07 22:02:56 +08:00
// GetUserApplication
// @Title GetUserApplication
// @Tag Application API
// @Description get the detail of the user's application
// @Param id query string true "The id ( owner/name ) of the user"
2021-04-19 01:14:41 +08:00
// @Success 200 {object} object.Application The Response object
// @router /get-user-application [get]
func (c *ApiController) GetUserApplication() {
userId := c.GetSessionUsername()
id := c.Input().Get("id")
user, err := object.GetUser(id)
if err != nil {
c.ResponseError(err.Error())
return
}
if user == nil {
2023-01-06 20:12:32 +08:00
c.ResponseError(fmt.Sprintf(c.T("general:The user: %s doesn't exist"), id))
2021-05-16 23:07:45 +08:00
return
}
application, err := object.GetApplicationByUser(user)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(object.GetMaskedApplication(application, userId))
2021-04-19 01:14:41 +08:00
}
// GetOrganizationApplications
// @Title GetOrganizationApplications
// @Tag Application API
// @Description get the detail of the organization's application
// @Param organization query string true "The organization name"
// @Success 200 {array} object.Application The Response object
// @router /get-organization-applications [get]
func (c *ApiController) GetOrganizationApplications() {
userId := c.GetSessionUsername()
organization := c.Input().Get("organization")
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 organization == "" {
2023-01-17 22:57:05 +08:00
c.ResponseError(c.T("general:Missing parameter") + ": organization")
return
}
if limit == "" || page == "" {
applications, err := object.GetOrganizationApplications(owner, organization)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(object.GetMaskedApplications(applications, userId))
} else {
limit := util.ParseInt(limit)
count, err := object.GetOrganizationApplicationCount(owner, organization, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}
paginator := pagination.SetPaginator(c.Ctx, limit, count)
2023-09-01 21:16:51 +08:00
application, err := object.GetPaginationOrganizationApplications(owner, organization, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}
2023-09-01 21:16:51 +08:00
applications := object.GetMaskedApplications(application, userId)
c.ResponseOk(applications, paginator.Nums())
}
}
2021-08-07 22:02:56 +08:00
// UpdateApplication
// @Title UpdateApplication
// @Tag Application API
// @Description update an application
// @Param id query string true "The id ( owner/name ) of the application"
// @Param body body object.Application true "The details of the application"
// @Success 200 {object} controllers.Response The Response object
// @router /update-application [post]
2020-12-20 23:24:09 +08:00
func (c *ApiController) UpdateApplication() {
id := c.Input().Get("id")
var application object.Application
err := json.Unmarshal(c.Ctx.Input.RequestBody, &application)
if err != nil {
c.ResponseError(err.Error())
return
2020-12-20 23:24:09 +08:00
}
2021-03-28 00:48:34 +08:00
c.Data["json"] = wrapActionResponse(object.UpdateApplication(id, &application))
2020-12-20 23:24:09 +08:00
c.ServeJSON()
}
2021-08-07 22:02:56 +08:00
// AddApplication
// @Title AddApplication
// @Tag Application API
// @Description add an application
// @Param body body object.Application true "The details of the application"
// @Success 200 {object} controllers.Response The Response object
// @router /add-application [post]
2020-12-20 23:24:09 +08:00
func (c *ApiController) AddApplication() {
var application object.Application
err := json.Unmarshal(c.Ctx.Input.RequestBody, &application)
if err != nil {
c.ResponseError(err.Error())
return
2020-12-20 23:24:09 +08:00
}
count, err := object.GetApplicationCount("", "", "")
if err != nil {
c.ResponseError(err.Error())
return
}
if err := checkQuotaForApplication(int(count)); err != nil {
c.ResponseError(err.Error())
return
}
2021-03-28 00:48:34 +08:00
c.Data["json"] = wrapActionResponse(object.AddApplication(&application))
2020-12-20 23:24:09 +08:00
c.ServeJSON()
}
2021-08-07 22:02:56 +08:00
// DeleteApplication
// @Title DeleteApplication
// @Tag Application API
// @Description delete an application
// @Param body body object.Application true "The details of the application"
// @Success 200 {object} controllers.Response The Response object
// @router /delete-application [post]
2020-12-20 23:24:09 +08:00
func (c *ApiController) DeleteApplication() {
var application object.Application
err := json.Unmarshal(c.Ctx.Input.RequestBody, &application)
if err != nil {
c.ResponseError(err.Error())
return
2020-12-20 23:24:09 +08:00
}
2021-03-28 00:48:34 +08:00
c.Data["json"] = wrapActionResponse(object.DeleteApplication(&application))
2020-12-20 23:24:09 +08:00
c.ServeJSON()
}