mirror of
https://github.com/documize/community.git
synced 2025-08-07 06:25:23 +02:00
moving endpoints to new API (WIP)
This commit is contained in:
parent
27640dffc4
commit
72b14def6d
36 changed files with 1371 additions and 472 deletions
94
domain/organization/endpoint.go
Normal file
94
domain/organization/endpoint.go
Normal file
|
@ -0,0 +1,94 @@
|
|||
// 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 organization
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/request"
|
||||
"github.com/documize/community/core/response"
|
||||
"github.com/documize/community/core/streamutil"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/model/org"
|
||||
)
|
||||
|
||||
// Handler contains the runtime information such as logging and database.
|
||||
type Handler struct {
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Get returns the requested organization.
|
||||
func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
method := "org.Get"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
orgID := request.Param(r, "orgID")
|
||||
|
||||
if orgID != ctx.OrgID {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
||||
org, err := h.Store.Organization.GetOrganization(ctx, ctx.OrgID)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteJSON(w, org)
|
||||
}
|
||||
|
||||
// Update saves organization amends.
|
||||
func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
method := "org.Update"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
if !ctx.Administrator {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
||||
defer streamutil.Close(r.Body)
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
var org = org.Organization{}
|
||||
err = json.Unmarshal(body, &org)
|
||||
|
||||
org.RefID = ctx.OrgID
|
||||
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.Store.Organization.UpdateOrganization(ctx, org)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
response.WriteJSON(w, org)
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
// 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 organization
|
||||
|
||||
import (
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
)
|
||||
|
||||
// Handler contains the runtime information such as logging and database.
|
||||
type Handler struct {
|
||||
Runtime *env.Runtime
|
||||
}
|
||||
|
||||
// Organization defines a company that uses this app.
|
||||
type Organization struct {
|
||||
domain.BaseEntity
|
||||
Company string `json:"-"`
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
URL string `json:"url"`
|
||||
Domain string `json:"domain"`
|
||||
Email string `json:"email"`
|
||||
AllowAnonymousAccess bool `json:"allowAnonymousAccess"`
|
||||
AuthProvider string `json:"authProvider"`
|
||||
AuthConfig string `json:"authConfig"`
|
||||
ConversionEndpoint string `json:"conversionEndpoint"`
|
||||
Serial string `json:"-"`
|
||||
Active bool `json:"-"`
|
||||
}
|
|
@ -21,16 +21,22 @@ import (
|
|||
"github.com/documize/community/core/streamutil"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/store/mysql"
|
||||
"github.com/documize/community/model/org"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Scope provides data access to MySQL.
|
||||
type Scope struct {
|
||||
Runtime *env.Runtime
|
||||
}
|
||||
|
||||
// AddOrganization inserts the passed organization record into the organization table.
|
||||
func AddOrganization(s domain.StoreContext, org Organization) error {
|
||||
func (s Scope) AddOrganization(ctx domain.RequestContext, org org.Organization) error {
|
||||
org.Created = time.Now().UTC()
|
||||
org.Revised = time.Now().UTC()
|
||||
|
||||
stmt, err := s.Context.Transaction.Preparex(
|
||||
stmt, err := ctx.Transaction.Preparex(
|
||||
"INSERT INTO organization (refid, company, title, message, url, domain, email, allowanonymousaccess, serial, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
|
@ -51,7 +57,7 @@ func AddOrganization(s domain.StoreContext, org Organization) error {
|
|||
}
|
||||
|
||||
// GetOrganization returns the Organization reocrod from the organization database table with the given id.
|
||||
func GetOrganization(s domain.StoreContext, id string) (org Organization, err error) {
|
||||
func (s Scope) GetOrganization(ctx domain.RequestContext, id string) (org org.Organization, err error) {
|
||||
stmt, err := s.Runtime.Db.Preparex("SELECT id, refid, company, title, message, url, domain, service as conversionendpoint, email, serial, active, allowanonymousaccess, authprovider, coalesce(authconfig,JSON_UNQUOTE('{}')) as authconfig, created, revised FROM organization WHERE refid=?")
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
|
@ -71,7 +77,7 @@ func GetOrganization(s domain.StoreContext, id string) (org Organization, err er
|
|||
}
|
||||
|
||||
// GetOrganizationByDomain returns the organization matching a given URL subdomain.
|
||||
func GetOrganizationByDomain(s domain.StoreContext, subdomain string) (org Organization, err error) {
|
||||
func (s Scope) GetOrganizationByDomain(ctx domain.RequestContext, subdomain string) (org org.Organization, err error) {
|
||||
err = nil
|
||||
subdomain = strings.ToLower(subdomain)
|
||||
|
||||
|
@ -98,10 +104,10 @@ func GetOrganizationByDomain(s domain.StoreContext, subdomain string) (org Organ
|
|||
}
|
||||
|
||||
// UpdateOrganization updates the given organization record in the database to the values supplied.
|
||||
func UpdateOrganization(s domain.StoreContext, org Organization) (err error) {
|
||||
func (s Scope) UpdateOrganization(ctx domain.RequestContext, org org.Organization) (err error) {
|
||||
org.Revised = time.Now().UTC()
|
||||
|
||||
stmt, err := s.Context.Transaction.PrepareNamed("UPDATE organization SET title=:title, message=:message, service=:conversionendpoint, email=:email, allowanonymousaccess=:allowanonymousaccess, revised=:revised WHERE refid=:refid")
|
||||
stmt, err := ctx.Transaction.PrepareNamed("UPDATE organization SET title=:title, message=:message, service=:conversionendpoint, email=:email, allowanonymousaccess=:allowanonymousaccess, revised=:revised WHERE refid=:refid")
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -119,14 +125,14 @@ func UpdateOrganization(s domain.StoreContext, org Organization) (err error) {
|
|||
}
|
||||
|
||||
// DeleteOrganization deletes the orgID organization from the organization table.
|
||||
func DeleteOrganization(s domain.StoreContext, orgID string) (rows int64, err error) {
|
||||
func (s Scope) DeleteOrganization(ctx domain.RequestContext, orgID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
return b.Delete(s.Context.Transaction, "organization", orgID)
|
||||
return b.Delete(ctx.Transaction, "organization", orgID)
|
||||
}
|
||||
|
||||
// RemoveOrganization sets the orgID organization to be inactive, thus executing a "soft delete" operation.
|
||||
func RemoveOrganization(s domain.StoreContext, rc domain.RequestContext, orgID string) (err error) {
|
||||
stmt, err := s.Context.Transaction.Preparex("UPDATE organization SET active=0 WHERE refid=?")
|
||||
func (s Scope) RemoveOrganization(ctx domain.RequestContext, orgID string) (err error) {
|
||||
stmt, err := ctx.Transaction.Preparex("UPDATE organization SET active=0 WHERE refid=?")
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -144,10 +150,10 @@ func RemoveOrganization(s domain.StoreContext, rc domain.RequestContext, orgID s
|
|||
}
|
||||
|
||||
// UpdateAuthConfig updates the given organization record in the database with the auth config details.
|
||||
func UpdateAuthConfig(s domain.StoreContext, org Organization) (err error) {
|
||||
func (s Scope) UpdateAuthConfig(ctx domain.RequestContext, org org.Organization) (err error) {
|
||||
org.Revised = time.Now().UTC()
|
||||
|
||||
stmt, err := s.Context.Transaction.PrepareNamed("UPDATE organization SET allowanonymousaccess=:allowanonymousaccess, authprovider=:authprovider, authconfig=:authconfig, revised=:revised WHERE refid=:refid")
|
||||
stmt, err := ctx.Transaction.PrepareNamed("UPDATE organization SET allowanonymousaccess=:allowanonymousaccess, authprovider=:authprovider, authconfig=:authconfig, revised=:revised WHERE refid=:refid")
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -165,7 +171,7 @@ func UpdateAuthConfig(s domain.StoreContext, org Organization) (err error) {
|
|||
}
|
||||
|
||||
// CheckDomain makes sure there is an organisation with the correct domain
|
||||
func CheckDomain(s domain.StoreContext, domain string) string {
|
||||
func (s Scope) CheckDomain(ctx domain.RequestContext, domain string) string {
|
||||
row := s.Runtime.Db.QueryRow("SELECT COUNT(*) FROM organization WHERE domain=? AND active=1", domain)
|
||||
|
||||
var count int
|
|
@ -14,22 +14,19 @@ package organization
|
|||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/domain"
|
||||
)
|
||||
|
||||
// GetRequestSubdomain extracts subdomain from referring URL.
|
||||
func GetRequestSubdomain(s domain.StoreContext, r *http.Request) string {
|
||||
return urlSubdomain(s, r.Referer())
|
||||
func GetRequestSubdomain(r *http.Request) string {
|
||||
return urlSubdomain(r.Referer())
|
||||
}
|
||||
|
||||
// GetSubdomainFromHost extracts the subdomain from the requesting URL.
|
||||
func GetSubdomainFromHost(s domain.StoreContext, r *http.Request) string {
|
||||
return urlSubdomain(s, r.Host)
|
||||
func GetSubdomainFromHost(r *http.Request) string {
|
||||
return urlSubdomain(r.Host)
|
||||
}
|
||||
|
||||
// Find the subdomain (which is actually the organisation).
|
||||
func urlSubdomain(s domain.StoreContext, url string) string {
|
||||
func urlSubdomain(url string) string {
|
||||
url = strings.ToLower(url)
|
||||
url = strings.Replace(url, "https://", "", 1)
|
||||
url = strings.Replace(url, "http://", "", 1)
|
||||
|
@ -42,5 +39,5 @@ func urlSubdomain(s domain.StoreContext, url string) string {
|
|||
url = ""
|
||||
}
|
||||
|
||||
return CheckDomain(s, url)
|
||||
return url
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue