2020-11-21 23:42:34 +01:00
|
|
|
package members
|
2020-11-21 23:39:24 +01:00
|
|
|
|
|
|
|
import (
|
2021-09-09 00:33:11 +02:00
|
|
|
"errors"
|
2020-11-21 23:39:24 +01:00
|
|
|
"net/http"
|
2021-03-14 20:26:58 +01:00
|
|
|
"strconv"
|
2020-11-21 23:39:24 +01:00
|
|
|
|
2022-07-14 00:58:22 +02:00
|
|
|
"github.com/demodesk/neko/pkg/types"
|
|
|
|
"github.com/demodesk/neko/pkg/utils"
|
2020-11-21 23:39:24 +01:00
|
|
|
)
|
|
|
|
|
2020-12-08 16:04:54 +01:00
|
|
|
type MemberDataPayload struct {
|
2021-03-14 20:26:58 +01:00
|
|
|
ID string `json:"id"`
|
|
|
|
Profile types.MemberProfile `json:"profile"`
|
2020-11-22 00:06:55 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:44:32 +01:00
|
|
|
type MemberCreatePayload struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Profile types.MemberProfile `json:"profile"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemberPasswordPayload struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func (h *MembersHandler) membersList(w http.ResponseWriter, r *http.Request) error {
|
2021-03-14 14:44:32 +01:00
|
|
|
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Default zero.
|
|
|
|
limit = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
offset, err := strconv.Atoi(r.URL.Query().Get("offset"))
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Default zero.
|
|
|
|
offset = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := h.members.SelectAll(limit, offset)
|
|
|
|
if err != nil {
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpInternalServerError().WithInternalErr(err)
|
2021-03-14 14:44:32 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 16:05:54 +01:00
|
|
|
members := []MemberDataPayload{}
|
2021-03-14 14:44:32 +01:00
|
|
|
for id, profile := range entries {
|
2020-12-08 16:05:54 +01:00
|
|
|
members = append(members, MemberDataPayload{
|
2021-03-14 16:19:23 +01:00
|
|
|
ID: id,
|
|
|
|
Profile: profile,
|
2020-12-08 16:05:54 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpSuccess(w, members)
|
2020-12-08 16:05:54 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) error {
|
2021-03-14 14:44:32 +01:00
|
|
|
data := &MemberCreatePayload{
|
2020-12-08 15:37:48 +01:00
|
|
|
// default values
|
2021-03-14 14:44:32 +01:00
|
|
|
Profile: types.MemberProfile{
|
2021-11-03 21:59:38 +01:00
|
|
|
IsAdmin: false,
|
|
|
|
CanLogin: true,
|
|
|
|
CanConnect: true,
|
|
|
|
CanWatch: true,
|
|
|
|
CanHost: true,
|
2021-12-09 21:48:02 +01:00
|
|
|
CanShareMedia: true,
|
2021-11-03 21:59:38 +01:00
|
|
|
CanAccessClipboard: true,
|
|
|
|
SendsInactiveCursor: true,
|
|
|
|
CanSeeInactiveCursors: true,
|
2020-12-08 15:14:20 +01:00
|
|
|
},
|
2020-12-08 14:13:12 +01:00
|
|
|
}
|
2021-03-14 20:26:58 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
if err := utils.HttpJsonRequest(w, r, data); err != nil {
|
|
|
|
return err
|
2020-11-22 00:06:55 +01:00
|
|
|
}
|
2020-11-21 23:39:24 +01:00
|
|
|
|
2021-03-14 14:44:32 +01:00
|
|
|
if data.Username == "" {
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpBadRequest("username cannot be empty")
|
2020-12-06 21:10:45 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:44:32 +01:00
|
|
|
if data.Password == "" {
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpBadRequest("password cannot be empty")
|
2020-11-25 22:39:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:44:32 +01:00
|
|
|
id, err := h.members.Insert(data.Username, data.Password, data.Profile)
|
2020-12-05 23:05:46 +01:00
|
|
|
if err != nil {
|
2021-09-09 00:33:11 +02:00
|
|
|
if errors.Is(err, types.ErrMemberAlreadyExists) {
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpUnprocessableEntity("member already exists")
|
2021-09-09 00:33:11 +02:00
|
|
|
}
|
2021-09-17 00:58:50 +02:00
|
|
|
|
|
|
|
return utils.HttpInternalServerError().WithInternalErr(err)
|
2020-12-05 23:05:46 +01:00
|
|
|
}
|
2021-03-14 20:26:58 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpSuccess(w, MemberDataPayload{
|
2021-03-14 16:19:23 +01:00
|
|
|
ID: id,
|
|
|
|
Profile: data.Profile,
|
2020-11-22 00:06:55 +01:00
|
|
|
})
|
2020-11-21 23:39:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) error {
|
2020-11-21 23:39:24 +01:00
|
|
|
member := GetMember(r)
|
2021-03-14 14:44:32 +01:00
|
|
|
profile := member.Profile
|
2020-11-21 23:39:24 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpSuccess(w, profile)
|
2020-11-21 23:39:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func (h *MembersHandler) membersUpdateProfile(w http.ResponseWriter, r *http.Request) error {
|
2020-11-21 23:39:24 +01:00
|
|
|
member := GetMember(r)
|
2021-09-17 00:58:50 +02:00
|
|
|
data := &member.Profile
|
2020-12-06 21:10:45 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
if err := utils.HttpJsonRequest(w, r, data); err != nil {
|
|
|
|
return err
|
2020-12-06 21:10:45 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
if err := h.members.UpdateProfile(member.ID, *data); err != nil {
|
|
|
|
return utils.HttpInternalServerError().WithInternalErr(err)
|
2021-03-14 14:44:32 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpSuccess(w)
|
2021-03-14 14:44:32 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func (h *MembersHandler) membersUpdatePassword(w http.ResponseWriter, r *http.Request) error {
|
2021-03-14 14:44:32 +01:00
|
|
|
member := GetMember(r)
|
2021-09-17 00:58:50 +02:00
|
|
|
data := &MemberPasswordPayload{}
|
2021-03-14 14:44:32 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
if err := utils.HttpJsonRequest(w, r, data); err != nil {
|
|
|
|
return err
|
2021-03-14 14:44:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.members.UpdatePassword(member.ID, data.Password); err != nil {
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpInternalServerError().WithInternalErr(err)
|
2020-12-05 22:18:45 +01:00
|
|
|
}
|
2020-12-02 17:43:34 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpSuccess(w)
|
2020-11-21 23:39:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) error {
|
2020-11-21 23:39:24 +01:00
|
|
|
member := GetMember(r)
|
|
|
|
|
2021-03-14 14:44:32 +01:00
|
|
|
if err := h.members.Delete(member.ID); err != nil {
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpInternalServerError().WithInternalErr(err)
|
2020-11-25 22:39:54 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpSuccess(w)
|
2020-11-21 23:39:24 +01:00
|
|
|
}
|