Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
11883e99
Commit
11883e99
authored
Jun 22, 2016
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix visibility of snippets when searching using ElasticSearch
parent
33be0d28
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
36 deletions
+67
-36
app/models/concerns/elastic/snippets_search.rb
app/models/concerns/elastic/snippets_search.rb
+20
-14
lib/gitlab/elastic/snippet_search_results.rb
lib/gitlab/elastic/snippet_search_results.rb
+2
-2
spec/models/concerns/elastic/snippet_spec.rb
spec/models/concerns/elastic/snippet_spec.rb
+45
-20
No files found.
app/models/concerns/elastic/snippets_search.rb
View file @
11883e99
...
...
@@ -45,7 +45,7 @@ module Elastic
def
self
.
elastic_search
(
query
,
options:
{})
query_hash
=
basic_query_hash
(
%w(title file_name)
,
query
)
query_hash
=
filter
(
query_hash
,
options
[
:
author_id
])
query_hash
=
filter
(
query_hash
,
options
[
:
user
])
self
.
__elasticsearch__
.
search
(
query_hash
)
end
...
...
@@ -59,7 +59,7 @@ module Elastic
}
}
query_hash
=
filter
(
query_hash
,
options
[
:
author_id
])
query_hash
=
filter
(
query_hash
,
options
[
:
user
])
query_hash
[
:sort
]
=
[
{
updated_at_sort:
{
order: :desc
,
mode: :min
}
},
...
...
@@ -71,18 +71,24 @@ module Elastic
self
.
__elasticsearch__
.
search
(
query_hash
)
end
def
self
.
filter
(
query_hash
,
author_id
)
if
author_id
query_hash
[
:query
][
:bool
][
:filter
]
=
{
bool:
{
should:
[
{
terms:
{
visibility_level:
[
Snippet
::
PUBLIC
,
Snippet
::
INTERNAL
]
}
},
{
term:
{
author_id:
author_id
}
}
]
}
}
end
def
self
.
filter
(
query_hash
,
user
)
return
query_hash
if
user
&&
user
.
admin?
filter
=
if
user
{
bool:
{
should:
[
{
terms:
{
visibility_level:
[
Snippet
::
PUBLIC
,
Snippet
::
INTERNAL
]
}
},
{
term:
{
author_id:
user
.
id
}
},
{
terms:
{
project_id:
user
.
authorized_projects
.
pluck
(
:id
)
}
},
]
}
}
else
{
term:
{
visibility_level:
Snippet
::
PUBLIC
}
}
end
query_hash
[
:query
][
:bool
][
:filter
]
=
filter
query_hash
end
end
...
...
lib/gitlab/elastic/snippet_search_results.rb
View file @
11883e99
...
...
@@ -21,7 +21,7 @@ module Gitlab
def
snippet_titles
opt
=
{
author_id:
@user
.
id
user:
@user
}
Snippet
.
elastic_search
(
query
,
options:
opt
)
...
...
@@ -29,7 +29,7 @@ module Gitlab
def
snippet_blobs
opt
=
{
author_id:
@user
.
id
user:
@user
}
Snippet
.
elastic_search_code
(
query
,
options:
opt
)
...
...
spec/models/concerns/elastic/snippet_spec.rb
View file @
11883e99
...
...
@@ -11,45 +11,70 @@ describe Snippet, elastic: true do
stub_application_setting
(
elasticsearch_search:
false
,
elasticsearch_indexing:
false
)
end
it
"searches snippets by code"
do
user
=
create
:user
it
'searches snippets by code'
do
author
=
create
(
:user
)
project
=
create
(
:project
)
snippet
=
create
:personal_snippet
,
:private
,
content:
'genius code'
,
author:
user
create
:personal_snippet
,
:private
,
content:
'genius code'
create
:personal_snippet
,
:private
public_snippet
=
create
(
:snippet
,
:public
,
content:
'password: XXX'
)
internal_snippet
=
create
(
:snippet
,
:internal
,
content:
'password: XXX'
)
private_snippet
=
create
(
:snippet
,
:private
,
content:
'password: XXX'
,
author:
author
)
snippet3
=
create
:personal_snippet
,
:public
,
content:
'genius code'
project_public_snippet
=
create
(
:snippet
,
:public
,
project:
project
,
content:
'password: XXX'
)
project_internal_snippet
=
create
(
:snippet
,
:internal
,
project:
project
,
content:
'password: XXX'
)
project_private_snippet
=
create
(
:snippet
,
:private
,
project:
project
,
content:
'password: XXX'
)
described_class
.
__elasticsearch__
.
refresh_index!
options
=
{
author_id:
user
.
id
}
result
=
described_class
.
elastic_search_code
(
'genius code'
,
options:
options
)
# returns only public snippets when user is blank
result
=
described_class
.
elastic_search_code
(
'password'
,
options:
{
user:
nil
})
expect
(
result
.
total_count
).
to
eq
(
2
)
expect
(
result
.
records
.
map
(
&
:id
)).
to
include
(
snippet
.
id
,
snippet3
.
id
)
expect
(
result
.
records
).
to
match_array
[
public_snippet
,
project_public_snippet
]
# returns only public, and internal snippets for regular users
regular_user
=
create
(
:user
)
result
=
described_class
.
elastic_search_code
(
'password'
,
options:
{
user:
regular_user
})
expect
(
result
.
total_count
).
to
eq
(
4
)
expect
(
result
.
records
).
to
match_array
[
public_snippet
,
internal_snippet
,
project_public_snippet
,
project_internal_snippet
]
# returns public, internal snippets and project private snippets for project members
member
=
create
(
:user
)
project
.
team
<<
[
member
,
:developer
]
result
=
described_class
.
elastic_search_code
(
'password'
,
options:
{
user:
member
})
expect
(
result
.
total_count
).
to
eq
(
5
)
expect
(
result
.
records
).
to
match_array
[
public_snippet
,
internal_snippet
,
project_public_snippet
,
project_internal_snippet
,
project_private_snippet
]
# returns private snippets where the user is the author
result
=
described_class
.
elastic_search_code
(
'password'
,
options:
{
user:
author
})
expect
(
result
.
total_count
).
to
eq
(
5
)
expect
(
result
.
records
).
to
match_array
[
public_snippet
,
internal_snippet
,
private_snippet
,
project_public_snippet
,
project_internal_snippet
]
# returns all snippets when for admins
admin
=
create
(
:admin
)
result
=
described_class
.
elastic_search_code
(
'password'
,
options:
{
user:
admin
})
expect
(
result
.
total_count
).
to
eq
(
6
)
expect
(
result
.
records
).
to
match_array
[
public_snippet
,
internal_snippet
,
private_snippet
,
project_public_snippet
,
project_internal_snippet
,
project_private_snippet
]
end
it
"searches snippets by title and file_name"
do
it
'searches snippets by title and file_name'
do
user
=
create
:user
create
:snippet
,
:public
,
title:
'home'
create
:snippet
,
:private
,
title:
'home 1'
create
:snippet
,
:public
,
file_name:
'index.php'
create
:snippet
create
(
:snippet
,
:public
,
title:
'home'
)
create
(
:snippet
,
:private
,
title:
'home 1'
)
create
(
:snippet
,
:public
,
file_name:
'index.php'
)
create
(
:snippet
)
described_class
.
__elasticsearch__
.
refresh_index!
options
=
{
author_id:
user
.
id
}
options
=
{
user:
user
}
expect
(
described_class
.
elastic_search
(
'home'
,
options:
options
).
total_count
).
to
eq
(
1
)
expect
(
described_class
.
elastic_search
(
'index.php'
,
options:
options
).
total_count
).
to
eq
(
1
)
end
it
"returns json with all needed elements"
do
snippet
=
create
:project_snippet
it
'returns json with all needed elements'
do
snippet
=
create
(
:project_snippet
)
expected_hash
=
snippet
.
attributes
.
extract!
(
expected_hash
=
snippet
.
attributes
.
extract!
(
'id'
,
'title'
,
'file_name'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment