add cert pages
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from .admins import router as admins
|
||||
from .custom_pricing import router as custom_pricing
|
||||
from .enrollments.scheduled import router as scheduled
|
||||
from .users import router as users
|
||||
|
||||
__all__ = ['admins', 'custom_pricing', 'scheduled']
|
||||
__all__ = ['admins', 'custom_pricing', 'scheduled', 'users']
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
from http import HTTPStatus
|
||||
from typing import Annotated
|
||||
|
||||
from aws_lambda_powertools.event_handler.api_gateway import Router
|
||||
from aws_lambda_powertools.event_handler.openapi.params import Body
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
|
||||
from api_gateway import JSONResponse
|
||||
from boto3clients import dynamodb_client
|
||||
from config import USER_TABLE
|
||||
|
||||
@@ -15,3 +20,14 @@ def get_admins(org_id: str):
|
||||
KeyPair(org_id, 'admins#'),
|
||||
limit=100,
|
||||
)
|
||||
|
||||
|
||||
@router.delete('/<org_id>/admins')
|
||||
def revoke(org_id: str, user_id: Annotated[str, Body(embed=True)]):
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.delete(
|
||||
# Post-migration: rename `admins` to `ADMIN`
|
||||
key=KeyPair(org_id, f'admins#{user_id}'),
|
||||
)
|
||||
|
||||
return JSONResponse(status_code=HTTPStatus.NO_CONTENT)
|
||||
|
||||
@@ -11,7 +11,7 @@ from aws_lambda_powertools.event_handler.openapi.params import Body
|
||||
from layercake.dateutils import now
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
|
||||
from layercake.extra_types import CnpjStr, CpfStr, NameStr
|
||||
from pydantic import BaseModel, EmailStr
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
|
||||
from api_gateway import JSONResponse
|
||||
from boto3clients import dynamodb_client
|
||||
@@ -22,7 +22,7 @@ dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
class Org(BaseModel):
|
||||
id: str
|
||||
id: str | None = Field(default=None, exclude=True)
|
||||
name: str
|
||||
cnpj: CnpjStr
|
||||
|
||||
@@ -44,56 +44,25 @@ class UserConflictError(ServiceError):
|
||||
super().__init__(HTTPStatus.CONFLICT, msg)
|
||||
|
||||
|
||||
class UserNotFoundError(NotFoundError): ...
|
||||
class UserMissingError(NotFoundError): ...
|
||||
|
||||
|
||||
class OrgMissingError(NotFoundError): ...
|
||||
|
||||
|
||||
@router.post('/')
|
||||
@router.post('/<org_id>/users')
|
||||
def add_user(
|
||||
org_id: str,
|
||||
user: Annotated[User, Body(embed=True)],
|
||||
org: Annotated[Org, Body(embed=True)],
|
||||
):
|
||||
org.id = org_id
|
||||
|
||||
if _create_user(user, org):
|
||||
return JSONResponse(HTTPStatus.CREATED)
|
||||
|
||||
now_ = now()
|
||||
user_id = _get_user_id(user)
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.update(
|
||||
key=KeyPair(user_id, '0'),
|
||||
update_expr='ADD org_id :org_id',
|
||||
expr_attr_values={
|
||||
':org_id': {org.id},
|
||||
},
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': user_id,
|
||||
# Post-migration: rename `orgs` to `ORG`
|
||||
'sk': f'orgs#{org.id}',
|
||||
'name': org.name,
|
||||
'cnpj': org.cnpj,
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
# Post-migration: rename `orgmembers` to `ORGMEMBER`
|
||||
'id': f'orgmembers#{org.id}',
|
||||
'sk': user_id,
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
exc_cls=UserConflictError,
|
||||
)
|
||||
transact.condition(
|
||||
key=KeyPair(org.id, '0'),
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=OrgMissingError,
|
||||
)
|
||||
_add_member(user_id, org)
|
||||
|
||||
return JSONResponse(HTTPStatus.NO_CONTENT)
|
||||
|
||||
@@ -163,7 +132,7 @@ def _create_user(user: User, org: Org) -> bool:
|
||||
}
|
||||
)
|
||||
transact.condition(
|
||||
key=KeyPair(org.id, '0'),
|
||||
key=KeyPair(org.id, '0'), # type: ignore
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=OrgMissingError,
|
||||
)
|
||||
@@ -173,6 +142,44 @@ def _create_user(user: User, org: Org) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def _add_member(user_id: str, org: Org) -> None:
|
||||
now_ = now()
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.update(
|
||||
key=KeyPair(user_id, '0'),
|
||||
update_expr='ADD org_id :org_id',
|
||||
expr_attr_values={
|
||||
':org_id': {org.id},
|
||||
},
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': user_id,
|
||||
# Post-migration: rename `orgs` to `ORG`
|
||||
'sk': f'orgs#{org.id}',
|
||||
'name': org.name,
|
||||
'cnpj': org.cnpj,
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
# Post-migration: rename `orgmembers` to `ORGMEMBER`
|
||||
'id': f'orgmembers#{org.id}',
|
||||
'sk': user_id,
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
exc_cls=UserConflictError,
|
||||
)
|
||||
transact.condition(
|
||||
key=KeyPair(org.id, '0'), # type: ignore
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=OrgMissingError,
|
||||
)
|
||||
|
||||
|
||||
def _get_user_id(user: User) -> str:
|
||||
user_id = dyn.collection.get_items(
|
||||
KeyPair(
|
||||
@@ -189,6 +196,6 @@ def _get_user_id(user: User) -> str:
|
||||
).get('id')
|
||||
|
||||
if not user_id:
|
||||
raise UserNotFoundError()
|
||||
raise UserMissingError()
|
||||
|
||||
return user_id
|
||||
@@ -7,11 +7,10 @@ from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
from boto3clients import dynamodb_client
|
||||
from config import USER_TABLE
|
||||
|
||||
from .add import router as add
|
||||
from .emails import router as emails
|
||||
from .orgs import router as orgs
|
||||
|
||||
__all__ = ['add', 'emails', 'orgs']
|
||||
__all__ = ['emails', 'orgs']
|
||||
|
||||
router = Router()
|
||||
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||
|
||||
Reference in New Issue
Block a user