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"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/casbin/casdoor/object"
|
|
|
|
"github.com/casbin/casdoor/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *ApiController) GetResources() {
|
|
|
|
owner := c.Input().Get("owner")
|
|
|
|
|
|
|
|
c.Data["json"] = object.GetResources(owner)
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) GetResource() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
|
|
|
c.Data["json"] = object.GetResource(id)
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-15 01:09:51 +08:00
|
|
|
func (c *ApiController) GetProviderParam() (*object.Provider, *object.User, bool) {
|
|
|
|
providerName := c.Input().Get("provider")
|
|
|
|
if providerName != "" {
|
|
|
|
provider := object.GetProvider(util.GetId(providerName))
|
|
|
|
if provider == nil {
|
|
|
|
c.ResponseError(fmt.Sprintf("The provider: %s is not found", providerName))
|
|
|
|
return nil, nil, false
|
|
|
|
}
|
|
|
|
return provider, nil, true
|
|
|
|
}
|
|
|
|
|
2021-08-15 00:41:51 +08:00
|
|
|
userId, ok := c.RequireSignedIn()
|
|
|
|
if !ok {
|
2021-08-15 01:09:51 +08:00
|
|
|
return nil, nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
user := object.GetUser(userId)
|
|
|
|
application := object.GetApplicationByUser(user)
|
|
|
|
provider := application.GetStorageProvider()
|
|
|
|
if provider == nil {
|
|
|
|
c.ResponseError(fmt.Sprintf("No storage provider is found for application: %s", application.Name))
|
|
|
|
return nil, nil, false
|
2021-08-15 00:41:51 +08:00
|
|
|
}
|
2021-08-15 01:09:51 +08:00
|
|
|
return provider, user, true
|
|
|
|
}
|
2021-08-15 00:41:51 +08:00
|
|
|
|
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-08-15 01:09:51 +08:00
|
|
|
provider, _, ok := c.GetProviderParam()
|
|
|
|
if !ok {
|
2021-08-15 00:41:51 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = object.DeleteFile(provider, resource.ObjectKey)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) UploadResource() {
|
|
|
|
owner := c.Input().Get("owner")
|
|
|
|
tag := c.Input().Get("tag")
|
|
|
|
parent := c.Input().Get("parent")
|
|
|
|
fullFilePath := c.Input().Get("fullFilePath")
|
|
|
|
|
|
|
|
file, header, err := c.GetFile("file")
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Base(fullFilePath)
|
|
|
|
fileBuffer := bytes.NewBuffer(nil)
|
|
|
|
if _, err = io.Copy(fileBuffer, file); err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-15 01:09:51 +08:00
|
|
|
provider, user, ok := c.GetProviderParam()
|
|
|
|
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")
|
|
|
|
if strings.HasPrefix(contentType, "image/") {
|
|
|
|
fileType = "image"
|
|
|
|
} else if strings.HasPrefix(contentType, "video/") {
|
|
|
|
fileType = "video"
|
|
|
|
}
|
|
|
|
|
2021-08-15 00:17:53 +08:00
|
|
|
fileUrl, objectKey, err := object.UploadFile(provider, fullFilePath, fileBuffer)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileFormat := filepath.Ext(fullFilePath)
|
|
|
|
fileSize := int(header.Size)
|
|
|
|
resource := &object.Resource{
|
|
|
|
Owner: owner,
|
|
|
|
Name: filename,
|
|
|
|
CreatedTime: util.GetCurrentTime(),
|
2021-08-15 01:09:51 +08:00
|
|
|
Provider: provider.Name,
|
2021-08-15 00:17:53 +08:00
|
|
|
Tag: tag,
|
|
|
|
Parent: parent,
|
|
|
|
FileType: fileType,
|
|
|
|
FileFormat: fileFormat,
|
|
|
|
FileSize: fileSize,
|
|
|
|
Url: fileUrl,
|
|
|
|
ObjectKey: objectKey,
|
|
|
|
}
|
|
|
|
object.AddOrUpdateResource(resource)
|
|
|
|
|
|
|
|
switch tag {
|
|
|
|
case "avatar":
|
|
|
|
user.Avatar = fileUrl
|
|
|
|
object.UpdateUser(user.GetId(), user)
|
|
|
|
case "termsOfUse":
|
|
|
|
applicationId := fmt.Sprintf("admin/%s", parent)
|
|
|
|
app := object.GetApplication(applicationId)
|
|
|
|
app.TermsOfUse = fileUrl
|
|
|
|
object.UpdateApplication(applicationId, app)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ResponseOk(fileUrl)
|
|
|
|
}
|