mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-06 07:59:55 +02:00
See #186: moved api authentication required setting to preference
This commit is contained in:
parent
6100b106c0
commit
a3b2125d2a
13 changed files with 62 additions and 34 deletions
20
api/funkwhale_api/common/dynamic_preferences_registry.py
Normal file
20
api/funkwhale_api/common/dynamic_preferences_registry.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from dynamic_preferences import types
|
||||
from dynamic_preferences.registries import global_preferences_registry
|
||||
|
||||
from funkwhale_api.common import preferences
|
||||
|
||||
common = types.Section('common')
|
||||
|
||||
|
||||
@global_preferences_registry.register
|
||||
class APIAutenticationRequired(
|
||||
preferences.DefaultFromSettingMixin, types.BooleanPreference):
|
||||
section = common
|
||||
name = 'api_authentication_required'
|
||||
verbose_name = 'API Requires authentication'
|
||||
setting = 'API_AUTHENTICATION_REQUIRED'
|
||||
help_text = (
|
||||
'If disabled, anonymous users will be able to query the API'
|
||||
'and access music data (as well as other data exposed in the API '
|
||||
'without specific permissions)'
|
||||
)
|
|
@ -5,11 +5,13 @@ from django.http import Http404
|
|||
|
||||
from rest_framework.permissions import BasePermission, DjangoModelPermissions
|
||||
|
||||
from funkwhale_api.common import preferences
|
||||
|
||||
|
||||
class ConditionalAuthentication(BasePermission):
|
||||
|
||||
def has_permission(self, request, view):
|
||||
if settings.API_AUTHENTICATION_REQUIRED:
|
||||
if preferences.get('common__api_authentication_required'):
|
||||
return request.user and request.user.is_authenticated
|
||||
return True
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ from django.conf import settings
|
|||
|
||||
from rest_framework.permissions import BasePermission
|
||||
|
||||
from funkwhale_api.common import preferences
|
||||
from funkwhale_api.federation import actors
|
||||
from funkwhale_api.federation import models
|
||||
|
||||
|
@ -12,6 +13,9 @@ class Listen(BasePermission):
|
|||
if not settings.PROTECT_AUDIO_FILES:
|
||||
return True
|
||||
|
||||
if not preferences.get('common__api_authentication_required'):
|
||||
return True
|
||||
|
||||
user = getattr(request, 'user', None)
|
||||
if user and user.is_authenticated:
|
||||
return True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue