fix-auth
This commit is contained in:
parent
11655b31ae
commit
e90099ae53
|
@ -3,6 +3,7 @@ from typing import Optional, Tuple
|
||||||
|
|
||||||
from graphql.type import GraphQLResolveInfo
|
from graphql.type import GraphQLResolveInfo
|
||||||
from sqlalchemy.orm import joinedload, exc
|
from sqlalchemy.orm import joinedload, exc
|
||||||
|
from sqlalchemy import select, and_
|
||||||
from starlette.authentication import AuthenticationBackend
|
from starlette.authentication import AuthenticationBackend
|
||||||
from starlette.requests import HTTPConnection
|
from starlette.requests import HTTPConnection
|
||||||
|
|
||||||
|
@ -35,24 +36,22 @@ class JWTAuthenticate(AuthenticationBackend):
|
||||||
payload = await SessionToken.verify(token)
|
payload = await SessionToken.verify(token)
|
||||||
if payload is None:
|
if payload is None:
|
||||||
return AuthCredentials(scopes=[]), AuthUser(user_id=None)
|
return AuthCredentials(scopes=[]), AuthUser(user_id=None)
|
||||||
|
user = None
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
try:
|
try:
|
||||||
user = (
|
q = select(
|
||||||
session.query(User).options(
|
User
|
||||||
joinedload(User.roles),
|
|
||||||
joinedload(User.ratings)
|
|
||||||
).filter(
|
).filter(
|
||||||
User.id == id
|
User.id == payload.user_id
|
||||||
).one()
|
).select_from(User)
|
||||||
)
|
user = session.execute(q).unique().one()
|
||||||
except exc.NoResultFound:
|
except exc.NoResultFound:
|
||||||
user = None
|
user = None
|
||||||
|
|
||||||
if not user:
|
if not user:
|
||||||
return AuthCredentials(scopes=[]), AuthUser(user_id=None)
|
return AuthCredentials(scopes=[]), AuthUser(user_id=None)
|
||||||
|
|
||||||
scopes = user.get_permission()
|
scopes = {} # await user.get_permission()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
AuthCredentials(
|
AuthCredentials(
|
||||||
|
|
|
@ -111,5 +111,5 @@ class User(Base):
|
||||||
return scope
|
return scope
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
# if __name__ == "__main__":
|
||||||
print(User.get_permission(user_id=1)) # type: ignore
|
# print(User.get_permission(user_id=1)) # type: ignore
|
||||||
|
|
|
@ -2,21 +2,21 @@ from base.redis import redis
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
async def get_unread_counter(chat_id: str, user_slug: str):
|
async def get_unread_counter(chat_id: str, user_id: int):
|
||||||
try:
|
try:
|
||||||
unread = await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}")
|
unread = await redis.execute("LLEN", f"chats/{chat_id.decode('utf-8')}/unread/{user_id}")
|
||||||
if unread:
|
if unread:
|
||||||
return unread
|
return unread
|
||||||
except Exception:
|
except Exception:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
async def get_total_unread_counter(user_slug: str):
|
async def get_total_unread_counter(user_id: int):
|
||||||
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
|
chats = await redis.execute("GET", f"chats_by_user/{str(user_id)}")
|
||||||
unread = 0
|
unread = 0
|
||||||
if chats:
|
if chats:
|
||||||
chats = json.loads(chats)
|
chats = json.loads(chats)
|
||||||
for chat_id in chats:
|
for chat_id in chats:
|
||||||
n = await get_unread_counter(chat_id.decode('utf-8'), user_slug)
|
n = await get_unread_counter(chat_id.decode('utf-8'), user_id)
|
||||||
unread += n
|
unread += n
|
||||||
return unread
|
return unread
|
||||||
|
|
|
@ -163,13 +163,19 @@ async def get_user_roles(slug):
|
||||||
@mutation.field("updateProfile")
|
@mutation.field("updateProfile")
|
||||||
@login_required
|
@login_required
|
||||||
async def update_profile(_, info, profile):
|
async def update_profile(_, info, profile):
|
||||||
|
print('[zine] update_profile')
|
||||||
|
print(profile)
|
||||||
auth = info.context["request"].auth
|
auth = info.context["request"].auth
|
||||||
user_id = auth.user_id
|
user_id = auth.user_id
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
user = session.query(User).filter(User.id == user_id).first()
|
session.query(User).filter(User.id == user_id).update({
|
||||||
if user:
|
"name": profile['name'],
|
||||||
User.update(user, **profile)
|
"slug": profile['slug'],
|
||||||
session.add(user)
|
"bio": profile['bio'],
|
||||||
|
"userpic": profile['userpic'],
|
||||||
|
"about": profile['about'],
|
||||||
|
"links": profile['links']
|
||||||
|
})
|
||||||
session.commit()
|
session.commit()
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
|
@ -108,10 +108,12 @@ input ShoutInput {
|
||||||
}
|
}
|
||||||
|
|
||||||
input ProfileInput {
|
input ProfileInput {
|
||||||
|
slug: String
|
||||||
name: String
|
name: String
|
||||||
userpic: String
|
userpic: String
|
||||||
links: [String]
|
links: [String]
|
||||||
bio: String
|
bio: String
|
||||||
|
about: String
|
||||||
}
|
}
|
||||||
|
|
||||||
input TopicInput {
|
input TopicInput {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user