mirror of
https://github.com/documize/community.git
synced 2025-07-20 21:59:42 +02:00
Experimental TX refactoring
This commit is contained in:
parent
73d91a2dae
commit
09635b67ab
2 changed files with 161 additions and 62 deletions
61
core/env/runtime.go
vendored
61
core/env/runtime.go
vendored
|
@ -13,20 +13,13 @@
|
|||
package env
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// Runtime provides access to database, logger and other server-level scoped objects.
|
||||
// Use Context for per-request values.
|
||||
type Runtime struct {
|
||||
Flags Flags
|
||||
Db *sqlx.DB
|
||||
StoreProvider StoreProvider
|
||||
Log Logger
|
||||
Product domain.Product
|
||||
}
|
||||
|
||||
const (
|
||||
// SiteModeNormal serves app
|
||||
SiteModeNormal = ""
|
||||
|
@ -40,3 +33,51 @@ const (
|
|||
// SiteModeBadDB redirects to db-error.html page
|
||||
SiteModeBadDB = "3"
|
||||
)
|
||||
|
||||
// Runtime provides access to database, logger and other server-level scoped objects.
|
||||
// Use Context for per-request values.
|
||||
type Runtime struct {
|
||||
Flags Flags
|
||||
Db *sqlx.DB
|
||||
StoreProvider StoreProvider
|
||||
Log Logger
|
||||
Product domain.Product
|
||||
}
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
// StartTx beings database transaction with application defined
|
||||
// database transaction isolation level.
|
||||
// Any error encountered during this operation is logged to runtime logger.
|
||||
func (r *Runtime) StartTx() (tx *sqlx.Tx, ok bool) {
|
||||
tx, err := r.Db.BeginTxx(ctx, &sql.TxOptions{Isolation: sql.LevelReadUncommitted})
|
||||
if err != nil {
|
||||
r.Log.Error("unable to start database transaction", err)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return tx, true
|
||||
}
|
||||
|
||||
// Rollback aborts active database transaction.
|
||||
// Any error encountered during this operation is logged to runtime logger.
|
||||
func (r *Runtime) Rollback(tx *sqlx.Tx) bool {
|
||||
if err := tx.Commit(); err != nil {
|
||||
r.Log.Error("unable to commit database transaction", err)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Commit flushes pending changes to database.
|
||||
// Any error encountered during this operation is logged to runtime logger.
|
||||
func (r *Runtime) Commit(tx *sqlx.Tx) bool {
|
||||
if err := tx.Commit(); err != nil {
|
||||
r.Log.Error("unable to commit database transaction", err)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ import (
|
|||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/mail"
|
||||
"github.com/documize/community/domain/organization"
|
||||
perm "github.com/documize/community/domain/permission"
|
||||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/model/account"
|
||||
"github.com/documize/community/model/activity"
|
||||
|
@ -460,25 +461,6 @@ func (h *Handler) GetViewable(w http.ResponseWriter, r *http.Request) {
|
|||
response.WriteJSON(w, sp)
|
||||
}
|
||||
|
||||
// GetAll returns every space for documize admin users to manage
|
||||
func (h *Handler) GetAll(w http.ResponseWriter, r *http.Request) {
|
||||
method := "space.getAll"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
if !ctx.Administrator {
|
||||
response.WriteForbiddenError(w)
|
||||
h.Runtime.Log.Info("rejected non-admin user request for all spaces")
|
||||
return
|
||||
}
|
||||
|
||||
sp, err := h.Store.Space.GetAll(ctx)
|
||||
if err != nil {
|
||||
h.Runtime.Log.Error(method, err)
|
||||
}
|
||||
|
||||
response.WriteJSON(w, sp)
|
||||
}
|
||||
|
||||
// Update processes request to save space object to the database
|
||||
func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
method := "space.update"
|
||||
|
@ -691,72 +673,64 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
|
|||
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)
|
||||
ok := true
|
||||
ctx.Transaction, ok = h.Runtime.StartTx()
|
||||
if !ok {
|
||||
response.WriteError(w, method)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = h.Store.Document.DeleteBySpace(ctx, id)
|
||||
_, err := h.Store.Document.DeleteBySpace(ctx, id)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
h.Runtime.Rollback(ctx.Transaction)
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = h.Store.Permission.DeleteSpacePermissions(ctx, id)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
h.Runtime.Rollback(ctx.Transaction)
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
// remove category permissions
|
||||
_, err = h.Store.Permission.DeleteSpaceCategoryPermissions(ctx, id)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
h.Runtime.Rollback(ctx.Transaction)
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = h.Store.Pin.DeletePinnedSpace(ctx, id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
ctx.Transaction.Rollback()
|
||||
h.Runtime.Rollback(ctx.Transaction)
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
// remove category and members for space
|
||||
_, err = h.Store.Category.DeleteBySpace(ctx, id)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
h.Runtime.Rollback(ctx.Transaction)
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = h.Store.Space.Delete(ctx, id)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
h.Runtime.Rollback(ctx.Transaction)
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Close out the delete process
|
||||
ctx.Transaction.Commit()
|
||||
h.Runtime.Commit(ctx.Transaction)
|
||||
|
||||
// Record this action.
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
ctx.Transaction, ok = h.Runtime.StartTx()
|
||||
if !ok {
|
||||
response.WriteError(w, method)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -765,10 +739,12 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
|
|||
SourceType: activity.SourceTypeSpace,
|
||||
ActivityType: activity.TypeDeleted})
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
h.Runtime.Log.Error(method, err)
|
||||
h.Runtime.Rollback(ctx.Transaction)
|
||||
response.WriteServerError(w, method, err)
|
||||
}
|
||||
|
||||
h.Runtime.Commit(ctx.Transaction)
|
||||
|
||||
h.Store.Audit.Record(ctx, audit.EventTypeSpaceDelete)
|
||||
|
||||
event.Handler().Publish(string(event.TypeRemoveSpace))
|
||||
|
@ -1030,3 +1006,85 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
response.WriteEmpty(w)
|
||||
}
|
||||
|
||||
// Manage returns all shared spaces and orphaned spaces that have no owner.
|
||||
func (h *Handler) Manage(w http.ResponseWriter, r *http.Request) {
|
||||
method := "space.Manage"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
if !ctx.Administrator {
|
||||
response.WriteForbiddenError(w)
|
||||
h.Runtime.Log.Info("rejected non-admin user request for all spaces")
|
||||
return
|
||||
}
|
||||
|
||||
sp, err := h.Store.Space.AdminList(ctx)
|
||||
if err != nil {
|
||||
h.Runtime.Log.Error(method, err)
|
||||
}
|
||||
|
||||
response.WriteJSON(w, sp)
|
||||
}
|
||||
|
||||
// ManageOwner adds current user as space owner.
|
||||
// Requires admin rights.
|
||||
func (h *Handler) ManageOwner(w http.ResponseWriter, r *http.Request) {
|
||||
method := "space.ManageOwner"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
var err error
|
||||
|
||||
if !ctx.Administrator {
|
||||
response.WriteForbiddenError(w)
|
||||
h.Runtime.Log.Info("rejected space.ManageOwner")
|
||||
return
|
||||
}
|
||||
|
||||
id := request.Param(r, "spaceID")
|
||||
if len(id) == 0 {
|
||||
response.WriteMissingDataError(w, method, "spaceID")
|
||||
return
|
||||
}
|
||||
|
||||
// If they are already space owner, skip.
|
||||
isOwner := perm.HasPermission(ctx, *h.Store, id, permission.SpaceOwner)
|
||||
if isOwner {
|
||||
response.WriteEmpty(w)
|
||||
return
|
||||
}
|
||||
// We need to check if user can see space before we make them owner!
|
||||
isViewer := perm.HasPermission(ctx, *h.Store, id, permission.SpaceView)
|
||||
|
||||
// Add current user as space owner
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
perm := permission.Permission{}
|
||||
perm.OrgID = ctx.OrgID
|
||||
perm.Who = permission.UserPermission
|
||||
perm.WhoID = ctx.UserID
|
||||
perm.Scope = permission.ScopeRow
|
||||
perm.Location = permission.LocationSpace
|
||||
perm.RefID = id
|
||||
perm.Action = "" // we send allowable actions in function call...
|
||||
if !isViewer {
|
||||
err = h.Store.Permission.AddPermissions(ctx, perm, permission.SpaceOwner, permission.SpaceView)
|
||||
} else {
|
||||
err = h.Store.Permission.AddPermissions(ctx, perm, permission.SpaceOwner)
|
||||
}
|
||||
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.EventTypeAssumedSpaceOwnership)
|
||||
|
||||
response.WriteEmpty(w)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue