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

4
  def users
5 6 7 8
    begin
      @users =
        if params[:project_id].present?
          project = Project.find(params[:project_id])
9

10 11 12 13 14
          if can?(current_user, :read_project, project)
            project.team.users
          end
        elsif params[:group_id]
          group = Group.find(params[:group_id])
15

16 17 18 19 20
          if can?(current_user, :read_group, group)
            group.users
          end
        elsif current_user
          User.all
21
        end
22 23 24
    rescue ActiveRecord::RecordNotFound
      if current_user
        return render json: {}, status: 404
25
      end
26 27 28 29 30
    end

    if @users.nil? && current_user.nil?
      authenticate_user!
    end
31

32
    @users ||= User.none
33 34
    @users = @users.search(params[:search]) if params[:search].present?
    @users = @users.active
35
    @users = @users.reorder(:name)
36
    @users = @users.page(params[:page]).per(PER_PAGE)
37 38 39 40 41 42 43 44

    unless params[:search].present?
      # Include current user if available to filter by "Me"
      if params[:current_user] && current_user
        @users = [*@users, current_user].uniq
      end
    end

45 46 47 48 49 50 51 52
    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
end