2021-08-15 00:17:53 +08:00
|
|
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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 (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-09-05 01:03:29 +08:00
|
|
|
"mime"
|
2021-08-15 00:17:53 +08:00
|
|
|
"path/filepath"
|
|
|
|
|
2021-11-06 11:32:22 +08:00
|
|
|
"github.com/astaxie/beego/utils/pagination"
|
2021-08-15 00:17:53 +08:00
|
|
|
"github.com/casbin/casdoor/object"
|
|
|
|
"github.com/casbin/casdoor/util"
|
|
|
|
)
|
|
|
|
|
2021-12-03 20:42:36 +08:00
|
|
|
// @router /get-resources [get]
|
|
|
|
// @Tag Resource API
|
|
|
|
// @Title GetResources
|
2021-08-15 00:17:53 +08:00
|
|
|
func (c *ApiController) GetResources() {
|
|
|
|
owner := c.Input().Get("owner")
|
2021-09-06 00:49:10 +08:00
|
|
|
user := c.Input().Get("user")
|
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 == "" {
|
|
|
|
c.Data["json"] = object.GetResources(owner, user)
|
|
|
|
c.ServeJSON()
|
|
|
|
} else {
|
|
|
|
limit := util.ParseInt(limit)
|
2021-12-25 10:55:10 +08:00
|
|
|
paginator := pagination.SetPaginator(c.Ctx, limit, int64(object.GetResourceCount(owner, user, field, value)))
|
|
|
|
resources := object.GetPaginationResources(owner, user, paginator.Offset(), limit, field, value, sortField, sortOrder)
|
2021-11-06 11:32:22 +08:00
|
|
|
c.ResponseOk(resources, paginator.Nums())
|
|
|
|
}
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
|
|
|
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Resource API
|
|
|
|
// @Title GetResource
|
|
|
|
// @router /get-resource [get]
|
2021-08-15 00:17:53 +08:00
|
|
|
func (c *ApiController) GetResource() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
|
|
|
c.Data["json"] = object.GetResource(id)
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Resource API
|
|
|
|
// @Title UpdateResource
|
|
|
|
// @router /update-resource [post]
|
2021-08-15 00:17:53 +08:00
|
|
|
func (c *ApiController) UpdateResource() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
|
|
|
var resource object.Resource
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &resource)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.UpdateResource(id, &resource))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Resource API
|
|
|
|
// @Title AddResource
|
|
|
|
// @router /add-resource [post]
|
2021-08-15 00:17:53 +08:00
|
|
|
func (c *ApiController) AddResource() {
|
|
|
|
var resource object.Resource
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &resource)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.AddResource(&resource))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Resource API
|
|
|
|
// @Title DeleteResource
|
|
|
|
// @router /delete-resource [post]
|
2021-08-15 01:09:51 +08:00
|
|
|
func (c *ApiController) DeleteResource() {
|
2021-08-15 00:17:53 +08:00
|
|
|
var resource object.Resource
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &resource)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-09-05 09:44:15 +08:00
|
|
|
provider, _, ok := c.GetProviderFromContext("Storage")
|
2021-08-15 01:09:51 +08:00
|
|
|
if !ok {
|
2021-08-15 00:41:51 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-04 15:02:11 +08:00
|
|
|
err = object.DeleteFile(provider, resource.Name)
|
2021-08-15 00:41:51 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-15 00:17:53 +08:00
|
|
|
c.Data["json"] = wrapActionResponse(object.DeleteResource(&resource))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Resource API
|
|
|
|
// @Title UploadResource
|
|
|
|
// @router /upload-resource [post]
|
2021-08-15 00:17:53 +08:00
|
|
|
func (c *ApiController) UploadResource() {
|
|
|
|
owner := c.Input().Get("owner")
|
2021-09-05 23:46:38 +08:00
|
|
|
username := c.Input().Get("user")
|
2021-09-04 16:50:26 +08:00
|
|
|
application := c.Input().Get("application")
|
2021-08-15 00:17:53 +08:00
|
|
|
tag := c.Input().Get("tag")
|
|
|
|
parent := c.Input().Get("parent")
|
|
|
|
fullFilePath := c.Input().Get("fullFilePath")
|
2021-11-21 16:21:35 +08:00
|
|
|
createdTime := c.Input().Get("createdTime")
|
|
|
|
description := c.Input().Get("description")
|
2021-08-15 00:17:53 +08:00
|
|
|
|
|
|
|
file, header, err := c.GetFile("file")
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-09-05 00:22:08 +08:00
|
|
|
defer file.Close()
|
2021-08-15 00:17:53 +08:00
|
|
|
|
2021-11-20 15:35:33 +08:00
|
|
|
if username == "" || fullFilePath == "" {
|
|
|
|
c.ResponseError(fmt.Sprintf("username or fullFilePath is empty: username = %s, fullFilePath = %s", username, fullFilePath))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-15 00:17:53 +08:00
|
|
|
filename := filepath.Base(fullFilePath)
|
|
|
|
fileBuffer := bytes.NewBuffer(nil)
|
|
|
|
if _, err = io.Copy(fileBuffer, file); err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 09:44:15 +08:00
|
|
|
provider, user, ok := c.GetProviderFromContext("Storage")
|
2021-08-15 01:09:51 +08:00
|
|
|
if !ok {
|
2021-08-15 00:17:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-15 00:41:51 +08:00
|
|
|
fileType := "unknown"
|
|
|
|
contentType := header.Header.Get("Content-Type")
|
2021-09-05 01:03:29 +08:00
|
|
|
fileType, _ = util.GetOwnerAndNameFromId(contentType)
|
|
|
|
|
|
|
|
if fileType != "image" && fileType != "video" {
|
|
|
|
ext := filepath.Ext(filename)
|
|
|
|
mimeType := mime.TypeByExtension(ext)
|
|
|
|
fileType, _ = util.GetOwnerAndNameFromId(mimeType)
|
2021-08-15 00:41:51 +08:00
|
|
|
}
|
|
|
|
|
2021-11-20 16:21:15 +08:00
|
|
|
fileUrl, objectKey, err := object.UploadFileSafe(provider, fullFilePath, fileBuffer)
|
2021-08-15 00:17:53 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:21:35 +08:00
|
|
|
if createdTime == "" {
|
|
|
|
createdTime = util.GetCurrentTime()
|
|
|
|
}
|
2021-08-15 00:17:53 +08:00
|
|
|
fileFormat := filepath.Ext(fullFilePath)
|
|
|
|
fileSize := int(header.Size)
|
|
|
|
resource := &object.Resource{
|
|
|
|
Owner: owner,
|
2021-09-04 15:02:11 +08:00
|
|
|
Name: objectKey,
|
2021-11-21 16:21:35 +08:00
|
|
|
CreatedTime: createdTime,
|
2021-09-05 23:46:38 +08:00
|
|
|
User: username,
|
2021-08-15 01:09:51 +08:00
|
|
|
Provider: provider.Name,
|
2021-09-04 16:50:26 +08:00
|
|
|
Application: application,
|
2021-08-15 00:17:53 +08:00
|
|
|
Tag: tag,
|
|
|
|
Parent: parent,
|
2021-09-04 15:02:11 +08:00
|
|
|
FileName: filename,
|
2021-08-15 00:17:53 +08:00
|
|
|
FileType: fileType,
|
|
|
|
FileFormat: fileFormat,
|
|
|
|
FileSize: fileSize,
|
|
|
|
Url: fileUrl,
|
2021-11-21 16:21:35 +08:00
|
|
|
Description: description,
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
|
|
|
object.AddOrUpdateResource(resource)
|
|
|
|
|
|
|
|
switch tag {
|
|
|
|
case "avatar":
|
2021-09-05 01:03:29 +08:00
|
|
|
if user == nil {
|
2021-11-20 15:46:54 +08:00
|
|
|
user = object.GetUserNoCheck(username)
|
2021-11-20 15:35:33 +08:00
|
|
|
if user == nil {
|
|
|
|
c.ResponseError("user is nil for tag: \"avatar\"")
|
|
|
|
return
|
|
|
|
}
|
2021-09-05 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
2021-08-15 00:17:53 +08:00
|
|
|
user.Avatar = fileUrl
|
2022-01-13 23:19:36 +08:00
|
|
|
object.UpdateUser(user.GetId(), user, []string{"avatar"}, false)
|
2021-08-15 00:17:53 +08:00
|
|
|
case "termsOfUse":
|
|
|
|
applicationId := fmt.Sprintf("admin/%s", parent)
|
|
|
|
app := object.GetApplication(applicationId)
|
|
|
|
app.TermsOfUse = fileUrl
|
|
|
|
object.UpdateApplication(applicationId, app)
|
|
|
|
}
|
|
|
|
|
2021-09-04 15:02:11 +08:00
|
|
|
c.ResponseOk(fileUrl, objectKey)
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|