2016-07-07 18:54:16 -07:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
2016-10-17 14:00:06 -07:00
|
|
|
// This software (Documize Community Edition) is licensed under
|
2016-07-07 18:54:16 -07:00
|
|
|
// 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
|
2016-10-17 14:00:06 -07:00
|
|
|
// by contacting <sales@documize.com>.
|
2016-07-07 18:54:16 -07:00
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
2017-07-18 21:55:17 +01:00
|
|
|
// Package space handles API calls and persistence for spaces.
|
|
|
|
// Spaces in Documize contain documents.
|
|
|
|
package space
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
"github.com/documize/community/core/env"
|
2017-07-24 16:24:21 +01:00
|
|
|
"github.com/documize/community/core/request"
|
|
|
|
"github.com/documize/community/core/response"
|
|
|
|
"github.com/documize/community/core/secrets"
|
|
|
|
"github.com/documize/community/core/streamutil"
|
|
|
|
"github.com/documize/community/core/stringutil"
|
|
|
|
"github.com/documize/community/core/uniqueid"
|
|
|
|
"github.com/documize/community/domain"
|
2017-08-02 15:26:31 +01:00
|
|
|
"github.com/documize/community/domain/mail"
|
2017-08-31 18:01:07 +01:00
|
|
|
"github.com/documize/community/domain/organization"
|
2017-07-26 10:50:26 +01:00
|
|
|
"github.com/documize/community/model/account"
|
|
|
|
"github.com/documize/community/model/audit"
|
2017-08-21 17:51:06 +01:00
|
|
|
"github.com/documize/community/model/doc"
|
|
|
|
"github.com/documize/community/model/page"
|
2017-07-26 10:50:26 +01:00
|
|
|
"github.com/documize/community/model/space"
|
2017-08-21 17:51:06 +01:00
|
|
|
uuid "github.com/nu7hatch/gouuid"
|
2017-07-24 16:24:21 +01:00
|
|
|
)
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
// Handler contains the runtime information such as logging and database.
|
|
|
|
type Handler struct {
|
|
|
|
Runtime *env.Runtime
|
|
|
|
Store *domain.Store
|
|
|
|
}
|
|
|
|
|
2017-07-24 16:24:21 +01:00
|
|
|
// Add creates a new space.
|
|
|
|
func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
2017-07-26 10:50:26 +01:00
|
|
|
method := "space.Add"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
if !h.Runtime.Product.License.IsValid() {
|
|
|
|
response.WriteBadLicense(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if !ctx.Editor {
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer streamutil.Close(r.Body)
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-21 17:51:06 +01:00
|
|
|
var model = space.NewSpaceRequest{}
|
|
|
|
err = json.Unmarshal(body, &model)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-21 17:51:06 +01:00
|
|
|
model.Name = strings.TrimSpace(model.Name)
|
|
|
|
model.CloneID = strings.TrimSpace(model.CloneID)
|
|
|
|
if len(model.Name) == 0 {
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteMissingDataError(w, method, "name")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-21 17:51:06 +01:00
|
|
|
var sp space.Space
|
|
|
|
sp.Name = model.Name
|
2017-08-07 14:42:02 +01:00
|
|
|
sp.RefID = uniqueid.Generate()
|
|
|
|
sp.OrgID = ctx.OrgID
|
|
|
|
sp.Type = space.ScopePrivate
|
|
|
|
sp.UserID = ctx.UserID
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-08-07 14:42:02 +01:00
|
|
|
err = h.Store.Space.Add(ctx, sp)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
perm := space.Permission{}
|
|
|
|
perm.OrgID = sp.OrgID
|
|
|
|
perm.Who = "user"
|
|
|
|
perm.WhoID = ctx.UserID
|
|
|
|
perm.Scope = "object"
|
|
|
|
perm.Location = "space"
|
|
|
|
perm.RefID = sp.RefID
|
|
|
|
perm.Action = "" // we send array for actions below
|
2017-08-07 14:42:02 +01:00
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
err = h.Store.Space.AddPermissions(ctx, perm, space.SpaceOwner, space.SpaceManage, space.SpaceView)
|
2017-08-07 14:42:02 +01:00
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-08-07 14:42:02 +01:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSpaceAdd)
|
|
|
|
|
2017-08-21 17:51:06 +01:00
|
|
|
// Get back new space
|
2017-08-07 14:42:02 +01:00
|
|
|
sp, _ = h.Store.Space.Get(ctx, sp.RefID)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-08-21 17:51:06 +01:00
|
|
|
// clone existing space?
|
|
|
|
if model.CloneID != "" && (model.CopyDocument || model.CopyPermission || model.CopyTemplate) {
|
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
spCloneRoles, err := h.Store.Space.GetPermissions(ctx, model.CloneID)
|
2017-08-21 17:51:06 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if model.CopyPermission {
|
|
|
|
for _, r := range spCloneRoles {
|
2017-09-13 19:22:38 +01:00
|
|
|
r.RefID = sp.RefID
|
2017-08-21 17:51:06 +01:00
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
err = h.Store.Space.AddPermission(ctx, r)
|
2017-08-21 17:51:06 +01:00
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toCopy := []doc.Document{}
|
|
|
|
spCloneTemplates, err := h.Store.Document.TemplatesBySpace(ctx, model.CloneID)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
toCopy = append(toCopy, spCloneTemplates...)
|
|
|
|
|
|
|
|
if model.CopyDocument {
|
|
|
|
docs, err := h.Store.Document.GetBySpace(ctx, model.CloneID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
toCopy = append(toCopy, docs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(toCopy) > 0 {
|
|
|
|
for _, t := range toCopy {
|
|
|
|
origID := t.RefID
|
|
|
|
|
|
|
|
documentID := uniqueid.Generate()
|
|
|
|
t.RefID = documentID
|
|
|
|
t.LabelID = sp.RefID
|
|
|
|
|
|
|
|
err = h.Store.Document.Add(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pages, _ := h.Store.Page.GetPages(ctx, origID)
|
|
|
|
for _, p := range pages {
|
|
|
|
meta, err2 := h.Store.Page.GetPageMeta(ctx, p.RefID)
|
|
|
|
if err2 != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.DocumentID = documentID
|
|
|
|
pageID := uniqueid.Generate()
|
|
|
|
p.RefID = pageID
|
|
|
|
|
|
|
|
meta.PageID = pageID
|
|
|
|
meta.DocumentID = documentID
|
|
|
|
|
|
|
|
model := page.NewPage{}
|
|
|
|
model.Page = p
|
|
|
|
model.Meta = meta
|
|
|
|
|
|
|
|
err = h.Store.Page.Add(ctx, model)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newUUID, _ := uuid.NewV4()
|
|
|
|
attachments, _ := h.Store.Attachment.GetAttachmentsWithData(ctx, origID)
|
|
|
|
for _, a := range attachments {
|
|
|
|
attachmentID := uniqueid.Generate()
|
|
|
|
a.RefID = attachmentID
|
|
|
|
a.DocumentID = documentID
|
|
|
|
a.Job = newUUID.String()
|
|
|
|
random := secrets.GenerateSalt()
|
|
|
|
a.FileID = random[0:9]
|
|
|
|
|
|
|
|
err = h.Store.Attachment.Add(ctx, a)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 18:47:59 +01:00
|
|
|
if model.CopyTemplate {
|
|
|
|
blocks, err := h.Store.Block.GetBySpace(ctx, model.CloneID)
|
|
|
|
|
|
|
|
for _, b := range blocks {
|
|
|
|
b.RefID = uniqueid.Generate()
|
|
|
|
b.LabelID = sp.RefID
|
|
|
|
b.UserID = ctx.UserID
|
|
|
|
|
|
|
|
err = h.Store.Block.Add(ctx, b)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 17:51:06 +01:00
|
|
|
ctx.Transaction.Commit()
|
|
|
|
}
|
|
|
|
|
2017-08-07 14:42:02 +01:00
|
|
|
response.WriteJSON(w, sp)
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the requested space.
|
|
|
|
func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "Get"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
id := request.Param(r, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
if len(id) == 0 {
|
2017-09-13 19:22:38 +01:00
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
sp, err := h.Store.Space.Get(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
response.WriteNotFoundError(w, method, id)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, sp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll returns spaces the user can see.
|
|
|
|
func (h *Handler) GetAll(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "GetAll"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
sp, err := h.Store.Space.GetAll(ctx)
|
2017-08-03 10:00:24 +01:00
|
|
|
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sp) == 0 {
|
2017-07-26 10:50:26 +01:00
|
|
|
sp = []space.Space{}
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, sp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSpaceViewers returns the users that can see the shared spaces.
|
|
|
|
func (h *Handler) GetSpaceViewers(w http.ResponseWriter, r *http.Request) {
|
2017-07-26 10:50:26 +01:00
|
|
|
method := "space.Viewers"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
v, err := h.Store.Space.Viewers(ctx)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(v) == 0 {
|
2017-07-26 10:50:26 +01:00
|
|
|
v = []space.Viewer{}
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update processes request to save space object to the database
|
|
|
|
func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "space.Update"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if !ctx.Editor {
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
spaceID := request.Param(r, "spaceID")
|
|
|
|
if len(spaceID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer streamutil.Close(r.Body)
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
var sp space.Space
|
2017-07-24 16:24:21 +01:00
|
|
|
err = json.Unmarshal(body, &sp)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, "marshal")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sp.Name) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "name")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
sp.RefID = spaceID
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
err = h.Store.Space.Update(ctx, sp)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSpaceUpdate)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
response.WriteJSON(w, sp)
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
// Remove moves documents to another space before deleting it
|
2017-07-24 16:24:21 +01:00
|
|
|
func (h *Handler) Remove(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "space.Remove"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
if !h.Runtime.Product.License.IsValid() {
|
|
|
|
response.WriteBadLicense(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if !ctx.Editor {
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
id := request.Param(r, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
if len(id) == 0 {
|
2017-09-13 19:22:38 +01:00
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
2017-07-26 10:50:26 +01:00
|
|
|
|
|
|
|
move := request.Param(r, "moveToId")
|
2017-07-24 16:24:21 +01:00
|
|
|
if len(move) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "moveToId")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
err = h.Store.Document.MoveDocumentSpace(ctx, id, move)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
_, err = h.Store.Space.Delete(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
_, err = h.Store.Space.DeletePermissions(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
_, err = h.Store.Pin.DeletePinnedSpace(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSpaceDelete)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
response.WriteEmpty(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes empty space.
|
|
|
|
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "space.Delete"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
if !h.Runtime.Product.License.IsValid() {
|
|
|
|
response.WriteBadLicense(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if !ctx.Editor {
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
id := request.Param(r, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
if len(id) == 0 {
|
2017-09-13 19:22:38 +01:00
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
_, err = h.Store.Space.Delete(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
_, err = h.Store.Space.DeletePermissions(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
_, err = h.Store.Pin.DeletePinnedSpace(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSpaceDelete)
|
|
|
|
|
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
response.WriteEmpty(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPermissions persists specified spac3 permissions
|
|
|
|
func (h *Handler) SetPermissions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "space.SetPermissions"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if !ctx.Editor {
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
id := request.Param(r, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
if len(id) == 0 {
|
2017-09-13 19:22:38 +01:00
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
sp, err := h.Store.Space.Get(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-09-15 11:08:05 +01:00
|
|
|
response.WriteNotFoundError(w, method, "space not found")
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if sp.UserID != ctx.UserID {
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer streamutil.Close(r.Body)
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-15 11:08:05 +01:00
|
|
|
var model = space.PermissionsModel{}
|
2017-07-24 16:24:21 +01:00
|
|
|
err = json.Unmarshal(body, &model)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We compare new permisions to what we had before.
|
2017-09-13 19:22:38 +01:00
|
|
|
// Why? So we can send out space invitation emails.
|
|
|
|
previousRoles, err := h.Store.Space.GetPermissions(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store all previous roles as map for easy querying
|
|
|
|
previousRoleUsers := make(map[string]bool)
|
|
|
|
for _, v := range previousRoles {
|
2017-09-13 19:22:38 +01:00
|
|
|
previousRoleUsers[v.WhoID] = true
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
// Who is sharing this space?
|
2017-07-26 10:50:26 +01:00
|
|
|
inviter, err := h.Store.User.Get(ctx, ctx.UserID)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
// Nuke all previous permissions for this space
|
|
|
|
_, err = h.Store.Space.DeletePermissions(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
me := false
|
|
|
|
hasEveryoneRole := false
|
|
|
|
roleCount := 0
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
url := ctx.GetAppURL(fmt.Sprintf("s/%s/%s", sp.RefID, stringutil.MakeSlug(sp.Name)))
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
for _, perm := range model.Permissions {
|
|
|
|
perm.OrgID = ctx.OrgID
|
2017-09-15 11:08:05 +01:00
|
|
|
perm.SpaceID = id
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
// Ensure the space owner always has access!
|
2017-09-15 11:08:05 +01:00
|
|
|
if perm.UserID == ctx.UserID {
|
2017-07-24 16:24:21 +01:00
|
|
|
me = true
|
|
|
|
}
|
|
|
|
|
2017-09-15 11:08:05 +01:00
|
|
|
if len(perm.UserID) == 0 {
|
2017-07-24 16:24:21 +01:00
|
|
|
hasEveryoneRole = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only persist if there is a role!
|
2017-09-15 11:08:05 +01:00
|
|
|
if space.HasAnyPermission(perm) {
|
|
|
|
r := space.EncodeUserPermissions(perm)
|
|
|
|
|
|
|
|
for _, p := range r {
|
|
|
|
err = h.Store.Space.AddPermission(ctx, p)
|
|
|
|
if err != nil {
|
|
|
|
h.Runtime.Log.Error("set permission", err)
|
|
|
|
}
|
2017-08-02 15:26:31 +01:00
|
|
|
|
2017-09-15 11:08:05 +01:00
|
|
|
roleCount++
|
|
|
|
}
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
// We send out space invitation emails to those users
|
2017-07-24 16:24:21 +01:00
|
|
|
// that have *just* been given permissions.
|
2017-09-15 11:08:05 +01:00
|
|
|
if _, isExisting := previousRoleUsers[perm.UserID]; !isExisting {
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
// we skip 'everyone' (user id != empty string)
|
2017-09-15 11:08:05 +01:00
|
|
|
if len(perm.UserID) > 0 {
|
|
|
|
existingUser, err := h.Store.User.Get(ctx, perm.UserID)
|
|
|
|
if err != nil {
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-09-15 11:08:05 +01:00
|
|
|
break
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
2017-09-15 11:08:05 +01:00
|
|
|
|
|
|
|
mailer := mail.Mailer{Runtime: h.Runtime, Store: h.Store, Context: ctx}
|
|
|
|
go mailer.ShareSpaceExistingUser(existingUser.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, existingUser.Email))
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do we need to ensure permissions for space owner when shared?
|
|
|
|
if !me {
|
2017-09-13 19:22:38 +01:00
|
|
|
perm := space.Permission{}
|
|
|
|
perm.OrgID = ctx.OrgID
|
|
|
|
perm.Who = "user"
|
|
|
|
perm.WhoID = ctx.UserID
|
|
|
|
perm.Scope = "object"
|
|
|
|
perm.Location = "space"
|
|
|
|
perm.RefID = id
|
|
|
|
perm.Action = "" // we send array for actions below
|
|
|
|
|
2017-09-15 11:08:05 +01:00
|
|
|
err = h.Store.Space.AddPermissions(ctx, perm, space.SpaceView, space.SpaceManage)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
// Mark up space type as either public, private or restricted access.
|
2017-07-24 16:24:21 +01:00
|
|
|
if hasEveryoneRole {
|
2017-07-26 10:50:26 +01:00
|
|
|
sp.Type = space.ScopePublic
|
2017-07-24 16:24:21 +01:00
|
|
|
} else {
|
|
|
|
if roleCount > 1 {
|
2017-07-26 10:50:26 +01:00
|
|
|
sp.Type = space.ScopeRestricted
|
2017-07-24 16:24:21 +01:00
|
|
|
} else {
|
2017-07-26 10:50:26 +01:00
|
|
|
sp.Type = space.ScopePrivate
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
err = h.Store.Space.Update(ctx, sp)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSpacePermission)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
response.WriteEmpty(w)
|
|
|
|
}
|
|
|
|
|
2017-09-14 12:54:57 +01:00
|
|
|
// GetPermissions returns permissions for alll users for given space.
|
2017-07-24 16:24:21 +01:00
|
|
|
func (h *Handler) GetPermissions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "space.GetPermissions"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
spaceID := request.Param(r, "spaceID")
|
|
|
|
if len(spaceID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
perms, err := h.Store.Space.GetPermissions(ctx, spaceID)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
return
|
|
|
|
}
|
2017-09-13 19:22:38 +01:00
|
|
|
if len(perms) == 0 {
|
|
|
|
perms = []space.Permission{}
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 12:54:57 +01:00
|
|
|
userPerms := make(map[string][]space.Permission)
|
|
|
|
for _, p := range perms {
|
|
|
|
userPerms[p.WhoID] = append(userPerms[p.WhoID], p)
|
|
|
|
}
|
|
|
|
|
|
|
|
records := []space.PermissionRecord{}
|
|
|
|
for _, up := range userPerms {
|
|
|
|
records = append(records, space.DecodeUserPermissions(up))
|
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, records)
|
2017-09-13 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserPermissions returns permissions for the requested space, for current user.
|
|
|
|
func (h *Handler) GetUserPermissions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "space.GetUserPermissions"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
|
|
|
spaceID := request.Param(r, "spaceID")
|
|
|
|
if len(spaceID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
perms, err := h.Store.Space.GetUserPermissions(ctx, spaceID)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(perms) == 0 {
|
|
|
|
perms = []space.Permission{}
|
|
|
|
}
|
|
|
|
|
2017-09-14 12:54:57 +01:00
|
|
|
record := space.DecodeUserPermissions(perms)
|
|
|
|
response.WriteJSON(w, record)
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptInvitation records the fact that a user has completed space onboard process.
|
|
|
|
func (h *Handler) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "space.AcceptInvitation"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-08-31 18:01:07 +01:00
|
|
|
ctx.Subdomain = organization.GetSubdomainFromHost(r)
|
2017-09-13 19:22:38 +01:00
|
|
|
|
|
|
|
spaceID := request.Param(r, "spaceID")
|
|
|
|
if len(spaceID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-02 15:58:39 +01:00
|
|
|
org, err := h.Store.Organization.GetOrganizationByDomain(ctx.Subdomain)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptShare does not authenticate the user hence the context needs to set up
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.OrgID = org.RefID
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
defer streamutil.Close(r.Body)
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
var model = space.AcceptShareModel{}
|
2017-07-24 16:24:21 +01:00
|
|
|
err = json.Unmarshal(body, &model)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(model.Serial) == 0 || len(model.Firstname) == 0 || len(model.Lastname) == 0 || len(model.Password) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "Serial, Firstname, Lastname, Password")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
u, err := h.Store.User.GetBySerial(ctx, model.Serial)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil && err == sql.ErrNoRows {
|
|
|
|
response.WriteDuplicateError(w, method, "user")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptShare does not authenticate the user hence the context needs to set up
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.UserID = u.RefID
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
u.Firstname = model.Firstname
|
|
|
|
u.Lastname = model.Lastname
|
|
|
|
u.Initials = stringutil.MakeInitials(u.Firstname, u.Lastname)
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
err = h.Store.User.UpdateUser(ctx, u)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
salt := secrets.GenerateSalt()
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
err = h.Store.User.UpdateUserPassword(ctx, u.RefID, salt, secrets.GeneratePassword(model.Password, salt))
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSpaceJoin)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
response.WriteJSON(w, u)
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
// Invite sends users space invitation emails.
|
2017-07-24 16:24:21 +01:00
|
|
|
func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "space.Invite"
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
id := request.Param(r, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
if len(id) == 0 {
|
2017-09-13 19:22:38 +01:00
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
sp, err := h.Store.Space.Get(ctx, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
if sp.UserID != ctx.UserID {
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer streamutil.Close(r.Body)
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, "body")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
var model = space.InvitationModel{}
|
2017-07-24 16:24:21 +01:00
|
|
|
err = json.Unmarshal(body, &model)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, "json")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
inviter, err := h.Store.User.Get(ctx, ctx.UserID)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, email := range model.Recipients {
|
2017-07-26 10:50:26 +01:00
|
|
|
u, err := h.Store.User.GetByEmail(ctx, email)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(u.RefID) > 0 {
|
|
|
|
// Ensure they have access to this organization
|
2017-07-26 10:50:26 +01:00
|
|
|
accounts, err2 := h.Store.Account.GetUserAccounts(ctx, u.RefID)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err2 != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// we create if they c
|
|
|
|
hasAccess := false
|
|
|
|
for _, a := range accounts {
|
2017-07-26 10:50:26 +01:00
|
|
|
if a.OrgID == ctx.OrgID {
|
2017-07-24 16:24:21 +01:00
|
|
|
hasAccess = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasAccess {
|
|
|
|
var a account.Account
|
|
|
|
a.UserID = u.RefID
|
2017-07-26 10:50:26 +01:00
|
|
|
a.OrgID = ctx.OrgID
|
2017-07-24 16:24:21 +01:00
|
|
|
a.Admin = false
|
|
|
|
a.Editor = false
|
2017-09-13 19:22:38 +01:00
|
|
|
a.Users = false
|
2017-07-24 16:24:21 +01:00
|
|
|
a.Active = true
|
|
|
|
accountID := uniqueid.Generate()
|
|
|
|
a.RefID = accountID
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
err = h.Store.Account.Add(ctx, a)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure they have space roles
|
2017-09-13 19:22:38 +01:00
|
|
|
h.Store.Space.DeleteUserPermissions(ctx, sp.RefID, u.RefID)
|
|
|
|
|
|
|
|
perm := space.Permission{}
|
|
|
|
perm.OrgID = sp.OrgID
|
|
|
|
perm.Who = "user"
|
|
|
|
perm.WhoID = u.RefID
|
|
|
|
perm.Scope = "object"
|
|
|
|
perm.Location = "space"
|
|
|
|
perm.RefID = sp.RefID
|
|
|
|
perm.Action = "" // we send array for actions below
|
|
|
|
|
|
|
|
err = h.Store.Space.AddPermissions(ctx, perm, space.SpaceView)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
url := ctx.GetAppURL(fmt.Sprintf("s/%s/%s", sp.RefID, stringutil.MakeSlug(sp.Name)))
|
2017-08-02 15:26:31 +01:00
|
|
|
mailer := mail.Mailer{Runtime: h.Runtime, Store: h.Store, Context: ctx}
|
2017-09-13 19:22:38 +01:00
|
|
|
go mailer.ShareSpaceExistingUser(email, inviter.Fullname(), url, sp.Name, model.Message)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
h.Runtime.Log.Info(fmt.Sprintf("%s is sharing space %s with existing user %s", inviter.Email, sp.Name, email))
|
|
|
|
} else {
|
|
|
|
// On-board new user
|
|
|
|
if strings.Contains(email, "@") {
|
2017-07-26 10:50:26 +01:00
|
|
|
url := ctx.GetAppURL(fmt.Sprintf("auth/share/%s/%s", sp.RefID, stringutil.MakeSlug(sp.Name)))
|
2017-08-02 15:26:31 +01:00
|
|
|
err = inviteNewUserToSharedSpace(ctx, h.Runtime, h.Store, email, inviter, url, sp, model.Message)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Runtime.Log.Info(fmt.Sprintf("%s is sharing space %s with new user %s", inviter.Email, sp.Name, email))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
// We ensure that the space is marked as restricted as a minimum!
|
2017-07-26 10:50:26 +01:00
|
|
|
if len(model.Recipients) > 0 && sp.Type == space.ScopePrivate {
|
|
|
|
sp.Type = space.ScopeRestricted
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
err = h.Store.Space.Update(ctx, sp)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2017-07-24 16:24:21 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-24 16:24:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSpaceInvite)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
ctx.Transaction.Commit()
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
response.WriteEmpty(w)
|
|
|
|
}
|