added possible-user-tasks
This commit is contained in:
@@ -94,7 +94,7 @@ func (a *Handler) getEventsAvailabilities() {
|
||||
|
||||
func (a *Handler) getEventUserAssignmentAvailability() {
|
||||
// retrieve the assignments
|
||||
if events, err := events.WithUserAvailability(a.UserName); err != nil {
|
||||
if events, err := a.UserName.WithUserAvailability(); err != nil {
|
||||
a.Status = fiber.StatusBadRequest
|
||||
|
||||
logger.Log().Msgf("getting events with tasks and user-availability failed: %v", err)
|
||||
@@ -104,7 +104,7 @@ func (a *Handler) getEventUserAssignmentAvailability() {
|
||||
}
|
||||
|
||||
func (a *Handler) getEventsUserPending() {
|
||||
if events, err := events.UserPending(a.UserName); err != nil {
|
||||
if events, err := a.UserName.UserPending(); err != nil {
|
||||
a.Status = fiber.StatusInternalServerError
|
||||
|
||||
logger.Warn().Msgf("can't query database for users %q pending events: %v", a.UserName, err)
|
||||
@@ -114,7 +114,7 @@ func (a *Handler) getEventsUserPending() {
|
||||
}
|
||||
|
||||
func (a *Handler) getEventsUserPendingCount() {
|
||||
if count, err := events.UserPendingCount(a.UserName); err != nil {
|
||||
if count, err := a.UserName.UserPendingCount(); err != nil {
|
||||
a.Status = fiber.StatusInternalServerError
|
||||
|
||||
logger.Warn().Msgf("can't query database for users %q pending events: %v", a.UserName, err)
|
||||
@@ -125,7 +125,7 @@ func (a *Handler) getEventsUserPendingCount() {
|
||||
|
||||
func (a *Handler) getEventsUserAssigned() {
|
||||
// retrieve the events from the database
|
||||
if events, err := events.User(a.UserName); err != nil {
|
||||
if events, err := a.UserName.GetAssignedEvents(); err != nil {
|
||||
a.Status = fiber.StatusBadRequest
|
||||
|
||||
logger.Log().Msgf("retrieval of user-assigned-events failed: %v", err)
|
||||
@@ -172,7 +172,7 @@ func (a *Handler) putEventUserAvailability() {
|
||||
}
|
||||
|
||||
// insert the availability into the database
|
||||
if err := events.SetUserAvailability(eventID, availabilityID, a.UserName); err != nil {
|
||||
if err := a.UserName.SetEventAvailability(eventID, availabilityID); err != nil {
|
||||
a.Status = fiber.StatusInternalServerError
|
||||
|
||||
logger.Error().Msgf("setting user-event-availability failed: can't write availability to database: %v", err)
|
||||
|
||||
@@ -3,6 +3,7 @@ package router
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/johannesbuehl/golunteer/backend/pkg/db"
|
||||
"github.com/johannesbuehl/golunteer/backend/pkg/db/users"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
@@ -45,8 +46,8 @@ func handleLogin(c *fiber.Ctx) error {
|
||||
|
||||
// extract username and password from the request
|
||||
requestBody := struct {
|
||||
Username string `json:"userName" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
users.UserName `json:"userName" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
}{}
|
||||
|
||||
if err := args.C.BodyParser(&requestBody); err != nil {
|
||||
@@ -60,21 +61,21 @@ func handleLogin(c *fiber.Ctx) error {
|
||||
} else {
|
||||
// query the database for the user
|
||||
var result userDB
|
||||
if err := db.DB.QueryRowx("SELECT password, admin, tokenID FROM USERS WHERE userName = ?", requestBody.Username).StructScan(&result); err != nil {
|
||||
if err := db.DB.QueryRowx("SELECT password, admin, tokenID FROM USERS WHERE userName = ?", requestBody.UserName).StructScan(&result); err != nil {
|
||||
args.Status = fiber.StatusForbidden
|
||||
args.Message = messageWrongLogin
|
||||
|
||||
logger.Info().Msgf("can't get user with userName = %q from database", requestBody.Username)
|
||||
logger.Info().Msgf("can't get user with userName = %q from database", requestBody.UserName)
|
||||
} else {
|
||||
// hash the password
|
||||
if bcrypt.CompareHashAndPassword(result.Password, []byte(requestBody.Password)) != nil {
|
||||
args.Status = fiber.StatusForbidden
|
||||
|
||||
logger.Info().Msgf("login denied: wrong password for user with userName = %q", requestBody.Username)
|
||||
logger.Info().Msgf("login denied: wrong password for user with userName = %q", requestBody.UserName)
|
||||
} else {
|
||||
// password is correct -> generate the JWT
|
||||
if jwt, err := config.SignJWT(JWTPayload{
|
||||
UserName: requestBody.Username,
|
||||
UserName: requestBody.UserName,
|
||||
TokenID: result.TokenID,
|
||||
}); err != nil {
|
||||
args.Status = fiber.StatusInternalServerError
|
||||
@@ -83,11 +84,11 @@ func handleLogin(c *fiber.Ctx) error {
|
||||
args.setSessionCookie(&jwt)
|
||||
|
||||
args.Data = UserChecked{
|
||||
UserName: requestBody.Username,
|
||||
UserName: requestBody.UserName,
|
||||
Admin: true,
|
||||
}
|
||||
|
||||
logger.Debug().Msgf("user %q logged in", requestBody.Username)
|
||||
logger.Debug().Msgf("user %q logged in", requestBody.UserName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
_config "github.com/johannesbuehl/golunteer/backend/pkg/config"
|
||||
"github.com/johannesbuehl/golunteer/backend/pkg/db"
|
||||
"github.com/johannesbuehl/golunteer/backend/pkg/db/users"
|
||||
_logger "github.com/johannesbuehl/golunteer/backend/pkg/logger"
|
||||
)
|
||||
|
||||
@@ -194,8 +195,8 @@ func (args Handler) removeSessionCookie() {
|
||||
|
||||
// payload of the JSON webtoken
|
||||
type JWTPayload struct {
|
||||
UserName string `json:"userName"`
|
||||
TokenID string `json:"tokenID"`
|
||||
users.UserName `json:"userName"`
|
||||
TokenID string `json:"tokenID"`
|
||||
}
|
||||
|
||||
// complete JSON webtoken
|
||||
@@ -207,7 +208,7 @@ type JWT struct {
|
||||
// extracts the json webtoken from the request
|
||||
//
|
||||
// @returns (userName, tokenID, error)
|
||||
func extractJWT(c *fiber.Ctx) (string, string, error) {
|
||||
func extractJWT(c *fiber.Ctx) (users.UserName, string, error) {
|
||||
// get the session-cookie
|
||||
cookie := c.Cookies("session")
|
||||
|
||||
@@ -240,8 +241,8 @@ type userDB struct {
|
||||
}
|
||||
|
||||
type UserChecked struct {
|
||||
UserName string `json:"userName" db:"userName"`
|
||||
Admin bool `json:"admin" db:"admin"`
|
||||
users.UserName `json:"userName" db:"userName"`
|
||||
Admin bool `json:"admin" db:"admin"`
|
||||
}
|
||||
|
||||
// checks wether the request is from a valid user
|
||||
|
||||
@@ -69,7 +69,7 @@ func (a *Handler) putPassword() {
|
||||
a.Status = fiber.StatusBadRequest
|
||||
|
||||
// send the password change to the database and get the new tokenID back
|
||||
} else if tokenID, err := users.ChangePassword(body); err != nil {
|
||||
} else if tokenID, err := body.UserName.ChangePassword(body.Password); err != nil {
|
||||
logger.Error().Msgf("can't update password: %v", err)
|
||||
|
||||
a.Status = fiber.StatusInternalServerError
|
||||
@@ -103,7 +103,7 @@ func (a *Handler) patchUser() {
|
||||
// parse the body
|
||||
var body struct {
|
||||
users.UserAdd
|
||||
NewName string `json:"newName"`
|
||||
NewName users.UserName `json:"newName"`
|
||||
}
|
||||
|
||||
if err := a.C.BodyParser(&body); err != nil {
|
||||
@@ -126,13 +126,7 @@ func (a *Handler) patchUser() {
|
||||
// if the password has length 0 assume the password shouldn't be changed
|
||||
} else {
|
||||
if len(body.Password) > 0 {
|
||||
// create a password-change-struct and validate it. use the old user-name, since the new isn't stored yet
|
||||
usePasswordChange := users.UserChangePassword{
|
||||
UserName: body.UserName,
|
||||
Password: body.Password,
|
||||
}
|
||||
|
||||
if _, err = users.ChangePassword(usePasswordChange); err != nil {
|
||||
if _, err = body.UserName.ChangePassword(body.Password); err != nil {
|
||||
a.Status = fiber.StatusInternalServerError
|
||||
|
||||
logger.Error().Msgf("can't change password: %v", err)
|
||||
@@ -143,7 +137,7 @@ func (a *Handler) patchUser() {
|
||||
|
||||
// only change the name, if it differs
|
||||
if body.NewName != body.UserName {
|
||||
if err := users.ChangeName(body.UserName, body.NewName); err != nil {
|
||||
if err := body.UserName.ChangeName(body.NewName); err != nil {
|
||||
a.Status = fiber.StatusInternalServerError
|
||||
|
||||
logger.Error().Msgf("can't change user-name: %v", err)
|
||||
@@ -153,15 +147,22 @@ func (a *Handler) patchUser() {
|
||||
}
|
||||
|
||||
// set the admin-status
|
||||
if err := users.SetAdmin(body.NewName, body.Admin); err != nil {
|
||||
if err := body.NewName.SetAdmin(body.Admin); err != nil {
|
||||
a.Status = fiber.StatusInternalServerError
|
||||
|
||||
logger.Error().Msgf("updating admin-status failed: %v", err)
|
||||
|
||||
// update the possible tasks
|
||||
} else if err := body.NewName.SetTasks(body.PossibleTasks); err != nil {
|
||||
a.Status = fiber.StatusInternalServerError
|
||||
|
||||
logger.Error().Msgf("updating possible user-tasks failed: %v", err)
|
||||
|
||||
} else {
|
||||
// if we modified ourself, update the session-cookie
|
||||
if body.UserName == a.UserName {
|
||||
if body.UserName != body.NewName {
|
||||
// get the tokenID
|
||||
if tokenID, err := users.TokenID(body.NewName); err != nil {
|
||||
if tokenID, err := body.NewName.TokenID(); err != nil {
|
||||
a.Status = fiber.StatusInternalServerError
|
||||
|
||||
logger.Error().Msgf("can't get tokenID: %v", err)
|
||||
@@ -200,7 +201,7 @@ func (a *Handler) deleteUser() {
|
||||
a.Status = fiber.StatusBadRequest
|
||||
|
||||
// check wether the user tries to delete himself
|
||||
} else if userName == a.UserName {
|
||||
} else if users.UserName(userName) == a.UserName {
|
||||
logger.Log().Msg("user-deletion failed: self-deletion is illegal")
|
||||
|
||||
a.Status = fiber.StatusBadRequest
|
||||
|
||||
Reference in New Issue
Block a user