query userRoles
This commit is contained in:
parent
7dea19495c
commit
0f3ba29fea
|
@ -1,6 +1,6 @@
|
||||||
from orm.rbac import Operation, Resource, Permission, Role
|
from orm.rbac import Operation, Resource, Permission, Role
|
||||||
from orm.community import Community
|
from orm.community import Community
|
||||||
from orm.user import User, UserRating
|
from orm.user import User, UserRating, UserRole
|
||||||
from orm.message import Message
|
from orm.message import Message
|
||||||
from orm.topic import Topic, TopicSubscription
|
from orm.topic import Topic, TopicSubscription
|
||||||
from orm.notification import Notification
|
from orm.notification import Notification
|
||||||
|
|
13
orm/user.py
13
orm/user.py
|
@ -25,11 +25,12 @@ class UserRating(Base):
|
||||||
user_id = Column(ForeignKey('user.id'), primary_key = True)
|
user_id = Column(ForeignKey('user.id'), primary_key = True)
|
||||||
value = Column(Integer)
|
value = Column(Integer)
|
||||||
|
|
||||||
UserRoles = Table("user_roles",
|
class UserRole(Base):
|
||||||
Base.metadata,
|
__tablename__ = "user_role"
|
||||||
Column('user_id', Integer, ForeignKey('user.id'), primary_key = True),
|
|
||||||
Column('role_id', Integer, ForeignKey('role.id'), primary_key = True)
|
id = None
|
||||||
)
|
user_id = Column(ForeignKey('user.id'), primary_key = True)
|
||||||
|
role_id = Column(ForeignKey('role.id'), primary_key = True)
|
||||||
|
|
||||||
UserTopics = Table("user_topics",
|
UserTopics = Table("user_topics",
|
||||||
Base.metadata,
|
Base.metadata,
|
||||||
|
@ -56,7 +57,7 @@ class User(Base):
|
||||||
oauth: str = Column(String, nullable=True)
|
oauth: str = Column(String, nullable=True)
|
||||||
notifications = relationship(lambda: UserNotifications)
|
notifications = relationship(lambda: UserNotifications)
|
||||||
ratings = relationship(UserRating, foreign_keys=UserRating.user_id)
|
ratings = relationship(UserRating, foreign_keys=UserRating.user_id)
|
||||||
roles = relationship(lambda: Role, secondary=UserRoles)
|
roles = relationship(lambda: Role, secondary=UserRole.__tablename__)
|
||||||
topics = relationship(lambda: Topic, secondary=UserTopics)
|
topics = relationship(lambda: Topic, secondary=UserTopics)
|
||||||
old_id: str = Column(String, nullable = True)
|
old_id: str = Column(String, nullable = True)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
from orm import User
|
from orm import User, UserRole, Role
|
||||||
from orm.base import local_session
|
from orm.base import local_session
|
||||||
from resolvers.base import mutation, query, subscription
|
from resolvers.base import mutation, query, subscription
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
|
|
||||||
|
from sqlalchemy.orm import selectinload
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
@query.field("getUserBySlug") # get a public profile
|
@query.field("getUserBySlug") # get a public profile
|
||||||
|
@ -19,3 +21,17 @@ async def get_current_user(_, info):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
user = session.query(User).filter(User.id == user_id).first()
|
user = session.query(User).filter(User.id == user_id).first()
|
||||||
return { "user": user }
|
return { "user": user }
|
||||||
|
|
||||||
|
@query.field("userRoles")
|
||||||
|
@login_required
|
||||||
|
async def user_roles(_, info):
|
||||||
|
auth = info.context["request"].auth
|
||||||
|
user_id = auth.user_id
|
||||||
|
|
||||||
|
with local_session() as session:
|
||||||
|
roles = session.query(Role).\
|
||||||
|
options(selectinload(Role.permissions)).\
|
||||||
|
join(UserRole).\
|
||||||
|
where(UserRole.user_id == user_id).all()
|
||||||
|
|
||||||
|
return roles
|
||||||
|
|
|
@ -114,6 +114,7 @@ type Query {
|
||||||
getCurrentUser: UserResult!
|
getCurrentUser: UserResult!
|
||||||
getUserBySlug(slug: String!): UserResult!
|
getUserBySlug(slug: String!): UserResult!
|
||||||
# rateUser(shout: Int): Int!
|
# rateUser(shout: Int): Int!
|
||||||
|
userRoles: [Role]!
|
||||||
|
|
||||||
# messages
|
# messages
|
||||||
getMessages(count: Int = 100, page: Int = 1): [Message!]!
|
getMessages(count: Int = 100, page: Int = 1): [Message!]!
|
||||||
|
@ -153,12 +154,27 @@ type Subscription {
|
||||||
|
|
||||||
############################################ Entities
|
############################################ Entities
|
||||||
|
|
||||||
|
type Resource {
|
||||||
|
id: Int!
|
||||||
|
name: String!
|
||||||
|
}
|
||||||
|
|
||||||
|
type Operation {
|
||||||
|
id: Int!
|
||||||
|
name: String!
|
||||||
|
}
|
||||||
|
|
||||||
|
type Permission {
|
||||||
|
operation_id: Int!
|
||||||
|
resource_id: Int!
|
||||||
|
}
|
||||||
|
|
||||||
type Role {
|
type Role {
|
||||||
id: Int!
|
id: Int!
|
||||||
name: String!
|
name: String!
|
||||||
community: Int!
|
community: Int!
|
||||||
desc: String
|
desc: String
|
||||||
permissions: [Int!]!
|
permissions: [Permission!]!
|
||||||
}
|
}
|
||||||
|
|
||||||
type Rating {
|
type Rating {
|
||||||
|
@ -192,7 +208,6 @@ type User {
|
||||||
links: [String]
|
links: [String]
|
||||||
emailConfirmed: Boolean # should contain all emails too
|
emailConfirmed: Boolean # should contain all emails too
|
||||||
muted: Boolean
|
muted: Boolean
|
||||||
roles: [Role]
|
|
||||||
updatedAt: DateTime
|
updatedAt: DateTime
|
||||||
wasOnlineAt: DateTime
|
wasOnlineAt: DateTime
|
||||||
rating: Int
|
rating: Int
|
||||||
|
|
Loading…
Reference in New Issue
Block a user