neko/internal/api/members/crud.go

111 lines
2.6 KiB
Go
Raw Normal View History

2020-11-22 11:42:34 +13:00
package members
2020-11-22 11:39:24 +13:00
import (
"net/http"
"demodesk/neko/internal/utils"
2020-11-26 10:39:54 +13:00
"demodesk/neko/internal/types"
2020-11-22 11:39:24 +13:00
)
2020-11-22 12:06:55 +13:00
type MemberCreatePayload struct {
2020-11-26 10:39:54 +13:00
ID string `json:"id"`
2020-11-22 12:06:55 +13:00
}
type MemberDataPayload struct {
2020-12-03 05:29:38 +13:00
ID string `json:"id"`
Secret string `json:"secret,omitempty"`
Name string `json:"name"`
IsAdmin bool `json:"is_admin"`
CanLogin bool `json:"can_login"`
CanConnect bool `json:"can_connect"`
CanWatch bool `json:"can_watch"`
CanHost bool `json:"can_host"`
CanAccessClipboard bool `json:"can_access_clipboard"`
2020-11-22 12:06:55 +13:00
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
2020-11-22 12:06:55 +13:00
data := &MemberDataPayload{}
if !utils.HttpJsonRequest(w, r, data) {
return
}
2020-11-22 11:39:24 +13:00
2020-12-03 04:50:30 +13:00
if data.ID == "" {
var err error
if data.ID, err = utils.NewUID(32); err != nil {
utils.HttpInternalServerError(w, err)
return
}
} else {
if _, ok := h.sessions.Get(data.ID); ok {
utils.HttpBadRequest(w, "Member ID already exists.")
return
}
2020-11-26 10:39:54 +13:00
}
2020-12-03 05:43:34 +13:00
// TODO: Join structs?
2020-12-03 04:50:30 +13:00
session := h.sessions.Create(data.ID, types.MemberProfile{
2020-11-28 08:00:14 +13:00
Secret: data.Secret,
Name: data.Name,
IsAdmin: data.IsAdmin,
2020-12-03 05:43:34 +13:00
CanLogin: data.CanLogin,
CanConnect: data.CanConnect,
CanWatch: data.CanWatch,
CanHost: data.CanHost,
CanAccessClipboard: data.CanAccessClipboard,
2020-11-28 08:00:14 +13:00
})
2020-11-22 12:06:55 +13:00
utils.HttpSuccess(w, MemberCreatePayload{
2020-11-26 10:39:54 +13:00
ID: session.ID(),
2020-11-22 12:06:55 +13:00
})
2020-11-22 11:39:24 +13:00
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
2020-11-22 11:39:24 +13:00
member := GetMember(r)
2020-12-03 05:43:34 +13:00
// TODO: Join structs?
2020-11-22 12:06:55 +13:00
utils.HttpSuccess(w, MemberDataPayload{
Name: member.Name(),
2020-12-03 04:50:30 +13:00
IsAdmin: member.IsAdmin(),
2020-12-03 05:43:34 +13:00
CanLogin: member.CanLogin(),
CanConnect: member.CanConnect(),
CanWatch: member.CanWatch(),
CanHost: member.CanHost(),
CanAccessClipboard: member.CanAccessClipboard(),
2020-11-22 12:06:55 +13:00
})
2020-11-22 11:39:24 +13:00
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
2020-11-22 12:06:55 +13:00
data := &MemberDataPayload{}
if !utils.HttpJsonRequest(w, r, data) {
return
}
2020-11-22 11:39:24 +13:00
member := GetMember(r)
2020-12-03 05:43:34 +13:00
// TODO: Join structs?
// TODO: Update independent props.
member.SetProfile(types.MemberProfile{
Secret: data.Secret,
Name: data.Name,
IsAdmin: data.IsAdmin,
CanLogin: data.CanLogin,
CanConnect: data.CanConnect,
CanWatch: data.CanWatch,
CanHost: data.CanHost,
CanAccessClipboard: data.CanAccessClipboard,
2020-11-22 12:06:55 +13:00
})
2020-12-03 05:43:34 +13:00
utils.HttpSuccess(w)
2020-11-22 11:39:24 +13:00
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) {
2020-11-22 11:39:24 +13:00
member := GetMember(r)
2020-11-26 10:39:54 +13:00
if err := h.sessions.Delete(member.ID()); err != nil {
2020-11-29 03:00:21 +13:00
utils.HttpInternalServerError(w, err)
2020-11-26 10:39:54 +13:00
return
}
utils.HttpSuccess(w)
2020-11-22 11:39:24 +13:00
}