Skip to content

Commit

Permalink
New User Model
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBurchLog committed Feb 20, 2024
1 parent 18e88b8 commit 1aae05f
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 304 deletions.
141 changes: 87 additions & 54 deletions brewtils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
"Event",
"Events",
"Queue",
"Principal",
"LegacyRole",
"RefreshToken",
"Job",
"RequestFile",
Expand All @@ -37,6 +35,8 @@
"Garden",
"Operation",
"Resolvable",
"Role",
"User",
]


Expand Down Expand Up @@ -1103,58 +1103,6 @@ def __repr__(self):
return "<Queue: name=%s, size=%s>" % (self.name, self.size)


class Principal(BaseModel):
schema = "PrincipalSchema"

def __init__(
self,
id=None, # noqa # shadows built-in
username=None,
roles=None,
permissions=None,
preferences=None,
metadata=None,
):
self.id = id
self.username = username
self.roles = roles
self.permissions = permissions
self.preferences = preferences
self.metadata = metadata

def __str__(self):
return "%s" % self.username

def __repr__(self):
return "<Principal: username=%s, roles=%s, permissions=%s>" % (
self.username,
self.roles,
self.permissions,
)


class LegacyRole(BaseModel):
schema = "LegacyRoleSchema"

def __init__(
self,
id=None, # noqa # shadows built-in
name=None,
description=None,
permissions=None,
):
self.id = id
self.name = name
self.description = description
self.permissions = permissions

def __str__(self):
return "%s" % self.name

def __repr__(self):
return "<LegacyRole: name=%s, permissions=%s>" % (self.name, self.permissions)


class RefreshToken(BaseModel):
schema = "RefreshTokenSchema"

Expand Down Expand Up @@ -1649,3 +1597,88 @@ def __repr__(self):
self.storage,
self.details,
)


class User(BaseModel):
schema = "UserSchema"

def __init__(
self,
username,
password=None,
roles=None,
assigned_roles=None,
is_remote=False,
metadata=None,
):
self.username = username
self.password = password
self.roles = roles or []
self.assigned_roles = assigned_roles or []
self.is_remote = is_remote
self.metadata = metadata

def __str__(self):
return "%s: %s" % (self.username, self.roles)

def __repr__(self):
return "<User: username=%s, roles=%s>" % (
self.username,
self.roles,
)


class Role(BaseModel):
schema = "RoleSchema"

PERMISSION_TYPES = {
"ADMIN",
"OPERATOR",
"READ_ONLY", # Default value if not role is provided
}

def __init__(
self,
name,
permission=None,
description=None,
id=None,
is_remote=False,
scope_gardens=None,
scope_namespaces=None,
scope_systems=None,
scope_instances=None,
scope_verisons=None,
scope_commands=None,
):
self.permission = permission or "READ_ONLY"
self.description = description
self.id = id
self.name = name
self.is_remote = is_remote
self.scope_gardens = scope_gardens or []
self.scope_namespaces = scope_namespaces or []
self.scope_systems = scope_systems or []
self.scope_instances = scope_instances or []
self.scope_verisons = scope_verisons or []
self.scope_commands = scope_commands or []

def __str__(self):
return "%s" % (self.name)

def __repr__(self):
return (
"<Role: id=%s, name=%s, permission=%s, is_remote=%s, scope_garden=%s, scope_namespaces=%s, "
"scope_systems=%s, scope_instances=%s, scope_versions=%s, scope_commands=%s>"
) % (
self.id,
self.name,
self.permission,
self.is_remote,
self.scope_gardens,
self.scope_namespaces,
self.scope_systems,
self.scope_instances,
self.scope_verisons,
self.scope_commands,
)
8 changes: 3 additions & 5 deletions brewtils/rest/easy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,17 +1088,15 @@ def forward(self, operation, **kwargs):
SchemaParser.serialize_operation(operation), **kwargs
)

