Skip to content

Commit 7a2693b

Browse files
committed
support Grid(search_queries=None)
1 parent 5efe9d6 commit 7a2693b

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

apps/_scaffold/settings.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,16 @@
104104
"mode": "ad", # Microsoft Active Directory
105105
"server": "mydc.domain.com", # FQDN or IP of one Domain Controller
106106
"base_dn": "cn=Users,dc=domain,dc=com", # base dn, i.e. where the users are located
107-
108107
# the following are only needed if you want to use groups
109108
"group_member_attrib": "member", # for AD, attribute that contains the user DN in the group
110109
"bind_dn": "CN=LdapBindUser,CN=users,DC=test,DC=local", # bind user DN
111110
"bind_pw": "P@ssw0rd", # bind user password
112111
"group_dn": "DC=test,DC=local", # group DN, where the groups are located, default = base_dn
113-
"allowed_groups": ["allowed_login_group"], # list of groups that are allowed to log in, default = everyone
112+
"allowed_groups": [
113+
"allowed_login_group"
114+
], # list of groups that are allowed to log in, default = everyone
114115
"denied_login_popup": True, # show an error at login if not in allowed groups
115-
# default = False
116+
# default = False
116117
}
117118

118119
# i18n settings

py4web/utils/auth.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,9 @@ def login(self, email, password):
708708
and getattr(plugin, "mode", None) == "ad"
709709
and getattr(plugin, "last_user_mail", None)
710710
):
711-
self.logger.debug(f"Using AD email from LDAP plugin: {plugin.last_user_mail}")
711+
self.logger.debug(
712+
f"Using AD email from LDAP plugin: {plugin.last_user_mail}"
713+
)
712714
user_info["email"] = plugin.last_user_mail
713715
elif "@" in email:
714716
self.logger.debug(f"Using email from login: {email}")
@@ -1331,11 +1333,15 @@ def login(auth):
13311333
and getattr(plugin, "mode", None) == "ad"
13321334
and getattr(plugin, "last_user_mail", None)
13331335
):
1334-
auth.logger.debug(f"AuthAPI.login: Using AD email from LDAP plugin: {plugin.last_user_mail}")
1336+
auth.logger.debug(
1337+
f"AuthAPI.login: Using AD email from LDAP plugin: {plugin.last_user_mail}"
1338+
)
13351339
# save the real email from AD to database
13361340
data["email"] = plugin.last_user_mail
13371341
else:
1338-
auth.logger.debug(f"AuthAPI.login: Not using AD email, plugin_name={plugin_name}, mode={getattr(plugin, 'mode', None)}, last_user_mail={getattr(plugin, 'last_user_mail', None)}")
1342+
auth.logger.debug(
1343+
f"AuthAPI.login: Not using AD email, plugin_name={plugin_name}, mode={getattr(plugin, 'mode', None)}, last_user_mail={getattr(plugin, 'last_user_mail', None)}"
1344+
)
13391345
if auth.db:
13401346
auth.logger.debug(
13411347
f"AuthAPI.login: Calling get_or_register_user with data={data}"

py4web/utils/auth_plugins/ldap_plugin.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ def check_credentials(self, username, password):
271271
try:
272272
allowed_groups = self.allowed_groups
273273
if allowed_groups and mode == "ad":
274-
if not self.is_user_in_allowed_groups(
275-
username
276-
):
277-
logger.warning(f"[{username}] refused login because not in allowed groups!")
278-
274+
if not self.is_user_in_allowed_groups(username):
275+
logger.warning(
276+
f"[{username}] refused login because not in allowed groups!"
277+
)
278+
279279
return False
280280
con = self._init_ldap()
281281
if mode == "ad":
@@ -314,12 +314,18 @@ def check_credentials(self, username, password):
314314
)[0][1]
315315

316316
# set last_user_mail from ldap result for further use
317-
if 'mail' in result and result['mail']:
318-
self.last_user_mail = str(result['mail'][0].decode() if isinstance(result['mail'][0], bytes) else result['mail'][0])
317+
if "mail" in result and result["mail"]:
318+
self.last_user_mail = str(
319+
result["mail"][0].decode()
320+
if isinstance(result["mail"][0], bytes)
321+
else result["mail"][0]
322+
)
319323
else:
320324
self.last_user_mail = None
321325
logger.info(f"Login result: {result}")
322-
logger.debug(f"LDAPPlugin: Set last_user_mail to {self.last_user_mail} for {username}")
326+
logger.debug(
327+
f"LDAPPlugin: Set last_user_mail to {self.last_user_mail} for {username}"
328+
)
323329
if not isinstance(result, dict):
324330
# result should be a dict in the form
325331
# {'sAMAccountName': [username_bare]}
@@ -564,7 +570,9 @@ def is_user_in_allowed_groups(self, username, password=None):
564570
for group in allowed_groups:
565571
if group in self.groups:
566572
# Match
567-
logger.debug(f"[{str(username)}] allowed login because in allowed groups")
573+
logger.debug(
574+
f"[{str(username)}] allowed login because in allowed groups"
575+
)
568576
return True
569577
# No match
570578
logger.warning(f"[{str(username)}] denied login because not in allowed groups")

py4web/utils/grid.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def __init__(
444444
self,
445445
query,
446446
search_form=None,
447-
search_queries=None,
447+
search_queries="auto",
448448
columns=None,
449449
field_id=None,
450450
show_id=None,
@@ -478,7 +478,7 @@ def __init__(
478478
:param path: The part of the URL to be parsed by this grid
479479
:param query: the query used to filter the data
480480
:param search_form: py4web FORM to be included as the search form
481-
:param search_queries: future use - pass a dict of name and a search query
481+
:param search_queries: future use - pass a list of search quereis, or "auto" or None
482482
:param columns: list of columns to display on the list page, if blank, glean tablename from first query
483483
: and use all fields of that table
484484
:param field_id: the id field of the primary table for the grid - if there are multiple tables (joined)
@@ -723,7 +723,7 @@ def _handle_mode_select(self):
723723
query = self.query
724724

725725
# use the default search for the table (we have to do this here because need self.table)
726-
if self.param.search_queries is None:
726+
if self.param.search_queries == "auto":
727727
self.param.search_queries = [make_default_search_query(self.table)]
728728

729729
# apply the search query
@@ -1271,6 +1271,7 @@ def _make_table(self):
12711271
kind="grid-new-button",
12721272
)
12731273
)
1274+
12741275
if self.param.header_elements and len(self.param.header_elements) > 0:
12751276
for element in self.param.header_elements:
12761277
element = maybe_call(element)

0 commit comments

Comments
 (0)