2021-03-13 23:06:03 +08:00
|
|
|
// Copyright 2021 The casbin 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"
|
2021-11-06 11:32:22 +08:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/utils/pagination"
|
2021-07-25 09:34:25 +08:00
|
|
|
"github.com/casbin/casdoor/object"
|
2021-11-06 11:32:22 +08:00
|
|
|
"github.com/casbin/casdoor/util"
|
2020-12-20 23:24:09 +08:00
|
|
|
)
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetApplications
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title GetApplications
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Application API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @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() {
|
|
|
|
owner := c.Input().Get("owner")
|
2021-11-06 11:32:22 +08:00
|
|
|
limit := c.Input().Get("pageSize")
|
|
|
|
page := c.Input().Get("p")
|
|
|
|
if limit == "" || page == "" {
|
|
|
|
c.Data["json"] = object.GetApplications(owner)
|
|
|
|
c.ServeJSON()
|
|
|
|
} else {
|
|
|
|
limit := util.ParseInt(limit)
|
|
|
|
paginator := pagination.SetPaginator(c.Ctx, limit, int64(object.GetApplicationCount(owner)))
|
|
|
|
applications := object.GetPaginationApplications(owner, paginator.Offset(), limit)
|
|
|
|
c.ResponseOk(applications, paginator.Nums())
|
|
|
|
}
|
2020-12-20 23:24:09 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetApplication
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title GetApplication
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Application API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description get the detail of an application
|
|
|
|
// @Param id query string true "The id 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() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
|
|
|
c.Data["json"] = object.GetApplication(id)
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetUserApplication
|
2021-07-11 23:49:06 +08:00
|
|
|
// @Title GetUserApplication
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Application API
|
2021-07-11 23:49:06 +08:00
|
|
|
// @Description get the detail of the user's application
|
|
|
|
// @Param id query string true "The id of the user"
|
2021-04-19 01:14:41 +08:00
|
|
|
// @Success 200 {object} object.Application The Response object
|
2021-07-11 23:49:06 +08:00
|
|
|
// @router /get-user-application [get]
|
|
|
|
func (c *ApiController) GetUserApplication() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
user := object.GetUser(id)
|
|
|
|
if user == nil {
|
|
|
|
c.ResponseError("No such user.")
|
2021-05-16 23:07:45 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = object.GetApplicationByUser(user)
|
2021-04-19 01:14:41 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// UpdateApplication
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title UpdateApplication
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Application API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description update an application
|
|
|
|
// @Param id query string true "The id 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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
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
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title AddApplication
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Application API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
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
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title DeleteApplication
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Application API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|