-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuser_data.py
275 lines (247 loc) · 10.7 KB
/
user_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import argparse
import io
import logging
from enum import IntEnum
from typing import List, Callable, Optional
import tableauserverclient as TSC
from tabcmd.commands.constants import Errors
from tabcmd.commands.server import Server
from tabcmd.execution.localize import _
class Userdata:
def __init__(self):
self.name = None
self.password = None
self.fullname = None
self.license_level = None
self.admin_level = None
self.publisher = None
self.email = None
self.auth = None
def populate(self, values: List[str]) -> None:
n_values = len(values)
self.name = values[0]
if n_values >= 2:
self.password = values[1]
if n_values >= 3:
self.fullname = values[2]
if n_values >= 4:
self.license_level = values[3]
if n_values >= 5:
self.admin_level = values[4]
if n_values >= 6:
self.publisher = values[5]
if n_values >= 7:
self.email = values[6]
if n_values >= 8:
self.auth = values[7]
def to_tsc_user(self) -> TSC.UserItem:
site_role = UserCommand.evaluate_site_role(self.license_level, self.admin_level, self.publisher)
if not site_role:
raise AttributeError("Site role is required")
user = TSC.UserItem(self.name, site_role, self.auth)
user.email = self.email
user.fullname = self.fullname
return user
CHOICES: List[List[str]] = [
[],
[],
[],
["creator", "explorer", "viewer", "unlicensed"], # license
["system", "site", "none", "no"], # admin
["yes", "true", "1", "no", "false", "0"], # publisher
[],
[TSC.UserItem.Auth.SAML, TSC.UserItem.Auth.OpenID, TSC.UserItem.Auth.ServerDefault], # auth
]
# username, password, display_name, license, admin_level, publishing, email, auth type
class Column(IntEnum):
USERNAME = 0
PASS = 1
DISPLAY_NAME = 2
LICENSE = 3 # aka site role
ADMIN = 4
PUBLISHER = 5
EMAIL = 6
AUTH = 7
MAX = 7
class UserCommand(Server):
"""
This class acts as a base class for user related group of commands
"""
# read the file containing usernames or user details and validate each line
# log out any errors encountered
# returns the number of valid lines in the file
# @param boolean strict: if true, die if any errors are found
@staticmethod
def validate_file_for_import(csv_file: io.TextIOWrapper, logger, detailed=False, strict=False) -> int:
num_errors = 0
num_valid_lines = 0
csv_file.seek(0) # set to start of file in case it has been read earlier
line: str = csv_file.readline()
while line and line != "":
try:
printable_line = line
if detailed:
# do not print passwords
printable_line = line.split(",")[0]
UserCommand._validate_user_or_throw(line, logger)
else:
logger.debug("> username - {}".format(line))
UserCommand._validate_username_or_throw(line)
num_valid_lines += 1
except Exception as exc:
logger.info(_("importcsvsummary.error.line").format(printable_line, exc, ""))
num_errors += 1
line = csv_file.readline()
if strict and num_errors > 0:
Errors.exit_with_error(logger, _("importcsvsummary.error.too_many_errors"))
return num_valid_lines
# valid: username, domain/username, username@domain, domain/username@email
@staticmethod
def _validate_username_or_throw(username) -> None:
if username is None or username == "" or username.strip(" ") == "":
raise AttributeError(_("user.input.name.err.empty"))
if username.find(" ") >= 0:
raise AttributeError(_("tabcmd.report.error.user.no_spaces_in_username"))
at_symbol = username.find("@")
if at_symbol >= 0:
username = username[:at_symbol] + "X" + username[at_symbol + 1 :]
if username.find("@") >= 0:
raise AttributeError(_("tabcmd.report.error.user_csv.at_char"))
@staticmethod
def _validate_user_or_throw(incoming, logger) -> None:
line = list(map(str.strip, incoming.split(",")))
logger.debug("> details - {}".format(line[0]))
if len(line) > Column.MAX:
raise AttributeError(_("tabcmd.report.error.user_csv.too_many_columns").format(len(line), Column.MAX))
username = line[Column.USERNAME.value]
UserCommand._validate_username_or_throw(username)
for i in range(1, len(line)):
logger.debug("column {}: {}".format(Column(i).name, line[i]))
UserCommand._validate_item(line[i], CHOICES[i], Column(i))
@staticmethod
def _validate_item(item: str, possible_values: List[str], column_type) -> None:
if item is None or item == "":
# value can be empty for any column except user, which is checked elsewhere
return
if item in possible_values or possible_values == []:
return
raise AttributeError(_("tabcmd.report.error.generic_attribute").format(column_type, item))
@staticmethod
def get_users_from_file(csv_file: io.TextIOWrapper, logger=None) -> List[TSC.UserItem]:
csv_file.seek(0) # set to start of file in case it has been read earlier
if logger:
logger.debug("Reading from file {}".format(csv_file.name))
user_list = []
line = csv_file.readline()
if logger:
logger.debug("> {}".format(line))
while line:
user: Optional[TSC.UserItem] = UserCommand._parse_line(line)
if user:
user_list.append(user)
line = csv_file.readline()
return user_list
@staticmethod
def _parse_line(line: str) -> Optional[TSC.UserItem]:
if line is None or line is False or line == "\n" or line == "":
return None
line = line.strip().lower()
line_parts: List[str] = line.split(",")
data = Userdata()
values: List[str] = list(map(str.strip, line_parts))
data.populate(values)
return data.to_tsc_user()
# https://help.tableau.com/current/server/en-us/csvguidelines.htm#settings_and_site_roles
@staticmethod
def evaluate_site_role(license_level, admin_level, publisher):
if not license_level or not admin_level or not publisher:
return "Unlicensed"
# ignore case everywhere
license_level = license_level.lower()
admin_level = admin_level.lower()
publisher = publisher.lower()
# don't need to check publisher for system/site admin
if admin_level == "system":
site_role = "SiteAdministrator"
elif admin_level == "site":
if license_level == "creator":
site_role = "SiteAdministratorCreator"
elif license_level == "explorer":
site_role = "SiteAdministratorExplorer"
else:
site_role = "SiteAdministratorExplorer"
else: # if it wasn't 'system' or 'site' then we can treat it as 'none'
if publisher == "yes":
if license_level == "creator":
site_role = "Creator"
elif license_level == "explorer":
site_role = "ExplorerCanPublish"
else:
site_role = "Unlicensed" # is this the expected outcome?
else: # publisher == 'no':
if license_level == "explorer" or license_level == "creator":
site_role = "Explorer"
elif license_level == "viewer":
site_role = "Viewer"
else: # if license_level == 'unlicensed'
site_role = "Unlicensed"
if site_role is None:
site_role = "Unlicensed"
return site_role
@staticmethod
def act_on_users(
logger: logging.Logger, server: object, action_name: str, server_method: Callable, args: argparse.Namespace
) -> None:
group = None
try:
group = UserCommand.find_group(logger, server, args.name)
except TSC.ServerResponseError as e:
Errors.exit_with_error(
logger, _("errors.reportable.impersonation.group_not_found").format(args.name), exception=e
)
n_users_handled: int = 0
number_of_errors: int = 0
n_users_listed: int = UserCommand.validate_file_for_import(args.users, logger, strict=args.require_all_valid)
logger.debug(_("importcsvsummary.line.processed").format(n_users_listed))
error_list = []
line_no = 0
user_obj_list: List[TSC.UserItem] = UserCommand.get_users_from_file(args.users)
logger.debug(_("tabcmd.result.success.parsed_users").format(len(user_obj_list)))
for user_obj in user_obj_list:
line_no += 1
if not user_obj.name:
number_of_errors += 1
error_list.append(_("importcsvsummary.error.line").format(line_no, "No username", ""))
continue
try:
username: str = user_obj.name
user_id: str = UserCommand.find_user(logger, server, username).id
logger.debug("{} user {} ({})".format(action_name, username, user_id))
except TSC.ServerResponseError as e:
number_of_errors += 1
error_list.append(
_("importcsvsummary.error.line").format(line_no, username, "{}: {}".format(e.code, e.detail))
)
logger.debug(_("tabcmd.result.failure.user").format(username))
continue
try:
server_method(group, user_id)
n_users_handled += 1
logger.info(_("tabcmd.result.success.user_actions").format(action_name, username, group))
except TSC.ServerResponseError as e:
number_of_errors += 1
error_list.append(
_("importcsvsummary.error.line").format(line_no, username, "{}: {}".format(e.code, e.detail))
)
logger.info(_("session.monitorjob.percent_complete").format(100))
logger.info(_("importcsvsummary.errors.count").format(number_of_errors))
if number_of_errors > 0:
i = 0
max_printing = 5
logger.info(_("importcsvsummary.error.details"))
while i < number_of_errors and i < max_printing:
logger.info(error_list[i])
i += 1
if number_of_errors > max_printing:
logger.info(_("importcsvsummary.error.too_many_errors"))
logger.info(_("importcsvsummary.remainingerrors"))