neko/internal/api/members/crud.go

83 lines
1.7 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-11-26 10:39:54 +13:00
ID string `json:"id"`
2020-11-28 08:00:14 +13:00
Secret string `json:"secret,omitempty"`
2020-11-26 10:39:54 +13:00
Name string `json:"name"`
IsAdmin bool `json:"is_admin"`
//Enabled bool `json:"enabled"`
//CanControl bool `json:"can_control"`
//CanWatch bool `json:"can_watch"`
//ClipboardAccess bool `json:"clipboard_access"`
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-11-28 08:00:14 +13:00
id, err := utils.NewUID(32)
2020-11-26 10:39:54 +13:00
if err != nil {
2020-11-29 03:00:21 +13:00
utils.HttpInternalServerError(w, err)
2020-11-26 10:39:54 +13:00
return
}
2020-11-28 08:00:14 +13:00
session := h.sessions.Create(id, types.MemberProfile{
Secret: data.Secret,
Name: data.Name,
IsAdmin: data.IsAdmin,
})
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-11-22 12:06:55 +13:00
utils.HttpSuccess(w, MemberDataPayload{
ID: member.ID(),
Name: member.Name(),
2020-11-26 10:39:54 +13:00
IsAdmin: member.Admin(),
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-11-22 12:06:55 +13:00
utils.HttpSuccess(w, MemberDataPayload{
ID: member.ID(),
Name: member.Name(),
2020-11-26 10:39:54 +13:00
IsAdmin: member.Admin(),
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) 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
}