Skip to content

Commit

Permalink
Working example for Condition fixture (#913)
Browse files Browse the repository at this point in the history
Working example for Condition fixture that check if user belongs to a group
  • Loading branch information
mbelletti committed Aug 27, 2024
1 parent ad37da0 commit 0ad7a36
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions docs/chapter-06.rst
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,9 @@ require that users action have specific group membership:
groups = Tags(db.auth_user)
class requires_membership(Fixture):
def __init__(self, group):
self.__prerequisites__ = [auth.user] # you must have a user before you can check
self.group = group # store the group when action defined
def on_request(self, context): # will be called if the action is called
if self.group not in groups.get(auth.user_id):
raise HTTP(401) # check and do something
@action("payroll")
@action.uses(auth, requires_membership("employees"))
@action.uses(auth,
Condition(lambda: 'employees' in groups.get(auth.user_id), on_false=lambda: redirect('index')))
def payroll():
return
Expand Down

0 comments on commit 0ad7a36

Please sign in to comment.