save "create sync group" table in database
This commit is contained in:
+30
-32
@@ -1,16 +1,16 @@
|
||||
package site
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"ruben/inventory2/internal/site/middleware"
|
||||
"ruben/inventory2/internal/site/response"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -65,7 +65,7 @@ func (s *Server) getTemplateNameAndArgs(r *http.Request, templateDir string) (na
|
||||
Account *accounts.Account
|
||||
*/
|
||||
"Identity",
|
||||
getIdentity(r.Context()),
|
||||
middleware.GetIdentity(r.Context()),
|
||||
"Auth",
|
||||
newTemplateAuthenticator(r),
|
||||
}
|
||||
@@ -136,37 +136,13 @@ func getMatchingGlobPatternsCapturingFilepathIncludingParametrizedFilepaths(file
|
||||
}
|
||||
|
||||
func (s *Server) handleTemplateError(err error, templateArgs ...any) (response.Response, error) {
|
||||
code := getHTTPStatusCode(err)
|
||||
|
||||
if code == http.StatusNotFound ||
|
||||
code == http.StatusForbidden ||
|
||||
code == http.StatusUnauthorized ||
|
||||
isFileNotFoundError(err) {
|
||||
|
||||
b, err := s.templater.ExecutePage("not-found", templateArgs...)
|
||||
if err != nil {
|
||||
fmt.Println("failed to render not found page:", err)
|
||||
return nil, response.NotFound().
|
||||
Wrap(err).
|
||||
Msg("resource not found")
|
||||
}
|
||||
|
||||
return response.Body(io.NopCloser(bytes.NewBuffer(b))), nil
|
||||
if isFileNotFoundError(err) {
|
||||
return nil, response.NotFound().
|
||||
Wrap(err).
|
||||
Msg("resource not found")
|
||||
}
|
||||
|
||||
if code == http.StatusConflict {
|
||||
b, err := s.templater.ExecutePage("conflict", templateArgs...)
|
||||
if err != nil {
|
||||
fmt.Println("failed to render conflict page:", err)
|
||||
return nil, response.Conflict().
|
||||
Wrap(err).
|
||||
Msg("conflict")
|
||||
}
|
||||
|
||||
return response.Body(io.NopCloser(bytes.NewBuffer(b))), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("failed to render page: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func isFileNotFoundError(err error) bool {
|
||||
@@ -230,3 +206,25 @@ type templateAuthorizationFunc = func() (string, error)
|
||||
func (a *templateAuthenticator) ByMatchingAccountID(acctIDPathPosition int) (string, error) {
|
||||
return "", authorizeByMatchingAccountID(a.req, acctIDPathPosition)
|
||||
}
|
||||
|
||||
func authorizeByMatchingAccountID(r *http.Request, acctIDPathPosition int) error {
|
||||
pathParts := strings.Split(strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/"), "/"), "/")
|
||||
if len(pathParts) < acctIDPathPosition {
|
||||
return fmt.Errorf("authorization failed due to unexpected path: %s", r.URL.Path)
|
||||
}
|
||||
|
||||
part := pathParts[acctIDPathPosition-1]
|
||||
acctID, err := strconv.ParseInt(part, 10, 64)
|
||||
if err != nil {
|
||||
return response.NotFound().
|
||||
Msgf("account does not exist: %s", part)
|
||||
}
|
||||
|
||||
id := middleware.GetIdentity(r.Context())
|
||||
if id.Account == nil || id.Account.AccountID != acctID {
|
||||
return response.Unauthorized().
|
||||
Msgf("user does not have access to account %d", acctID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user