Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ef47de

Browse files
committed
用户发布的信息,限制多久内能修改
1 parent ac65edb commit 2ef47de

File tree

19 files changed

+246
-33
lines changed

19 files changed

+246
-33
lines changed

src/global/chan.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ package global
99
var AuthorityChan = make(chan struct{}, 1)
1010
var RoleChan = make(chan struct{}, 1)
1111
var RoleAuthChan = make(chan struct{}, 1)
12+
var UserSetting = make(chan struct{}, 1)

src/http/controller/article.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (ArticleController) Modify(ctx echo.Context) error {
167167
}
168168

169169
me := ctx.Get("user").(*model.Me)
170-
if article.Author != me.Username && !me.IsRoot {
170+
if !logic.CanEdit(me, article) {
171171
return fail(ctx, 3, "没有修改权限")
172172
}
173173

src/http/controller/wiki.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type WikiController struct{}
3131
// 注册路由
3232
func (self WikiController) RegisterRoute(g *echo.Group) {
3333
g.Match([]string{"GET", "POST"}, "/wiki/new", self.Create, middleware.NeedLogin(), middleware.Sensivite())
34+
g.Match([]string{"GET", "POST"}, "/wiki/modify", self.Modify, middleware.NeedLogin(), middleware.Sensivite())
3435
g.GET("/wiki", self.ReadList)
3536
g.GET("/wiki/:uri", self.Detail)
3637
}
@@ -52,6 +53,31 @@ func (WikiController) Create(ctx echo.Context) error {
5253
return success(ctx, nil)
5354
}
5455

56+
// Modify 修改 Wiki 页
57+
func (WikiController) Modify(ctx echo.Context) error {
58+
id := goutils.MustInt(ctx.FormValue("id"))
59+
if id == 0 {
60+
return ctx.Redirect(http.StatusSeeOther, "/wiki")
61+
}
62+
63+
if ctx.Request().Method() != "POST" {
64+
wiki := logic.DefaultWiki.FindById(ctx, id)
65+
if wiki.Id == 0 {
66+
return ctx.Redirect(http.StatusSeeOther, "/wiki")
67+
}
68+
69+
return render(ctx, "wiki/new.html", map[string]interface{}{"activeWiki": "active", "wiki": wiki})
70+
}
71+
72+
me := ctx.Get("user").(*model.Me)
73+
err := logic.DefaultWiki.Modify(ctx, me, ctx.FormParams())
74+
if err != nil {
75+
return fail(ctx, 1, "内部服务错误")
76+
}
77+
78+
return success(ctx, nil)
79+
}
80+
5581
// Detail 展示wiki页
5682
func (WikiController) Detail(ctx echo.Context) error {
5783
wiki := logic.DefaultWiki.FindOne(ctx, ctx.Param("uri"))

src/http/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ var funcMap = template.FuncMap{
9292
}
9393
return time.Now().Unix()
9494
},
95+
"canEdit": logic.CanEdit,
9596
}
9697

9798
const (

src/logic/common.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ package logic
99
import (
1010
"errors"
1111
"fmt"
12+
"model"
1213
"os"
1314
"regexp"
15+
"time"
1416

1517
"github.com/gorilla/schema"
1618
"github.com/polaris1119/logger"
@@ -53,3 +55,85 @@ func parseAtUser(ctx context.Context, content string) string {
5355
return fmt.Sprintf(`<a href="/user/%s" title="%s">%s</a>`, username, matched, matched)
5456
})
5557
}
58+
59+
// CanEdit 判断能否编辑
60+
func CanEdit(me *model.Me, curModel interface{}) bool {
61+
if me == nil {
62+
return false
63+
}
64+
65+
if me.IsAdmin {
66+
return true
67+
}
68+
69+
canEditTime := time.Duration(UserSetting["can_edit_time"]) * time.Second
70+
71+
switch entity := curModel.(type) {
72+
case *model.Topic:
73+
if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
74+
return false
75+
}
76+
77+
if me.Uid == entity.Uid {
78+
return true
79+
}
80+
case *model.Article:
81+
if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
82+
return false
83+
}
84+
85+
if me.Username == entity.Author {
86+
return true
87+
}
88+
case *model.Resource:
89+
if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
90+
return false
91+
}
92+
93+
if me.Uid == entity.Uid {
94+
return true
95+
}
96+
case *model.OpenProject:
97+
if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
98+
return false
99+
}
100+
101+
if me.Username == entity.Username {
102+
return true
103+
}
104+
case *model.Wiki:
105+
if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
106+
return false
107+
}
108+
109+
if me.Uid == entity.Uid {
110+
return true
111+
}
112+
case map[string]interface{}:
113+
if ctime, ok := entity["ctime"]; ok {
114+
if time.Now().Sub(time.Time(ctime.(model.OftenTime))) > canEditTime {
115+
return false
116+
}
117+
}
118+
119+
if createdAt, ok := entity["created_at"]; ok {
120+
if time.Now().Sub(time.Time(createdAt.(model.OftenTime))) > canEditTime {
121+
return false
122+
}
123+
}
124+
125+
if uid, ok := entity["uid"]; ok {
126+
if me.Uid == uid.(int) {
127+
return true
128+
}
129+
}
130+
131+
if username, ok := entity["username"]; ok {
132+
if me.Username == username.(string) {
133+
return true
134+
}
135+
}
136+
}
137+
138+
return false
139+
}