@wrap_response(
parse_method="parse_principal", parse_many=False, default_exc=FetchError
)
@wrap_response(parse_method="parse_user", parse_many=False, default_exc=FetchError)
def get_user(self, user_identifier):
"""Find a user
Args:
user_identifier (str): User ID or username
Returns:
Principal: The User
User: The User
"""
return self.client.get_user(user_identifier)
Expand All @@ -1107,7 +1105,7 @@ def who_am_i(self):
"""Find user using the current set of credentials
Returns:
Principal: The User
User: The User
"""
return self.get_user(self.client.username or "anonymous")
Expand Down
37 changes: 14 additions & 23 deletions brewtils/schema_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@ class SchemaParser(object):
"QueueSchema": brewtils.models.Queue,
"ParameterSchema": brewtils.models.Parameter,
"PatchSchema": brewtils.models.PatchOperation,
"PrincipalSchema": brewtils.models.Principal,
"UserSchema": brewtils.models.User,
"RefreshTokenSchema": brewtils.models.RefreshToken,
"RequestSchema": brewtils.models.Request,
"RequestFileSchema": brewtils.models.RequestFile,
"FileSchema": brewtils.models.File,
"FileChunkSchema": brewtils.models.FileChunk,
"FileStatusSchema": brewtils.models.FileStatus,
"RequestTemplateSchema": brewtils.models.RequestTemplate,
"LegacyRoleSchema": brewtils.models.LegacyRole,
"SystemSchema": brewtils.models.System,
"OperationSchema": brewtils.models.Operation,
"RunnerSchema": brewtils.models.Runner,
"ResolvableSchema": brewtils.models.Resolvable,
"RoleSchema": brewtils.models.Role,
"UserSchema": brewtils.models.User,
}

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -253,20 +254,18 @@ def parse_queue(cls, queue, from_string=False, **kwargs):
)

@classmethod
def parse_principal(cls, principal, from_string=False, **kwargs):
"""Convert raw JSON string or dictionary to a principal model object
def parse_user(cls, user, from_string=False, **kwargs):
"""Convert raw JSON string or dictionary to a user model object
Args:
principal: The raw input
user: The raw input
from_string: True if input is a JSON string, False if a dictionary
**kwargs: Additional parameters to be passed to the Schema (e.g. many=True)
Returns:
A Principal object
A User object
"""
return cls.parse(
principal, brewtils.models.Principal, from_string=from_string, **kwargs
)
return cls.parse(user, brewtils.models.User, from_string=from_string, **kwargs)

@classmethod
def parse_role(cls, role, from_string=False, **kwargs):
Expand All @@ -280,9 +279,7 @@ def parse_role(cls, role, from_string=False, **kwargs):
Returns:
A Role object
"""
return cls.parse(
role, brewtils.models.LegacyRole, from_string=from_string, **kwargs
)
return cls.parse(role, brewtils.models.Role, from_string=from_string, **kwargs)

@classmethod
def parse_refresh_token(cls, refresh_token, from_string=False, **kwargs):
Expand Down Expand Up @@ -674,11 +671,11 @@ def serialize_queue(cls, queue, to_string=True, **kwargs):
)

@classmethod
def serialize_principal(cls, principal, to_string=True, **kwargs):
"""Convert a principal model into serialized form
def serialize_user(cls, user, to_string=True, **kwargs):
"""Convert a user model into serialized form
Args:
principal: The principal object(s) to be serialized
user: The user object(s) to be serialized
to_string: True to generate a JSON-formatted string, False to generate a
dictionary
**kwargs: Additional parameters to be passed to the Schema (e.g. many=True)
Expand All @@ -687,10 +684,7 @@ def serialize_principal(cls, principal, to_string=True, **kwargs):
Serialized representation
"""
return cls.serialize(
principal,
to_string=to_string,
schema_name=brewtils.models.Principal.schema,
**kwargs
user, to_string=to_string, schema_name=brewtils.models.User.schema, **kwargs
)

@classmethod
Expand All @@ -707,10 +701,7 @@ def serialize_role(cls, role, to_string=True, **kwargs):
Serialized representation
"""
return cls.serialize(
role,
to_string=to_string,
schema_name=brewtils.models.LegacyRole.schema,
**kwargs
role, to_string=to_string, schema_name=brewtils.models.Role.schema, **kwargs
)

@classmethod
Expand Down
Loading

0 comments on commit 1aae05f

Please sign in to comment.