1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00

pointers to Runtime!

This commit is contained in:
Harvey Kandola 2017-07-24 19:10:49 +01:00
parent 792c3e2ce8
commit 27640dffc4
15 changed files with 315 additions and 82 deletions

View file

@ -18,7 +18,7 @@ import (
// Handler contains the runtime information such as logging and database. // Handler contains the runtime information such as logging and database.
type Handler struct { type Handler struct {
Runtime env.Runtime Runtime *env.Runtime
} }
// Account links a User to an Organization. // Account links a User to an Organization.

View file

@ -23,7 +23,7 @@ import (
) )
// GenerateJWT generates JSON Web Token (http://jwt.io) // GenerateJWT generates JSON Web Token (http://jwt.io)
func GenerateJWT(rt env.Runtime, user, org, domain string) string { func GenerateJWT(rt *env.Runtime, user, org, domain string) string {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"iss": "Documize", "iss": "Documize",
"sub": "webapp", "sub": "webapp",
@ -61,7 +61,7 @@ func FindJWT(r *http.Request) (token string) {
} }
// DecodeJWT decodes raw token. // DecodeJWT decodes raw token.
func DecodeJWT(rt env.Runtime, tokenString string) (c domain.RequestContext, claims jwt.Claims, err error) { func DecodeJWT(rt *env.Runtime, tokenString string) (c domain.RequestContext, claims jwt.Claims, err error) {
// sensible defaults // sensible defaults
c.UserID = "" c.UserID = ""
c.OrgID = "" c.OrgID = ""

View file

@ -18,7 +18,7 @@ import (
// Handler contains the runtime information such as logging and database. // Handler contains the runtime information such as logging and database.
type Handler struct { type Handler struct {
Runtime env.Runtime Runtime *env.Runtime
} }
// AuthenticationModel details authentication token and user details. // AuthenticationModel details authentication token and user details.

View file

@ -53,12 +53,11 @@ func GetRequestContext(r *http.Request) RequestContext {
// StoreContext provides data persistence methods with runtime and request context. // StoreContext provides data persistence methods with runtime and request context.
type StoreContext struct { type StoreContext struct {
Runtime env.Runtime Runtime *env.Runtime
Context RequestContext Context RequestContext
} }
// NewContexts returns request scoped user context and store context for persistence logic. // NewContext returns request scoped user context and store context for persistence logic.
func NewContexts(rt env.Runtime, r *http.Request) (RequestContext, StoreContext) { func NewContext(rt *env.Runtime, r *http.Request) StoreContext {
ctx := GetRequestContext(r) return StoreContext{Runtime: rt, Context: GetRequestContext(r)}
return ctx, StoreContext{Runtime: rt, Context: ctx}
} }

View file

@ -18,7 +18,7 @@ import (
// Handler contains the runtime information such as logging and database. // Handler contains the runtime information such as logging and database.
type Handler struct { type Handler struct {
Runtime env.Runtime Runtime *env.Runtime
} }
// Organization defines a company that uses this app. // Organization defines a company that uses this app.

236
domain/pin/endpoint.go Normal file
View file

@ -0,0 +1,236 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package pin
import (
"database/sql"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"github.com/documize/community/core/request"
"github.com/documize/community/core/response"
"github.com/documize/community/core/uniqueid"
"github.com/documize/community/domain"
"github.com/documize/community/domain/eventing"
)
// Add saves pinned item.
func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
method := "pin.Add"
s := domain.NewContext(h.Runtime, r)
userID := request.Param(r, "userID")
if len(userID) == 0 {
response.WriteMissingDataError(w, method, "userID")
return
}
if !h.Runtime.Product.License.IsValid() {
response.WriteBadLicense(w)
return
}
if !s.Context.Authenticated {
response.WriteForbiddenError(w)
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
response.WriteBadRequestError(w, method, "body")
return
}
var pin Pin
err = json.Unmarshal(body, &pin)
if err != nil {
response.WriteBadRequestError(w, method, "pin")
return
}
pin.RefID = uniqueid.Generate()
pin.OrgID = s.Context.OrgID
pin.UserID = s.Context.UserID
pin.Pin = strings.TrimSpace(pin.Pin)
if len(pin.Pin) > 20 {
pin.Pin = pin.Pin[0:20]
}
s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
response.WriteServerError(w, method, err)
return
}
err = Add(s, pin)
if err != nil {
s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err)
return
}
eventing.Record(s, eventing.EventTypePinAdd)
s.Context.Transaction.Commit()
newPin, err := GetPin(s, pin.RefID)
if err != nil {
response.WriteServerError(w, method, err)
return
}
response.WriteJSON(w, newPin)
}
// GetUserPins returns users' pins.
func (h *Handler) GetUserPins(w http.ResponseWriter, r *http.Request) {
method := "pin.GetUserPins"
s := domain.NewContext(h.Runtime, r)
userID := request.Param(r, "userID")
if len(userID) == 0 {
response.WriteMissingDataError(w, method, "userID")
return
}
if !s.Context.Authenticated {
response.WriteForbiddenError(w)
return
}
pins, err := GetUserPins(s, userID)
if err != nil && err != sql.ErrNoRows {
response.WriteServerError(w, method, err)
return
}
if err == sql.ErrNoRows {
pins = []Pin{}
}
response.WriteJSON(w, pins)
}
// DeleteUserPin removes saved user pin.
func (h *Handler) DeleteUserPin(w http.ResponseWriter, r *http.Request) {
method := "pin.DeleteUserPin"
s := domain.NewContext(h.Runtime, r)
userID := request.Param(r, "userID")
if len(userID) == 0 {
response.WriteMissingDataError(w, method, "userID")
return
}
pinID := request.Param(r, "pinID")
if len(pinID) == 0 {
response.WriteMissingDataError(w, method, "pinID")
return
}
if !h.Runtime.Product.License.IsValid() {
response.WriteBadLicense(w)
return
}
if s.Context.UserID != userID {
response.WriteForbiddenError(w)
return
}
var err error
s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
response.WriteServerError(w, method, err)
return
}
_, err = DeletePin(s, pinID)
if err != nil && err != sql.ErrNoRows {
s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err)
return
}
eventing.Record(s, eventing.EventTypePinDelete)
s.Context.Transaction.Commit()
response.WriteEmpty(w)
}
// UpdatePinSequence records order of pinned items.
func (h *Handler) UpdatePinSequence(w http.ResponseWriter, r *http.Request) {
method := "pin.DeleteUserPin"
s := domain.NewContext(h.Runtime, r)
userID := request.Param(r, "userID")
if len(userID) == 0 {
response.WriteMissingDataError(w, method, "userID")
return
}
if !h.Runtime.Product.License.IsValid() {
response.WriteBadLicense(w)
return
}
if !s.Context.Authenticated {
response.WriteForbiddenError(w)
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
response.WriteBadRequestError(w, method, err.Error())
return
}
var pins []string
err = json.Unmarshal(body, &pins)
if err != nil {
response.WriteBadRequestError(w, method, err.Error())
return
}
s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
response.WriteServerError(w, method, err)
return
}
for k, v := range pins {
err = UpdatePinSequence(s, v, k+1)
if err != nil {
s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err)
return
}
}
eventing.Record(s, eventing.EventTypePinResequence)
s.Context.Transaction.Commit()
newPins, err := GetUserPins(s, userID)
if err != nil {
response.WriteServerError(w, method, err)
return
}
response.WriteJSON(w, newPins)
}

