prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  add facility setting to disable 'guest' browsing
  
  ### Observed behavior
  
  Some organizations prefer that their users always be logged in
  
  ### Expected behavior
  
  A facility setting can be added, e.g. "Disable guest access".
  
  When this setting is active:
  
  * "Access as guest" is hidden on the log-in page
  * When a user visits a page without an active session, they are redirected to the log-in page
  
  ### User-facing consequences
  
  Sometimes students are accessing as a guest because it's easier than dealing with a log-in
  
  ### Context
  
  k0.10.0
  
  
  ----
  
  cc @jtamiace @rtibbles this seem reasonable?
  
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `kolibri/core/device/serializers.py`
  Content:
  ```
  1 from django.db import transaction
  2 from django.utils.translation import check_for_language
  3 from django.utils.translation import ugettext_lazy as _
  4 from rest_framework import serializers
  5 
  6 from .models import DevicePermissions
  7 from .models import DeviceSettings
  8 from kolibri.core.auth.constants.facility_presets import choices
  9 from kolibri.core.auth.constants.facility_presets import mappings
  10 from kolibri.core.auth.constants.role_kinds import ADMIN
  11 from kolibri.core.auth.models import Facility
  12 from kolibri.core.auth.models import FacilityUser
  13 from kolibri.core.auth.serializers import FacilitySerializer
  14 from kolibri.core.auth.serializers import FacilityUserSerializer
  15 
  16 
  17 class DevicePermissionsSerializer(serializers.ModelSerializer):
  18 
  19     user = serializers.PrimaryKeyRelatedField(queryset=FacilityUser.objects.all())
  20 
  21     class Meta:
  22         model = DevicePermissions
  23         fields = (
  24             'user', 'is_superuser', 'can_manage_content',
  25         )
  26 
  27 
  28 class NoFacilityFacilityUserSerializer(FacilityUserSerializer):
  29 
  30     class Meta:
  31         model = FacilityUser
  32         fields = ('id', 'username', 'full_name', 'password', )
  33 
  34 
  35 class DeviceProvisionSerializer(serializers.Serializer):
  36     facility = FacilitySerializer()
  37     preset = serializers.ChoiceField(choices=choices)
  38     superuser = NoFacilityFacilityUserSerializer()
  39     language_id = serializers.CharField(max_length=15)
  40 
  41     class Meta:
  42         fields = ('facility', 'dataset', 'superuser', 'language_id')
  43 
  44     def validate_language_id(self, language_id):
  45         """
  46         Check that the language_id is supported by Kolibri
  47         """
  48         if not check_for_language(language_id):
  49             raise serializers.ValidationError(_("Language is not supported by Kolibri"))
  50         return language_id
  51 
  52     def create(self, validated_data):
  53         """
  54         Endpoint for initial setup of a device.
  55         Expects a value for:
  56         default language - the default language of this Kolibri device
  57         facility - the required fields for setting up a facility
  58         facilitydataset - facility configuration options
  59         superuser - the required fields for a facilityuser who will be set as the super user for this device
  60         """
  61         with transaction.atomic():
  62             facility = Facility.objects.create(**validated_data.pop('facility'))
  63             preset = validated_data.pop('preset')
  64             dataset_data = mappings[preset]
  65             for key, value in dataset_data.items():
  66                 setattr(facility.dataset, key, value)
  67             facility.dataset.save()
  68             superuser_data = validated_data.pop('superuser')
  69             superuser_data['facility'] = facility
  70             superuser = FacilityUserSerializer(data=superuser_data).create(superuser_data)
  71             superuser.set_password(superuser_data["password"])
  72             superuser.save()
  73             facility.add_role(superuser, ADMIN)
  74             DevicePermissions.objects.create(user=superuser, is_superuser=True)
  75             language_id = validated_data.pop('language_id')
  76             device_settings, created = DeviceSettings.objects.get_or_create()
  77             device_settings.is_provisioned = True
  78             device_settings.language_id = language_id
  79             device_settings.default_facility = facility
  80             device_settings.save()
  81             return {
  82                 "facility": facility,
  83                 "preset": preset,
  84                 "superuser": superuser,
  85                 "language_id": language_id
  86             }
  87
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/kolibri/core/device/serializers.py b/kolibri/core/device/serializers.py
  --- a/kolibri/core/device/serializers.py
  +++ b/kolibri/core/device/serializers.py
  @@ -37,9 +37,10 @@
       preset = serializers.ChoiceField(choices=choices)
       superuser = NoFacilityFacilityUserSerializer()
       language_id = serializers.CharField(max_length=15)
  +    settings = serializers.JSONField()
   
       class Meta:
  -        fields = ('facility', 'dataset', 'superuser', 'language_id')
  +        fields = ('facility', 'dataset', 'superuser', 'language_id', 'settings',)
   
       def validate_language_id(self, language_id):
           """
  @@ -64,6 +65,10 @@
               dataset_data = mappings[preset]
               for key, value in dataset_data.items():
                   setattr(facility.dataset, key, value)
  +            # overwrite the settings in dataset_data with validated_data.settings
  +            custom_settings = validated_data.pop('settings')
  +            for key, value in custom_settings.items():
  +                setattr(facility.dataset, key, value)
               facility.dataset.save()
               superuser_data = validated_data.pop('superuser')
               superuser_data['facility'] = facility
  @@ -82,5 +87,6 @@
                   "facility": facility,
                   "preset": preset,
                   "superuser": superuser,
  -                "language_id": language_id
  +                "language_id": language_id,
  +                "settings": custom_settings,
               }
  
verification_info: {"golden_diff": "diff --git a/kolibri/core/device/serializers.py b/kolibri/core/device/serializers.py\n--- a/kolibri/core/device/serializers.py\n+++ b/kolibri/core/device/serializers.py\n@@ -37,9 +37,10 @@\n     preset = serializers.ChoiceField(choices=choices)\n     superuser = NoFacilityFacilityUserSerializer()\n     language_id = serializers.CharField(max_length=15)\n+    settings = serializers.JSONField()\n \n     class Meta:\n-        fields = ('facility', 'dataset', 'superuser', 'language_id')\n+        fields = ('facility', 'dataset', 'superuser', 'language_id', 'settings',)\n \n     def validate_language_id(self, language_id):\n         \"\"\"\n@@ -64,6 +65,10 @@\n             dataset_data = mappings[preset]\n             for key, value in dataset_data.items():\n                 setattr(facility.dataset, key, value)\n+            # overwrite the settings in dataset_data with validated_data.settings\n+            custom_settings = validated_data.pop('settings')\n+            for key, value in custom_settings.items():\n+                setattr(facility.dataset, key, value)\n             facility.dataset.save()\n             superuser_data = validated_data.pop('superuser')\n             superuser_data['facility'] = facility\n@@ -82,5 +87,6 @@\n                 \"facility\": facility,\n                 \"preset\": preset,\n                 \"superuser\": superuser,\n-                \"language_id\": language_id\n+                \"language_id\": language_id,\n+                \"settings\": custom_settings,\n             }\n", "issue": "add facility setting to disable 'guest' browsing\n\r\n### Observed behavior\r\n\r\nSome organizations prefer that their users always be logged in\r\n\r\n### Expected behavior\r\n\r\nA facility setting can be added, e.g. \"Disable guest access\".\r\n\r\nWhen this setting is active:\r\n\r\n* \"Access as guest\" is hidden on the log-in page\r\n* When a user visits a page without an active session, they are redirected to the log-in page\r\n\r\n### User-facing consequences\r\n\r\nSometimes students are accessing as a guest because it's easier than dealing with a log-in\r\n\r\n### Context\r\n\r\nk0.10.0\r\n\r\n\r\n----\r\n\r\ncc @jtamiace @rtibbles this seem reasonable?\r\n\r\n\n", "before_files": [{"content": "from django.db import transaction\nfrom django.utils.translation import check_for_language\nfrom django.utils.translation import ugettext_lazy as _\nfrom rest_framework import serializers\n\nfrom .models import DevicePermissions\nfrom .models import DeviceSettings\nfrom kolibri.core.auth.constants.facility_presets import choices\nfrom kolibri.core.auth.constants.facility_presets import mappings\nfrom kolibri.core.auth.constants.role_kinds import ADMIN\nfrom kolibri.core.auth.models import Facility\nfrom kolibri.core.auth.models import FacilityUser\nfrom kolibri.core.auth.serializers import FacilitySerializer\nfrom kolibri.core.auth.serializers import FacilityUserSerializer\n\n\nclass DevicePermissionsSerializer(serializers.ModelSerializer):\n\n    user = serializers.PrimaryKeyRelatedField(queryset=FacilityUser.objects.all())\n\n    class Meta:\n        model = DevicePermissions\n        fields = (\n            'user', 'is_superuser', 'can_manage_content',\n        )\n\n\nclass NoFacilityFacilityUserSerializer(FacilityUserSerializer):\n\n    class Meta:\n        model = FacilityUser\n        fields = ('id', 'username', 'full_name', 'password', )\n\n\nclass DeviceProvisionSerializer(serializers.Serializer):\n    facility = FacilitySerializer()\n    preset = serializers.ChoiceField(choices=choices)\n    superuser = NoFacilityFacilityUserSerializer()\n    language_id = serializers.CharField(max_length=15)\n\n    class Meta:\n        fields = ('facility', 'dataset', 'superuser', 'language_id')\n\n    def validate_language_id(self, language_id):\n        \"\"\"\n        Check that the language_id is supported by Kolibri\n        \"\"\"\n        if not check_for_language(language_id):\n            raise serializers.ValidationError(_(\"Language is not supported by Kolibri\"))\n        return language_id\n\n    def create(self, validated_data):\n        \"\"\"\n        Endpoint for initial setup of a device.\n        Expects a value for:\n        default language - the default language of this Kolibri device\n        facility - the required fields for setting up a facility\n        facilitydataset - facility configuration options\n        superuser - the required fields for a facilityuser who will be set as the super user for this device\n        \"\"\"\n        with transaction.atomic():\n            facility = Facility.objects.create(**validated_data.pop('facility'))\n            preset = validated_data.pop('preset')\n            dataset_data = mappings[preset]\n            for key, value in dataset_data.items():\n                setattr(facility.dataset, key, value)\n            facility.dataset.save()\n            superuser_data = validated_data.pop('superuser')\n            superuser_data['facility'] = facility\n            superuser = FacilityUserSerializer(data=superuser_data).create(superuser_data)\n            superuser.set_password(superuser_data[\"password\"])\n            superuser.save()\n            facility.add_role(superuser, ADMIN)\n            DevicePermissions.objects.create(user=superuser, is_superuser=True)\n            language_id = validated_data.pop('language_id')\n            device_settings, created = DeviceSettings.objects.get_or_create()\n            device_settings.is_provisioned = True\n            device_settings.language_id = language_id\n            device_settings.default_facility = facility\n            device_settings.save()\n            return {\n                \"facility\": facility,\n                \"preset\": preset,\n                \"superuser\": superuser,\n                \"language_id\": language_id\n            }\n", "path": "kolibri/core/device/serializers.py"}], "after_files": [{"content": "from django.db import transaction\nfrom django.utils.translation import check_for_language\nfrom django.utils.translation import ugettext_lazy as _\nfrom rest_framework import serializers\n\nfrom .models import DevicePermissions\nfrom .models import DeviceSettings\nfrom kolibri.core.auth.constants.facility_presets import choices\nfrom kolibri.core.auth.constants.facility_presets import mappings\nfrom kolibri.core.auth.constants.role_kinds import ADMIN\nfrom kolibri.core.auth.models import Facility\nfrom kolibri.core.auth.models import FacilityUser\nfrom kolibri.core.auth.serializers import FacilitySerializer\nfrom kolibri.core.auth.serializers import FacilityUserSerializer\n\n\nclass DevicePermissionsSerializer(serializers.ModelSerializer):\n\n    user = serializers.PrimaryKeyRelatedField(queryset=FacilityUser.objects.all())\n\n    class Meta:\n        model = DevicePermissions\n        fields = (\n            'user', 'is_superuser', 'can_manage_content',\n        )\n\n\nclass NoFacilityFacilityUserSerializer(FacilityUserSerializer):\n\n    class Meta:\n        model = FacilityUser\n        fields = ('id', 'username', 'full_name', 'password', )\n\n\nclass DeviceProvisionSerializer(serializers.Serializer):\n    facility = FacilitySerializer()\n    preset = serializers.ChoiceField(choices=choices)\n    superuser = NoFacilityFacilityUserSerializer()\n    language_id = serializers.CharField(max_length=15)\n    settings = serializers.JSONField()\n\n    class Meta:\n        fields = ('facility', 'dataset', 'superuser', 'language_id', 'settings',)\n\n    def validate_language_id(self, language_id):\n        \"\"\"\n        Check that the language_id is supported by Kolibri\n        \"\"\"\n        if not check_for_language(language_id):\n            raise serializers.ValidationError(_(\"Language is not supported by Kolibri\"))\n        return language_id\n\n    def create(self, validated_data):\n        \"\"\"\n        Endpoint for initial setup of a device.\n        Expects a value for:\n        default language - the default language of this Kolibri device\n        facility - the required fields for setting up a facility\n        facilitydataset - facility configuration options\n        superuser - the required fields for a facilityuser who will be set as the super user for this device\n        \"\"\"\n        with transaction.atomic():\n            facility = Facility.objects.create(**validated_data.pop('facility'))\n            preset = validated_data.pop('preset')\n            dataset_data = mappings[preset]\n            for key, value in dataset_data.items():\n                setattr(facility.dataset, key, value)\n            # overwrite the settings in dataset_data with validated_data.settings\n            custom_settings = validated_data.pop('settings')\n            for key, value in custom_settings.items():\n                setattr(facility.dataset, key, value)\n            facility.dataset.save()\n            superuser_data = validated_data.pop('superuser')\n            superuser_data['facility'] = facility\n            superuser = FacilityUserSerializer(data=superuser_data).create(superuser_data)\n            superuser.set_password(superuser_data[\"password\"])\n            superuser.save()\n            facility.add_role(superuser, ADMIN)\n            DevicePermissions.objects.create(user=superuser, is_superuser=True)\n            language_id = validated_data.pop('language_id')\n            device_settings, created = DeviceSettings.objects.get_or_create()\n            device_settings.is_provisioned = True\n            device_settings.language_id = language_id\n            device_settings.default_facility = facility\n            device_settings.save()\n            return {\n                \"facility\": facility,\n                \"preset\": preset,\n                \"superuser\": superuser,\n                \"language_id\": language_id,\n                \"settings\": custom_settings,\n            }\n", "path": "kolibri/core/device/serializers.py"}]}