.
This commit is contained in:
1 parent
90cff1ac5b
commit
6f6eea5529
7 files changed
+179
-105
No files matched your search
+8
-36
@@ -4,7 +4,6 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"party.at/party/cmd/party/common"
|
||||
"party.at/party/internal/data"
|
||||
@@ -41,38 +40,14 @@ func (api *Api) ListUsers(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (api *Api) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
var input struct {
|
||||
ProviderId int64 `json:"provider_id"`
|
||||
Username string `json:"username"`
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
Country string `json:"country"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Name string `json:"name"`
|
||||
AltName *string `json:"alt_name"`
|
||||
DateOfBirth time.Time `json:"date_of_birth"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
var input common.RegisterUserInput
|
||||
|
||||
if err := common.ReadJSON(w, r, &input); err != nil {
|
||||
api.errorResponse(w, r, data.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
user, authToken, err := api.App.RegisterUser(common.RegisterUserInput{
|
||||
ProviderID: input.ProviderId,
|
||||
Username: input.Username,
|
||||
PhoneNumber: input.PhoneNumber,
|
||||
Country: input.Country,
|
||||
Email: input.Email,
|
||||
Password: input.Password,
|
||||
Name: input.Name,
|
||||
AltName: input.AltName,
|
||||
DateOfBirth: input.DateOfBirth,
|
||||
Address: input.Address,
|
||||
})
|
||||
user, authToken, err := api.App.RegisterUser(input)
|
||||
if err != nil {
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
@@ -84,9 +59,11 @@ func (api *Api) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (api *Api) ReadUser(w http.ResponseWriter, r *http.Request) {
|
||||
var id int64
|
||||
param := r.PathValue("id")
|
||||
user := common.GetUser(r)
|
||||
|
||||
param := r.PathValue("id")
|
||||
|
||||
var id int64
|
||||
if param == "me" {
|
||||
id = common.GetUser(r).ID
|
||||
} else {
|
||||
@@ -98,12 +75,7 @@ func (api *Api) ReadUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if common.GetUser(r).ID != id {
|
||||
api.errorResponse(w, r, data.ErrNotPermitted)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := api.App.GetUser(id)
|
||||
target, err := api.App.GetUser(user, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, data.ErrRecordNotFound) {
|
||||
api.errorResponse(w, r, data.ErrRecordNotFound)
|
||||
@@ -113,7 +85,7 @@ func (api *Api) ReadUser(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"user": user}, nil); err != nil {
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"user": target}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
+61
-14
@@ -8,16 +8,17 @@ import (
|
||||
)
|
||||
|
||||
type RegisterUserInput struct {
|
||||
ProviderID int64
|
||||
Username string
|
||||
PhoneNumber string
|
||||
Country string
|
||||
Email string
|
||||
Password string
|
||||
Name string
|
||||
AltName *string
|
||||
DateOfBirth time.Time
|
||||
Address string
|
||||
ProviderID int64 `json:"provider_id"`
|
||||
Username string `json:"username"`
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
Country string `json:"country"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Name string `json:"name"`
|
||||
AltName *string `json:"alt_name"`
|
||||
DateOfBirth time.Time `json:"date_of_birth"`
|
||||
Address string `json:"address"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
func (app *Application) RegisterUser(input RegisterUserInput) (*data.User, *data.Token, error) {
|
||||
@@ -53,8 +54,8 @@ func (app *Application) RegisterUser(input RegisterUserInput) (*data.User, *data
|
||||
}
|
||||
|
||||
role := "viewer"
|
||||
if app.Config.Env == "development" {
|
||||
role = "admin"
|
||||
if app.Config.Env == "development" && input.Role != "" {
|
||||
role = input.Role
|
||||
}
|
||||
|
||||
if err := app.Models.Roles.AssignToUser(user.ID, role); err != nil {
|
||||
@@ -85,8 +86,54 @@ func (app *Application) RegisterUser(input RegisterUserInput) (*data.User, *data
|
||||
return user, authToken, nil
|
||||
}
|
||||
|
||||
func (app *Application) GetUser(id int64) (*data.User, error) {
|
||||
return app.Models.Users.Get(id)
|
||||
func (app *Application) GetUser(user *data.User, id int64) (*data.User, error) {
|
||||
var ret data.User
|
||||
|
||||
roles, err := app.Models.Roles.GetAllForUser(user.ID)
|
||||
if err != nil {
|
||||
return &ret, err
|
||||
}
|
||||
|
||||
target, err := app.Models.Users.Get(id)
|
||||
if err != nil {
|
||||
return &ret, err
|
||||
}
|
||||
|
||||
if user.ID == target.ID {
|
||||
return target, nil
|
||||
}
|
||||
|
||||
if roles[0].Code == "viewer" {
|
||||
return nil, data.ErrNotPermitted
|
||||
}
|
||||
|
||||
if roles[0].Code == "contributor" {
|
||||
return nil, data.ErrInvalidCredentials
|
||||
}
|
||||
|
||||
if roles[0].Code == "member_of_parliament" {
|
||||
ret.Email = target.Email
|
||||
ret.PhoneNumber = target.PhoneNumber
|
||||
ret.Country = target.Country
|
||||
ret.Name = target.Name
|
||||
ret.AltName = target.AltName
|
||||
ret.DateOfBirth = target.DateOfBirth
|
||||
ret.Address = target.Address
|
||||
}
|
||||
|
||||
if roles[0].Code == "party_leadership" {
|
||||
|
||||
}
|
||||
|
||||
if roles[0].Code == "admin" {
|
||||
ret.ID = target.ID
|
||||
ret.LastLogin = target.LastLogin
|
||||
ret.Activated = target.Activated
|
||||
ret.Created = target.Created
|
||||
ret.Version = target.Version
|
||||
}
|
||||
|
||||
return &ret, nil
|
||||
}
|
||||
|
||||
func (app *Application) ListUsers(filters data.Filters) ([]*data.User, data.Metadata, error) {
|
||||
|
||||
@@ -92,8 +92,14 @@ func (ts *testServer) get(t *testing.T, path string) (int, http.Header, []byte)
|
||||
return rs.StatusCode, rs.Header, body
|
||||
}
|
||||
|
||||
func (ts *testServer) registerAndLogin(t *testing.T, email, password string) string {
|
||||
func (ts *testServer) registerAndLogin(t *testing.T, email, password string, roles ...string) string {
|
||||
t.Helper()
|
||||
|
||||
var role string
|
||||
if len(role) > 0 {
|
||||
role = roles[0]
|
||||
}
|
||||
|
||||
registerBody := map[string]any{
|
||||
"email": email,
|
||||
"password": password,
|
||||
@@ -101,6 +107,7 @@ func (ts *testServer) registerAndLogin(t *testing.T, email, password string) str
|
||||
"name": "Test User",
|
||||
"alt_name": "",
|
||||
"provider_id": 1,
|
||||
"role": role,
|
||||
}
|
||||
code, _, body := ts.postJSON(t, "/v1/users", registerBody)
|
||||
if code != http.StatusCreated {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
// "net/http"
|
||||
// "strconv"
|
||||
"testing"
|
||||
// "time"
|
||||
)
|
||||
|
||||
func TestReadUserHandler(t *testing.T) {
|
||||
app := newTestApplication(t)
|
||||
ts := newTestServer(t, app, routes(app))
|
||||
defer ts.Close()
|
||||
|
||||
token := ts.registerAndLogin(t, uniqueEmail(), "pa$$word123", "viewer")
|
||||
|
||||
code, _, body := ts.getWithToken(t, "/v1/users/1", token)
|
||||
|
||||
if code != 200 {
|
||||
t.Errorf("want %d; got %d", 200, code)
|
||||
}
|
||||
|
||||
if !bytes.Contains(body, []byte("An old silent pond...")) {
|
||||
t.Errorf("want body to contain %q; got %q", "", string(body))
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,7 @@ func (web *Web) ProfilePage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
fullUser, err := web.App.GetUser(user.ID)
|
||||
fullUser, err := web.App.GetUser(user, user.ID)
|
||||
if err != nil {
|
||||
if errors.Is(err, data.ErrRecordNotFound) {
|
||||
http.NotFound(w, r)
|
||||
|
||||
Reference in new issue
Block a user