2017-04-20 18:19:32 +08:00
|
|
|
package controllers
|
|
|
|
|
2017-04-26 18:17:38 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-26 14:26:19 +08:00
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2021-04-12 21:12:26 +08:00
|
|
|
"github.com/beego/i18n"
|
2021-03-23 15:09:17 +08:00
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
"github.com/mindoc-org/mindoc/models"
|
2017-04-26 18:17:38 +08:00
|
|
|
)
|
|
|
|
|
2017-04-20 18:19:32 +08:00
|
|
|
type BookMemberController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:17:38 +08:00
|
|
|
// AddMember 参加参与用户.
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *BookMemberController) AddMember() {
|
2017-04-26 18:17:38 +08:00
|
|
|
identify := c.GetString("identify")
|
2021-03-30 14:24:14 +08:00
|
|
|
account, _ := c.GetInt("account")
|
2018-11-05 18:50:01 +08:00
|
|
|
roleId, _ := c.GetInt("role_id", 3)
|
2021-03-30 14:24:14 +08:00
|
|
|
logs.Info(account)
|
2018-02-03 15:25:10 +08:00
|
|
|
if identify == "" || account <= 0 {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := c.IsPermission()
|
2017-04-26 18:17:38 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6001, err.Error())
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
member := models.NewMember()
|
|
|
|
|
2018-02-03 15:25:10 +08:00
|
|
|
if _, err := member.Find(account); err != nil {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(404, i18n.Tr(c.Lang, "message.user_not_existed"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
if member.Status == 1 {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.user_disable"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if _, err := models.NewRelationship().FindForRoleId(book.BookId, member.MemberId); err == nil {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.user_exist_in_proj"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
relationship := models.NewRelationship()
|
|
|
|
relationship.BookId = book.BookId
|
|
|
|
relationship.MemberId = member.MemberId
|
2018-11-05 18:50:01 +08:00
|
|
|
relationship.RoleId = conf.BookRole(roleId)
|
2017-04-26 18:17:38 +08:00
|
|
|
|
|
|
|
if err := relationship.Insert(); err == nil {
|
|
|
|
memberRelationshipResult := models.NewMemberRelationshipResult().FromMember(member)
|
2018-11-05 18:50:01 +08:00
|
|
|
memberRelationshipResult.RoleId = conf.BookRole(roleId)
|
2017-04-26 18:17:38 +08:00
|
|
|
memberRelationshipResult.RelationshipId = relationship.RelationshipId
|
|
|
|
memberRelationshipResult.BookId = book.BookId
|
2021-04-12 21:12:26 +08:00
|
|
|
memberRelationshipResult.ResolveRoleName(c.Lang)
|
2017-04-26 18:17:38 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok", memberRelationshipResult)
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(500, err.Error())
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
2017-04-26 18:17:38 +08:00
|
|
|
// 变更指定用户在指定项目中的权限
|
|
|
|
func (c *BookMemberController) ChangeRole() {
|
|
|
|
identify := c.GetString("identify")
|
2018-11-05 18:50:01 +08:00
|
|
|
memberId, _ := c.GetInt("member_id", 0)
|
2018-01-26 17:17:38 +08:00
|
|
|
role, _ := c.GetInt("role_id", 0)
|
2017-04-26 18:17:38 +08:00
|
|
|
|
2018-11-05 18:50:01 +08:00
|
|
|
if identify == "" || memberId <= 0 {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-11-05 18:50:01 +08:00
|
|
|
if memberId == c.Member.MemberId {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(6006, i18n.Tr(c.Lang, "message.cannot_change_own_priv"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
|
2017-04-26 18:17:38 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(403, i18n.Tr(c.Lang, "message.no_permission"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
if err == orm.ErrNoRows {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(404, i18n.Tr(c.Lang, "message.item_not_exist"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6002, err.Error())
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
if book.RoleId != 0 && book.RoleId != 1 {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(403, i18n.Tr(c.Lang, "message.no_permission"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
member := models.NewMember()
|
|
|
|
|
2018-11-05 18:50:01 +08:00
|
|
|
if _, err := member.Find(memberId); err != nil {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.user_not_existed"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
if member.Status == 1 {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(6004, i18n.Tr(c.Lang, "message.user_disable"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
|
2018-11-05 18:50:01 +08:00
|
|
|
relationship, err := models.NewRelationship().UpdateRoleId(book.BookId, memberId, conf.BookRole(role))
|
2017-04-26 18:17:38 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
logs.Error("变更用户在项目中的权限 => ", err)
|
|
|
|
c.JsonResult(6005, err.Error())
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memberRelationshipResult := models.NewMemberRelationshipResult().FromMember(member)
|
|
|
|
memberRelationshipResult.RoleId = relationship.RoleId
|
|
|
|
memberRelationshipResult.RelationshipId = relationship.RelationshipId
|
|
|
|
memberRelationshipResult.BookId = book.BookId
|
2021-04-12 21:12:26 +08:00
|
|
|
memberRelationshipResult.ResolveRoleName(c.Lang)
|
2017-04-26 18:17:38 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok", memberRelationshipResult)
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 删除参与者.
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *BookMemberController) RemoveMember() {
|
2017-04-26 18:17:38 +08:00
|
|
|
identify := c.GetString("identify")
|
2018-01-26 17:17:38 +08:00
|
|
|
member_id, _ := c.GetInt("member_id", 0)
|
2017-04-26 18:17:38 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if identify == "" || member_id <= 0 {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
if member_id == c.Member.MemberId {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(6006, i18n.Tr(c.Lang, "message.cannot_delete_self"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
|
2017-04-26 18:17:38 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(403, i18n.Tr(c.Lang, "message.no_permission"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
if err == orm.ErrNoRows {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(404, i18n.Tr(c.Lang, "message.item_not_exist"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6002, err.Error())
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
//如果不是创始人也不是管理员则不能操作
|
|
|
|
if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
|
2021-04-12 21:12:26 +08:00
|
|
|
c.JsonResult(403, i18n.Tr(c.Lang, "message.no_permission"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
err = models.NewRelationship().DeleteByBookIdAndMemberId(book.BookId, member_id)
|
2017-04-20 18:19:32 +08:00
|
|
|
|
2017-04-26 18:17:38 +08:00
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(6007, err.Error())
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
c.JsonResult(0, "ok")
|
2017-04-20 18:19:32 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *BookMemberController) IsPermission() (*models.BookResult, error) {
|
2017-04-26 18:17:38 +08:00
|
|
|
identify := c.GetString("identify")
|
2018-01-26 17:17:38 +08:00
|
|
|
book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
|
2017-04-20 18:19:32 +08:00
|
|
|
|
2017-04-26 18:17:38 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
2021-04-12 21:12:26 +08:00
|
|
|
return book, errors.New(i18n.Tr(c.Lang, "message.no_permission"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
if err == orm.ErrNoRows {
|
2021-04-12 21:12:26 +08:00
|
|
|
return book, errors.New(i18n.Tr(c.Lang, "message.item_not_exist"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
return book, err
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
|
|
|
if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
|
2021-04-12 21:12:26 +08:00
|
|
|
return book, errors.New(i18n.Tr(c.Lang, "message.no_permission"))
|
2017-04-26 18:17:38 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
return book, nil
|
|
|
|
}
|