Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
9ee9be33
Commit
9ee9be33
authored
Oct 09, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/bugfix' into bugfix
parents
ce530e41
c8cade62
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
13 deletions
+32
-13
api.py
apps/settings/api.py
+4
-3
utils.py
apps/settings/utils.py
+28
-10
No files found.
apps/settings/api.py
View file @
9ee9be33
...
@@ -101,10 +101,11 @@ class LDAPUserListApi(generics.ListAPIView):
...
@@ -101,10 +101,11 @@ class LDAPUserListApi(generics.ListAPIView):
def
get_queryset
(
self
):
def
get_queryset
(
self
):
if
hasattr
(
self
,
'swagger_fake_view'
):
if
hasattr
(
self
,
'swagger_fake_view'
):
return
[]
return
[]
util
=
LDAPUtil
()
q
=
self
.
request
.
query_params
.
get
(
'search'
)
try
:
try
:
users
=
util
.
search_user_items
()
util
=
LDAPUtil
()
extra_filter
=
util
.
construct_extra_filter
(
util
.
SEARCH_FIELD_ALL
,
q
)
users
=
util
.
search_user_items
(
extra_filter
)
except
Exception
as
e
:
except
Exception
as
e
:
users
=
[]
users
=
[]
logger
.
error
(
e
)
logger
.
error
(
e
)
...
...
apps/settings/utils.py
View file @
9ee9be33
...
@@ -22,6 +22,9 @@ class LDAPOUGroupException(Exception):
...
@@ -22,6 +22,9 @@ class LDAPOUGroupException(Exception):
class
LDAPUtil
:
class
LDAPUtil
:
_conn
=
None
_conn
=
None
SEARCH_FIELD_ALL
=
'all'
SEARCH_FIELD_USERNAME
=
'username'
def
__init__
(
self
,
use_settings_config
=
True
,
server_uri
=
None
,
bind_dn
=
None
,
def
__init__
(
self
,
use_settings_config
=
True
,
server_uri
=
None
,
bind_dn
=
None
,
password
=
None
,
use_ssl
=
None
,
search_ougroup
=
None
,
password
=
None
,
use_ssl
=
None
,
search_ougroup
=
None
,
search_filter
=
None
,
attr_map
=
None
,
auth_ldap
=
None
):
search_filter
=
None
,
attr_map
=
None
,
auth_ldap
=
None
):
...
@@ -84,7 +87,8 @@ class LDAPUtil:
...
@@ -84,7 +87,8 @@ class LDAPUtil:
def
_search_user_items_ou
(
self
,
search_ou
,
extra_filter
=
None
,
cookie
=
None
):
def
_search_user_items_ou
(
self
,
search_ou
,
extra_filter
=
None
,
cookie
=
None
):
search_filter
=
self
.
search_filter
%
{
"user"
:
"*"
}
search_filter
=
self
.
search_filter
%
{
"user"
:
"*"
}
if
extra_filter
:
if
extra_filter
:
search_filter
=
'(&({})({}))'
.
format
(
search_filter
,
extra_filter
)
search_filter
=
'(&{}{})'
.
format
(
search_filter
,
extra_filter
)
ok
=
self
.
connection
.
search
(
ok
=
self
.
connection
.
search
(
search_ou
,
search_filter
,
search_ou
,
search_filter
,
attributes
=
list
(
self
.
attr_map
.
values
()),
attributes
=
list
(
self
.
attr_map
.
values
()),
...
@@ -111,14 +115,10 @@ class LDAPUtil:
...
@@ -111,14 +115,10 @@ class LDAPUtil:
cookie
=
self
.
connection
.
result
[
'controls'
][
'1.2.840.113556.1.4.319'
][
'value'
][
'cookie'
]
cookie
=
self
.
connection
.
result
[
'controls'
][
'1.2.840.113556.1.4.319'
][
'value'
][
'cookie'
]
return
cookie
return
cookie
def
search_user_items
(
self
,
q
=
None
):
def
search_user_items
(
self
,
extra_filter
=
None
):
user_items
=
[]
user_items
=
[]
logger
.
info
(
"Search user items"
)
logger
.
info
(
"Search user items"
)
extra_filter
=
''
if
q
:
for
attr
in
self
.
attr_map
.
values
():
extra_filter
+=
'({}={})'
.
format
(
attr
,
q
)
extra_filter
=
'(|{})'
.
format
(
extra_filter
)
for
search_ou
in
str
(
self
.
search_ougroup
)
.
split
(
"|"
):
for
search_ou
in
str
(
self
.
search_ougroup
)
.
split
(
"|"
):
logger
.
info
(
"Search user search ou: {}"
.
format
(
search_ou
))
logger
.
info
(
"Search user search ou: {}"
.
format
(
search_ou
))
_user_items
=
self
.
_search_user_items_ou
(
search_ou
,
extra_filter
=
extra_filter
)
_user_items
=
self
.
_search_user_items_ou
(
search_ou
,
extra_filter
=
extra_filter
)
...
@@ -130,10 +130,28 @@ class LDAPUtil:
...
@@ -130,10 +130,28 @@ class LDAPUtil:
logger
.
info
(
"Search user items end"
)
logger
.
info
(
"Search user items end"
)
return
user_items
return
user_items
def
construct_extra_filter
(
self
,
field
,
q
):
if
not
q
:
return
None
extra_filter
=
''
if
field
==
self
.
SEARCH_FIELD_ALL
:
for
attr
in
self
.
attr_map
.
values
():
extra_filter
+=
'({}={})'
.
format
(
attr
,
q
)
extra_filter
=
'(|{})'
.
format
(
extra_filter
)
return
extra_filter
if
field
==
self
.
SEARCH_FIELD_USERNAME
and
isinstance
(
q
,
list
):
attr
=
self
.
attr_map
.
get
(
'username'
)
for
username
in
q
:
extra_filter
+=
'({}={})'
.
format
(
attr
,
username
)
extra_filter
=
'(|{})'
.
format
(
extra_filter
)
return
extra_filter
def
search_filter_user_items
(
self
,
username_list
):
def
search_filter_user_items
(
self
,
username_list
):
user_items
=
self
.
search_user_items
()
extra_filter
=
self
.
construct_extra_filter
(
if
username_list
:
self
.
SEARCH_FIELD_USERNAME
,
username_list
user_items
=
[
u
for
u
in
user_items
if
u
[
'username'
]
in
username_list
]
)
user_items
=
self
.
search_user_items
(
extra_filter
)
return
user_items
return
user_items
@staticmethod
@staticmethod
...
...
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