feat: specify user creator + list only users related to admin
This commit is contained in:
@ -28,7 +28,19 @@ func NewUserDTO(row *repository.User) types.UserDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *AdminHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
|
func (h *AdminHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
|
||||||
users, err := h.repo.FindAllUsers(r.Context())
|
userId, ok := util.GetRequestUserId(r.Context())
|
||||||
|
if !ok {
|
||||||
|
web.Error(w, "failed to get user id from auth session", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := h.repo.FindUserId(r.Context(), uuid.MustParse(userId))
|
||||||
|
if err != nil {
|
||||||
|
web.Error(w, "failed to get access information", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
users, err := h.repo.FindAdminUsers(r.Context(), &user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERR: Failed to query users from db:", err)
|
log.Println("ERR: Failed to query users from db:", err)
|
||||||
web.Error(w, "failed to get all users", http.StatusInternalServerError)
|
web.Error(w, "failed to get all users", http.StatusInternalServerError)
|
||||||
@ -89,6 +101,18 @@ type CreateUserRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *AdminHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
func (h *AdminHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userId, ok := util.GetRequestUserId(r.Context())
|
||||||
|
if !ok {
|
||||||
|
web.Error(w, "failed to get user id from auth session", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := h.repo.FindUserId(r.Context(), uuid.MustParse(userId))
|
||||||
|
if err != nil {
|
||||||
|
web.Error(w, "failed to get access information", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var req CreateUserRequest
|
var req CreateUserRequest
|
||||||
|
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
@ -112,7 +136,7 @@ func (h *AdminHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := h.repo.FindUserEmail(r.Context(), req.Email)
|
_, err = h.repo.FindUserEmail(r.Context(), req.Email)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
web.Error(w, "user with provided email already exists", http.StatusBadRequest)
|
web.Error(w, "user with provided email already exists", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@ -130,11 +154,12 @@ func (h *AdminHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
FullName: req.FullName,
|
FullName: req.FullName,
|
||||||
PasswordHash: hash,
|
PasswordHash: hash,
|
||||||
IsAdmin: false,
|
IsAdmin: false,
|
||||||
|
CreatedBy: &user.ID,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("INFO: params for user creation:", params)
|
log.Println("INFO: params for user creation:", params)
|
||||||
|
|
||||||
userId, err := h.repo.InsertUser(r.Context(), params)
|
id, err := h.repo.InsertUser(r.Context(), params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERR: Failed to insert user into database:", err)
|
log.Println("ERR: Failed to insert user into database:", err)
|
||||||
web.Error(w, "failed to create user", http.StatusInternalServerError)
|
web.Error(w, "failed to create user", http.StatusInternalServerError)
|
||||||
@ -147,7 +172,7 @@ func (h *AdminHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
if err := encoder.Encode(Response{
|
if err := encoder.Encode(Response{
|
||||||
ID: userId.String(),
|
ID: id.String(),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
web.Error(w, "failed to encode response", http.StatusInternalServerError)
|
web.Error(w, "failed to encode response", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user