判断用户是否可以阅读文档.
(identify, token string, this *DocumentController)
| 95 | |
| 96 | // 判断用户是否可以阅读文档. |
| 97 | func isReadable(identify, token string, this *DocumentController) *models.BookResult { |
| 98 | book, err := models.NewBook().FindByFieldFirst("identify", identify) |
| 99 | if err != nil { |
| 100 | beego.Error(err) |
| 101 | this.Abort("404") |
| 102 | } |
| 103 | |
| 104 | //如果文档是私有的 |
| 105 | if book.PrivatelyOwned == 1 && !this.Member.IsAdministrator() { |
| 106 | isOk := false |
| 107 | if this.Member != nil { |
| 108 | _, err := models.NewRelationship().FindForRoleId(book.BookId, this.Member.MemberId) |
| 109 | if err == nil { |
| 110 | isOk = true |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if book.PrivateToken != "" && !isOk { |
| 115 | //如果有访问的Token,并且该书籍设置了访问Token,并且和用户提供的相匹配,则记录到Session中. |
| 116 | //如果用户未提供Token且用户登录了,则判断用户是否参与了该书籍. |
| 117 | //如果用户未登录,则从Session中读取Token. |
| 118 | if token != "" && strings.EqualFold(token, book.PrivateToken) { |
| 119 | this.SetSession(identify, token) |
| 120 | } else if token, ok := this.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) { |
| 121 | hasErr := "" |
| 122 | if this.Ctx.Request.Method == "POST" { |
| 123 | hasErr = "true" |
| 124 | } |
| 125 | this.Redirect(beego.URLFor("DocumentController.Index", ":key", identify)+"?with-password=true&err="+hasErr, 302) |
| 126 | this.StopRun() |
| 127 | } |
| 128 | } else if !isOk { |
| 129 | this.Abort("404") |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | bookResult := book.ToBookResult() |
| 134 | if this.Member != nil { |
| 135 | rel, err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId, this.Member.MemberId) |
| 136 | if err == nil { |
| 137 | bookResult.MemberId = book.MemberId |
| 138 | bookResult.RoleId = rel.RoleId |
| 139 | bookResult.RelationshipId = rel.RelationshipId |
| 140 | } |
| 141 | } |
| 142 | //判断是否需要显示评论框 |
| 143 | switch bookResult.CommentStatus { |
| 144 | case "closed": |
| 145 | bookResult.IsDisplayComment = false |
| 146 | case "open": |
| 147 | bookResult.IsDisplayComment = true |
| 148 | case "group_only": |
| 149 | bookResult.IsDisplayComment = bookResult.RelationshipId > 0 |
| 150 | case "registered_only": |
| 151 | bookResult.IsDisplayComment = true |
| 152 | } |
| 153 | return bookResult |
| 154 | } |
no test coverage detected