mirror of
https://github.com/documize/community.git
synced 2025-07-24 15:49:44 +02:00
wip
This commit is contained in:
parent
0b51608383
commit
9d1af37f6e
22 changed files with 2215 additions and 647 deletions
|
@ -176,3 +176,60 @@ type licenseJSON struct {
|
|||
<Signature>some signature</Signature>
|
||||
</DocumizeLicense>
|
||||
*/
|
||||
|
||||
// SaveAuthConfig returns installation-wide authentication configuration
|
||||
func SaveAuthConfig(w http.ResponseWriter, r *http.Request) {
|
||||
method := "SaveAuthConfig"
|
||||
p := request.GetPersister(r)
|
||||
|
||||
if !p.Context.Global {
|
||||
writeForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
writePayloadError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
var data authData
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
writePayloadError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
org, err := p.GetOrganization(p.Context.OrgID)
|
||||
if err != nil {
|
||||
util.WriteGeneralSQLError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
org.AuthProvider = data.AuthProvider
|
||||
org.AuthConfig = data.AuthConfig
|
||||
|
||||
p.Context.Transaction, err = request.Db.Beginx()
|
||||
if err != nil {
|
||||
writeTransactionError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = p.UpdateAuthConfig(org)
|
||||
if err != nil {
|
||||
util.WriteGeneralSQLError(w, method, err)
|
||||
p.Context.Transaction.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
p.Context.Transaction.Commit()
|
||||
|
||||
util.WriteSuccessEmptyJSON(w)
|
||||
}
|
||||
|
||||
type authData struct {
|
||||
AuthProvider string `json:"authProvider"`
|
||||
AuthConfig string `json:"authConfig"`
|
||||
}
|
||||
|
|
|
@ -33,7 +33,6 @@ func GetMeta(w http.ResponseWriter, r *http.Request) {
|
|||
data.URL = request.GetSubdomainFromHost(r)
|
||||
|
||||
org, err := p.GetOrganizationByDomain(data.URL)
|
||||
|
||||
if err != nil {
|
||||
log.Info(fmt.Sprintf("%s URL not found", data.URL))
|
||||
writeForbiddenError(w)
|
||||
|
@ -44,12 +43,13 @@ func GetMeta(w http.ResponseWriter, r *http.Request) {
|
|||
data.Title = org.Title
|
||||
data.Message = org.Message
|
||||
data.AllowAnonymousAccess = org.AllowAnonymousAccess
|
||||
data.AuthProvider = org.AuthProvider
|
||||
data.AuthConfig = org.AuthConfig
|
||||
data.Version = Product.Version
|
||||
data.Edition = Product.License.Edition
|
||||
data.Valid = Product.License.Valid
|
||||
|
||||
json, err := json.Marshal(data)
|
||||
|
||||
if err != nil {
|
||||
writeJSONMarshalError(w, method, "meta", err)
|
||||
return
|
||||
|
|
|
@ -233,6 +233,7 @@ func init() {
|
|||
log.IfErr(Add(RoutePrefixPrivate, "global/smtp", []string{"PUT", "OPTIONS"}, nil, SaveSMTPConfig))
|
||||
log.IfErr(Add(RoutePrefixPrivate, "global/license", []string{"GET", "OPTIONS"}, nil, GetLicense))
|
||||
log.IfErr(Add(RoutePrefixPrivate, "global/license", []string{"PUT", "OPTIONS"}, nil, SaveLicense))
|
||||
log.IfErr(Add(RoutePrefixPrivate, "global/auth", []string{"PUT", "OPTIONS"}, nil, SaveAuthConfig))
|
||||
|
||||
// Pinned items
|
||||
log.IfErr(Add(RoutePrefixPrivate, "pin/{userID}", []string{"POST", "OPTIONS"}, nil, AddPin))
|
||||
|
|
|
@ -34,8 +34,8 @@ var Product core.ProdInfo
|
|||
|
||||
func init() {
|
||||
Product.Major = "0"
|
||||
Product.Minor = "43"
|
||||
Product.Patch = "1"
|
||||
Product.Minor = "44"
|
||||
Product.Patch = "0"
|
||||
Product.Version = fmt.Sprintf("%s.%s.%s", Product.Major, Product.Minor, Product.Patch)
|
||||
Product.Edition = "Community"
|
||||
Product.Title = fmt.Sprintf("%s Edition", Product.Edition)
|
||||
|
|
|
@ -74,6 +74,8 @@ type Organization struct {
|
|||
Domain string `json:"domain"`
|
||||
Email string `json:"email"`
|
||||
AllowAnonymousAccess bool `json:"allowAnonymousAccess"`
|
||||
AuthProvider string `json:"authProvider"`
|
||||
AuthConfig string `json:"authConfig"`
|
||||
Serial string `json:"-"`
|
||||
Active bool `json:"-"`
|
||||
}
|
||||
|
@ -333,6 +335,8 @@ type SiteMeta struct {
|
|||
Message string `json:"message"`
|
||||
URL string `json:"url"`
|
||||
AllowAnonymousAccess bool `json:"allowAnonymousAccess"`
|
||||
AuthProvider string `json:"authProvider"`
|
||||
AuthConfig string `json:"authConfig"`
|
||||
Version string `json:"version"`
|
||||
Edition string `json:"edition"`
|
||||
Valid bool `json:"valid"`
|
||||
|
|
|
@ -59,7 +59,7 @@ func (p *Persister) AddOrganization(org entity.Organization) error {
|
|||
|
||||
// GetOrganization returns the Organization reocrod from the organization database table with the given id.
|
||||
func (p *Persister) GetOrganization(id string) (org entity.Organization, err error) {
|
||||
stmt, err := Db.Preparex("SELECT id, refid, company, title, message, url, domain, email, serial, active, allowanonymousaccess, created, revised FROM organization WHERE refid=?")
|
||||
stmt, err := Db.Preparex("SELECT id, refid, company, title, message, url, domain, email, serial, active, allowanonymousaccess, authprovider, coalesce(authconfig,JSON_UNQUOTE('{}')) as authconfig, created, revised FROM organization WHERE refid=?")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -77,24 +77,6 @@ func (p *Persister) GetOrganization(id string) (org entity.Organization, err err
|
|||
return
|
||||
}
|
||||
|
||||
// CheckDomain makes sure there is an organisation with the correct domain
|
||||
func CheckDomain(domain string) string {
|
||||
row := Db.QueryRow("SELECT COUNT(*) FROM organization WHERE domain=? AND active=1", domain)
|
||||
|
||||
var count int
|
||||
err := row.Scan(&count)
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if count == 1 {
|
||||
return domain
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetOrganizationByDomain returns the organization matching a given URL subdomain.
|
||||
func (p *Persister) GetOrganizationByDomain(subdomain string) (org entity.Organization, err error) {
|
||||
err = nil
|
||||
|
@ -104,7 +86,7 @@ func (p *Persister) GetOrganizationByDomain(subdomain string) (org entity.Organi
|
|||
|
||||
var stmt *sqlx.Stmt
|
||||
|
||||
stmt, err = Db.Preparex("SELECT id, refid, company, title, message, url, domain, email, serial, active, allowanonymousaccess, created, revised FROM organization WHERE domain=? AND active=1")
|
||||
stmt, err = Db.Preparex("SELECT id, refid, company, title, message, url, domain, email, serial, active, allowanonymousaccess, authprovider, coalesce(authconfig,JSON_UNQUOTE('{}')) as authconfig, created, revised FROM organization WHERE domain=? AND active=1")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -186,3 +168,42 @@ func (p *Persister) RemoveOrganization(orgID string) (err error) {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateAuthConfig updates the given organization record in the database with the auth config details.
|
||||
func (p *Persister) UpdateAuthConfig(org entity.Organization) (err error) {
|
||||
org.Revised = time.Now().UTC()
|
||||
|
||||
stmt, err := p.Context.Transaction.PrepareNamed("UPDATE organization SET allowanonymousaccess=:allowanonymousaccess, authprovider=:authprovider, authconfig=:authconfig, revised=:revised WHERE refid=:refid")
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to prepare UpdateAuthConfig %s", org.RefID), err)
|
||||
return
|
||||
}
|
||||
|
||||
defer utility.Close(stmt)
|
||||
|
||||
_, err = stmt.Exec(&org)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute UpdateAuthConfig %s", org.RefID), err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CheckDomain makes sure there is an organisation with the correct domain
|
||||
func CheckDomain(domain string) string {
|
||||
row := Db.QueryRow("SELECT COUNT(*) FROM organization WHERE domain=? AND active=1", domain)
|
||||
|
||||
var count int
|
||||
err := row.Scan(&count)
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if count == 1 {
|
||||
return domain
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue