BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
872e7b7e
Commit
872e7b7e
authored
May 15, 2017
by
George Andrinopoulos
Committed by
Rémy Coutable
May 15, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create a Users Finder
parent
f5e5afc7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
10 deletions
+141
-10
users_finder.rb
app/finders/users_finder.rb
+74
-0
users.rb
lib/api/users.rb
+1
-10
users_finder_spec.rb
spec/finders/users_finder_spec.rb
+66
-0
No files found.
app/finders/users_finder.rb
0 → 100644
View file @
872e7b7e
# UsersFinder
#
# Used to filter users by set of params
#
# Arguments:
# current_user - which user use
# params:
# username: string
# extern_uid: string
# provider: string
# search: string
# active: boolean
# blocked: boolean
# external: boolean
#
class
UsersFinder
attr_accessor
:current_user
,
:params
def
initialize
(
current_user
,
params
=
{})
@current_user
=
current_user
@params
=
params
end
def
execute
users
=
User
.
all
users
=
by_username
(
users
)
users
=
by_search
(
users
)
users
=
by_blocked
(
users
)
users
=
by_active
(
users
)
users
=
by_external_identity
(
users
)
users
=
by_external
(
users
)
users
end
private
def
by_username
(
users
)
return
users
unless
params
[
:username
]
users
.
where
(
username:
params
[
:username
])
end
def
by_search
(
users
)
return
users
unless
params
[
:search
].
present?
users
.
search
(
params
[
:search
])
end
def
by_blocked
(
users
)
return
users
unless
params
[
:blocked
]
users
.
blocked
end
def
by_active
(
users
)
return
users
unless
params
[
:active
]
users
.
active
end
def
by_external_identity
(
users
)
return
users
unless
current_user
.
admin?
&&
params
[
:extern_uid
]
&&
params
[
:provider
]
users
.
joins
(
:identities
).
merge
(
Identity
.
with_extern_uid
(
params
[
:provider
],
params
[
:extern_uid
]))
end
def
by_external
(
users
)
return
users
=
users
.
where
.
not
(
external:
true
)
unless
current_user
.
admin?
return
users
unless
params
[
:external
]
users
.
external
end
end
lib/api/users.rb
View file @
872e7b7e
...
...
@@ -56,16 +56,7 @@ module API
authenticated_as_admin!
if
params
[
:external
].
present?
||
(
params
[
:extern_uid
].
present?
&&
params
[
:provider
].
present?
)
users
=
User
.
all
users
=
User
.
where
(
username:
params
[
:username
])
if
params
[
:username
]
users
=
users
.
active
if
params
[
:active
]
users
=
users
.
search
(
params
[
:search
])
if
params
[
:search
].
present?
users
=
users
.
blocked
if
params
[
:blocked
]
if
current_user
.
admin?
users
=
users
.
joins
(
:identities
).
merge
(
Identity
.
with_extern_uid
(
params
[
:provider
],
params
[
:extern_uid
]))
if
params
[
:extern_uid
]
&&
params
[
:provider
]
users
=
users
.
external
if
params
[
:external
]
end
users
=
UsersFinder
.
new
(
current_user
,
params
).
execute
entity
=
current_user
.
admin?
?
Entities
::
UserPublic
:
Entities
::
UserBasic
present
paginate
(
users
),
with:
entity
...
...
spec/finders/users_finder_spec.rb
0 → 100644
View file @
872e7b7e
require
'spec_helper'
describe
UsersFinder
do
describe
'#execute'
do
let!
(
:user1
)
{
create
(
:user
,
username:
'johndoe'
)
}
let!
(
:user2
)
{
create
(
:user
,
:blocked
,
username:
'notsorandom'
,
)
}
let!
(
:external_user
)
{
create
(
:user
,
:external
)
}
let!
(
:omniauth_user
)
{
create
(
:omniauth_user
,
provider:
'twitter'
,
extern_uid:
'123456'
)
}
context
'with a normal user'
do
let
(
:user
)
{
create
(
:user
)
}
it
'returns all users'
do
users
=
described_class
.
new
(
user
).
execute
expect
(
users
).
to
contain_exactly
(
user
,
user1
,
user2
,
omniauth_user
)
end
it
'filters by username'
do
users
=
described_class
.
new
(
user
,
username:
'johndoe'
).
execute
expect
(
users
).
to
contain_exactly
(
user1
)
end
it
'filters by search'
do
users
=
described_class
.
new
(
user
,
search:
'orando'
).
execute
expect
(
users
).
to
contain_exactly
(
user2
)
end
it
'filters by blocked users'
do
users
=
described_class
.
new
(
user
,
blocked:
true
).
execute
expect
(
users
).
to
contain_exactly
(
user2
)
end
it
'filters by active users'
do
users
=
described_class
.
new
(
user
,
active:
true
).
execute
expect
(
users
).
to
contain_exactly
(
user
,
user1
,
omniauth_user
)
end
it
'returns no external users'
do
users
=
described_class
.
new
(
user
,
external:
true
).
execute
expect
(
users
).
to
contain_exactly
(
user
,
user1
,
user2
,
omniauth_user
)
end
end
context
'with an admin user'
do
let
(
:admin
)
{
create
(
:admin
)
}
it
'filters by external users'
do
users
=
described_class
.
new
(
admin
,
external:
true
).
execute
expect
(
users
).
to
contain_exactly
(
external_user
)
end
it
'returns all users'
do
users
=
described_class
.
new
(
admin
).
execute
expect
(
users
).
to
contain_exactly
(
admin
,
user1
,
user2
,
external_user
,
omniauth_user
)
end
end
end
end
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