debian-mirror-gitlab/app/controllers/autocomplete_controller.rb

53 lines
1.4 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
class AutocompleteController < ApplicationController
2015-09-11 14:41:01 +05:30
skip_before_action :authenticate_user!, only: [:users]
2015-04-26 12:48:37 +05:30
def users
2015-09-11 14:41:01 +05:30
begin
@users =
if params[:project_id].present?
project = Project.find(params[:project_id])
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
if can?(current_user, :read_project, project)
project.team.users
end
elsif params[:group_id]
group = Group.find(params[:group_id])
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
if can?(current_user, :read_group, group)
group.users
end
elsif current_user
User.all
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
rescue ActiveRecord::RecordNotFound
if current_user
return render json: {}, status: 404
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
end
if @users.nil? && current_user.nil?
authenticate_user!
end
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
@users ||= User.none
2015-04-26 12:48:37 +05:30
@users = @users.search(params[:search]) if params[:search].present?
@users = @users.active
2015-09-25 12:07:36 +05:30
@users = @users.reorder(:name)
2015-04-26 12:48:37 +05:30
@users = @users.page(params[:page]).per(PER_PAGE)
2015-09-11 14:41:01 +05:30
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
2015-04-26 12:48:37 +05:30
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