Source code for codegrade.models.login_response_permissions
"""The module that defines the ``LoginResponsePermissions`` model.
SPDX-License-Identifier: AGPL-3.0-only OR BSD-3-Clause-Clear
"""
from __future__ import annotations
import typing as t
from dataclasses import dataclass, field
import cg_request_args as rqa
from .. import parsers
from ..utils import to_dict
from .global_perm_map import GlobalPermMap
from .tenant_perm_map import TenantPermMap
[docs]
@dataclass
class LoginResponsePermissions:
"""The permissions of the logged-in user, split by scope."""
#: The tenant-scoped permissions of the logged-in user.
tenant: TenantPermMap
#: The global-scoped permissions of the logged-in user.
global_: GlobalPermMap
raw_data: t.Optional[t.Dict[str, t.Any]] = field(init=False, repr=False)
data_parser: t.ClassVar[t.Any] = rqa.Lazy(
lambda: rqa.FixedMapping(
rqa.RequiredArgument(
"tenant",
parsers.ParserFor.make(TenantPermMap),
doc="The tenant-scoped permissions of the logged-in user.",
),
rqa.RequiredArgument(
"global",
parsers.ParserFor.make(GlobalPermMap),
doc="The global-scoped permissions of the logged-in user.",
),
).use_readable_describe(True)
)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"tenant": to_dict(self.tenant),
"global": to_dict(self.global_),
}
return res
@classmethod
def from_dict(
cls: t.Type[LoginResponsePermissions], d: t.Dict[str, t.Any]
) -> LoginResponsePermissions:
parsed = cls.data_parser.try_parse(d)
res = cls(
tenant=parsed.tenant,
global_=getattr(parsed, "global"),
)
res.raw_data = d
return res