View file

@ -18,7 +18,7 @@ import (
// Handler contains the runtime information such as logging and database. // Handler contains the runtime information such as logging and database.
type Handler struct { type Handler struct {
Runtime env.Runtime Runtime *env.Runtime
} }
// Pin defines a saved link to a document or space // Pin defines a saved link to a document or space

View file

@ -41,14 +41,14 @@ import (
// Add creates a new space. // Add creates a new space.
func (h *Handler) Add(w http.ResponseWriter, r *http.Request) { func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
method := "AddSpace" method := "AddSpace"
ctx, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
if !h.Runtime.Product.License.IsValid() { if !h.Runtime.Product.License.IsValid() {
response.WriteBadLicense(w) response.WriteBadLicense(w)
return return
} }
if !ctx.Editor { if !s.Context.Editor {
response.WriteForbiddenError(w) response.WriteForbiddenError(w)
return return
} }
@ -72,25 +72,25 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
return return
} }
ctx.Transaction, err = h.Runtime.Db.Beginx() s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
space.RefID = uniqueid.Generate() space.RefID = uniqueid.Generate()
space.OrgID = ctx.OrgID space.OrgID = s.Context.OrgID
err = addSpace(s, space) err = addSpace(s, space)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
eventing.Record(s, eventing.EventTypeSpaceAdd) eventing.Record(s, eventing.EventTypeSpaceAdd)
ctx.Transaction.Commit() s.Context.Transaction.Commit()
space, _ = Get(s, space.RefID) space, _ = Get(s, space.RefID)
@ -100,7 +100,7 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
// Get returns the requested space. // Get returns the requested space.
func (h *Handler) Get(w http.ResponseWriter, r *http.Request) { func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
method := "Get" method := "Get"
_, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
id := request.Param(r, "folderID") id := request.Param(r, "folderID")
if len(id) == 0 { if len(id) == 0 {
@ -124,7 +124,7 @@ func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
// GetAll returns spaces the user can see. // GetAll returns spaces the user can see.
func (h *Handler) GetAll(w http.ResponseWriter, r *http.Request) { func (h *Handler) GetAll(w http.ResponseWriter, r *http.Request) {
method := "GetAll" method := "GetAll"
_, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
sp, err := GetAll(s) sp, err := GetAll(s)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
@ -142,7 +142,7 @@ func (h *Handler) GetAll(w http.ResponseWriter, r *http.Request) {
// GetSpaceViewers returns the users that can see the shared spaces. // GetSpaceViewers returns the users that can see the shared spaces.
func (h *Handler) GetSpaceViewers(w http.ResponseWriter, r *http.Request) { func (h *Handler) GetSpaceViewers(w http.ResponseWriter, r *http.Request) {
method := "GetSpaceViewers" method := "GetSpaceViewers"
_, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
v, err := Viewers(s) v, err := Viewers(s)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
@ -160,9 +160,9 @@ func (h *Handler) GetSpaceViewers(w http.ResponseWriter, r *http.Request) {
// Update processes request to save space object to the database // Update processes request to save space object to the database
func (h *Handler) Update(w http.ResponseWriter, r *http.Request) { func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
method := "space.Update" method := "space.Update"
ctx, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
if !ctx.Editor { if !s.Context.Editor {
response.WriteForbiddenError(w) response.WriteForbiddenError(w)
return return
} }
@ -194,7 +194,7 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
sp.RefID = folderID sp.RefID = folderID
ctx.Transaction, err = h.Runtime.Db.Beginx() s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
@ -202,14 +202,14 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
err = Update(s, sp) err = Update(s, sp)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
eventing.Record(s, eventing.EventTypeSpaceUpdate) eventing.Record(s, eventing.EventTypeSpaceUpdate)
ctx.Transaction.Commit() s.Context.Transaction.Commit()
response.WriteJSON(w, sp) response.WriteJSON(w, sp)
} }
@ -217,14 +217,14 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
// Remove moves documents to another folder before deleting it // Remove moves documents to another folder before deleting it
func (h *Handler) Remove(w http.ResponseWriter, r *http.Request) { func (h *Handler) Remove(w http.ResponseWriter, r *http.Request) {
method := "space.Remove" method := "space.Remove"
ctx, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
if !h.Runtime.Product.License.IsValid() { if !h.Runtime.Product.License.IsValid() {
response.WriteBadLicense(w) response.WriteBadLicense(w)
return return
} }
if !ctx.Editor { if !s.Context.Editor {
response.WriteForbiddenError(w) response.WriteForbiddenError(w)
return return
} }
@ -242,7 +242,7 @@ func (h *Handler) Remove(w http.ResponseWriter, r *http.Request) {
} }
var err error var err error
ctx.Transaction, err = h.Runtime.Db.Beginx() s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
@ -250,35 +250,35 @@ func (h *Handler) Remove(w http.ResponseWriter, r *http.Request) {
_, err = Delete(s, id) _, err = Delete(s, id)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
err = document.MoveDocumentSpace(s, id, move) err = document.MoveDocumentSpace(s, id, move)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
err = MoveSpaceRoles(s, id, move) err = MoveSpaceRoles(s, id, move)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
_, err = pin.DeletePinnedSpace(s, id) _, err = pin.DeletePinnedSpace(s, id)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
eventing.Record(s, eventing.EventTypeSpaceDelete) eventing.Record(s, eventing.EventTypeSpaceDelete)
ctx.Transaction.Commit() s.Context.Transaction.Commit()
response.WriteEmpty(w) response.WriteEmpty(w)
} }
@ -286,14 +286,14 @@ func (h *Handler) Remove(w http.ResponseWriter, r *http.Request) {
// Delete deletes empty space. // Delete deletes empty space.
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) { func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
method := "space.Delete" method := "space.Delete"
ctx, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
if !h.Runtime.Product.License.IsValid() { if !h.Runtime.Product.License.IsValid() {
response.WriteBadLicense(w) response.WriteBadLicense(w)
return return
} }
if !ctx.Editor { if !s.Context.Editor {
response.WriteForbiddenError(w) response.WriteForbiddenError(w)
return return
} }
@ -305,7 +305,7 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
} }
var err error var err error
ctx.Transaction, err = h.Runtime.Db.Beginx() s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
@ -313,37 +313,37 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
_, err = Delete(s, id) _, err = Delete(s, id)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
_, err = DeleteSpaceRoles(s, id) _, err = DeleteSpaceRoles(s, id)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
_, err = pin.DeletePinnedSpace(s, id) _, err = pin.DeletePinnedSpace(s, id)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
eventing.Record(s, eventing.EventTypeSpaceDelete) eventing.Record(s, eventing.EventTypeSpaceDelete)
ctx.Transaction.Commit() s.Context.Transaction.Commit()
response.WriteEmpty(w) response.WriteEmpty(w)
} }
// SetPermissions persists specified spac3 permissions // SetPermissions persists specified spac3 permissions
func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) { func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) {
method := "space.SetPermissions" method := "space.SetPermissions"
ctx, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
if !ctx.Editor { if !s.Context.Editor {
response.WriteForbiddenError(w) response.WriteForbiddenError(w)
return return
} }
@ -379,7 +379,7 @@ func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) {
return return
} }
ctx.Transaction, err = h.Runtime.Db.Beginx() s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
@ -389,7 +389,7 @@ func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) {
// Why? So we can send out folder invitation emails. // Why? So we can send out folder invitation emails.
previousRoles, err := GetRoles(s, id) previousRoles, err := GetRoles(s, id)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -404,7 +404,7 @@ func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) {
// Who is sharing this folder? // Who is sharing this folder?
inviter, err := user.Get(s, s.Context.UserID) inviter, err := user.Get(s, s.Context.UserID)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -412,7 +412,7 @@ func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) {
// Nuke all previous permissions for this folder // Nuke all previous permissions for this folder
_, err = DeleteSpaceRoles(s, id) _, err = DeleteSpaceRoles(s, id)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -479,7 +479,7 @@ func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) {
err = AddRole(s, role) err = AddRole(s, role)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -498,14 +498,14 @@ func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) {
err = Update(s, sp) err = Update(s, sp)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
eventing.Record(s, eventing.EventTypeSpacePermission) eventing.Record(s, eventing.EventTypeSpacePermission)
ctx.Transaction.Commit() s.Context.Transaction.Commit()
response.WriteEmpty(w) response.WriteEmpty(w)
} }
@ -513,7 +513,7 @@ func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) {
// GetPermissions returns user permissions for the requested folder. // GetPermissions returns user permissions for the requested folder.
func (h *Handler) GetPermissions(w http.ResponseWriter, r *http.Request) { func (h *Handler) GetPermissions(w http.ResponseWriter, r *http.Request) {
method := "space.GetPermissions" method := "space.GetPermissions"
_, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
folderID := request.Param(r, "folderID") folderID := request.Param(r, "folderID")
if len(folderID) == 0 { if len(folderID) == 0 {
@ -537,7 +537,7 @@ func (h *Handler) GetPermissions(w http.ResponseWriter, r *http.Request) {
// AcceptInvitation records the fact that a user has completed space onboard process. // AcceptInvitation records the fact that a user has completed space onboard process.
func (h *Handler) AcceptInvitation(w http.ResponseWriter, r *http.Request) { func (h *Handler) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
method := "space.AcceptInvitation" method := "space.AcceptInvitation"
ctx, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
folderID := request.Param(r, "folderID") folderID := request.Param(r, "folderID")
if len(folderID) == 0 { if len(folderID) == 0 {
@ -545,14 +545,13 @@ func (h *Handler) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
return return
} }
org, err := organization.GetOrganizationByDomain(s, ctx.Subdomain) org, err := organization.GetOrganizationByDomain(s, s.Context.Subdomain)
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
// AcceptShare does not authenticate the user hence the context needs to set up // AcceptShare does not authenticate the user hence the context needs to set up
ctx.OrgID = org.RefID
s.Context.OrgID = org.RefID s.Context.OrgID = org.RefID
defer streamutil.Close(r.Body) defer streamutil.Close(r.Body)
@ -581,14 +580,13 @@ func (h *Handler) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
} }
// AcceptShare does not authenticate the user hence the context needs to set up // AcceptShare does not authenticate the user hence the context needs to set up
ctx.UserID = u.RefID
s.Context.UserID = u.RefID s.Context.UserID = u.RefID
u.Firstname = model.Firstname u.Firstname = model.Firstname
u.Lastname = model.Lastname u.Lastname = model.Lastname
u.Initials = stringutil.MakeInitials(u.Firstname, u.Lastname) u.Initials = stringutil.MakeInitials(u.Firstname, u.Lastname)
ctx.Transaction, err = h.Runtime.Db.Beginx() s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
@ -596,7 +594,7 @@ func (h *Handler) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
err = user.UpdateUser(s, u) err = user.UpdateUser(s, u)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -605,14 +603,14 @@ func (h *Handler) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
err = user.UpdateUserPassword(s, u.RefID, salt, secrets.GeneratePassword(model.Password, salt)) err = user.UpdateUserPassword(s, u.RefID, salt, secrets.GeneratePassword(model.Password, salt))
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
eventing.Record(s, eventing.EventTypeSpaceJoin) eventing.Record(s, eventing.EventTypeSpaceJoin)
ctx.Transaction.Commit() s.Context.Transaction.Commit()
response.WriteJSON(w, u) response.WriteJSON(w, u)
} }
@ -620,7 +618,7 @@ func (h *Handler) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
// Invite sends users folder invitation emails. // Invite sends users folder invitation emails.
func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) { func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
method := "space.Invite" method := "space.Invite"
ctx, s := domain.NewContexts(h.Runtime, r) s := domain.NewContext(h.Runtime, r)
id := request.Param(r, "folderID") id := request.Param(r, "folderID")
if len(id) == 0 { if len(id) == 0 {
@ -653,13 +651,13 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
return return
} }
ctx.Transaction, err = h.Runtime.Db.Beginx() s.Context.Transaction, err = h.Runtime.Db.Beginx()
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
inviter, err := user.Get(s, ctx.UserID) inviter, err := user.Get(s, s.Context.UserID)
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
@ -668,7 +666,7 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
for _, email := range model.Recipients { for _, email := range model.Recipients {
u, err := user.GetByEmail(s, email) u, err := user.GetByEmail(s, email)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -677,7 +675,7 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
// Ensure they have access to this organization // Ensure they have access to this organization
accounts, err2 := account.GetUserAccounts(s, u.RefID) accounts, err2 := account.GetUserAccounts(s, u.RefID)
if err2 != nil { if err2 != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -702,7 +700,7 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
err = account.Add(s, a) err = account.Add(s, a)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -713,7 +711,7 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
role := Role{} role := Role{}
role.LabelID = sp.RefID role.LabelID = sp.RefID
role.OrgID = ctx.OrgID role.OrgID = s.Context.OrgID
role.UserID = u.RefID role.UserID = u.RefID
role.CanEdit = false role.CanEdit = false
role.CanView = true role.CanView = true
@ -722,23 +720,23 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
err = AddRole(s, role) err = AddRole(s, role)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
url := ctx.GetAppURL(fmt.Sprintf("s/%s/%s", sp.RefID, stringutil.MakeSlug(sp.Name))) url := s.Context.GetAppURL(fmt.Sprintf("s/%s/%s", sp.RefID, stringutil.MakeSlug(sp.Name)))
go mail.ShareFolderExistingUser(email, inviter.Fullname(), url, sp.Name, model.Message) go mail.ShareFolderExistingUser(email, inviter.Fullname(), url, sp.Name, model.Message)
h.Runtime.Log.Info(fmt.Sprintf("%s is sharing space %s with existing user %s", inviter.Email, sp.Name, email)) h.Runtime.Log.Info(fmt.Sprintf("%s is sharing space %s with existing user %s", inviter.Email, sp.Name, email))
} else { } else {
// On-board new user // On-board new user
if strings.Contains(email, "@") { if strings.Contains(email, "@") {
url := ctx.GetAppURL(fmt.Sprintf("auth/share/%s/%s", sp.RefID, stringutil.MakeSlug(sp.Name))) url := s.Context.GetAppURL(fmt.Sprintf("auth/share/%s/%s", sp.RefID, stringutil.MakeSlug(sp.Name)))
err = inviteNewUserToSharedSpace(s, email, inviter, url, sp, model.Message) err = inviteNewUserToSharedSpace(s, email, inviter, url, sp, model.Message)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -754,7 +752,7 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
err = Update(s, sp) err = Update(s, sp)
if err != nil { if err != nil {
ctx.Transaction.Rollback() s.Context.Transaction.Rollback()
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
return return
} }
@ -762,7 +760,7 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
eventing.Record(s, eventing.EventTypeSpaceInvite) eventing.Record(s, eventing.EventTypeSpaceInvite)
ctx.Transaction.Commit() s.Context.Transaction.Commit()
response.WriteEmpty(w) response.WriteEmpty(w)
} }

View file

@ -20,7 +20,7 @@ import (
// Handler contains the runtime information such as logging and database. // Handler contains the runtime information such as logging and database.
type Handler struct { type Handler struct {
Runtime env.Runtime Runtime *env.Runtime
} }
// Space defines a container for documents. // Space defines a container for documents.

View file

@ -21,7 +21,7 @@ import (
// Handler contains the runtime information such as logging and database. // Handler contains the runtime information such as logging and database.
type Handler struct { type Handler struct {
Runtime env.Runtime Runtime *env.Runtime
} }
// User defines a login. // User defines a login.

View file

@ -65,5 +65,5 @@ func main() {
section.Register(rt) section.Register(rt)
ready := make(chan struct{}, 1) // channel signals router ready ready := make(chan struct{}, 1) // channel signals router ready
server.Start(rt, ready) server.Start(&rt, ready)
} }

View file

@ -28,7 +28,7 @@ import (
) )
type middleware struct { type middleware struct {
Runtime env.Runtime Runtime *env.Runtime
} }
func (m *middleware) cors(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { func (m *middleware) cors(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
@ -194,7 +194,7 @@ func (m *middleware) Authorize(w http.ResponseWriter, r *http.Request, next http
// Certain assets/URL do not require authentication. // Certain assets/URL do not require authentication.
// Just stops the log files being clogged up with failed auth errors. // Just stops the log files being clogged up with failed auth errors.
func preAuthorizeStaticAssets(rt env.Runtime, r *http.Request) bool { func preAuthorizeStaticAssets(rt *env.Runtime, r *http.Request) bool {
if strings.ToLower(r.URL.Path) == "/" || if strings.ToLower(r.URL.Path) == "/" ||
strings.ToLower(r.URL.Path) == "/validate" || strings.ToLower(r.URL.Path) == "/validate" ||
strings.ToLower(r.URL.Path) == "/favicon.ico" || strings.ToLower(r.URL.Path) == "/favicon.ico" ||

View file

@ -21,7 +21,7 @@ import (
) )
// RegisterEndpoints register routes for serving API endpoints // RegisterEndpoints register routes for serving API endpoints
func RegisterEndpoints(rt env.Runtime) { func RegisterEndpoints(rt *env.Runtime) {
//************************************************** //**************************************************
// Non-secure routes // Non-secure routes
//************************************************** //**************************************************

View file

@ -62,7 +62,7 @@ func (s routeSorter) Less(i, j int) bool {
return s[i].ord < s[j].ord return s[i].ord < s[j].ord
} }
func routesKey(rt env.Runtime, prefix, path string, methods, queries []string) (string, error) { func routesKey(rt *env.Runtime, prefix, path string, methods, queries []string) (string, error) {
rd := routeDef{ rd := routeDef{
Prefix: prefix, Prefix: prefix,
Path: path, Path: path,
@ -79,7 +79,7 @@ func routesKey(rt env.Runtime, prefix, path string, methods, queries []string) (
} }
// Add an endpoint to those that will be processed when Serve() is called. // Add an endpoint to those that will be processed when Serve() is called.
func Add(rt env.Runtime, prefix, path string, methods, queries []string, endPtFn RouteFunc) error { func Add(rt *env.Runtime, prefix, path string, methods, queries []string, endPtFn RouteFunc) error {
k, e := routesKey(rt, prefix, path, methods, queries) k, e := routesKey(rt, prefix, path, methods, queries)
if e != nil { if e != nil {
return e return e
@ -89,7 +89,7 @@ func Add(rt env.Runtime, prefix, path string, methods, queries []string, endPtFn
} }
// Remove an endpoint. // Remove an endpoint.
func Remove(rt env.Runtime, prefix, path string, methods, queries []string) error { func Remove(rt *env.Runtime, prefix, path string, methods, queries []string) error {
k, e := routesKey(rt, prefix, path, methods, queries) k, e := routesKey(rt, prefix, path, methods, queries)
if e != nil { if e != nil {
return e return e
@ -99,7 +99,7 @@ func Remove(rt env.Runtime, prefix, path string, methods, queries []string) erro
} }
// BuildRoutes returns all matching routes for specified scope. // BuildRoutes returns all matching routes for specified scope.
func BuildRoutes(rt env.Runtime, prefix string) *mux.Router { func BuildRoutes(rt *env.Runtime, prefix string) *mux.Router {
var rs routeSorter var rs routeSorter
for k, v := range routes { for k, v := range routes {
var rd routeDef var rd routeDef

View file

@ -30,7 +30,7 @@ import (
var testHost string // used during automated testing var testHost string // used during automated testing
// Start router to handle all HTTP traffic. // Start router to handle all HTTP traffic.
func Start(rt env.Runtime, ready chan struct{}) { func Start(rt *env.Runtime, ready chan struct{}) {
err := plugins.LibSetup() err := plugins.LibSetup()
if err != nil { if err != nil {