Skip to content

Commit 4a770f2

Browse files
committed
项目操作权限
1 parent 8c84cdf commit 4a770f2

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

change.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
- [ ] 增加 wiki 功能
2828
- [ ] ElasticSearch支持搜索博客、wiki和实体书
2929
- [ ] 增加程序员工具箱功能(长期工作)
30+
- [ ] 管理后台,增加一个环境监测,用于检测网站各项依赖是否正常
31+
3032

3133
## v2.2 升级日志
3234

@@ -41,10 +43,9 @@
4143
- [x] 配置管理,增加图片链接由`http://`自动替换为`https://`开关,因为 --> [Chrome 将不再允许 https:// 页面加载 HTTP 资源](https://www.oschina.net/news/110345/chrome-to-block-http-in-https)
4244
- [x] 使用`Cache-Control`实现API缓存设置
4345
- [x] 如果只有作者才能写作,则如果项目创建者不是作者,已创建了项目的,不允许私有转公有、不允许私密分享
44-
- [ ] 项目成员PC端无法访问和编辑项目的问题处理
45-
- [ ] 管理员和创始人可以设置书籍信息以及生成电子书
46-
- [ ] 编辑者以上权限可以克隆、上传等对项目进行编辑操作
47-
- [ ] 管理后台,增加一个环境监测,用于检测网站各项依赖是否正常
46+
- [x] 项目成员PC端无法访问和编辑项目的问题处理
47+
- [x] 管理员和创始人可以设置书籍信息以及生成电子书
48+
- [x] 编辑者以上权限可以克隆、上传等对项目进行编辑操作
4849

4950

5051
## V2.1 升级日志

controllers/BookController.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -739,16 +739,19 @@ func (this *BookController) Release() {
739739
//加锁,防止用户不停地点击生成下载文档造成服务器资源开销.
740740
func (this *BookController) Generate() {
741741
identify := this.GetString(":key")
742-
book, err := models.NewBook().FindByIdentify(identify)
743742

744-
//书籍正在生成离线文档
745-
if isGenerating := utils.BooksGenerate.Exist(book.BookId); isGenerating {
746-
this.JsonResult(1, "上一次下载文档生成任务正在后台执行,请您稍后再执行新的下载文档生成操作")
743+
if !models.NewBook().HasProjectAccess(identify, this.Member.MemberId, conf.BookAdmin) {
744+
this.JsonResult(1, "您没有操作权限,只有项目创始人和项目管理员才有权限")
747745
}
748746

749-
if err != nil || book.MemberId != this.Member.MemberId {
747+
book, err := models.NewBook().FindByIdentify(identify)
748+
if err != nil {
750749
beego.Error(err)
751-
this.JsonResult(1, "项目不存在;或您不是文档创始人,没有文档生成权限")
750+
this.JsonResult(1, "项目不存在")
751+
}
752+
//书籍正在生成离线文档
753+
if isGenerating := utils.BooksGenerate.Exist(book.BookId); isGenerating {
754+
this.JsonResult(1, "上一次下载文档生成任务正在后台执行,请您稍后再执行新的下载文档生成操作")
752755
}
753756

754757
baseUrl := "http://localhost:" + beego.AppConfig.String("httpport")
@@ -811,6 +814,7 @@ func (this *BookController) SaveSort() {
811814
this.JsonResult(0, "ok")
812815
}
813816

817+
// 判断是否具有管理员或管理员以上权限
814818
func (this *BookController) IsPermission() (*models.BookResult, error) {
815819

816820
identify := this.GetString("identify")
@@ -876,16 +880,12 @@ func (this *BookController) GitPull() {
876880
//2、解压zip到当前目录,然后移除非图片文件
877881
//3、将文件夹移动到uploads目录下
878882

879-
if _, err := this.IsPermission(); err != nil {
880-
this.JsonResult(1, err.Error())
881-
}
883+
identify := this.GetString("identify")
882884

883-
//普通用户没有权限
884-
if this.Member.Role > 1 {
885-
this.JsonResult(1, "您没有操作权限")
885+
if !models.NewBook().HasProjectAccess(identify, this.Member.MemberId, conf.BookEditor) {
886+
this.JsonResult(1, "无操作权限")
886887
}
887888

888-
identify := this.GetString("identify")
889889
book, _ := models.NewBookResult().FindByIdentify(identify, this.Member.MemberId)
890890
if book.BookId == 0 {
891891
this.JsonResult(1, "导入失败,只有项目创建人才有权限导入项目")
@@ -910,21 +910,18 @@ func (this *BookController) UploadProject() {
910910
//1、接受上传上来的zip文件,并存放到store/temp目录下
911911
//2、解压zip到当前目录,然后移除非图片文件
912912
//3、将文件夹移动到uploads目录下
913-
if _, err := this.IsPermission(); err != nil {
914-
this.JsonResult(1, err.Error())
915-
}
916-
917-
//普通用户没法上传项目
918-
if this.Member.Role > 1 {
919-
this.JsonResult(1, "您没有操作权限")
920-
}
921913

922914
identify := this.GetString("identify")
923915

916+
if !models.NewBook().HasProjectAccess(identify, this.Member.MemberId, conf.BookEditor) {
917+
this.JsonResult(1, "无操作权限")
918+
}
919+
924920
book, _ := models.NewBookResult().FindByIdentify(identify, this.Member.MemberId)
925921
if book.BookId == 0 {
926-
this.JsonResult(1, "导入失败,只有项目创建人才有权限导入项目")
922+
this.JsonResult(1, "项目不存在")
927923
}
924+
928925
f, h, err := this.GetFile("zipfile")
929926
if err != nil {
930927
this.JsonResult(1, err.Error())

models/book.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ func NewBook() *Book {
8787
return &Book{}
8888
}
8989

90+
// minRole 最小的角色权限
91+
//conf.BookFounder
92+
//conf.BookAdmin
93+
//conf.BookEditor
94+
//conf.BookObserver
95+
func (m *Book) HasProjectAccess(identify string, memberId int, minRole int) bool {
96+
book := NewBook()
97+
rel := NewRelationship()
98+
o := orm.NewOrm()
99+
o.QueryTable(book).Filter("identify", identify).One(book, "book_id")
100+
if book.BookId <= 0 {
101+
return false
102+
}
103+
o.QueryTable(rel).Filter("book_id", book.BookId).Filter("member_id", memberId).One(rel)
104+
if rel.RelationshipId <= 0 {
105+
return false
106+
}
107+
return rel.RoleId <= minRole
108+
}
109+
90110
func (m *Book) Insert() (err error) {
91111
o := orm.NewOrm()
92112
if _, err = o.Insert(m); err != nil {

models/book_result.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (m *BookResult) FindByIdentify(identify string, memberId int) (result *Book
7878

7979
var relationship2 Relationship
8080

81-
err = o.QueryTable(relationship.TableNameWithPrefix()).Filter("book_id", book.BookId).Filter("role_id", 0).One(&relationship2)
81+
err = o.QueryTable(relationship.TableNameWithPrefix()).Filter("book_id", book.BookId).Filter("role_id", conf.BookFounder).One(&relationship2)
8282
if err != nil {
8383
logs.Error("根据项目标识查询项目以及指定用户权限的信息 => ", err)
8484
return result, ErrPermissionDenied

0 commit comments

Comments
 (0)