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-12-23 02:04:40 +05:30
|
|
|
before_action :find_users, only: [:users]
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def users
|
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)
|
2016-06-02 11:05:42 +05:30
|
|
|
@users = @users.page(params[:page])
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
if params[:search].blank?
|
2015-09-11 14:41:01 +05:30
|
|
|
# Include current user if available to filter by "Me"
|
|
|
|
if params[:current_user] && current_user
|
2016-06-02 11:05:42 +05:30
|
|
|
@users = [*@users, current_user]
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
if params[:author_id].present?
|
|
|
|
author = User.find_by_id(params[:author_id])
|
|
|
|
@users = [author, *@users] if author
|
|
|
|
end
|
|
|
|
|
|
|
|
@users.uniq!
|
2015-09-11 14:41:01 +05:30
|
|
|
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
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def projects
|
|
|
|
project = Project.find_by_id(params[:project_id])
|
|
|
|
|
|
|
|
projects = current_user.authorized_projects
|
|
|
|
projects = projects.search(params[:search]) if params[:search].present?
|
|
|
|
projects = projects.select do |project|
|
|
|
|
current_user.can?(:admin_issue, project)
|
|
|
|
end
|
|
|
|
|
|
|
|
no_project = {
|
|
|
|
id: 0,
|
|
|
|
name_with_namespace: 'No project',
|
|
|
|
}
|
|
|
|
projects.unshift(no_project)
|
|
|
|
projects.delete(project)
|
|
|
|
|
|
|
|
render json: projects.to_json(only: [:id, :name_with_namespace], methods: :name_with_namespace)
|
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
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
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|