src/logic/data.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ var (
3939
WebsiteSetting = model.WebsiteSetting
4040

4141
DefaultAvatars []string
42+
43+
userSettingLocker sync.RWMutex
44+
UserSetting map[string]int
4245
)
4346

4447
// 将所有 权限 加载到内存中;后台修改权限时,重新加载一次
@@ -169,6 +172,27 @@ func LoadWebsiteSetting() error {
169172
return nil
170173
}
171174

175+
func LoadUserSetting() error {
176+
userSettings := make([]*model.UserSetting, 0)
177+
err := MasterDB.Find(&userSettings)
178+
if err != nil {
179+
logger.Errorln("LoadUserSetting Find fail:", err)
180+
return err
181+
}
182+
183+
userSettingLocker.Lock()
184+
defer userSettingLocker.Unlock()
185+
186+
UserSetting = make(map[string]int)
187+
for _, userSetting := range userSettings {
188+
UserSetting[userSetting.Key] = userSetting.Value
189+
}
190+
191+
logger.Infoln("LoadUserSetting successfully!")
192+
193+
return nil
194+
}
195+
172196
func LoadDefaultAvatar() error {
173197
defaultAvatars := make([]*model.DefaultAvatar, 0)
174198
err := MasterDB.Find(&defaultAvatars)

src/logic/project.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ func (self ProjectLogic) Publish(ctx context.Context, user *model.Me, form url.V
4343
objLog.Errorln("Publish Project find error:", err)
4444
return
4545
}
46-
if project.Username != user.Username && !user.IsAdmin {
46+
47+
if !CanEdit(user, project) {
4748
err = NotModifyAuthorityErr
4849
return
4950
}

src/logic/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (ResourceLogic) Publish(ctx context.Context, me *model.Me, form url.Values)
3939
return
4040
}
4141

42-
if resource.Uid != uid && !me.IsAdmin {
42+
if !CanEdit(me, resource) {
4343
err = NotModifyAuthorityErr
4444
return
4545
}

src/logic/topic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (self TopicLogic) Publish(ctx context.Context, me *model.Me, form url.Value
4040
return
4141
}
4242

43-
if topic.Uid != me.Uid && !me.IsAdmin {
43+
if !CanEdit(me, topic) {
4444
err = NotModifyAuthorityErr
4545
return
4646
}

src/logic/wiki.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
package logic
88

99
import (
10+
"errors"
1011
"net/url"
12+
"strconv"
1113
"strings"
1214

1315
. "db"
@@ -47,6 +49,46 @@ func (WikiLogic) Create(ctx context.Context, me *model.Me, form url.Values) erro
4749
return nil
4850
}
4951

52+
func (self WikiLogic) Modify(ctx context.Context, me *model.Me, form url.Values) error {
53+
objLog := GetLogger(ctx)
54+
55+
id := goutils.MustInt(form.Get("id"))
56+
wiki := self.FindById(ctx, id)
57+
if !CanEdit(me, wiki) {
58+
return errors.New("没有权限")
59+
}
60+
61+
if wiki.Uid != me.Uid {
62+
hasExists := false
63+
cuids := strings.Split(wiki.Cuid, ",")
64+
for _, cuid := range cuids {
65+
if me.Uid == goutils.MustInt(cuid) {
66+
hasExists = true
67+
break
68+
}
69+
}
70+
71+
if !hasExists {
72+
cuids = append(cuids, strconv.Itoa(me.Uid))
73+
wiki.Cuid = strings.Join(cuids, ",")
74+
}
75+
}
76+
77+
wiki.Title = form.Get("title")
78+
wiki.Content = form.Get("content")
79+
80+
_, err := MasterDB.Id(id).Update(wiki)
81+
if err != nil {
82+
objLog.Errorf("更新wiki 【%d】 信息失败:%s\n", id, err)
83+
return err
84+
}
85+
86+
// 修改wiki,活跃度+2
87+
go DefaultUser.IncrUserWeight("uid", me.Uid, 2)
88+
89+
return nil
90+
}
91+
5092
// FindBy 获取 wiki 列表(分页)
5193
func (WikiLogic) FindBy(ctx context.Context, limit int, lastIds ...int) []*model.Wiki {
5294
objLog := GetLogger(ctx)
@@ -76,6 +118,18 @@ func (WikiLogic) FindBy(ctx context.Context, limit int, lastIds ...int) []*model
76118
return wikis
77119
}
78120

121+
// FindById 通过ID获取Wiki
122+
func (WikiLogic) FindById(ctx context.Context, id int) *model.Wiki {
123+
objLog := GetLogger(ctx)
124+
125+
wiki := &model.Wiki{}
126+
if _, err := MasterDB.Where("id=?", id).Get(wiki); err != nil {
127+
objLog.Errorln("wiki logic FindById error:", err)
128+
return nil
129+
}
130+
return wiki
131+
}
132+
79133
// FindOne 某个wiki页面详细信息
80134
func (WikiLogic) FindOne(ctx context.Context, uri string) *model.Wiki {
81135
objLog := GetLogger(ctx)

src/model/user_setting.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author: polaris [email protected]
6+
7+
package model
8+
9+
import "time"
10+
11+
const (
12+
KeyNewUserWait = "new_user_wait" // 新用户注册多久能发布主题,单位秒,0表示没限制
13+
KeyCanEditTime = "can_edit_time" // 发布后多久内能够编辑,单位秒
14+
)
15+
16+
type UserSetting struct {
17+
Id int `xorm:"pk autoincr"`
18+
Key string
19+
Value int
20+
CreatedAt time.Time `xorm:"created"`
21+
}

src/server/studygolang/background.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func loadData() {
6060
logic.LoadCategories()
6161
logic.LoadWebsiteSetting()
6262
logic.LoadDefaultAvatar()
63+
logic.LoadUserSetting()
6364

6465
for {
6566
select {
@@ -69,6 +70,8 @@ func loadData() {
6970
logic.LoadRoles()
7071
case <-global.RoleAuthChan:
7172
logic.LoadRoleAuthorities()
73+
case <-global.UserSettingChan:
74+
logic.LoadUserSetting()
7275
}
7376
}
7477
}

template/articles/detail.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
<div class="title text-center">
1616
<h1 id="title" data-id="{{.article.Id}}">
1717
{{.article.Title}}
18-
{{if .me}}
19-
{{if or (eq .me.Username .article.Author) .me.IsRoot}}
18+
{{if canEdit .me .article}}
2019
<button id="edit" type="button" class="btn btn-primary btn-xs">编辑</button>
21-
{{end}}
2220
{{end}}
2321
</h1>
2422
</div>

template/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,13 @@ <h2><a href="/articles/[%:id%]" target="_blank" title="[%:title%]">
460460
];
461461

462462
$.views.settings.delimiters("[%", "%]");
463-
$.views.converters("substring", function(val, len, suffix='...') {
463+
$.views.converters("substring", function(val, len) {
464464
if (val.length > len) {
465+
if (arguments.length >= 3) {
466+
suffix = arguments[2];
467+
} else {
468+
suffix = '...';
469+
}
465470
val = val.substr(0, len)+suffix;
466471
}
467472
return val;

template/projects/detail.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
<h1>
1717
{{if .project.Logo}}<img src="{{.project.Logo}}" alt="{{.project.Name}}" width="48px" />{{end}}
1818
{{.project.Category}} <u>{{.project.Name}}</u>
19-
{{if .me}}
20-
{{if or (eq .me.Username .project.Username) .me.IsAdmin}}
21-
<a class="edit" href="/project/modify?id={{.project.Id}}" title="修改"><i class="glyphicon glyphicon-pencil"></i></a>
22-
{{end}}
19+
{{if canEdit .me .project}}
20+
<a class="btn btn-primary btn-xs" href="/project/modify?id={{.project.Id}}" title="编辑">编辑</a>
2321
{{end}}
2422
</h1>
2523
</div>

template/resources/detail.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
<div class="title text-center">
1717
<h1>
1818
{{.resource.title}}
19-
{{if .me}}
20-
{{if or (eq .me.Username .resource.user.Username) .me.IsAdmin}}
21-
<a class="edit" href="/resources/modify?id={{.resource.id}}" title="编辑"><i class="glyphicon glyphicon-pencil"></i> 编辑</a>
22-
{{end}}
19+
{{if canEdit .me .resource}}
20+
<a class="btn btn-primary btn-xs" href="/resources/modify?id={{.resource.id}}" title="编辑">编辑</a>
2321
{{end}}
2422
</h1>
2523
</div>

0 commit comments

Comments
 (0)