BigW Consortium Gitlab

autocomplete_controller.rb 1.95 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])
11

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

18 19 20
      if params[:author_id].present?
        author = User.find_by_id(params[:author_id])
        @users = [author, *@users] if author
21 22 23
      end

      @users.uniq!
24 25
    end

26 27 28 29 30 31 32
    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
33

34 35 36 37
  def projects
    project = Project.find_by_id(params[:project_id])

    projects = current_user.authorized_projects
38
    projects = projects.search(params[:search]) if params[:search].present?
39 40 41 42
    projects = projects.select do |project|
      current_user.can?(:admin_issue, project)
    end

Phil Hughes committed
43 44 45 46 47
    no_project = {
      id: 0,
      name_with_namespace: 'No project',
    }
    projects.unshift(no_project)
48 49 50 51 52
    projects.delete(project)

    render json: projects.to_json(only: [:id, :name_with_namespace], methods: :name_with_namespace)
  end

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  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
73
end