from prettytable import PrettyTable
import json
import logging
+import time
logger = logging.getLogger("osmclient")
"new_password",
help="user's new password to update in expiry condition",
)
+@click.option(
+ "--unlock",
+ is_flag=True,
+ help="unlock user",
+)
+@click.option(
+ "--renew",
+ is_flag=True,
+ help="renew user",
+)
@click.pass_context
def user_update(
ctx,
remove_project_role,
change_password,
new_password,
+ unlock,
+ renew,
):
"""Update a user information
REMOVE_PROJECT_ROLE: removing mappings for project/role(s)
CHANGE_PASSWORD: user's current password to change
NEW_PASSWORD: user's new password to update in expiry condition
+ UNLOCK: unlock user
+ RENEW: renew user
"""
logger.debug("")
user = {}
user["remove-project-role"] = remove_project_role
user["change_password"] = change_password
user["new_password"] = new_password
+ user["unlock"] = unlock
+ user["renew"] = renew
utils.check_client_version(ctx.obj, ctx.command.name)
ctx.obj.user.update(username, user)
utils.check_client_version(ctx.obj, ctx.command.name)
if filter:
filter = "&".join(filter)
- resp = ctx.obj.user.list(filter)
- table = PrettyTable(["name", "id"])
+ resp, admin_show = ctx.obj.user.list(filter)
for user in resp:
- table.add_row([user["username"], user["_id"]])
+ if user["username"] == "admin":
+ user["_admin"]["account_expire_time"] = "N/A"
+ if admin_show:
+ table = PrettyTable(["name", "id", "user_status", "expires_in"])
+ for user in resp:
+ table.add_row(
+ [
+ user["username"],
+ user["_id"],
+ user["_admin"]["user_status"].upper(),
+ time.strftime(
+ "%b-%d-%Y %X",
+ time.gmtime(user["_admin"]["account_expire_time"]),
+ )
+ if not user["username"] == "admin"
+ else user["_admin"]["account_expire_time"],
+ ]
+ )
+ else:
+ table = PrettyTable(["name", "id"])
+ for user in resp:
+ table.add_row([user["username"], user["_id"]])
table.align = "l"
print(table)
for (key, val) in list(self._headers.items())
]
self._http_client.set_http_header(http_header)
+ return token
def get_version(self):
_, resp = self._http_client.get2_cmd(endpoint="/version", skip_query_admin=True)
"""Updates an existing OSM user identified by name"""
self._logger.debug("")
if pwd_change:
- self._client.get_token(pwd_change)
+ token_info = self._client.get_token(pwd_change)
else:
- self._client.get_token()
+ token_info = self._client.get_token()
# print(user)
myuser = self.get(name)
update_user = {
mapping = {"project": project, "role": role}
update_user["remove_project_role_mappings"].append(mapping)
+ if user.get("unlock"):
+ if token_info.get("admin_show"):
+ update_user["unlock"] = user["unlock"]
+ update_user["system_admin_id"] = token_info.get("user_id")
+ else:
+ raise ClientException(
+ "{} does not have privilege to unlock {}".format(
+ token_info.get("username"), myuser.get("username")
+ )
+ )
+
+ if user.get("renew"):
+ if token_info.get("admin_show"):
+ update_user["renew"] = user["renew"]
+ update_user["system_admin_id"] = token_info.get("user_id")
+ else:
+ raise ClientException(
+ "{} does not have privilege to renew {}".format(
+ token_info.get("username"), myuser.get("username")
+ )
+ )
+
if not update_user["remove_project_role_mappings"]:
del update_user["remove_project_role_mappings"]
if not update_user["add_project_role_mappings"]:
def list(self, filter=None):
"""Returns the list of OSM users"""
self._logger.debug("")
- self._client.get_token()
+ response = self._client.get_token()
+ admin_show = None
+ if response:
+ admin_show = response.get("admin_show")
+
filter_string = ""
if filter:
filter_string = "?{}".format(filter)
"{}{}".format(self._apiBase, filter_string), skip_query_admin=True
)
# print('RESP: {}'.format(resp))
- if resp:
+ if resp and response:
+ return json.loads(resp), admin_show
+ elif resp:
return json.loads(resp)
return list()