pointer struct to members crud.

This commit is contained in:
Miroslav Šedivý 2020-12-06 21:10:45 +01:00
parent 2467c94c59
commit 9ca4e04e0b
2 changed files with 75 additions and 40 deletions

View File

@ -12,15 +12,15 @@ type MemberCreatePayload struct {
} }
type MemberDataPayload struct { type MemberDataPayload struct {
ID string `json:"id"` ID *string `json:"id"`
Secret string `json:"secret,omitempty"` Secret *string `json:"secret,omitempty"`
Name string `json:"name"` Name *string `json:"name"`
IsAdmin bool `json:"is_admin"` IsAdmin *bool `json:"is_admin"`
CanLogin bool `json:"can_login"` CanLogin *bool `json:"can_login"`
CanConnect bool `json:"can_connect"` CanConnect *bool `json:"can_connect"`
CanWatch bool `json:"can_watch"` CanWatch *bool `json:"can_watch"`
CanHost bool `json:"can_host"` CanHost *bool `json:"can_host"`
CanAccessClipboard bool `json:"can_access_clipboard"` CanAccessClipboard *bool `json:"can_access_clipboard"`
} }
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
@ -29,29 +29,40 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
return return
} }
if data.ID == "" { if data.Secret == nil || *data.Secret == "" {
utils.HttpBadRequest(w, "Secret cannot be empty.")
return
}
if data.Name == nil || *data.Name == "" {
utils.HttpBadRequest(w, "Name cannot be empty.")
return
}
var ID string
if data.ID == nil || *data.ID == "" {
var err error var err error
if data.ID, err = utils.NewUID(32); err != nil { if ID, err = utils.NewUID(32); err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return
} }
} else { } else {
if _, ok := h.sessions.Get(data.ID); ok { ID = *data.ID
if _, ok := h.sessions.Get(ID); ok {
utils.HttpBadRequest(w, "Member ID already exists.") utils.HttpBadRequest(w, "Member ID already exists.")
return return
} }
} }
// TODO: Join structs? session, err := h.sessions.Create(ID, types.MemberProfile{
session, err := h.sessions.Create(data.ID, types.MemberProfile{ Secret: *data.Secret,
Secret: data.Secret, Name: *data.Name,
Name: data.Name, IsAdmin: defaultBool(data.IsAdmin, false),
IsAdmin: data.IsAdmin, CanLogin: defaultBool(data.CanLogin, true),
CanLogin: data.CanLogin, CanConnect: defaultBool(data.CanConnect, true),
CanConnect: data.CanConnect, CanWatch: defaultBool(data.CanWatch, true),
CanWatch: data.CanWatch, CanHost: defaultBool(data.CanHost, true),
CanHost: data.CanHost, CanAccessClipboard: defaultBool(data.CanAccessClipboard, true),
CanAccessClipboard: data.CanAccessClipboard,
}) })
if err != nil { if err != nil {
@ -68,14 +79,15 @@ func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
member := GetMember(r) member := GetMember(r)
// TODO: Join structs? // TODO: Join structs?
// TODO: Ugly.
utils.HttpSuccess(w, MemberDataPayload{ utils.HttpSuccess(w, MemberDataPayload{
Name: member.Name(), Name: func(v string) *string { return &v }(member.Name()),
IsAdmin: member.IsAdmin(), IsAdmin: func(v bool) *bool { return &v }(member.IsAdmin()),
CanLogin: member.CanLogin(), CanLogin: func(v bool) *bool { return &v }(member.CanLogin()),
CanConnect: member.CanConnect(), CanConnect: func(v bool) *bool { return &v }(member.CanConnect()),
CanWatch: member.CanWatch(), CanWatch: func(v bool) *bool { return &v }(member.CanWatch()),
CanHost: member.CanHost(), CanHost: func(v bool) *bool { return &v }(member.CanHost()),
CanAccessClipboard: member.CanAccessClipboard(), CanAccessClipboard: func(v bool) *bool { return &v }(member.CanAccessClipboard()),
}) })
} }
@ -87,18 +99,29 @@ func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
member := GetMember(r) member := GetMember(r)
secret := ""
if data.Secret != nil && *data.Secret != "" {
secret = *data.Secret
}
name := member.Name()
if data.Name != nil && *data.Name != "" {
name = *data.Name
}
// TODO: Join structs? // TODO: Join structs?
// TODO: Update independent props. err := h.sessions.Update(member.ID(), types.MemberProfile{
if err := h.sessions.Update(member.ID(), types.MemberProfile{ Secret: secret,
Secret: data.Secret, Name: name,
Name: data.Name, IsAdmin: defaultBool(data.IsAdmin, member.IsAdmin()),
IsAdmin: data.IsAdmin, CanLogin: defaultBool(data.CanLogin, member.CanLogin()),
CanLogin: data.CanLogin, CanConnect: defaultBool(data.CanConnect, member.CanConnect()),
CanConnect: data.CanConnect, CanWatch: defaultBool(data.CanWatch, member.CanWatch()),
CanWatch: data.CanWatch, CanHost: defaultBool(data.CanHost, member.CanHost()),
CanHost: data.CanHost, CanAccessClipboard: defaultBool(data.CanAccessClipboard, member.CanAccessClipboard()),
CanAccessClipboard: data.CanAccessClipboard, })
}); err != nil {
if err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return
} }
@ -116,3 +139,10 @@ func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) {
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
func defaultBool(val *bool, def bool) bool {
if val != nil {
return *val
}
return def
}

View File

@ -132,6 +132,11 @@ func (manager *SessionManagerCtx) Update(id string, profile types.MemberProfile)
return fmt.Errorf("Member not found.") return fmt.Errorf("Member not found.")
} }
// preserve secret if not updated
if profile.Secret == "" {
profile.Secret = session.profile.Secret
}
err := manager.database.Update(id, profile) err := manager.database.Update(id, profile)
if err != nil { if err != nil {
manager.membersMu.Unlock() manager.membersMu.Unlock()