Skip to content

Commit 5c2c805

Browse files
committed
fix permission checker
1 parent ee35c2a commit 5c2c805

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ set `API_PERMISSION_CONF` in your settings.py as a dict.
4343

4444
```python
4545
API_PERMISSION_CONF = {
46-
'API_PREFIX': ['api/topic/'], # default is /
47-
'PERMISSION_DENIED_CODE': 400, # default is 1
46+
'API_PREFIX': ['api/topic/'], # default is /api/
47+
'PERMISSION_DENIED_CODE': 1, # default is 1
4848
'AUTHORIZATION_HEADER': 'HTTP_AUTHORIZATION', # default is HTTP_AUTHORIZATION
4949
'ADMIN_SITE_PATH': '/admin/' # default is /admin/
5050
}

api_permission/middleware.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ def process_request(self, request):
1717
method = request.method
1818
header_token = request.META.get(AUTHORIZATION_HEADER, None)
1919
user = request.user or AnonymousUser()
20-
if request.user and header_token is not None:
20+
if header_token:
2121
try:
2222
token = header_token.strip().split(' ')
2323
assert len(token) > 0, f"token maybe invalid: {header_token}"
2424
token_obj = Token.objects.get(key=token[-1])
2525
user = token_obj.user
2626
except Token.DoesNotExist as e:
27-
msg = f"api_permission checker: bearer token not exists: {e}"
27+
msg = f"api_permission checker: token not exists: {e}"
2828
return self._return_403_res(msg)
2929
except Exception as e:
3030
msg = f"{e}"
@@ -45,8 +45,7 @@ def process_request(self, request):
4545
prefix = '/' + str(prefix)
4646
api_prefix_list.append(prefix)
4747

48-
49-
if not path.startswith(ADMIN_SITE_PATH) or not user.is_superuser:
48+
if not path.startswith(ADMIN_SITE_PATH) and not user.is_superuser:
5049
for api_prefix in api_prefix_list:
5150
if path.startswith(api_prefix):
5251
if not self._has_permission(path, user, method):

0 commit comments

Comments
 (0)