mirror of
https://github.com/mindoc-org/mindoc.git
synced 2026-02-27 17:03:57 +08:00
1、重新实现数据库初始化
2、新增超级管理员可以转让项目 3、新增超级管理员可以公开项目
This commit is contained in:
@@ -161,6 +161,7 @@ func (c *BookController) SaveBook() {
|
||||
c.JsonResult(0,"ok",bookResult)
|
||||
}
|
||||
|
||||
//设置项目私有状态.
|
||||
func (c *BookController) PrivatelyOwned() {
|
||||
|
||||
status := c.GetString("status")
|
||||
@@ -184,7 +185,6 @@ func (c *BookController) PrivatelyOwned() {
|
||||
if bookResult.RoleId != conf.BookFounder {
|
||||
c.JsonResult(6002,"权限不足")
|
||||
}
|
||||
fmt.Printf("%+v",bookResult)
|
||||
|
||||
book,err := models.NewBook().Find(bookResult.BookId)
|
||||
|
||||
@@ -193,8 +193,6 @@ func (c *BookController) PrivatelyOwned() {
|
||||
}
|
||||
book.PrivatelyOwned = state
|
||||
|
||||
logs.Info("",state,status)
|
||||
|
||||
err = book.Update()
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"strings"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/lifei6671/godoc/conf"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/lifei6671/godoc/utils"
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/lifei6671/godoc/conf"
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"github.com/lifei6671/godoc/utils"
|
||||
)
|
||||
|
||||
type ManagerController struct {
|
||||
@@ -29,7 +28,7 @@ func (c *ManagerController) Index() {
|
||||
}
|
||||
|
||||
// 用户列表.
|
||||
func (c *ManagerController) Users() {
|
||||
func (c *ManagerController) Users() {
|
||||
c.Prepare()
|
||||
c.TplName = "manager/users.tpl"
|
||||
|
||||
@@ -37,9 +36,9 @@ func (c *ManagerController) Users() {
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
pageIndex,_ := c.GetInt("page",0)
|
||||
pageIndex, _ := c.GetInt("page", 0)
|
||||
|
||||
members,totalCount,err := models.NewMember().FindToPager(pageIndex,15)
|
||||
members, totalCount, err := models.NewMember().FindToPager(pageIndex, 15)
|
||||
|
||||
if err != nil {
|
||||
c.Data["ErrorMessage"] = err.Error()
|
||||
@@ -50,23 +49,23 @@ func (c *ManagerController) Users() {
|
||||
html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 10, int(totalCount))
|
||||
|
||||
c.Data["PageHtml"] = html
|
||||
}else{
|
||||
} else {
|
||||
c.Data["PageHtml"] = ""
|
||||
}
|
||||
|
||||
b,err := json.Marshal(members)
|
||||
b, err := json.Marshal(members)
|
||||
|
||||
if err != nil {
|
||||
c.Data["Result"] = template.JS("[]")
|
||||
}else{
|
||||
} else {
|
||||
c.Data["Result"] = template.JS(string(b))
|
||||
}
|
||||
}
|
||||
|
||||
// 添加用户
|
||||
// 添加用户.
|
||||
func (c *ManagerController) CreateMember() {
|
||||
c.Prepare()
|
||||
if !c.Member.IsAdministrator(){
|
||||
if !c.Member.IsAdministrator() {
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
@@ -75,22 +74,22 @@ func (c *ManagerController) CreateMember() {
|
||||
password2 := strings.TrimSpace(c.GetString("password2"))
|
||||
email := strings.TrimSpace(c.GetString("email"))
|
||||
phone := strings.TrimSpace(c.GetString("phone"))
|
||||
role,_ := c.GetInt("role",1)
|
||||
status,_ := c.GetInt("status",0)
|
||||
role, _ := c.GetInt("role", 1)
|
||||
status, _ := c.GetInt("status", 0)
|
||||
|
||||
if ok,err := regexp.MatchString(conf.RegexpAccount,account); account == "" || !ok || err != nil {
|
||||
c.JsonResult(6001,"账号只能由英文字母数字组成,且在3-50个字符")
|
||||
if ok, err := regexp.MatchString(conf.RegexpAccount, account); account == "" || !ok || err != nil {
|
||||
c.JsonResult(6001, "账号只能由英文字母数字组成,且在3-50个字符")
|
||||
}
|
||||
if l := strings.Count(password1,"") ; password1 == "" || l > 50 || l < 6{
|
||||
c.JsonResult(6002,"密码必须在6-50个字符之间")
|
||||
if l := strings.Count(password1, ""); password1 == "" || l > 50 || l < 6 {
|
||||
c.JsonResult(6002, "密码必须在6-50个字符之间")
|
||||
}
|
||||
if password1 != password2 {
|
||||
c.JsonResult(6003,"确认密码不正确")
|
||||
c.JsonResult(6003, "确认密码不正确")
|
||||
}
|
||||
if ok,err := regexp.MatchString(conf.RegexpEmail,email); !ok || err != nil || email == "" {
|
||||
c.JsonResult(6004,"邮箱格式不正确")
|
||||
if ok, err := regexp.MatchString(conf.RegexpEmail, email); !ok || err != nil || email == "" {
|
||||
c.JsonResult(6004, "邮箱格式不正确")
|
||||
}
|
||||
if role != 0 && role != 1 {
|
||||
if role != 0 && role != 1 && role != 2 {
|
||||
role = 1
|
||||
}
|
||||
if status != 0 && status != 1 {
|
||||
@@ -99,8 +98,8 @@ func (c *ManagerController) CreateMember() {
|
||||
|
||||
member := models.NewMember()
|
||||
|
||||
if _,err := member.FindByAccount(account); err == nil && member.MemberId > 0 {
|
||||
c.JsonResult(6005,"账号已存在")
|
||||
if _, err := member.FindByAccount(account); err == nil && member.MemberId > 0 {
|
||||
c.JsonResult(6005, "账号已存在")
|
||||
}
|
||||
|
||||
member.Account = account
|
||||
@@ -114,80 +113,81 @@ func (c *ManagerController) CreateMember() {
|
||||
}
|
||||
|
||||
if err := member.Add(); err != nil {
|
||||
c.JsonResult(6006,err.Error())
|
||||
c.JsonResult(6006, err.Error())
|
||||
}
|
||||
|
||||
c.JsonResult(0,"ok",member)
|
||||
c.JsonResult(0, "ok", member)
|
||||
}
|
||||
|
||||
//更新用户状态.
|
||||
func (c *ManagerController) UpdateMemberStatus() {
|
||||
func (c *ManagerController) UpdateMemberStatus() {
|
||||
c.Prepare()
|
||||
|
||||
if !c.Member.IsAdministrator() {
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
member_id,_ := c.GetInt("member_id",0)
|
||||
status ,_ := c.GetInt("status",0)
|
||||
member_id, _ := c.GetInt("member_id", 0)
|
||||
status, _ := c.GetInt("status", 0)
|
||||
|
||||
if member_id <= 0 {
|
||||
c.JsonResult(6001,"参数错误")
|
||||
c.JsonResult(6001, "参数错误")
|
||||
}
|
||||
if status != 0 && status != 1 {
|
||||
status = 0
|
||||
}
|
||||
member := models.NewMember()
|
||||
|
||||
if _,err := member.Find(member_id); err != nil {
|
||||
c.JsonResult(6002,"用户不存在")
|
||||
if _, err := member.Find(member_id); err != nil {
|
||||
c.JsonResult(6002, "用户不存在")
|
||||
}
|
||||
member.Status = status
|
||||
|
||||
if err := member.Update();err != nil {
|
||||
logs.Error("",err)
|
||||
c.JsonResult(6003,"用户状态设置失败")
|
||||
if err := member.Update(); err != nil {
|
||||
logs.Error("", err)
|
||||
c.JsonResult(6003, "用户状态设置失败")
|
||||
}
|
||||
c.JsonResult(0,"ok",member)
|
||||
c.JsonResult(0, "ok", member)
|
||||
}
|
||||
|
||||
func (c *ManagerController) ChangeMemberRole() {
|
||||
//变更用户权限.
|
||||
func (c *ManagerController) ChangeMemberRole() {
|
||||
c.Prepare()
|
||||
|
||||
if !c.Member.IsAdministrator() {
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
member_id,_ := c.GetInt("member_id",0)
|
||||
role ,_ := c.GetInt("role",0)
|
||||
member_id, _ := c.GetInt("member_id", 0)
|
||||
role, _ := c.GetInt("role", 0)
|
||||
if member_id <= 0 {
|
||||
c.JsonResult(6001,"参数错误")
|
||||
c.JsonResult(6001, "参数错误")
|
||||
}
|
||||
if role != conf.MemberAdminRole && role != conf.MemberGeneralRole {
|
||||
c.JsonResult(6001,"用户权限不正确")
|
||||
c.JsonResult(6001, "用户权限不正确")
|
||||
}
|
||||
member := models.NewMember()
|
||||
|
||||
if _,err := member.Find(member_id); err != nil {
|
||||
c.JsonResult(6002,"用户不存在")
|
||||
if _, err := member.Find(member_id); err != nil {
|
||||
c.JsonResult(6002, "用户不存在")
|
||||
}
|
||||
member.Role = role
|
||||
|
||||
if err := member.Update();err != nil {
|
||||
logs.Error("",err)
|
||||
c.JsonResult(6003,"用户权限设置失败")
|
||||
if err := member.Update(); err != nil {
|
||||
logs.Error("", err)
|
||||
c.JsonResult(6003, "用户权限设置失败")
|
||||
}
|
||||
member.ResolveRoleName()
|
||||
c.JsonResult(0,"ok",member)
|
||||
c.JsonResult(0, "ok", member)
|
||||
}
|
||||
|
||||
func (c *ManagerController) Books() {
|
||||
func (c *ManagerController) Books() {
|
||||
c.Prepare()
|
||||
c.TplName = "manager/books.tpl"
|
||||
|
||||
pageIndex, _ := c.GetInt("page", 1)
|
||||
|
||||
books,totalCount,err := models.NewBookResult().FindToPager(pageIndex,conf.PageSize)
|
||||
books, totalCount, err := models.NewBookResult().FindToPager(pageIndex, conf.PageSize)
|
||||
|
||||
if err != nil {
|
||||
c.Abort("500")
|
||||
@@ -197,7 +197,7 @@ func (c *ManagerController) Books() {
|
||||
html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount)
|
||||
|
||||
c.Data["PageHtml"] = html
|
||||
}else {
|
||||
} else {
|
||||
c.Data["PageHtml"] = ""
|
||||
}
|
||||
|
||||
@@ -205,80 +205,79 @@ func (c *ManagerController) Books() {
|
||||
}
|
||||
|
||||
//编辑项目
|
||||
func (c *ManagerController) EditBook() {
|
||||
func (c *ManagerController) EditBook() {
|
||||
c.TplName = "manager/edit_book.tpl"
|
||||
identify := c.GetString(":key")
|
||||
|
||||
if identify == "" {
|
||||
c.Abort("404")
|
||||
}
|
||||
book,err := models.NewBook().FindByFieldFirst("identify",identify)
|
||||
book, err := models.NewBook().FindByFieldFirst("identify", identify)
|
||||
if err != nil {
|
||||
c.Abort("500")
|
||||
}
|
||||
if c.Ctx.Input.IsPost() {
|
||||
|
||||
book_name := strings.TrimSpace(c.GetString("book_name"))
|
||||
description := strings.TrimSpace(c.GetString("description",""))
|
||||
description := strings.TrimSpace(c.GetString("description", ""))
|
||||
comment_status := c.GetString("comment_status")
|
||||
tag := strings.TrimSpace(c.GetString("label"))
|
||||
order_index ,_ := c.GetInt("order_index",0)
|
||||
order_index, _ := c.GetInt("order_index", 0)
|
||||
|
||||
if strings.Count(description,"") > 500 {
|
||||
c.JsonResult(6004,"项目描述不能大于500字")
|
||||
if strings.Count(description, "") > 500 {
|
||||
c.JsonResult(6004, "项目描述不能大于500字")
|
||||
}
|
||||
if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
|
||||
comment_status = "closed"
|
||||
}
|
||||
if tag != ""{
|
||||
tags := strings.Split(tag,";")
|
||||
if tag != "" {
|
||||
tags := strings.Split(tag, ";")
|
||||
if len(tags) > 10 {
|
||||
c.JsonResult(6005,"最多允许添加10个标签")
|
||||
c.JsonResult(6005, "最多允许添加10个标签")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
book.BookName = book_name
|
||||
book.Description = description
|
||||
book.CommentStatus = comment_status
|
||||
book.Label = tag
|
||||
book.OrderIndex = order_index
|
||||
|
||||
if err := book.Update();err != nil {
|
||||
c.JsonResult(6006,"保存失败")
|
||||
if err := book.Update(); err != nil {
|
||||
c.JsonResult(6006, "保存失败")
|
||||
}
|
||||
c.JsonResult(0,"ok")
|
||||
c.JsonResult(0, "ok")
|
||||
}
|
||||
if book.PrivateToken != "" {
|
||||
book.PrivateToken = c.BaseUrl() + beego.URLFor("DocumentController.Index",":key",book.Identify,"token",book.PrivateToken)
|
||||
book.PrivateToken = c.BaseUrl() + beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
|
||||
}
|
||||
c.Data["Model"] = book
|
||||
}
|
||||
|
||||
// 删除项目.
|
||||
func (c *ManagerController) DeleteBook() {
|
||||
func (c *ManagerController) DeleteBook() {
|
||||
c.Prepare()
|
||||
if !c.Member.IsAdministrator() {
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
book_id,_ := c.GetInt("book_id",0)
|
||||
book_id, _ := c.GetInt("book_id", 0)
|
||||
|
||||
if book_id <= 0{
|
||||
c.JsonResult(6001,"参数错误")
|
||||
if book_id <= 0 {
|
||||
c.JsonResult(6001, "参数错误")
|
||||
}
|
||||
book := models.NewBook()
|
||||
|
||||
err := book.ThoroughDeleteBook(book_id)
|
||||
|
||||
if err == orm.ErrNoRows {
|
||||
c.JsonResult(6002,"项目不存在")
|
||||
c.JsonResult(6002, "项目不存在")
|
||||
}
|
||||
if err != nil {
|
||||
logs.Error("DeleteBook => ",err)
|
||||
c.JsonResult(6003,"删除失败")
|
||||
logs.Error("DeleteBook => ", err)
|
||||
c.JsonResult(6003, "删除失败")
|
||||
}
|
||||
c.JsonResult(0,"ok")
|
||||
c.JsonResult(0, "ok")
|
||||
}
|
||||
|
||||
// CreateToken 创建访问来令牌.
|
||||
@@ -288,10 +287,10 @@ func (c *ManagerController) CreateToken() {
|
||||
|
||||
identify := c.GetString("identify")
|
||||
|
||||
book,err := models.NewBook().FindByFieldFirst("identify",identify);
|
||||
book, err := models.NewBook().FindByFieldFirst("identify", identify)
|
||||
|
||||
if err != nil {
|
||||
c.JsonResult(6001,"项目不存在")
|
||||
c.JsonResult(6001, "项目不存在")
|
||||
}
|
||||
if action == "create" {
|
||||
|
||||
@@ -304,17 +303,18 @@ func (c *ManagerController) CreateToken() {
|
||||
logs.Error("生成阅读令牌失败 => ", err)
|
||||
c.JsonResult(6003, "生成阅读令牌失败")
|
||||
}
|
||||
c.JsonResult(0, "ok", c.BaseUrl() + beego.URLFor("DocumentController.Index",":key",book.Identify,"token",book.PrivateToken))
|
||||
}else{
|
||||
c.JsonResult(0, "ok", c.BaseUrl()+beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
|
||||
} else {
|
||||
book.PrivateToken = ""
|
||||
if err := book.Update();err != nil {
|
||||
logs.Error("CreateToken => ",err)
|
||||
c.JsonResult(6004,"删除令牌失败")
|
||||
if err := book.Update(); err != nil {
|
||||
logs.Error("CreateToken => ", err)
|
||||
c.JsonResult(6004, "删除令牌失败")
|
||||
}
|
||||
c.JsonResult(0,"ok","")
|
||||
c.JsonResult(0, "ok", "")
|
||||
}
|
||||
}
|
||||
|
||||
//项目设置.
|
||||
func (c *ManagerController) Setting() {
|
||||
c.Prepare()
|
||||
c.TplName = "manager/setting.tpl"
|
||||
@@ -323,14 +323,14 @@ func (c *ManagerController) Setting() {
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
options,err := models.NewOption().All()
|
||||
options, err := models.NewOption().All()
|
||||
|
||||
if c.Ctx.Input.IsPost() {
|
||||
for _,item := range options {
|
||||
for _, item := range options {
|
||||
item.OptionValue = c.GetString(item.OptionName)
|
||||
item.InsertOrUpdate()
|
||||
}
|
||||
c.JsonResult(0,"ok")
|
||||
c.JsonResult(0, "ok")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -338,14 +338,60 @@ func (c *ManagerController) Setting() {
|
||||
}
|
||||
c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
|
||||
|
||||
for _,item := range options {
|
||||
for _, item := range options {
|
||||
c.Data[item.OptionName] = item
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
func (c *ManagerController) Comments() {
|
||||
// Transfer 转让项目.
|
||||
func (c *ManagerController) Transfer() {
|
||||
c.Prepare()
|
||||
account := c.GetString("account")
|
||||
|
||||
if account == "" {
|
||||
c.JsonResult(6004, "接受者账号不能为空")
|
||||
}
|
||||
member, err := models.NewMember().FindByAccount(account)
|
||||
|
||||
if err != nil {
|
||||
logs.Error("FindByAccount => ", err)
|
||||
c.JsonResult(6005, "接受用户不存在")
|
||||
}
|
||||
if member.Status != 0 {
|
||||
c.JsonResult(6006, "接受用户已被禁用")
|
||||
}
|
||||
|
||||
if !c.Member.IsAdministrator() {
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
identify := c.GetString("identify")
|
||||
|
||||
book, err := models.NewBook().FindByFieldFirst("identify", identify)
|
||||
if err != nil {
|
||||
c.JsonResult(6001, err.Error())
|
||||
}
|
||||
rel, err := models.NewRelationship().FindFounder(book.BookId)
|
||||
|
||||
if err != nil {
|
||||
beego.Error("FindFounder => ", err)
|
||||
c.JsonResult(6009, "查询项目创始人失败")
|
||||
}
|
||||
if member.MemberId == rel.MemberId {
|
||||
c.JsonResult(6007, "不能转让给自己")
|
||||
}
|
||||
|
||||
err = models.NewRelationship().Transfer(book.BookId, rel.MemberId, member.MemberId)
|
||||
|
||||
if err != nil {
|
||||
logs.Error("Transfer => ", err)
|
||||
c.JsonResult(6008, err.Error())
|
||||
}
|
||||
c.JsonResult(0, "ok")
|
||||
}
|
||||
|
||||
func (c *ManagerController) Comments() {
|
||||
c.Prepare()
|
||||
c.TplName = "manager/comments.tpl"
|
||||
if !c.Member.IsAdministrator() {
|
||||
@@ -355,49 +401,65 @@ func (c *ManagerController) Comments() {
|
||||
}
|
||||
|
||||
//DeleteComment 标记评论为已删除
|
||||
func (c *ManagerController) DeleteComment() {
|
||||
func (c *ManagerController) DeleteComment() {
|
||||
c.Prepare()
|
||||
if !c.Member.IsAdministrator() {
|
||||
c.Abort("403")
|
||||
}
|
||||
comment_id,_ := c.GetInt("comment_id",0)
|
||||
comment_id, _ := c.GetInt("comment_id", 0)
|
||||
|
||||
if comment_id <= 0 {
|
||||
c.JsonResult(6001,"参数错误")
|
||||
c.JsonResult(6001, "参数错误")
|
||||
}
|
||||
|
||||
comment := models.NewComment()
|
||||
|
||||
if _,err := comment.Find(comment_id); err != nil {
|
||||
c.JsonResult(6002,"评论不存在")
|
||||
if _, err := comment.Find(comment_id); err != nil {
|
||||
c.JsonResult(6002, "评论不存在")
|
||||
}
|
||||
|
||||
comment.Approved = 3
|
||||
|
||||
if err := comment.Update("approved");err != nil {
|
||||
c.JsonResult(6003,"删除评论失败")
|
||||
if err := comment.Update("approved"); err != nil {
|
||||
c.JsonResult(6003, "删除评论失败")
|
||||
}
|
||||
c.JsonResult(0,"ok",comment)
|
||||
c.JsonResult(0, "ok", comment)
|
||||
}
|
||||
|
||||
//设置项目私有状态.
|
||||
func (c *ManagerController) PrivatelyOwned() {
|
||||
|
||||
status := c.GetString("status")
|
||||
identify := c.GetString("identify")
|
||||
|
||||
if status != "open" && status != "close" {
|
||||
c.JsonResult(6003, "参数错误")
|
||||
}
|
||||
state := 0
|
||||
if status == "open" {
|
||||
state = 0
|
||||
} else {
|
||||
state = 1
|
||||
}
|
||||
|
||||
if !c.Member.IsAdministrator() {
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
book, err := models.NewBook().FindByFieldFirst("identify", identify)
|
||||
if err != nil {
|
||||
c.JsonResult(6001, err.Error())
|
||||
}
|
||||
|
||||
book.PrivatelyOwned = state
|
||||
|
||||
logs.Info("", state, status)
|
||||
|
||||
err = book.Update()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if err != nil {
|
||||
logs.Error("PrivatelyOwned => ", err)
|
||||
c.JsonResult(6004, "保存失败")
|
||||
}
|
||||
c.JsonResult(0, "ok")
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ func (c *SettingController) Index() {
|
||||
if err := member.Update(); err != nil {
|
||||
c.JsonResult(602, err.Error())
|
||||
}
|
||||
c.SetMember(*member)
|
||||
c.JsonResult(0, "ok")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user