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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
c50fc090
Commit
c50fc090
authored
Sep 26, 2018
by
Mark Chao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Project#members_among to obtain only authorized users of the project
parent
6e6aa12e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
0 deletions
+55
-0
app/models/project.rb
app/models/project.rb
+12
-0
spec/models/project_spec.rb
spec/models/project_spec.rb
+43
-0
No files found.
app/models/project.rb
View file @
c50fc090
...
...
@@ -1365,6 +1365,18 @@ class Project < ActiveRecord::Base
end
end
# Filters `users` to return only authorized users of the project
def
members_among
(
users
)
if
users
.
is_a?
(
ActiveRecord
::
Relation
)
&&
!
users
.
loaded?
authorized_users
.
merge
(
users
)
else
return
[]
if
users
.
empty?
user_ids
=
authorized_users
.
where
(
users:
{
id:
users
.
map
(
&
:id
)
}).
pluck
(
:id
)
users
.
select
{
|
user
|
user_ids
.
include?
(
user
.
id
)
}
end
end
def
default_branch
@default_branch
||=
repository
.
root_ref
if
repository
.
exists?
end
...
...
spec/models/project_spec.rb
View file @
c50fc090
...
...
@@ -3995,6 +3995,49 @@ describe Project do
end
end
context
'#members_among'
do
let
(
:users
)
{
create_list
(
:user
,
3
)
}
set
(
:group
)
{
create
(
:group
)
}
set
(
:project
)
{
create
(
:project
,
namespace:
group
)
}
before
do
project
.
add_guest
(
users
.
first
)
project
.
group
.
add_maintainer
(
users
.
last
)
end
context
'when users is an Array'
do
it
'returns project members among the users'
do
expect
(
project
.
members_among
(
users
)).
to
eq
([
users
.
first
,
users
.
last
])
end
it
'maintains input order'
do
expect
(
project
.
members_among
(
users
.
reverse
)).
to
eq
([
users
.
last
,
users
.
first
])
end
it
'returns empty array if users is empty'
do
result
=
project
.
members_among
([])
expect
(
result
).
to
be_empty
end
end
context
'when users is a relation'
do
it
'returns project members among the users'
do
result
=
project
.
members_among
(
User
.
where
(
id:
users
.
map
(
&
:id
)))
expect
(
result
).
to
be_a
(
ActiveRecord
::
Relation
)
expect
(
result
).
to
eq
([
users
.
first
,
users
.
last
])
end
it
'returns empty relation if users is empty'
do
result
=
project
.
members_among
(
User
.
none
)
expect
(
result
).
to
be_a
(
ActiveRecord
::
Relation
)
expect
(
result
).
to
be_empty
end
end
end
def
rugged_config
Gitlab
::
GitalyClient
::
StorageSettings
.
allow_disk_access
do
project
.
repository
.
rugged
.
config
...
...
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