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