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

239 lines
8.3 KiB
Go
Raw Normal View History

// 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 space
import (
2017-10-09 10:56:59 -04:00
"database/sql"
"fmt"
"time"
"github.com/documize/community/domain"
"github.com/documize/community/domain/store"
2017-07-26 10:50:26 +01:00
"github.com/documize/community/model/space"
"github.com/pkg/errors"
)
// Store provides data access to space information.
type Store struct {
store.Context
store.SpaceStorer
2017-07-26 10:50:26 +01:00
}
// Add adds new folder into the store.
func (s Store) Add(ctx domain.RequestContext, sp space.Space) (err error) {
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_space (c_refid, c_name, c_orgid, c_userid, c_type, c_lifecycle, c_likes, c_icon, c_desc, c_count_category, c_count_content, c_labelid, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"),
sp.RefID, sp.Name, sp.OrgID, sp.UserID, sp.Type, sp.Lifecycle, sp.Likes,
sp.Icon, sp.Description, sp.CountCategory, sp.CountContent, sp.LabelID,
sp.Created, sp.Revised)
if err != nil {
err = errors.Wrap(err, "unable to execute insert for space")
}
return
}
// Get returns a space from the store.
func (s Store) Get(ctx domain.RequestContext, id string) (sp space.Space, err error) {
err = s.Runtime.Db.Get(&sp, s.Bind(`SELECT id, c_refid AS refid,
2018-09-19 16:03:29 +01:00
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_icon AS icon, c_labelid AS labelid, c_desc AS description,
c_count_category As countcategory, c_count_content AS countcontent,
2018-09-19 16:03:29 +01:00
c_created AS created, c_revised AS revised
2018-09-18 20:55:40 +01:00
FROM dmz_space
WHERE c_orgid=? and c_refid=?`),
2017-09-25 14:37:11 +01:00
ctx.OrgID, id)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("unable to execute select for space %s", id))
}
return
}
2017-08-07 14:42:02 +01:00
// PublicSpaces returns spaces that anyone can see.
func (s Store) PublicSpaces(ctx domain.RequestContext, orgID string) (sp []space.Space, err error) {
qry := s.Bind(`SELECT id, c_refid AS refid,
2018-09-19 16:03:29 +01:00
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_icon AS icon, c_labelid AS labelid, c_desc AS desc,
c_count_category as countcategory, c_count_content AS countcontent,
2018-09-19 16:03:29 +01:00
c_created AS created, c_revised AS revised
2018-09-18 20:55:40 +01:00
FROM dmz_space
WHERE c_orgid=? AND c_type=1`)
err = s.Runtime.Db.Select(&sp, qry, orgID)
if err == sql.ErrNoRows {
err = nil
sp = []space.Space{}
}
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("Unable to execute GetPublicFolders for org %s", orgID))
}
return
}
2017-09-26 20:13:44 +01:00
// GetViewable returns spaces that the user can see.
2017-08-07 14:42:02 +01:00
// Also handles which spaces can be seen by anonymous users.
func (s Store) GetViewable(ctx domain.RequestContext) (sp []space.Space, err error) {
q := s.Bind(`SELECT id, c_refid AS refid,
2018-09-19 16:03:29 +01:00
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_icon AS icon, c_labelid AS labelid, c_desc AS description,
c_count_category AS countcategory, c_count_content AS countcontent,
2018-09-19 16:03:29 +01:00
c_created AS created, c_revised AS revised
2018-09-18 20:55:40 +01:00
FROM dmz_space
WHERE c_orgid=? AND c_refid IN
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_location='space' AND c_refid IN
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_who='user' AND (c_whoid=? OR c_whoid='0') AND c_location='space' AND c_action='view'
UNION ALL
2018-09-19 16:03:29 +01:00
SELECT p.c_refid from dmz_permission p LEFT JOIN dmz_group_member r ON p.c_whoid=r.c_groupid WHERE p.c_orgid=? AND p.c_who='role'
2018-09-18 20:55:40 +01:00
AND p.c_location='space' AND p.c_action='view' AND (r.c_userid=? OR r.c_userid='0')
)
)
ORDER BY c_name`)
2017-10-09 10:56:59 -04:00
err = s.Runtime.Db.Select(&sp, q,
2017-07-26 10:50:26 +01:00
ctx.OrgID,
ctx.OrgID,
ctx.OrgID,
2017-09-15 11:08:05 +01:00
ctx.UserID,
2017-07-26 10:50:26 +01:00
ctx.OrgID,
ctx.UserID)
2017-10-09 10:56:59 -04:00
if err == sql.ErrNoRows {
err = nil
sp = []space.Space{}
}
2017-09-26 20:13:44 +01:00
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed space.GetViewable org %s", ctx.OrgID))
}
return
}
// AdminList returns all shared spaces and orphaned spaces that have no owner.
func (s Store) AdminList(ctx domain.RequestContext) (sp []space.Space, err error) {
qry := s.Bind(`
SELECT id, c_refid AS refid,
2018-09-19 16:03:29 +01:00
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_created AS created, c_revised AS revised,
c_icon AS icon, c_labelid AS labelid, c_desc AS description,
c_count_category AS countcategory, c_count_content AS countcontent
FROM dmz_space
WHERE c_orgid=? AND (c_type=? OR c_type=?)
UNION ALL
SELECT id, c_refid AS refid,
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_created AS created, c_revised AS revised,
c_icon AS icon, c_labelid AS labelid, c_desc AS description,
c_count_category AS countcategory, c_count_content AS countcontent
FROM dmz_space
WHERE c_orgid=? AND (c_type=? OR c_type=?) AND c_refid NOT IN
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_action='own')
ORDER BY name`)
err = s.Runtime.Db.Select(&sp, qry,
ctx.OrgID, space.ScopePublic, space.ScopeRestricted,
ctx.OrgID, space.ScopePublic, space.ScopeRestricted,
ctx.OrgID)
if err == sql.ErrNoRows {
err = nil
sp = []space.Space{}
}
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed space.AdminList org %s", ctx.OrgID))
}
return
}
// Update saves space changes.
func (s Store) Update(ctx domain.RequestContext, sp space.Space) (err error) {
sp.Revised = time.Now().UTC()
_, err = ctx.Transaction.NamedExec(`
UPDATE dmz_space
SET c_name=:name, c_type=:type, c_lifecycle=:lifecycle, c_userid=:userid,
c_likes=:likes, c_desc=:description, c_labelid=:labelid, c_icon=:icon,
c_count_category=:countcategory, c_count_content=:countcontent,
c_revised=:revised
WHERE c_orgid=:orgid AND c_refid=:refid`, &sp)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("unable to execute update for space %s", sp.RefID))
}
return
}
// Delete removes space from the store.
func (s Store) Delete(ctx domain.RequestContext, id string) (rows int64, err error) {
return s.DeleteConstrained(ctx.Transaction, "dmz_space", ctx.OrgID, id)
}
// IncrementCategoryCount increments usage counter for space category.
func (s Store) IncrementCategoryCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_category=c_count_category+1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute increment category count")
}
return
}
// DecrementCategoryCount decrements usage counter for space category.
func (s Store) DecrementCategoryCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_category=c_count_category-1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute decrement category count")
}
return
}
// IncrementContentCount increments usage counter for space category.
func (s Store) IncrementContentCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_content=c_count_content+1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute increment content count")
}
return
}
// DecrementContentCount decrements usage counter for space category.
func (s Store) DecrementContentCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_content=c_count_content-1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute decrement category count")
}
return
}