Skip to content

Commit

Permalink
Patch user rule
Browse files Browse the repository at this point in the history
  • Loading branch information
islean committed Jan 28, 2025
1 parent 594366b commit af08fda
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cg/services/orders/validation/rules/order/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
UserNotAssociatedWithCustomerError,
)
from cg.services.orders.validation.models.order import Order
from cg.store.models import User
from cg.store.store import Store


Expand All @@ -22,13 +23,13 @@ def validate_customer_exists(
def validate_user_belongs_to_customer(
order: Order, store: Store, **kwargs
) -> list[UserNotAssociatedWithCustomerError]:
user: User = store.get_user_by_entry_id(order._user_id)
has_access: bool = store.is_user_associated_with_customer(
user_id=order._user_id,
customer_internal_id=order.customer,
)

errors: list[UserNotAssociatedWithCustomerError] = []
if not has_access:
if not user.is_admin or has_access:
error = UserNotAssociatedWithCustomerError()
errors.append(error)
return errors
Expand Down
8 changes: 8 additions & 0 deletions cg/store/crud/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,14 @@ def get_user_by_email(self, email: str) -> User | None:
filter_functions=[UserFilter.BY_EMAIL],
).first()

def get_user_by_entry_id(self, id: int) -> User | None:
"""Return a user by its entry id."""
return apply_user_filter(
users=self._get_query(table=User),
user_id=id,
filter_functions=[UserFilter.BY_ID],
).first()

def is_user_associated_with_customer(self, user_id: int, customer_internal_id: str) -> bool:
user: User | None = apply_user_filter(
users=self._get_query(table=User),
Expand Down

0 comments on commit af08fda

Please sign in to comment.