mindoc/controllers/TemplateController.go

188 lines
4.7 KiB
Go
Raw Normal View History

2018-08-13 19:05:49 +08:00
package controllers
import (
2018-11-13 17:33:13 +08:00
"errors"
2018-08-13 19:05:49 +08:00
"strings"
"github.com/beego/beego/v2/client/orm"
2021-04-21 13:37:47 +08:00
"github.com/beego/i18n"
"github.com/mindoc-org/mindoc/conf"
"github.com/mindoc-org/mindoc/models"
2018-08-13 19:05:49 +08:00
)
type TemplateController struct {
BaseController
BookId int
}
2021-04-21 13:37:47 +08:00
func (c *TemplateController) isPermission() error {
2018-08-13 19:05:49 +08:00
c.Prepare()
bookIdentify := c.GetString("identify", "")
2018-08-13 19:05:49 +08:00
if bookIdentify == "" {
2021-04-21 13:37:47 +08:00
return errors.New(i18n.Tr(c.Lang, "message.param_error"))
}
2018-08-13 19:05:49 +08:00
if !c.Member.IsAdministrator() {
2018-11-13 17:33:13 +08:00
book, err := models.NewBookResult().FindByIdentify(bookIdentify, c.Member.MemberId)
if err != nil {
if err == orm.ErrNoRows {
return errors.New("项目不存在或没有权限")
2018-08-13 19:05:49 +08:00
}
return errors.New("查询项目模板失败")
}
c.BookId = book.BookId
} else {
book, err := models.NewBook().FindByIdentify(bookIdentify, "book_id")
if err != nil {
if err == orm.ErrNoRows {
return errors.New("项目不存在或没有权限")
2018-08-13 19:05:49 +08:00
}
return errors.New("查询项目模板失败")
2018-08-13 19:05:49 +08:00
}
c.BookId = book.BookId
2018-08-13 19:05:49 +08:00
}
return nil
}
2021-04-21 13:37:47 +08:00
//获取指定模板信息
func (c *TemplateController) Get() {
2021-04-21 13:37:47 +08:00
if err := c.isPermission(); err != nil {
c.JsonResult(500, err.Error())
}
2021-04-21 13:37:47 +08:00
templateId, _ := c.GetInt("template_id", 0)
2021-04-21 13:37:47 +08:00
template, err := models.NewTemplate().Find(templateId)
if err != nil {
2021-04-21 13:37:47 +08:00
c.JsonResult(500, "读取模板失败")
}
if template.IsGlobal == 0 && template.BookId != c.BookId {
2021-04-21 13:37:47 +08:00
c.JsonResult(404, "模板不存在或已删除")
}
2021-04-21 13:37:47 +08:00
c.JsonResult(0, "OK", template)
2018-08-13 19:05:49 +08:00
}
//获取模板列表
func (c *TemplateController) List() {
c.TplName = "template/list.tpl"
2021-04-21 13:37:47 +08:00
if err := c.isPermission(); err != nil {
c.Data["ErrorMessage"] = err.Error()
return
2018-08-13 19:05:49 +08:00
}
2021-04-21 13:37:47 +08:00
templateList, err := models.NewTemplate().FindAllByBookId(c.BookId)
2018-08-13 19:05:49 +08:00
2021-04-21 13:37:47 +08:00
if err != nil && err != orm.ErrNoRows {
c.Data["ErrorMessage"] = "查询项目模板失败"
}
if templateList != nil {
2021-04-21 13:37:47 +08:00
for i, t := range templateList {
templateList[i] = t.Preload()
2018-08-13 19:05:49 +08:00
}
}
//c.JsonResult(0,"OK",templateList)
c.Data["List"] = templateList
2018-08-13 19:05:49 +08:00
}
//添加模板
2018-08-13 19:05:49 +08:00
func (c *TemplateController) Add() {
2021-04-21 13:37:47 +08:00
if err := c.isPermission(); err != nil {
c.JsonResult(500, err.Error())
2018-08-13 19:05:49 +08:00
}
2021-04-21 13:37:47 +08:00
templateId, _ := c.GetInt("template_id", 0)
2018-08-13 19:05:49 +08:00
content := c.GetString("content")
2021-04-21 13:37:47 +08:00
isGlobal, _ := c.GetInt("is_global", 0)
templateName := c.GetString("template_name", "")
2018-08-13 19:05:49 +08:00
2021-04-21 13:37:47 +08:00
if templateName == "" || strings.Count(templateName, "") > 300 {
c.JsonResult(500, "模板名称不能为空且必须小于300字")
2018-08-13 19:05:49 +08:00
}
template := models.NewTemplate()
2018-08-13 19:05:49 +08:00
if templateId > 0 {
2021-04-21 13:37:47 +08:00
t, err := template.Find(templateId)
if err != nil {
2021-04-21 13:37:47 +08:00
c.JsonResult(500, "模板不存在")
}
template = t
2018-08-13 19:05:49 +08:00
template.ModifyAt = c.Member.MemberId
}
template.TemplateId = templateId
template.BookId = c.BookId
template.TemplateContent = content
template.TemplateName = templateName
2018-08-13 19:05:49 +08:00
//只有管理员才能设置全局模板
if c.Member.IsAdministrator() {
template.IsGlobal = isGlobal
2021-04-21 13:37:47 +08:00
} else {
2018-08-13 19:05:49 +08:00
template.IsGlobal = 0
//如果不是管理员需要判断是否有项目权限
2021-04-21 13:37:47 +08:00
rel, err := models.NewRelationship().FindByBookIdAndMemberId(c.BookId, c.Member.MemberId)
if err != nil || rel.RoleId == conf.BookObserver {
2021-04-21 13:37:47 +08:00
c.JsonResult(403, "没有权限")
}
//如果修改的模板不是本人创建的,并且又不是项目创建者则禁止修改
if template.MemberId > 0 && template.MemberId != c.Member.MemberId && rel.RoleId != conf.BookFounder {
2021-04-21 13:37:47 +08:00
c.JsonResult(403, "没有权限")
}
2018-08-13 19:05:49 +08:00
}
template.MemberId = c.Member.MemberId
2018-08-13 19:05:49 +08:00
var cols []string
if templateId > 0 {
2021-04-21 13:37:47 +08:00
cols = []string{"template_content", "modify_time", "modify_at", "version"}
2018-08-13 19:05:49 +08:00
}
if err := template.Save(cols...); err != nil {
2021-04-21 13:37:47 +08:00
c.JsonResult(500, "报错模板失败")
2018-08-13 19:05:49 +08:00
}
2021-04-21 13:37:47 +08:00
c.JsonResult(0, "OK", template)
2018-08-13 19:05:49 +08:00
}
//删除模板
2018-08-13 19:05:49 +08:00
func (c *TemplateController) Delete() {
2021-04-21 13:37:47 +08:00
if err := c.isPermission(); err != nil {
c.JsonResult(500, err.Error())
2018-08-13 19:05:49 +08:00
}
2021-04-21 13:37:47 +08:00
templateId, _ := c.GetInt("template_id", 0)
2018-08-13 19:05:49 +08:00
2021-04-21 13:37:47 +08:00
template, err := models.NewTemplate().Find(templateId)
if err != nil {
2021-04-21 13:37:47 +08:00
c.JsonResult(404, "模板不存在")
}
2018-08-13 19:05:49 +08:00
if c.Member.IsAdministrator() {
2021-04-21 13:37:47 +08:00
err := models.NewTemplate().Delete(templateId, 0)
2018-08-13 19:05:49 +08:00
if err != nil {
2021-04-21 13:37:47 +08:00
c.JsonResult(500, "删除失败")
2018-08-13 19:05:49 +08:00
}
2021-04-21 13:37:47 +08:00
} else {
//如果不是管理员需要判断是否有项目权限
2021-04-21 13:37:47 +08:00
rel, err := models.NewRelationship().FindByBookIdAndMemberId(template.BookId, c.Member.MemberId)
if err != nil || rel.RoleId == conf.BookObserver {
2021-04-21 13:37:47 +08:00
c.JsonResult(403, "没有权限")
}
//如果是创始人或管理者可以直接删除模板
2021-04-21 13:37:47 +08:00
if rel.RoleId == conf.BookFounder || rel.RoleId == conf.BookAdmin {
err := models.NewTemplate().Delete(templateId, 0)
if err != nil {
2021-04-21 13:37:47 +08:00
c.JsonResult(500, "删除失败")
}
}
2021-04-21 13:37:47 +08:00
if err := models.NewTemplate().Delete(templateId, c.Member.MemberId); err != nil {
c.JsonResult(500, "删除失败")
2018-08-13 19:05:49 +08:00
}
}
2021-04-21 13:37:47 +08:00
c.JsonResult(0, "OK")
}