mirror of
https://github.com/mindoc-org/mindoc.git
synced 2026-02-27 17:03:57 +08:00
1、实现网站首页
2、实现文档阅读 3、实现项目发布 4、实现用户权限
This commit is contained in:
@@ -77,3 +77,6 @@ func (c *AccountController) Logout(){
|
||||
c.Redirect(beego.URLFor("AccountController.Login"),302)
|
||||
}
|
||||
|
||||
func (c *AccountController) Captcha() {
|
||||
|
||||
}
|
||||
@@ -7,32 +7,42 @@ import (
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"github.com/lifei6671/godoc/conf"
|
||||
"github.com/astaxie/beego"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
type BaseController struct {
|
||||
beego.Controller
|
||||
Member *models.Member
|
||||
Option map[string]string
|
||||
EnableAnonymous bool
|
||||
}
|
||||
|
||||
// Prepare 预处理.
|
||||
func (c *BaseController) Prepare (){
|
||||
c.Data["SiteName"] = "MinDoc"
|
||||
c.Data["Member"] = models.Member{}
|
||||
c.EnableAnonymous = false
|
||||
|
||||
|
||||
if member,ok := c.GetSession(conf.LoginSessionName).(models.Member); ok && member.MemberId > 0{
|
||||
c.Member = &member
|
||||
c.Data["Member"] = c.Member
|
||||
}else{
|
||||
c.Member = models.NewMember()
|
||||
c.Member.Find(1)
|
||||
c.Data["Member"] = *c.Member
|
||||
//c.Member = models.NewMember()
|
||||
//c.Member.Find(1)
|
||||
//c.Data["Member"] = *c.Member
|
||||
}
|
||||
c.Data["BaseUrl"] = c.Ctx.Input.Scheme() + "://" + c.Ctx.Request.Host
|
||||
|
||||
if options,err := models.NewOption().All();err == nil {
|
||||
c.Option = make(map[string]string,len(options))
|
||||
for _,item := range options {
|
||||
c.Data[item.OptionName] = item.OptionValue
|
||||
c.Option[item.OptionName] = item.OptionValue
|
||||
if strings.EqualFold(item.OptionName,"ENABLE_ANONYMOUS") && item.OptionValue == "true" {
|
||||
c.EnableAnonymous = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -499,7 +499,28 @@ func (c *BookController) Delete() {
|
||||
|
||||
//发布项目
|
||||
func (c *BookController) Release() {
|
||||
c.JsonResult(0,"ok")
|
||||
c.Prepare()
|
||||
|
||||
identify := c.GetString("identify")
|
||||
book ,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
|
||||
|
||||
if err != nil {
|
||||
if err == models.ErrPermissionDenied {
|
||||
c.JsonResult(6001,"权限不足")
|
||||
}
|
||||
if err == orm.ErrNoRows {
|
||||
c.JsonResult(6002,"项目不存在")
|
||||
}
|
||||
beego.Error(err)
|
||||
c.JsonResult(6003,"未知错误")
|
||||
}
|
||||
if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder && book.RoleId != conf.BookEditor{
|
||||
c.JsonResult(6003,"权限不足")
|
||||
}
|
||||
|
||||
go models.NewDocument().ReleaseContent(book.BookId)
|
||||
|
||||
c.JsonResult(0,"发布任务已推送到任务队列,稍后将在后台执行。")
|
||||
}
|
||||
|
||||
func (c *BookController) SaveSort() {
|
||||
|
||||
@@ -21,12 +21,135 @@ type DocumentController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
func (p *DocumentController) Index() {
|
||||
p.TplName = "document/index.tpl"
|
||||
func isReadable (identify,token string,c *DocumentController) *models.BookResult {
|
||||
book,err := models.NewBook().FindByFieldFirst("identify",identify)
|
||||
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
c.Abort("500")
|
||||
}
|
||||
//如果文档是私有的
|
||||
if book.PrivatelyOwned == 1 {
|
||||
|
||||
is_ok := false
|
||||
|
||||
if c.Member != nil{
|
||||
_, err := models.NewRelationship().FindForRoleId(book.BookId, c.Member.MemberId)
|
||||
if err == nil {
|
||||
is_ok = true
|
||||
}
|
||||
}
|
||||
if book.PrivateToken != "" && !is_ok {
|
||||
//如果有访问的Token,并且该项目设置了访问Token,并且和用户提供的相匹配,则记录到Session中.
|
||||
//如果用户未提供Token且用户登录了,则判断用户是否参与了该项目.
|
||||
//如果用户未登录,则从Session中读取Token.
|
||||
if token != "" && strings.EqualFold(token, book.PrivateToken) {
|
||||
c.SetSession(identify, token)
|
||||
|
||||
} else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) {
|
||||
c.Abort("403")
|
||||
}
|
||||
}else{
|
||||
c.Abort("403")
|
||||
}
|
||||
|
||||
}
|
||||
bookResult := book.ToBookResult()
|
||||
|
||||
if c.Member != nil {
|
||||
rel ,err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId,c.Member.MemberId)
|
||||
|
||||
if err == nil {
|
||||
bookResult.MemberId = rel.MemberId
|
||||
bookResult.RoleId = rel.RoleId
|
||||
bookResult.RelationshipId = rel.RelationshipId
|
||||
}
|
||||
}
|
||||
return bookResult
|
||||
}
|
||||
|
||||
func (p *DocumentController) Read() {
|
||||
p.TplName = "document/kancloud.tpl"
|
||||
func (c *DocumentController) Index() {
|
||||
c.Prepare()
|
||||
identify := c.Ctx.Input.Param(":key")
|
||||
token := c.GetString("token")
|
||||
|
||||
if identify == "" {
|
||||
c.Abort("404")
|
||||
}
|
||||
bookResult := isReadable(identify,token,c)
|
||||
|
||||
c.TplName = "document/" + bookResult.Theme + "_read.tpl"
|
||||
|
||||
tree,err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId,0)
|
||||
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
c.Abort("500")
|
||||
}
|
||||
|
||||
|
||||
c.Data["Model"] = bookResult
|
||||
c.Data["Result"] = template.HTML(tree)
|
||||
c.Data["Title"] = "概要"
|
||||
c.Data["Content"] = bookResult.Description
|
||||
}
|
||||
|
||||
func (c *DocumentController) Read() {
|
||||
c.Prepare()
|
||||
identify := c.Ctx.Input.Param(":key")
|
||||
token := c.GetString("token")
|
||||
id := c.GetString(":id")
|
||||
|
||||
if identify == "" || id == ""{
|
||||
c.Abort("404")
|
||||
}
|
||||
bookResult := isReadable(identify,token,c)
|
||||
|
||||
c.TplName = "document/" + bookResult.Theme + "_read.tpl"
|
||||
|
||||
doc := models.NewDocument()
|
||||
|
||||
if doc_id,err := strconv.Atoi(id);err == nil {
|
||||
doc,err = doc.Find(doc_id)
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
c.Abort("500")
|
||||
}
|
||||
}else{
|
||||
doc,err = doc.FindByFieldFirst("identify",id)
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
c.Abort("500")
|
||||
}
|
||||
}
|
||||
|
||||
if doc.BookId != bookResult.BookId {
|
||||
c.Abort("403")
|
||||
}
|
||||
if c.IsAjax() {
|
||||
var data struct{
|
||||
DocTitle string `json:"doc_title"`
|
||||
Body string `json:"body"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
data.DocTitle = doc.DocumentName
|
||||
data.Body = doc.Release
|
||||
data.Title = doc.DocumentName + " - Powered by MinDoc"
|
||||
|
||||
c.JsonResult(0,"ok",data)
|
||||
}
|
||||
|
||||
tree,err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId,doc.DocumentId)
|
||||
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
c.Abort("500")
|
||||
}
|
||||
|
||||
c.Data["Model"] = bookResult
|
||||
c.Data["Result"] = template.HTML(tree)
|
||||
c.Data["Title"] = doc.DocumentName
|
||||
c.Data["Content"] = template.HTML(doc.Release)
|
||||
}
|
||||
|
||||
func (c *DocumentController) Edit() {
|
||||
@@ -67,7 +190,7 @@ func (c *DocumentController) Edit() {
|
||||
c.Data["Result"] = template.JS("[]")
|
||||
|
||||
trees ,err := models.NewDocument().FindDocumentTree(bookResult.BookId)
|
||||
beego.Info("",trees)
|
||||
|
||||
if err != nil {
|
||||
beego.Error("FindDocumentTree => ", err)
|
||||
}else{
|
||||
|
||||
@@ -1,9 +1,44 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"github.com/lifei6671/godoc/utils"
|
||||
)
|
||||
|
||||
type HomeController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
func (p *HomeController) Index() {
|
||||
p.TplName = "home/index.tpl"
|
||||
func (c *HomeController) Index() {
|
||||
c.Prepare()
|
||||
c.TplName = "home/index.tpl"
|
||||
//如果没有开启匿名访问,则跳转到登录页面
|
||||
if !c.EnableAnonymous && c.Member == nil {
|
||||
c.Redirect(beego.URLFor("AccountController.Login"),302)
|
||||
}
|
||||
pageIndex,_ := c.GetInt("page",1)
|
||||
pageSize := 18
|
||||
|
||||
member_id := 0
|
||||
|
||||
if c.Member != nil {
|
||||
member_id = c.Member.MemberId
|
||||
}
|
||||
books,totalCount,err := models.NewBook().FindForHomeToPager(pageIndex,pageSize,member_id)
|
||||
|
||||
if err != nil {
|
||||
beego.Error(err)
|
||||
c.Abort("500")
|
||||
}
|
||||
if totalCount > 0 {
|
||||
html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, pageSize, totalCount)
|
||||
|
||||
c.Data["PageHtml"] = html
|
||||
}else {
|
||||
c.Data["PageHtml"] = ""
|
||||
}
|
||||
|
||||
c.Data["Lists"] = books
|
||||
|
||||
}
|
||||
|
||||
@@ -320,18 +320,27 @@ func (c *ManagerController) Setting() {
|
||||
if !c.Member.IsAdministrator() {
|
||||
c.Abort("403")
|
||||
}
|
||||
if c.Ctx.Input.IsPost() {
|
||||
|
||||
}
|
||||
options,err := models.NewOption().All()
|
||||
|
||||
if c.Ctx.Input.IsPost() {
|
||||
for _,item := range options {
|
||||
item.OptionValue = c.GetString(item.OptionName)
|
||||
item.InsertOrUpdate()
|
||||
}
|
||||
c.JsonResult(0,"ok")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.Abort("500")
|
||||
}
|
||||
c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
|
||||
|
||||
for _,item := range options {
|
||||
c.Data[item.OptionName] = item
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
func (c *ManagerController) Comments() {
|
||||
|
||||
Reference in New Issue
Block a user