<commit_before>require 'yodatra/models_controller'

# API controller to manage Users
class UsersController < Yodatra::ModelsController
  disable :read_all, :create, :delete, :nested_delete, :nested_create, :nested_update

  enable_search_on :name, :email

  # Get my personal information when authenticated
  # @returns your data
  get '/users/me' do
    current_user.as_json(read_scope).to_json
  end

  # Change your password
  # @params {
  #   password: '<PASSWORD>'
  # }
  # @returns 200 | 400
  put '/users/me/change_password' do
    if params[:password].nil?
      status 400
      [Errors::BAD_REQUEST].to_json
    else
      if current_user.change_password(params[:password])
        status 200
        'ok'.to_json
      else
        status 400
        'error'.to_json
      end
    end
  end

  # Add a user to a team
  # Receive mandatory data as shown below :
  # @params {
  #     'permissions' : 128,
  #     'id'     : 1
  #   }
  post '/teams/:team_id/users' do
    team = Team.find params[:team_id]
    user = User.find params[:id]

    if team.nil? || user.nil? || params[:permissions].blank?
      status 400
      [Errors::BAD_REQUEST].to_json
    else
      role = UserRole.new(
          user_id: user.id,
          team_id: team.id,
          permissions: params[:permissions].to_i<commit_msg>fix(users_controller): accept old param :user_id for POST /teams/:id/users action<commit_after>require 'yodatra/models_controller'

# API controller to manage Users
class UsersController < Yodatra::ModelsController
  disable :read_all, :create, :delete, :nested_delete, :nested_create, :nested_update

  enable_search_on :name, :email

  # Get my personal information when authenticated
  # @returns your data
  get '/users/me' do
    current_user.as_json(read_scope).to_json
  end

  # Change your password
  # @params {
  #   password: '<PASSWORD>'
  # }
  # @returns 200 | 400
  put '/users/me/change_password' do
    if params[:password].nil?
      status 400
      [Errors::BAD_REQUEST].to_json
    else
      if current_user.change_password(params[:password])
        status 200
        'ok'.to_json
      else
        status 400
        'error'.to_json
      end
    end
  end

  # Add a user to a team
  # Receive mandatory data as shown below :
  # @params {
  #     'permissions' : 128,
  #     'id'     : 1
  #   }
  post '/teams/:team_id/users' do
    team = Team.find params[:team_id]
    user = User.find params[:id] || params[:user_id]

    if team.nil? || user.nil? || params[:permissions].blank?
      status 400
      [Errors::BAD_REQUEST].to_json
    else
      role = UserRole.new(
          user_id: user.id,
          team_id: team.id,
          permissions: params[:permissions].to_i
{'fraction_non_alphanumeric': 0.10903083700440529, 'fraction_numerical': 0.016152716593245228, 'mean_word_length': 2.6824324324324325, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4535413', 'n_tokens_mistral': 925, 'n_tokens_neox': 872, 'n_words': 220}