mirror of
				https://git.isptech.ru/ISPsystem/isp-maintenance.git
				synced 2025-10-30 07:03:02 +01:00 
			
		
		
		
	Update: access funcs
This commit is contained in:
		| @@ -38,16 +38,22 @@ def user(): | |||||||
| ) | ) | ||||||
| def ls(all, admins): | def ls(all, admins): | ||||||
|     if all: |     if all: | ||||||
|         user_cursor.echo_users(role='all') |         users = user_cursor.get_users(role='all') | ||||||
|     elif admins: |     elif admins: | ||||||
|         user_cursor.echo_users(role='admin') |         users = user_cursor.get_users(role='admin') | ||||||
|     else: |     else: | ||||||
|         user_cursor.echo_users(role='all') |         users = user_cursor.get_users(role='all') | ||||||
|  |     # print users: | ||||||
|  |     user_cursor.echo_users(users) | ||||||
|  |  | ||||||
|  |  | ||||||
| @user.command(help='Generate access key and return auth link(s)') | @user.command( | ||||||
|  |     help='Generate an access key and return auth link(s)', | ||||||
|  |     no_args_is_help=True | ||||||
|  | ) | ||||||
| @click.option( | @click.option( | ||||||
|     '--id', |     '--id', | ||||||
|  |     '_id', | ||||||
|     required=False, |     required=False, | ||||||
|     type=int, |     type=int, | ||||||
|     help='User id' |     help='User id' | ||||||
| @@ -62,7 +68,7 @@ def ls(all, admins): | |||||||
|     '--random', |     '--random', | ||||||
|     is_flag=True, |     is_flag=True, | ||||||
|     required=False, |     required=False, | ||||||
|     help='Interactive mode, ignores other keys' |     help='Generate access key for the first available admin' | ||||||
| ) | ) | ||||||
| @click.option( | @click.option( | ||||||
|     '--interactive', |     '--interactive', | ||||||
| @@ -70,24 +76,21 @@ def ls(all, admins): | |||||||
|     required=False, |     required=False, | ||||||
|     help='Interactive mode, ignores other keys' |     help='Interactive mode, ignores other keys' | ||||||
| ) | ) | ||||||
| def access(id, count, interactive, random): | def access(_id, count, interactive, random): | ||||||
|     if id and not count: |     if _id and not count: | ||||||
|         keys = user_cursor.get_access_keys(user=id, count=1) |         keys = user_cursor.get_access_keys(user=_id, count=1) | ||||||
|         links = user_cursor.gen_access_links(keys) |     elif _id and count: | ||||||
|         user_cursor.echo_access_links(links) |         keys = user_cursor.get_access_keys(user=_id, count=count) | ||||||
|     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: |     elif random: | ||||||
|         admin = user_cursor.get_first_random_admin() |         admin = user_cursor.get_first_random_admin() | ||||||
|         keys = user_cursor.get_access_keys(user=admin.get('id', 3), count=1) |         keys = user_cursor.get_access_keys(user=admin.get('id', 3)) | ||||||
|         links = user_cursor.gen_access_links(keys) |     elif interactive: | ||||||
|         user_cursor.echo_access_links(links) |         user_cursor.gen_access_links_interactive() | ||||||
|  |         return  # exit from func | ||||||
|     else: |     else: | ||||||
|         pass |         pass | ||||||
|  |     links = user_cursor.gen_access_links(keys) | ||||||
|  |     user_cursor.echo_access_links(links) | ||||||
|  |  | ||||||
|  |  | ||||||
| @user.command(help='Generate API token for mgrctl user') | @user.command(help='Generate API token for mgrctl user') | ||||||
|   | |||||||
| @@ -10,38 +10,43 @@ class UserAPI(object): | |||||||
|         self.callback_class = callback_class |         self.callback_class = callback_class | ||||||
|         self.callback = callback_class() |         self.callback = callback_class() | ||||||
|  |  | ||||||
|     def get_users(self, role: str) -> dict: |     def get_users(self, role: str) -> list: | ||||||
|         data = {} |         data = {} | ||||||
|         if role == 'admin': |         if role == 'admin': | ||||||
|             data = {"where": "((roles+CP+'%@admin%')+AND+(state+EQ+'active'))"} |             data = {"where": "((roles+CP+'%@admin%')+AND+(state+EQ+'active'))"} | ||||||
|         return self.callback.call_api( |         response = self.callback.call_api( | ||||||
|             url='/user', |             url='/user', | ||||||
|             method='GET', |             method='GET', | ||||||
|             data=data |             data=data | ||||||
|         ) |         ) | ||||||
|  |         users = self._extract_users(users=response) | ||||||
|  |         return users | ||||||
|  |  | ||||||
|     def _format_users(self, users: dict) -> list: |     def _extract_users(self, users: dict) -> list: | ||||||
|  |         return users.get('list', []) | ||||||
|  |  | ||||||
|  |     def _format_users(self, users: list) -> list: | ||||||
|         output = [] |         output = [] | ||||||
|         for user in users.get('list', []): |         for user in users: | ||||||
|             output.append({ |             output.append({ | ||||||
|                 'id': user.get('id', ''), |                 'id': user.get('id', ''), | ||||||
|                 'email': user.get('email', ''), |                 'email': user.get('email', ''), | ||||||
|                 'roles': user.get('roles', []), |                 'roles': user.get('roles', []), | ||||||
|                 'state': user.get('state', '') |                 'state': user.get('state', ''), | ||||||
|  |                 # add more fields here... | ||||||
|             }) |             }) | ||||||
|         return output |         return output | ||||||
|  |  | ||||||
|     def get_first_random_admin(self): |     def get_first_random_admin(self): | ||||||
|         users = self.get_users(role='admin') |         users = self.get_users(role='admin') | ||||||
|         admin = {} |         admin = {} | ||||||
|         for user in users.get('list', []): |         for user in users: | ||||||
|             if '@admin' in admin.get('roles', []): |             if '@admin' in user.get('roles', []): | ||||||
|                 admin = user |                 admin = user | ||||||
|                 break |                 break | ||||||
|         return admin |         return admin | ||||||
|  |  | ||||||
|     def echo_users(self, role: str) -> None: |     def echo_users(self, users: list) -> None: | ||||||
|         users = self.get_users(role) |  | ||||||
|         output = self._format_users(users) |         output = self._format_users(users) | ||||||
|         click.echo(tabulate(output, headers='keys')) |         click.echo(tabulate(output, headers='keys')) | ||||||
|  |  | ||||||
| @@ -66,6 +71,19 @@ class UserAPI(object): | |||||||
|     def echo_access_links(self, links: list) -> None: |     def echo_access_links(self, links: list) -> None: | ||||||
|         click.echo(tabulate(links, headers='keys')) |         click.echo(tabulate(links, headers='keys')) | ||||||
|  |  | ||||||
|  |     def gen_access_links_interactive(self) -> None: | ||||||
|  |         users = self.get_users(role='admin') | ||||||
|  |         self.echo_users(users) | ||||||
|  |         try: | ||||||
|  |             click.echo('Choose user id and count of keys') | ||||||
|  |             _id = int(input('User ID: ')) | ||||||
|  |             count = int(input('Count of keys: ')) | ||||||
|  |             keys = self.get_access_keys(user=_id, count=count) | ||||||
|  |             links = self.gen_access_links(keys) | ||||||
|  |             self.echo_access_links(links) | ||||||
|  |         except ValueError: | ||||||
|  |             click.echo('Error: Invalid, value is not a valid integer') | ||||||
|  |  | ||||||
|     def gen_api_token(self, email=None, password=None): |     def gen_api_token(self, email=None, password=None): | ||||||
|         token = self.callback.get_auth_token(email, password) |         token = self.callback.get_auth_token(email, password) | ||||||
|         return token |         return token | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user