<commit_before>package accounts

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"net/http"
	"strconv"

	"github.com/RichardKnop/recall/accounts/roles"
	"github.com/RichardKnop/recall/response"
	"github.com/gorilla/mux"
)

var (
	errUpdateUserPermission = errors.New("Need permission to update users")
)

// Handles requests to update a user (PUT /v1/accounts/users/{id:[0-9]+})
func (s *Service) updateUserHandler(w http.ResponseWriter, r *http.Request) {
	// Get the authenticated user from the request context
	authenticatedUser, err := GetAuthenticatedUser(r)
	if err != nil {
		response.UnauthorizedError(w, err.Error())
		return
	}

	// Request body cannot be nil
	if r.Body == nil {
		response.Error(w, "Request body cannot be nil", http.StatusBadRequest)
		return
	}

	// Read the request body
	payload, err := ioutil.ReadAll(r.Body)
	if err != nil {
		response.Error(w, "Error reading request body", http.StatusBadRequest)
		return
	}

	// Unmarshal the request body into the request prototype
	userRequest := new(UserRequest)
	if err := json.Unmarshal(payload, userRequest); err != nil {
		logger.Errorf("Failed to unmarshal user request: %s", payload)
		response.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Get the id from request URI and type assert it
	vars := mux.Vars(r)
	userID, err := strconv.Atoi(vars["id"])
	if err != nil {
		response.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Fetch the user we want to update
	user, err := s.FindUserByID(uint(userID))
	if err != nil {
		response.Error(w, err.Error(), http.StatusNotFound)
		return
	}

	// Check permissions
	if err := checkUpdateUserPermissions(authenticatedUser, user); err != nil {
		response.Error(w, err.Error(), http.StatusForbidden)
		return
	}

	// Update the user
	if err := s.UpdateUser(user, userRequest); err != nil {
		logger.Errorf("Update user error: %s", err)
		code, ok := errStatusCodeMap[err]
		if !ok {
			code = http.StatusInternalServerError
		}
		response.Error(w, err.Error(), code)
		return
	}

	// Create response
	userResponse, err := NewUserResponse(user)
	if err != nil {
		response.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	// Write JSON response
	response.WriteJSON(w, userResponse, http.StatusOK)
}

func checkUpdateUserPermissions(authenticatedUser, user *User) error {
	// Superadmins can update any users
	if authenticatedUser.Role.Name == roles.Superuser {
		return nil
	}

	// Users can update their own accounts
	if authenticatedUser.ID == user.ID {
		return nil
	}

	// The user doesn't have the permission
	return errUpdateUserPermission
}<commit_msg>Fixed broken test.<commit_after>package accounts

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"net/http"
	"strconv"

	"github.com/RichardKnop/recall/accounts/roles"
	"github.com/RichardKnop/recall/response"
	"github.com/gorilla/mux"
)

var (
	// ErrUpdateUserPermission ...
	ErrUpdateUserPermission = errors.New("Need permission to update users")
)

// Handles requests to update a user (PUT /v1/accounts/users/{id:[0-9]+})
func (s *Service) updateUserHandler(w http.ResponseWriter, r *http.Request) {
	// Get the authenticated user from the request context
	authenticatedUser, err := GetAuthenticatedUser(r)
	if err != nil {
		response.UnauthorizedError(w, err.Error())
		return
	}

	// Request body cannot be nil
	if r.Body == nil {
		response.Error(w, "Request body cannot be nil", http.StatusBadRequest)
		return
	}

	// Read the request body
	payload, err := ioutil.ReadAll(r.Body)
	if err != nil {
		response.Error(w, "Error reading request body", http.StatusBadRequest)
		return
	}

	// Unmarshal the request body into the request prototype
	userRequest := new(UserRequest)
	if err := json.Unmarshal(payload, userRequest); err != nil {
		logger.Errorf("Failed to unmarshal user request: %s", payload)
		response.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Get the id from request URI and type assert it
	vars := mux.Vars(r)
	userID, err := strconv.Atoi(vars["id"])
	if err != nil {
		response.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Fetch the user we want to update
	user, err := s.FindUserByID(uint(userID))
	if err != nil {
		response.Error(w, err.Error(), http.StatusNotFound)
		return
	}

	// Check permissions
	if err := checkUpdateUserPermissions(authenticatedUser, user); err != nil {
		response.Error(w, err.Error(), http.StatusForbidden)
		return
	}

	// Update the user
	if err := s.UpdateUser(user, userRequest); err != nil {
		logger.Errorf("Update user error: %s", err)
		code, ok := errStatusCodeMap[err]
		if !ok {
			code = http.StatusInternalServerError
		}
		response.Error(w, err.Error(), code)
		return
	}

	// Create response
	userResponse, err := NewUserResponse(user)
	if err != nil {
		response.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	// Write JSON response
	response.WriteJSON(w, userResponse, http.StatusOK)
}

func checkUpdateUserPermissions(authenticatedUser, user *User) error {
	// Superadmins can update any users
	if authenticatedUser.Role.Name == roles.Superuser {
		return nil
	}

	// Users can update their own accounts
	if authenticatedUser.ID == user.ID {
		return nil
	}

	// The user doesn't have the permission
	return ErrUpdateUserPermission
}
{'fraction_non_alphanumeric': 0.12398412398412398, 'fraction_numerical': 0.001134001134001134, 'mean_word_length': 4.0256410256410255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1322400', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1733, 'n_words': 483}