Skip to content

Commit

Permalink
feature(rules): add user name and groups to rules context (#489)
Browse files Browse the repository at this point in the history
```json
   [...]
    "user": {
        "groups_domain": [],
        "groups_local": [
            "adm",
            "cdrom",
            "dip",
            "docker",
            "lpadmin",
            "lxd",
            "microk8s",
            "plugdev",
            "sambashare",
            "sudo",
            "vboxusers",
            "www-data"
        ],
        "name": "jmo"
    }
```
  • Loading branch information
Guts authored Apr 22, 2024
2 parents 80851c6 + 3097e80 commit ca95ad2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
7 changes: 6 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from qgis_deployment_toolbelt.utils.computer_environment import (
date_dict,
environment_dict,
user_dict,
)

# -- Build environment -----------------------------------------------------
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from qgis_deployment_toolbelt.utils.computer_environment import (
date_dict,
environment_dict,
user_dict,
)

# #############################################################################
Expand Down Expand Up @@ -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}")
Expand Down
26 changes: 23 additions & 3 deletions qgis_deployment_toolbelt/utils/computer_environment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! python3 # noqa: E265

"""
Base of QDT jobs.
Rules context.
Author: Julien Moura (https://github.com/guts)
"""
Expand All @@ -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 ###############
# ##################################
Expand Down Expand Up @@ -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(),
}

0 comments on commit ca95ad2

Please sign in to comment.