add user config page and admin menu

This commit is contained in:
2019-12-17 19:28:30 +01:00
parent 4c33b0841e
commit 7653470a7c
16 changed files with 648 additions and 107 deletions

View File

@@ -1,5 +1,7 @@
package pixelapi
import "net/url"
// IsAdmin is the response to the /admin/is_admin API
type IsAdmin struct {
Success bool `json:"success"`
@@ -14,3 +16,31 @@ func (p *PixelAPI) UserIsAdmin() (resp IsAdmin, err error) {
}
return resp, nil
}
// AdminGlobal is a global setting in pixeldrain's back-end
type AdminGlobal struct {
Key string `json:"key"`
Value string `json:"value"`
}
// AdminGlobals is an array of globals
type AdminGlobals struct {
Success bool `json:"success"`
Globals []AdminGlobal `json:"globals"`
}
// AdminGetGlobals returns if the logged in user is an admin user
func (p *PixelAPI) AdminGetGlobals() (resp AdminGlobals, err error) {
if err = p.jsonRequest("GET", p.apiEndpoint+"/admin/globals", &resp); err != nil {
return resp, err
}
return resp, nil
}
// AdminSetGlobals returns if the logged in user is an admin user
func (p *PixelAPI) AdminSetGlobals(key, value string) (resp SuccessResponse, err error) {
var form = url.Values{}
form.Add("key", key)
form.Add("value", value)
return resp, p.form("POST", p.apiEndpoint+"/admin/globals", form, &resp, true)
}

View File

@@ -164,7 +164,6 @@ func (p *PixelAPI) form(
}
func parseJSONResponse(resp *http.Response, target interface{}, catchErrors bool) error {
var jdec = json.NewDecoder(resp.Body)
var err error
// Test for client side and server side errors
@@ -172,8 +171,7 @@ func parseJSONResponse(resp *http.Response, target interface{}, catchErrors bool
var errResp = Error{
ReqError: false,
}
err = jdec.Decode(&errResp)
if err != nil {
if err = json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
log.Error("Can't decode this: %v", err)
return Error{
ReqError: true,
@@ -185,8 +183,11 @@ func parseJSONResponse(resp *http.Response, target interface{}, catchErrors bool
return errResp
}
err = jdec.Decode(target)
if err != nil {
if target == nil {
return nil
}
if err = json.NewDecoder(resp.Body).Decode(target); err != nil {
r, _ := ioutil.ReadAll(resp.Body)
log.Error("Can't decode this: %v. %s", err, r)
return Error{

View File

@@ -3,6 +3,7 @@ package pixelapi
import (
"fmt"
"net/url"
"strconv"
)
// Registration is the response to the UserRegister API. The register API can
@@ -66,6 +67,7 @@ func (p *PixelAPI) UserLogin(username, password string, saveKey bool) (resp *Log
type UserInfo struct {
Success bool `json:"success"`
Username string `json:"username"`
Email string `json:"email"`
}
// UserInfo returns information about the logged in user. Requires an API key
@@ -80,13 +82,8 @@ func (p *PixelAPI) UserInfo() (resp *UserInfo, err error) {
// UserSessionDestroy destroys an API key so it can no longer be used to perform
// actions
func (p *PixelAPI) UserSessionDestroy(key string) (resp *SuccessResponse, err error) {
resp = &SuccessResponse{}
err = p.jsonRequest("DELETE", p.apiEndpoint+"/user/session", resp)
if err != nil {
return nil, err
}
return resp, nil
func (p *PixelAPI) UserSessionDestroy(key string) (err error) {
return p.jsonRequest("DELETE", p.apiEndpoint+"/user/session", nil)
}
type UserFiles struct {
@@ -114,25 +111,64 @@ type UserLists struct {
func (p *PixelAPI) UserLists(page, limit int) (resp *UserLists, err error) {
resp = &UserLists{Lists: make([]List, 0)}
err = p.jsonRequest(
if err = p.jsonRequest(
"GET",
fmt.Sprintf("%s/user/lists?page=%d&limit=%d", p.apiEndpoint, page, limit),
resp,
)
if err != nil {
); err != nil {
return nil, err
}
return resp, nil
}
func (p *PixelAPI) UserPasswordSet(oldPW, newPW string) (resp *SuccessResponse, err error) {
resp = &SuccessResponse{}
func (p *PixelAPI) UserPasswordSet(oldPW, newPW string) (err error) {
var form = url.Values{}
form.Add("old_password", oldPW)
form.Add("new_password", newPW)
err = p.form("PUT", p.apiEndpoint+"/user/password", form, resp, true)
if err != nil {
return nil, err
}
return resp, nil
return p.form("PUT", p.apiEndpoint+"/user/password", form, nil, true)
}
// UserEmailReset starts the e-mail change process. An email will be sent to the
// new address to verify that it's real. Once the link in the e-mail is clicked
// the key it contains can be sent to the API with UserEmailResetConfirm and the
// change will be applied
func (p *PixelAPI) UserEmailReset(email string, delete bool) (err error) {
var form = url.Values{}
form.Add("new_email", email)
form.Add("delete", strconv.FormatBool(delete))
return p.form("PUT", p.apiEndpoint+"/user/email_reset", form, nil, true)
}
// UserEmailResetConfirm finishes process of changing a user's e-mail address
func (p *PixelAPI) UserEmailResetConfirm(key string) (err error) {
var form = url.Values{}
form.Add("key", key)
return p.form("PUT", p.apiEndpoint+"/user/email_reset_cofirm", form, nil, true)
}
// UserPasswordReset starts the password reset process. An email will be sent
// the user to verify that it really wanted to reset the password. Once the link
// in the e-mail is clicked the key it contains can be sent to the API with
// UserPasswordResetConfirm and a new password can be set
func (p *PixelAPI) UserPasswordReset(email string, recaptchaResponse string) (err error) {
var form = url.Values{}
form.Add("email", email)
form.Add("recaptcha_response", recaptchaResponse)
return p.form("PUT", p.apiEndpoint+"/user/password_reset", form, nil, true)
}
// UserPasswordResetConfirm finishes process of resetting a user's password. If
// the key is valid the new_password parameter will be saved as the new password
func (p *PixelAPI) UserPasswordResetConfirm(key string, newPassword string) (err error) {
var form = url.Values{}
form.Add("key", key)
form.Add("new_password", newPassword)
return p.form("PUT", p.apiEndpoint+"/user/password_reset_confirm", form, nil, true)
}
// UserSetUsername changes the user's username.
func (p *PixelAPI) UserSetUsername(username string) (err error) {
var form = url.Values{}
form.Add("new_username", username)
return p.form("PUT", p.apiEndpoint+"/user/username", form, nil, true)
}