Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Commit

Permalink
fix: Use predefined values for role and status args for CreateUser/Mo…
Browse files Browse the repository at this point in the history
…difyUser mutations (#172)

* Previously `status` was using a wrong type (`bool`).

Backported-From: main (21.09)
Backported-To: 20.09
  • Loading branch information
achimnol committed Jul 21, 2021
1 parent f37a365 commit 0975fb2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions changes/172.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix wrong type and default value of `status` argument for `User.create()` and `User.update()` functional APIs and let `role` and `status` arguments accept explicit enum types in addition to raw strings
44 changes: 37 additions & 7 deletions src/ai/backend/client/func/user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import annotations

import enum
import textwrap
from typing import AsyncIterator, Iterable, Sequence, Union
import uuid
Expand All @@ -10,6 +13,8 @@

__all__ = (
'User',
'UserStatus',
'UserRole',
)

_default_list_fields = (
Expand All @@ -24,6 +29,26 @@
)


class UserRole(str, enum.Enum):
"""
The role (privilege level) of users.
"""
SUPERADMIN = 'superadmin'
ADMIN = 'admin'
USER = 'user'
MONITOR = 'monitor'


class UserStatus(enum.Enum):
"""
The detailed status of users to represent the signup process and account lifecycles.
"""
ACTIVE = 'active'
INACTIVE = 'inactive'
DELETED = 'deleted'
BEFORE_VERIFICATION = 'before-verification'


class User(BaseFunction):
"""
Provides interactions with users.
Expand Down Expand Up @@ -186,8 +211,12 @@ async def detail_by_uuid(
async def create(
cls,
domain_name: str,
email: str, password: str, username: str = None, full_name: str = None,
role: str = 'user', status: bool = True,
email: str,
password: str,
username: str = None,
full_name: str = None,
role: UserRole | str = UserRole.USER,
status: UserStatus | str = UserStatus.ACTIVE,
need_password_change: bool = False,
description: str = '',
group_ids: Iterable[str] = None,
Expand All @@ -213,8 +242,8 @@ async def create(
'password': password,
'username': username,
'full_name': full_name,
'role': role,
'status': status,
'role': role.value if isinstance(role, UserRole) else role,
'status': status.value if isinstance(status, UserStatus) else status,
'need_password_change': need_password_change,
'description': description,
'domain_name': domain_name,
Expand All @@ -232,7 +261,8 @@ async def update(
password: str = None, username: str = None,
full_name: str = None,
domain_name: str = None,
role: str = None, status: bool = None,
role: UserRole | str = UserRole.USER,
status: UserStatus | str = UserStatus.ACTIVE,
need_password_change: bool = None,
description: str = None,
group_ids: Iterable[str] = None,
Expand All @@ -256,8 +286,8 @@ async def update(
'username': username,
'full_name': full_name,
'domain_name': domain_name,
'role': role,
'status': status,
'role': role.value if isinstance(role, UserRole) else role,
'status': status.value if isinstance(status, UserStatus) else status,
'need_password_change': need_password_change,
'description': description,
'group_ids': group_ids,
Expand Down

0 comments on commit 0975fb2

Please sign in to comment.