Commit 6051c28f authored by Valery Sizov's avatar Valery Sizov

Allow groups to appear in the search results if the group owner allows it

parent 363900a3
......@@ -22,6 +22,7 @@ v 8.2.0 (unreleased)
- Include commit logs in project search
- Add "added", "modified" and "removed" properties to commit object in webhook
- Rename "Back to" links to "Go to" because its not always a case it point to place user come from
- Allow groups to appear in the search results if the group owner allows it
v 8.1.3
- Spread out runner contacted_at updates
......
......@@ -4,12 +4,12 @@ class GroupsController < Groups::ApplicationController
before_action :group, except: [:new, :create]
# Authorize
before_action :authorize_read_group!, except: [:show, :new, :create]
before_action :authorize_read_group!, except: [:show, :new, :create, :autocomplete]
before_action :authorize_admin_group!, only: [:edit, :update, :destroy, :projects]
before_action :authorize_create_group!, only: [:new, :create]
# Load group projects
before_action :load_projects, except: [:new, :create, :projects, :edit, :update]
before_action :load_projects, except: [:new, :create, :projects, :edit, :update, :autocomplete]
before_action :event_filter, only: :show
layout :determine_layout
......@@ -133,7 +133,7 @@ class GroupsController < Groups::ApplicationController
end
def group_params
params.require(:group).permit(:name, :description, :path, :avatar)
params.require(:group).permit(:name, :description, :path, :avatar, :public)
end
def load_events
......
......@@ -6,7 +6,7 @@ class GroupsFinder
private
def all_groups(current_user)
if current_user
group_ids = if current_user
if current_user.authorized_groups.any?
# User has access to groups
#
......@@ -15,9 +15,8 @@ class GroupsFinder
# groups with internal projects
# groups with joined projects
#
group_ids = Project.public_and_internal_only.pluck(:namespace_id) +
Project.public_and_internal_only.pluck(:namespace_id) +
current_user.authorized_groups.pluck(:id)
Group.where(id: group_ids)
else
# User has no group membership
#
......@@ -25,14 +24,16 @@ class GroupsFinder
# groups with public projects
# groups with internal projects
#
Group.where(id: Project.public_and_internal_only.pluck(:namespace_id))
Project.public_and_internal_only.pluck(:namespace_id)
end
else
# Not authenticated
#
# Return only:
# groups with public projects
Group.where(id: Project.public_only.pluck(:namespace_id))
Project.public_only.pluck(:namespace_id)
end
Group.where("public IS TRUE OR id IN(?)", group_ids)
end
end
......@@ -70,7 +70,7 @@ module SearchHelper
# Autocomplete results for the current user's groups
def groups_autocomplete(term, limit = 5)
current_user.authorized_groups.search(term).limit(limit).map do |group|
GroupsFinder.new.execute(current_user).search(term).limit(limit).map do |group|
{
label: "group: #{search_result_sanitize(group.name)}",
url: group_path(group)
......
......@@ -120,7 +120,7 @@ class Group < Namespace
end
def public_profile?
projects.public_only.any?
self.public || projects.public_only.any?
end
def post_create_hook
......
......@@ -25,6 +25,15 @@
%hr
= link_to 'Remove avatar', group_avatar_path(@group.to_param), data: { confirm: "Group avatar will be removed. Are you sure?"}, method: :delete, class: "btn btn-remove btn-sm remove-avatar"
.form-group
%hr
= f.label :public, class: 'control-label' do
Public
.col-sm-10
.checkbox
= f.check_box :public
%span.descr Make this group public (even if there is no any public project inside this group)
.form-actions
= f.submit 'Save group', class: "btn btn-save"
......
class AddPublicToGroup < ActiveRecord::Migration
def change
add_column :namespaces, :public, :boolean, default: false
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20151026182941) do
ActiveRecord::Schema.define(version: 20151103001141) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -509,6 +509,7 @@ ActiveRecord::Schema.define(version: 20151026182941) do
t.string "type"
t.string "description", default: "", null: false
t.string "avatar"
t.boolean "public", default: false
end
add_index "namespaces", ["created_at", "id"], name: "index_namespaces_on_created_at_and_id", using: :btree
......
require 'spec_helper'
describe GroupsFinder do
let(:user) { create :user }
let!(:group) { create :group }
let!(:public_group) { create :group, public: true }
describe :execute do
it 'finds public group' do
groups = GroupsFinder.new.execute(user)
expect(groups.size).to eq(1)
expect(groups.first).to eq(public_group)
end
end
end
......@@ -42,6 +42,11 @@ describe SearchHelper do
expect(search_autocomplete_opts(project.name).size).to eq(1)
end
it "includes the public group" do
group = create(:group, public: true)
expect(search_autocomplete_opts(group.name).size).to eq(1)
end
context "with a current project" do
before { @project = create(:project) }
......
......@@ -84,4 +84,23 @@ describe Group do
expect(group.avatar_type).to eq(["only images allowed"])
end
end
describe "public_profile?" do
it "returns true for public group" do
group = create(:group, public: true)
expect(group.public_profile?).to be_truthy
end
it "returns true for non-public group with public project" do
group = create(:group)
create(:project, :public, group: group)
expect(group.public_profile?).to be_truthy
end
it "returns false for non-public group with no public projects" do
group = create(:group)
create(:project, group: group)
expect(group.public_profile?).to be_falsy
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment