Package: drf-guard
drf-guardCreate flexible and simple to use access rules for Django REST Framework(DRF). Works with both class based DRF permissions, Django permissions and Django groups. This library allows you to build complex access rules in a very simple way, it allows you to combine permissions and groups with logical operators.Have you ever had multiple permissions or groups and wanted to be able to do something like below to your endpoint?.# Check if user has certain permissions with `and`, `or` & `not` operatorspermissions:(IsAdminOr(IsObjectOwnerAndIsAllowedToEdit))Or# Evaluate if user in certain groups with `and`, `or` & `not` operatorsgroups:('admin'Or'client'AndNot'seller')Well you are not alone, this library allows you to do that withAnd,Or&Notoperators to each endpoint however you want regardless whether you are using class based DRF permissions, Django permissions or Django grops, it can deal with all those.RequirementsPython >= 3.5Django >= 1.11Django REST Framework >= 3.5Installingpipinstalldrf-guardGetting startedUsingdrf-guardis very simple, below is an example# views.py# Import operators & permissions from drf_guardfromdrf_guard.operatorsimportAnd,Or,Notfromdrf_guard.permissionsimportHasRequiredGroups,HasRequiredPermissionsclassUserViewSet(viewsets.ModelViewSet):queryset=User.objects.all()serializer_class=UserSerializer# Use drf_guard permissions herepermission_classes=(HasRequiredGroups,HasRequiredPermissions)# Now define access rules for your API endpoint with groups and permissions as you wishaccess_rules={'GET':{'list':{# To access this the user must belongs to admin or client group'groups':['admin',Or,'client'],'permissions':[IsAuthenticated]# Also the user must be authenticated},'retrieve':{'groups':[Not,'admin'],# The user must not be in admin group'permissions':[IsAuthenticated,And,IsAllowedUser]# Must be authenticated and allowed},},'POST':{'groups':[],# Don't allow at all(This evaluates to False always)'permissions':[]# Don't allow at all(This evaluates to False always)},'PUT':{'groups':'__any__',# Belongs to any group or none'permissions':'__any__'# Has any permission or none},'PATCH':{'groups':['client',And,Not,'admin'],# User belongs to client and not admin group'permissions':[IsAuthenticated,IsAllowedUser]# This is = [IsAuthenticated, And, IsAllowedUser]},'DELETE':{'groups':['client',Or,[Not,'client',And,'admin']],# You can basically do any combination'permissions':[IsAuthenticated]}}What's important here is to know what goes into groups and permissionsGroups takes group names and Django group objects, so you can use those operators however you want with these two, you can even mix the two types together, e.g'groups':[Group.objects.get(name='admin'),Or,'client']Permissions takes DRF permissions(class based), Django permission objects and Django permission names(codenames), so you can use those operators however you want with these three, you can even use all three types together, e.g'permissions':[IsAuthenticated,And,Permissions.objects.get('view_user'),Or,'change_user']Note:And,Or&Notare the equvalent operators forand,or&notrespectivelyUnlikeand,or&notthe operatorsAnd,Or&Nothave no precedence they are evaluated from left to right, if you want precedence use list or tuple to make one i.e[IsAuthenticated, And, [IsAdmin, Or, IsClient]]The'__any__'on groups/permissions stands for any group/permission or noneThe GET-list stands for permission & groups inGET: /users/routeThe GET-retrieve stands for groups & permissions inGET: /users/{id}/routesThe POST stands for groups & permissions inPOST: /users/routeThe PUT stands for groups & permissions inPUT: /users/{id}/routesThe PATCH stands for groups & permissions inPATCH: /users/{id}/routesThe DELETE stands for groups & permissions inDELETE: /users/{id}/routes