mirror of
https://github.com/documize/community.git
synced 2025-07-30 18:49:43 +02:00
join and leave group persistence
This commit is contained in:
parent
0680a72ee2
commit
e4d78904dc
6 changed files with 161 additions and 34 deletions
|
@ -243,3 +243,107 @@ func (h *Handler) GetGroupMembers(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
response.WriteJSON(w, m)
|
||||
}
|
||||
|
||||
// JoinGroup adds user to group.
|
||||
func (h *Handler) JoinGroup(w http.ResponseWriter, r *http.Request) {
|
||||
method := "group.JoinGroup"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
// Should be no reason for non-admin to see members
|
||||
if !ctx.Administrator {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
||||
groupID := request.Param(r, "groupID")
|
||||
if len(groupID) == 0 {
|
||||
response.WriteMissingDataError(w, method, "groupID")
|
||||
return
|
||||
}
|
||||
|
||||
userID := request.Param(r, "userID")
|
||||
if len(userID) == 0 {
|
||||
response.WriteMissingDataError(w, method, "userID")
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
// safety first
|
||||
err = h.Store.Group.LeaveGroup(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
// now we can join group
|
||||
err = h.Store.Group.JoinGroup(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
h.Store.Audit.Record(ctx, audit.EventTypeGroupJoin)
|
||||
|
||||
response.WriteEmpty(w)
|
||||
}
|
||||
|
||||
// LeaveGroup removes user to group.
|
||||
func (h *Handler) LeaveGroup(w http.ResponseWriter, r *http.Request) {
|
||||
method := "group.LeaveGroup"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
// Should be no reason for non-admin to see members
|
||||
if !ctx.Administrator {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
||||
groupID := request.Param(r, "groupID")
|
||||
if len(groupID) == 0 {
|
||||
response.WriteMissingDataError(w, method, "groupID")
|
||||
return
|
||||
}
|
||||
|
||||
userID := request.Param(r, "userID")
|
||||
if len(userID) == 0 {
|
||||
response.WriteMissingDataError(w, method, "userID")
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.Store.Group.LeaveGroup(ctx, groupID, userID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
h.Store.Audit.Record(ctx, audit.EventTypeGroupLeave)
|
||||
|
||||
response.WriteEmpty(w)
|
||||
}
|
||||
|
|
|
@ -120,3 +120,24 @@ func (s Scope) GetGroupMembers(ctx domain.RequestContext, groupID string) (membe
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
// JoinGroup adds user to group.
|
||||
func (s Scope) JoinGroup(ctx domain.RequestContext, groupID, userID string) (err error) {
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO rolemember (orgid, roleid, userid) VALUES (?, ?, ?)", ctx.OrgID, groupID, userID)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "insert group member")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// LeaveGroup removes user from group.
|
||||
func (s Scope) LeaveGroup(ctx domain.RequestContext, groupID, userID string) (err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
_, err = b.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM rolemember WHERE orgid=\"%s\" AND roleid=\"%s\" AND userid=\"%s\"", ctx.OrgID, groupID, userID))
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "clear group member")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -277,4 +277,6 @@ type GroupStorer interface {
|
|||
Update(ctx RequestContext, g group.Group) (err error)
|
||||
Delete(ctx RequestContext, refID string) (rows int64, err error)
|
||||
GetGroupMembers(ctx RequestContext, groupID string) (m []group.Member, err error)
|
||||
JoinGroup(ctx RequestContext, groupID, userID string) (err error)
|
||||
LeaveGroup(ctx RequestContext, groupID, userID string) (err error)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue