Schemas
Before sending ORM models to client, we need to validate and serialize them. This is done by using a pydantic v2
library. All schemas are located in fastauth.schema
module.
User schema
For CRUD operations we need to inherit BaseUserRead
, BaseUserCreate
and BaseUserUpdate
schemas, and expand them
with fields accordingly to our ORM model. For read schema we need pass ID Type as in our ORM model.
from fastauth.schema import BaseUserRead, BaseUserUpdate, BaseUserCreate
import uuid
class UserRead(BaseUserRead[uuid.UUID]):
pass
class UserUpdate(BaseUserUpdate):
pass
class UserCreate(BaseUserCreate):
pass
RBAC schemas
Permission schema
For permission schema we need to inherit BasePermissionRead
, BasePermissionCreate
and BasePermissionUpdate
schemas
import uuid
from fastauth.schema import (
BasePermissionRead,
BasePermissionCreate,
BasePermissionUpdate,
BaseRoleRead,
BaseRoleUpdate,
BaseRoleCreate,
BaseUserRead,
RBACMixin,
BaseUserCreate,
)
class PermissionRead(BasePermissionRead):
pass
class PermissionCreate(BasePermissionCreate):
pass
class PermissionUpdate(BasePermissionUpdate):
pass
Role schema
For role schema we need to inherit BaseRoleRead
, BaseRoleCreate
and BaseRoleUpdate
schemas. Also we need pass PermissionRead
class
to Generic type, to bind Role and Permission schema.
import uuid
from fastauth.schema import (
BasePermissionRead,
BasePermissionCreate,
BasePermissionUpdate,
BaseRoleRead,
BaseRoleUpdate,
BaseRoleCreate,
BaseUserRead,
RBACMixin,
BaseUserCreate,
)
class RoleRead(BaseRoleRead[PermissionRead]):
pass
class RoleCreate(BaseRoleCreate):
pass
class RoleUpdate(BaseRoleUpdate):
pass
RBAC Mixin
To bind User with Roles and Permission we need to extend user create and read schemas with RBACMixin
class. And pass RoleRead
and PermissionRead
classes to Generic type.
import uuid
from fastauth.schema import (
BasePermissionRead,
BasePermissionCreate,
BasePermissionUpdate,
BaseRoleRead,
BaseRoleUpdate,
BaseRoleCreate,
BaseUserRead,
RBACMixin,
BaseUserCreate,
)
class UserRead(BaseUserRead[uuid.UUID], RBACMixin[RoleRead, PermissionRead]):
pass
class UserCreate(BaseUserCreate, RBACMixin):
pass
OAuth schemas
OAuthAccount schema
We need create only read schema for OAuthAccount model. Inherit BaseOAuthAccountRead
and pass ID field type to Generic.
import uuid
from fastauth.schema import BaseUserRead, OAuthMixin, BaseOAuthRead
class OAuthRead(BaseUserRead[uuid.UUID]):
pass
OAuthMixin
To bind User with OAuthAccount we need to extend UserRead
with OAuthMixin
class. And pass OAuthAccountRead
class to Generic type.
import uuid
from fastauth.schema import BaseUserRead, OAuthMixin, BaseOAuthRead
class OAuthRead(BaseUserRead[uuid.UUID]):
pass
class User(BaseUserRead[uuid.UUID], OAuthMixin[OAuthRead]):
pass
Full User Schema
To get full featured User schema we need to inherit all mixins and pass all Generic types.
from fastauth.schema import BaseUserRead, BaseUserCreate, RBACMixin, BaseUserUpdate
import uuid
...
class UserRead(BaseUserRead[uuid.UUID], RBACMixin[RoleRead, PermissionRead], OAuthMixin[OAuthAccountRead]):
pass
class UserCreate(BaseUserCreate, RBACMixin):
pass
class UserUpdate(BaseUserUpdate):
pass
...