Skip to content

Commit

Permalink
refactor: move join-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
runar-rkmedia committed May 25, 2022
1 parent 6b38af7 commit c569bfc
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 96 deletions.
96 changes: 0 additions & 96 deletions handlers/apiHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,102 +113,6 @@ func EndpointsHandler(
rw.Header().Set("Content-Disposition", `attachment; filename="swagger-skiver.yaml"`)
rw.Write(swaggerYml)
return
case "join":
if isPost || isGet {
joinId := getStringSliceIndex(paths, 1)
if joinId == "" {
rc.WriteError("Missing join-id", requestContext.CodeErrIDEmpty)
return
}

orgs, err := ctx.DB.GetOrganizations()
if err != nil {
rc.WriteErr(err, requestContext.CodeErrOrganization)
return
}
var org *types.Organization
for _, o := range orgs {
if o.JoinID == joinId {
org = &o
break
}
}
if org == nil {
rc.WriteError("Not found", requestContext.CodeErrOrganizationNotFound)
return
}
if org.JoinIDExpires.Before(time.Now()) {
rc.WriteError("Not found", requestContext.CodeErrOrganizationNotFound)
return
}
if isPost {
var joinInput models.JoinInput
err := rc.ValidateBytes(body, &joinInput)
if err != nil {
return
}

pass, err := pw.Hash(*joinInput.Password)
if err != nil {
rc.L.Error().Err(err).Msg("there was an error with hashing the password")
rc.WriteError("Failure in password-creation", requestContext.CodeErrPasswordHashing)
return
}
u := types.User{
Entity: types.Entity{
CreatedAt: time.Time{},
CreatedBy: "join",
OrganizationID: org.ID,
},
UserName: *joinInput.Username,
Active: true,
Store: types.UserStoreLocal,
TemporaryPassword: false,
PW: pass,
CanCreateOrganization: false,
CanCreateUsers: false,
CanCreateProjects: true,
CanCreateTranslations: true,
CanCreateLocales: false,
CanUpdateOrganization: false,
CanUpdateUsers: false,
CanUpdateProjects: true,
CanUpdateTranslations: true,
CanUpdateLocales: false,
CanManageSnapshots: true,
}
existingUsers := false
{
orgUsers, err := ctx.DB.FindUsers(1, types.User{Entity: types.Entity{OrganizationID: org.ID}})
if err != nil {
rc.WriteErr(err, requestContext.CodeErrNotFoundUser)
return
}
existingUsers = len(orgUsers) > 0
}
if existingUsers {
u.CanUpdateOrganization = true
// user is the first to join, should have organization-administrative permissions
}

user, err := ctx.DB.CreateUser(u)
if err != nil {
rc.WriteErr(err, requestContext.CodeErrNotFoundUser)
return
}
// TODO: loginUser
rc.WriteOutput(types.LoginResponse{
User: user,
Organization: *org,
Ok: true,
}, http.StatusOK)
return
}

rc.WriteOutput(org, http.StatusOK)
return

}
case "logout":
{
if isPost {
Expand Down
5 changes: 5 additions & 0 deletions handlers/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func ErrApiMissingArgument(key string) error {
func ErrApiInputValidation(msg, key string) error {
return NewApiError(msg, http.StatusBadRequest, "InputValidation:"+key)
}
func ErrApiInternalError(msg, key string, err error) error {
a := NewApiError(msg, http.StatusBadGateway, "InternalError:"+key)
a.InternalError = err
return a
}
func ErrApiDatabase(key string, err error) error {
if err == nil {
return nil
Expand Down
116 changes: 116 additions & 0 deletions handlers/join.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package handlers

import (
"net/http"
"time"

"github.com/runar-rkmedia/skiver/models"
"github.com/runar-rkmedia/skiver/requestContext"
"github.com/runar-rkmedia/skiver/types"
)

func getOrgForJoinID(db types.OrgStorage, joinID string) (*types.Organization, error) {

if joinID == "" {
return nil, ErrApiMissingArgument("JoinID")
}

orgs, err := db.GetOrganizations()
if err != nil {
return nil, ErrApiDatabase("Organization", err)
}
var org *types.Organization
for _, o := range orgs {
if o.JoinID == joinID {
org = &o
break
}
}
if org == nil {
return nil, ErrApiNotFound("Organization", joinID)

}
if org.JoinIDExpires.Before(time.Now()) {
return nil, ErrApiNotFound("Organization", joinID)
}
return org, nil
}

func GetOrgForJoinID(db types.Storage) AppHandler {
return func(rc requestContext.ReqContext, w http.ResponseWriter, r *http.Request) (interface{}, error) {
joinID := GetParams(r).ByName("join-id")
org, err := getOrgForJoinID(db, joinID)
return org, err
}
}

func JoinOrgFromJoinID(db types.Storage, pw PasswordKeeper) AppHandler {

return func(rc requestContext.ReqContext, w http.ResponseWriter, r *http.Request) (interface{}, error) {
joinID := GetParams(r).ByName("join-id")
org, err := getOrgForJoinID(db, joinID)
if err != nil {
return nil, err
}

var joinInput models.JoinInput
err = rc.ValidateBody(&joinInput, false)
if err != nil {
return nil, err
}

pass, err := pw.Hash(*joinInput.Password)
if err != nil {
rc.L.Error().Err(err).Msg("there was an error with hashing the password")
return nil, ErrApiInternalError("Failure in password-creator", "Password", err)
}
u := types.User{
Entity: types.Entity{
CreatedAt: time.Time{},
CreatedBy: "join",
OrganizationID: org.ID,
},
UserName: *joinInput.Username,
Active: true,
Store: types.UserStoreLocal,
TemporaryPassword: false,
PW: pass,
CanCreateOrganization: false,
CanCreateUsers: false,
CanCreateProjects: true,
CanCreateTranslations: true,
CanCreateLocales: false,
CanUpdateOrganization: false,
CanUpdateUsers: false,
CanUpdateProjects: true,
CanUpdateTranslations: true,
CanUpdateLocales: false,
CanManageSnapshots: true,
}
existingUsers := false
{
orgUsers, err := db.FindUsers(1, types.User{Entity: types.Entity{OrganizationID: org.ID}})
if err != nil {
return nil, ErrApiDatabase("User", err)
}
existingUsers = len(orgUsers) > 0
}
if existingUsers {
u.CanUpdateOrganization = true
// user is the first to join, should have organization-administrative permissions
}

user, err := db.CreateUser(u)
if err != nil {
return nil, ErrApiDatabase("User", err)
}
// TODO: loginUser
out := types.LoginResponse{
User: user,
Organization: *org,
Ok: true,
}
return out, nil

}
}

0 comments on commit c569bfc

Please sign in to comment.