|
3 | 3 | from tempfile import NamedTemporaryFile
|
4 | 4 | from typing import Any, Sequence, IO
|
5 | 5 |
|
| 6 | +from compiler_admin import RESULT_SUCCESS |
| 7 | + |
6 | 8 | # import and alias CallGAMCommand so we can simplify usage in this app
|
7 | 9 | from gam import CallGAMCommand as __CallGAMCommand, initializeLogging
|
8 | 10 |
|
@@ -96,9 +98,38 @@ def user_exists(username: str) -> bool:
|
96 | 98 | print(f"User not in domain: {username}")
|
97 | 99 | return False
|
98 | 100 |
|
99 |
| - res = CallGAMCommand(("info", "user", username, "quick")) |
| 101 | + info = user_info(username) |
| 102 | + |
| 103 | + return info != {} |
| 104 | + |
100 | 105 |
|
101 |
| - return res == 0 |
| 106 | +def user_info(username: str) -> dict: |
| 107 | + """Get a dict of basic user information. |
| 108 | +
|
| 109 | + Args: |
| 110 | + username (str): The [email protected] to get. |
| 111 | + Returns: |
| 112 | + A dict of user information |
| 113 | + """ |
| 114 | + if not str(username).endswith(DOMAIN): |
| 115 | + print(f"User not in domain: {username}") |
| 116 | + return {} |
| 117 | + |
| 118 | + with NamedTemporaryFile("w+") as stdout: |
| 119 | + res = CallGAMCommand(("info", "user", username, "quick"), stdout=stdout.name) |
| 120 | + if res != RESULT_SUCCESS: |
| 121 | + # user doesn't exist |
| 122 | + return {} |
| 123 | + # user exists, read data |
| 124 | + lines = stdout.readlines() |
| 125 | + # split on newline and filter out lines that aren't line "Key:Value" and empty value lines like "Key:<empty>" |
| 126 | + lines = [L.strip() for L in lines if len(L.split(":")) == 2 and L.split(":")[1].strip()] |
| 127 | + # make a map by splitting the lines, trimming key and value |
| 128 | + info = {} |
| 129 | + for line in lines: |
| 130 | + k, v = line.split(":") |
| 131 | + info[k.strip()] = v.strip() |
| 132 | + return info |
102 | 133 |
|
103 | 134 |
|
104 | 135 | def user_in_group(username: str, group: str) -> bool:
|
|
0 commit comments