Add: auth cmd prototype, dummy_platform for api

This commit is contained in:
2024-06-05 00:38:17 +09:00
parent bb3b20e6c8
commit 72bfeb9193
8 changed files with 256 additions and 164 deletions

View File

@@ -1,9 +1,11 @@
import click
from mgrctl.db.vm6.databases import isp_database
from mgrctl.db.vm6.models import AuthUser
from mgrctl.apps.vm6.auth import __version__
from mgrctl.api.vm6 import AuthAPI
from mgrctl.utils.api_users import UserAPI
user_cursor = UserAPI(callback_class=AuthAPI)
@click.group(help='auth cmd for auth in VMmanager 6')
@@ -16,16 +18,79 @@ def cli():
pass
@cli.command(help='show all users and their roles on platform (DEMO EXAMPLE)')
def users():
# check and init connection to db:
isp_database.connect()
# get all fields from auth_user table
# SELECT * FROM auth_user;
all_users = AuthUser.select()
# Iterate fields and print to console users' email and role
for user in all_users:
result = f'{user.email} | {user.roles[0]}'
click.echo(result)
# Close connection
isp_database.close()
@cli.group(help='Manage users')
def user():
pass
@user.command(help='List users')
@click.option(
'--all',
is_flag=True,
required=False,
help='Show all users'
)
@click.option(
'--admins',
is_flag=True,
required=False,
help='Show all active admins',
)
def ls(all, admins):
if all:
user_cursor.echo_users(role='all')
elif admins:
user_cursor.echo_users(role='admin')
else:
user_cursor.echo_users(role='all')
@user.command(help='Generate access key and return auth link(s)')
@click.option(
'--id',
required=False,
type=int,
help='User id'
)
@click.option(
'--count',
required=False,
type=int,
help='Number of access keys generated',
)
@click.option(
'--random',
is_flag=True,
required=False,
help='Interactive mode, ignores other keys'
)
@click.option(
'--interactive',
is_flag=True,
required=False,
help='Interactive mode, ignores other keys'
)
def access(id, count, interactive, random):
if id and not count:
keys = user_cursor.get_access_keys(user=id, count=1)
links = user_cursor.gen_access_links(keys)
user_cursor.echo_access_links(links)
elif id and count:
keys = user_cursor.get_access_keys(user=id, count=count)
links = user_cursor.gen_access_links(keys)
user_cursor.echo_access_links(links)
elif interactive:
pass
elif random:
admin = user_cursor.get_first_random_admin()
keys = user_cursor.get_access_keys(user=admin.get('id', 3), count=1)
links = user_cursor.gen_access_links(keys)
user_cursor.echo_access_links(links)
else:
pass
@user.command(help='Generate API token for mgrctl user')
def token():
token = user_cursor.gen_api_token()
user_cursor.echo_api_token(token)