|
|
@@ -97,6 +97,9 @@ type adminUpdateUserRequest struct {
|
|
|
func (h *UserHandler) AdminUpdateUser(w http.ResponseWriter, r *http.Request) {
|
|
|
id := chi.URLParam(r, "id")
|
|
|
|
|
|
+ currentUserID := middleware.GetUserID(r.Context())
|
|
|
+ currentUserRole := middleware.GetUserRole(r.Context())
|
|
|
+
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodySize)
|
|
|
var req adminUpdateUserRequest
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
@@ -110,6 +113,21 @@ func (h *UserHandler) AdminUpdateUser(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
if req.Role != nil {
|
|
|
+ if id == currentUserID {
|
|
|
+ if currentUserRole != "superadmin" {
|
|
|
+ writeError(w, http.StatusForbidden, "вы не можете сменить свою роль", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ count, err := h.userRepo.CountByRole(r.Context(), "superadmin")
|
|
|
+ if err != nil {
|
|
|
+ writeError(w, http.StatusInternalServerError, "failed to count superadmins", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if count <= 1 {
|
|
|
+ writeError(w, http.StatusForbidden, "нельзя сменить роль единственного суперадмина", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
if err := h.userRepo.UpdateRole(r.Context(), id, *req.Role); err != nil {
|
|
|
writeError(w, http.StatusInternalServerError, "failed to update role", err)
|
|
|
return
|