diff --git a/docs/conf.py b/docs/conf.py index 7faa6dc3..466ba366 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,6 +20,7 @@ from qgis_deployment_toolbelt.utils.computer_environment import ( date_dict, environment_dict, + user_dict, ) # -- Build environment ----------------------------------------------------- @@ -220,7 +221,11 @@ def generate_rules_context(_): """Generate context object as JSON that it passed to rules engine to check profiles conditions.""" - context_object = {"date": date_dict(), "environment": environment_dict()} + context_object = { + "date": date_dict(), + "environment": environment_dict(), + "user": user_dict(), + } with Path("./docs/reference/rules_context.json").open( mode="w", encoding="utf-8" ) as out_json: diff --git a/qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py b/qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py index b71ea49b..4e5310bd 100644 --- a/qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py +++ b/qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py @@ -27,6 +27,7 @@ from qgis_deployment_toolbelt.utils.computer_environment import ( date_dict, environment_dict, + user_dict, ) # ############################################################################# @@ -132,7 +133,11 @@ def filter_profiles_on_rules( li_profiles_matched = [] li_profiles_unmatched = [] - context_object = {"date": date_dict(), "environment": environment_dict()} + context_object = { + "date": date_dict(), + "environment": environment_dict(), + "user": user_dict(), + } for profile in li_downloaded_profiles: if profile.rules is None: logger.debug(f"No rules to apply to {profile.name}") diff --git a/qgis_deployment_toolbelt/utils/computer_environment.py b/qgis_deployment_toolbelt/utils/computer_environment.py index a0921c46..4a770789 100644 --- a/qgis_deployment_toolbelt/utils/computer_environment.py +++ b/qgis_deployment_toolbelt/utils/computer_environment.py @@ -1,7 +1,7 @@ #! python3 # noqa: E265 """ - Base of QDT jobs. + Rules context. Author: Julien Moura (https://github.com/guts) """ @@ -11,13 +11,19 @@ # ########## Libraries ############# # ################################## +# Standard library import logging import platform - -# Standard library from datetime import date +from getpass import getuser from sys import platform as opersys +# package +from qgis_deployment_toolbelt.utils.user_groups import ( + get_user_domain_groups, + get_user_local_groups, +) + # ############################################################################# # ########## Globals ############### # ################################## @@ -74,3 +80,17 @@ def environment_dict() -> dict: # custom Windows "windows_edition": platform.win32_edition(), } + + +def user_dict() -> dict: + """Returns a dictionary containing user informations that can be used in QDT Rules + context. + + Returns: + dict: dict user information. + """ + return { + "name": getuser(), + "groups_local": get_user_local_groups(), + "groups_domain": get_user_domain_groups(), + }