mirror of https://gitlab.com/litecord/litecord.git
revamp how Flags works
instead of pulling a hack and injecting from_int() in `__init_subclass__`, we just make an actual function, and cache the wanted attributes in the subclass at its creation this fixes statis analyzers claiming from_int() doesn't exist on subclasses, for good reason, as they'd be turing-complete to do so, lol - auth: fix some mypy issues about reusing same variable
This commit is contained in:
parent
7278c15d9c
commit
420646e76f
|
|
@ -56,20 +56,20 @@ async def raw_token_check(token: str, db=None) -> int:
|
|||
# just try by fragments instead of
|
||||
# unpacking
|
||||
fragments = token.split(".")
|
||||
user_id = fragments[0]
|
||||
user_id_str = fragments[0]
|
||||
|
||||
try:
|
||||
user_id = base64.b64decode(user_id.encode())
|
||||
user_id = int(user_id)
|
||||
user_id_decoded = base64.b64decode(user_id_str.encode())
|
||||
user_id = int(user_id_decoded)
|
||||
except (ValueError, binascii.Error):
|
||||
raise Unauthorized("Invalid user ID type")
|
||||
|
||||
pwd_hash = await db.fetchval(
|
||||
"""
|
||||
SELECT password_hash
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
""",
|
||||
SELECT password_hash
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
""",
|
||||
user_id,
|
||||
)
|
||||
|
||||
|
|
@ -88,10 +88,10 @@ async def raw_token_check(token: str, db=None) -> int:
|
|||
# with people leaving their clients open forever)
|
||||
await db.execute(
|
||||
"""
|
||||
UPDATE users
|
||||
SET last_session = (now() at time zone 'utc')
|
||||
WHERE id = $1
|
||||
""",
|
||||
UPDATE users
|
||||
SET last_session = (now() at time zone 'utc')
|
||||
WHERE id = $1
|
||||
""",
|
||||
user_id,
|
||||
)
|
||||
|
||||
|
|
@ -128,10 +128,10 @@ async def admin_check() -> int:
|
|||
|
||||
flags = await app.db.fetchval(
|
||||
"""
|
||||
SELECT flags
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
""",
|
||||
SELECT flags
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
""",
|
||||
user_id,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -54,27 +54,21 @@ class Flags:
|
|||
"""
|
||||
|
||||
def __init_subclass__(cls, **_kwargs):
|
||||
attrs = inspect.getmembers(cls, lambda x: not inspect.isroutine(x))
|
||||
# get only the members that represent a field
|
||||
cls._attrs = inspect.getmembers(cls, lambda x: isinstance(x, int))
|
||||
|
||||
def _make_int(value):
|
||||
res = Flags()
|
||||
@classmethod
|
||||
def from_int(cls, value: int):
|
||||
"""Create a Flags from a given int value."""
|
||||
res = Flags()
|
||||
setattr(res, "value", value)
|
||||
|
||||
setattr(res, "value", value)
|
||||
for attr, val in cls._attrs:
|
||||
has_attr = (value & val) == val
|
||||
# set attributes dynamically
|
||||
setattr(res, f"is_{attr}", has_attr)
|
||||
|
||||
for attr, val in attrs:
|
||||
# get only the ones that represent a field in the
|
||||
# number's bits
|
||||
if not isinstance(val, int):
|
||||
continue
|
||||
|
||||
has_attr = (value & val) == val
|
||||
|
||||
# set each attribute
|
||||
setattr(res, f"is_{attr}", has_attr)
|
||||
|
||||
return res
|
||||
|
||||
cls.from_int = _make_int
|
||||
return res
|
||||
|
||||
|
||||
class ChannelType(EasyEnum):
|
||||
|
|
|
|||
Loading…
Reference in New Issue