BigW Consortium Gitlab

autocomplete_controller.rb 1.27 KB
Newer Older
1
class AutocompleteController < ApplicationController
2
  skip_before_action :authenticate_user!, only: [:users]
3
  before_action :find_users, only: [:users]
4

5
  def users
6
    @users ||= User.none
7 8
    @users = @users.search(params[:search]) if params[:search].present?
    @users = @users.active
9
    @users = @users.reorder(:name)
10
    @users = @users.page(params[:page]).per(PER_PAGE)
11

12
    if params[:search].blank?
13 14 15 16 17 18
      # Include current user if available to filter by "Me"
      if params[:current_user] && current_user
        @users = [*@users, current_user].uniq
      end
    end

19 20 21 22 23 24 25
    render json: @users, only: [:name, :username, :id], methods: [:avatar_url]
  end

  def user
    @user = User.find(params[:id])
    render json: @user, only: [:name, :username, :id], methods: [:avatar_url]
  end
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

  private

  def find_users
    @users =
      if params[:project_id].present?
        project = Project.find(params[:project_id])
        return render_404 unless can?(current_user, :read_project, project)

        project.team.users
      elsif params[:group_id].present?
        group = Group.find(params[:group_id])
        return render_404 unless can?(current_user, :read_group, group)

        group.users
      elsif current_user
        User.all
      else
        User.none
      end
  end
47
end