def create_superuser_and_provision_device(self, username, dataset_id): # Prompt user to pick a superuser if one does not currently exist while not DevicePermissions.objects.filter(is_superuser=True).exists(): # specify username of account that will become a superuser if not username: username = input( "Please enter username of account that will become the superuser on this device: " ) if not FacilityUser.objects.filter(username=username).exists(): self.stderr.write( "User with username {} does not exist".format(username) ) username = None continue # make the user with the given credentials, a superuser for this device user = FacilityUser.objects.get(username=username, dataset_id=dataset_id) # create permissions for the authorized user DevicePermissions.objects.update_or_create( user=user, defaults={"is_superuser": True, "can_manage_content": True} ) # if device has not been provisioned, set it up if not device_provisioned(): provision_device()
def automatic_provisiondevice(): from kolibri.core.device.utils import device_provisioned from kolibri.dist.django.core.management import call_command from kolibri.utils.conf import KOLIBRI_HOME AUTOMATIC_PROVISION_FILE = os.path.join(KOLIBRI_HOME, "automatic_provision.json") if not os.path.exists(AUTOMATIC_PROVISION_FILE): return elif device_provisioned(): return try: with open(AUTOMATIC_PROVISION_FILE, "r") as f: logging.info( "Running provisiondevice from 'automatic_provision.json'") options = json.load(f) except ValueError as e: logging.error( "Attempted to load 'automatic_provision.json' but failed to parse JSON:\n{}" .format(e)) except FileNotFoundError: options = None if isinstance(options, Mapping): options.setdefault("superusername", None) options.setdefault("superuserpassword", None) options.setdefault("preset", "nonformal") options.setdefault("language_id", None) options.setdefault("facility_settings", {}) options.setdefault("device_settings", {}) call_command("provisiondevice", interactive=False, **options)
def provision_single_user_device(user_id): user = FacilityUser.objects.get(id=user_id) # if device has not been provisioned, set it up if not device_provisioned(): provision_device(default_facility=user.facility) DevicePermissions.objects.get_or_create(user=user, defaults={ "is_superuser": False, "can_manage_content": True })
def create_superuser_and_provision_device(username, dataset_id, noninteractive=False): facility = Facility.objects.get(dataset_id=dataset_id) # if device has not been provisioned, set it up if not device_provisioned(): device_settings, created = DeviceSettings.objects.get_or_create() device_settings.is_provisioned = True device_settings.default_facility = facility device_settings.save() # Prompt user to pick a superuser if one does not currently exist while not DevicePermissions.objects.filter(is_superuser=True).exists(): # specify username of account that will become a superuser if not username: if ( noninteractive ): # we don't want to setup a device without a superuser, so create a temporary one superuser = FacilityUser.objects.create(username="******", facility=facility) superuser.set_password("password") superuser.save() DevicePermissions.objects.create(user=superuser, is_superuser=True, can_manage_content=True) print( "Temporary superuser with username: `superuser` and password: `password` created" ) return username = input( "Please enter username of account that will become the superuser on this device: " ) if not FacilityUser.objects.filter(username=username).exists(): print( "User with username `{}` does not exist on this device".format( username)) username = None continue # make the user with the given credentials, a superuser for this device user = FacilityUser.objects.get(username=username, dataset_id=dataset_id) # create permissions for the authorized user DevicePermissions.objects.update_or_create(user=user, defaults={ "is_superuser": True, "can_manage_content": True })
def process_request(self, request): # If a DevicePermissions with is_superuser has already been created, no need to do anything here self.device_provisioned = self.device_provisioned or device_provisioned() if self.device_provisioned: if request.path.startswith(reverse("kolibri:setupwizardplugin:setupwizard")): return redirect(reverse("kolibri:learnplugin:learn")) return # Don't redirect for URLs that are required for the setup wizard allowed_paths = [reverse(name) for name in ALLOWED_PATH_LIST] if any(request.path.startswith(path_prefix) for path_prefix in allowed_paths): return # If we've gotten this far, we want to redirect to the setup wizard return redirect(reverse("kolibri:setupwizardplugin:setupwizard"))
def create_superuser_and_provision_device(self, username, dataset_id): # Prompt user to pick a superuser if one does not currently exist while not DevicePermissions.objects.filter(is_superuser=True).exists(): # specify username of account that will become a superuser if not username: username = input('Please enter username of account that will become the superuser on this device: ') if not FacilityUser.objects.filter(username=username).exists(): print("User with username {} does not exist".format(username)) username = None continue # make the user with the given credentials, a superuser for this device user = FacilityUser.objects.get(username=username, dataset_id=dataset_id) # create permissions for the authorized user DevicePermissions.objects.update_or_create(user=user, defaults={'is_superuser': True, 'can_manage_content': True}) # if device has not been provisioned, set it up if not device_provisioned(): device_settings, created = DeviceSettings.objects.get_or_create() device_settings.is_provisioned = True device_settings.save()
def has_permission(self, request, view): from kolibri.core.device.utils import device_provisioned return not device_provisioned()
def is_provisioned(): # First check if the device has been provisioned global device_is_provisioned device_is_provisioned = device_is_provisioned or device_provisioned() return device_is_provisioned
def dispatch(self, *args, **kwargs): if device_provisioned(): return redirect(reverse("kolibri:core:redirect_user")) return super(SetupWizardView, self).dispatch(*args, **kwargs)