def index(request): # Get the current user's Business Units user = request.user # Count the number of users. If there is only one, they need to be made a GA if User.objects.count() == 1: # The first user created by syncdb won't have a profile. If there isn't # one, make sure they get one. try: profile = UserProfile.objects.get(user=user) except UserProfile.DoesNotExist: profile = UserProfile(user=user) profile.level = ProfileLevel.global_admin profile.save() user_is_ga = is_global_admin(user) if not user_is_ga: # user has many BU's display them all in a friendly manner if user.businessunit_set.count() == 0: c = { 'user': request.user, } return render(request, 'server/no_access.html', c) if user.businessunit_set.count() == 1: # user only has one BU, redirect to it return redirect('bu_dashboard', bu_id=user.businessunit_set.all()[0].id) # Load in the default plugins if needed utils.load_default_plugins() plugins = sal.plugin.PluginManager().get_all_plugins() reports = utils.get_report_names(plugins) output = utils.get_plugin_placeholder_markup(plugins) # If the user is GA level, and hasn't decided on a data sending # choice, template will reflect this. data_choice = False if ( user_is_ga and utils.get_setting('send_data') is None) else True # get the user level - if they're a global admin, show all of the # machines. If not, show only the machines they have access to if user_is_ga: business_units = BusinessUnit.objects.all() else: business_units = user.businessunit_set.all() context = { 'user': request.user, 'business_units': business_units, 'output': output, 'data_setting_decided': data_choice, 'reports': reports } context.update(utils.check_version()) return render(request, 'server/index.html', context)
def index(request): # Get the current user's Business Units user = request.user # Count the number of users. If there is only one, they need to be made a GA if User.objects.count() == 1: # The first user created by syncdb won't have a profile. If there isn't # one, make sure they get one. try: profile = UserProfile.objects.get(user=user) except UserProfile.DoesNotExist: profile = UserProfile(user=user) profile.level = ProfileLevel.global_admin profile.save() user_is_ga = is_global_admin(user) if not user_is_ga: # user has many BU's display them all in a friendly manner if user.businessunit_set.count() == 0: c = {'user': request.user, } return render(request, 'server/no_access.html', c) if user.businessunit_set.count() == 1: # user only has one BU, redirect to it return redirect('bu_dashboard', bu_id=user.businessunit_set.all()[0].id) # Load in the default plugins if needed utils.load_default_plugins() plugins = sal.plugin.PluginManager().get_all_plugins() reports = utils.get_report_names(plugins) output = utils.get_plugin_placeholder_markup(plugins) # If the user is GA level, and hasn't decided on a data sending # choice, template will reflect this. data_choice = False if (user_is_ga and utils.get_setting('send_data') is None) else True # get the user level - if they're a global admin, show all of the # machines. If not, show only the machines they have access to if user_is_ga: business_units = BusinessUnit.objects.all() else: business_units = user.businessunit_set.all() context = { 'user': request.user, 'business_units': business_units, 'output': output, 'data_setting_decided': data_choice, 'reports': reports} context.update(utils.check_version()) return render(request, 'server/index.html', context)
def get_queryset(self, request, **kwargs): """Get a filtered queryset for this plugin's Machine model. Filters machines to only members of the passed group. Filters undeployed machines if deployed is True. Filters out machines that don't match this plugin's supported_os_families. Args: request (Request): The request passed from the View. **kwargs: Expected kwargs follow group_type (str): One of 'all' (the default), 'business_unit', or 'machine_group'. group_id (int, str): ID of the group_type's object to filter by. Default to 0. deployed (bool): Filter by Machine.deployed. Defaults to True. Returns: Filtered queryset. """ group_type = kwargs.get('group_type', 'all') group_id = kwargs.get('group_id', 0) # Check access before doing anything else. handle_access(request, group_type, group_id) queryset = self.model.objects.filter( os_family__in=self.get_supported_os_families()) # By default, plugins filter out undeployed machines. if self.only_use_deployed_machines: queryset = self.model.objects.filter(deployed=True) if group_type == "business_unit": queryset = queryset.filter( machine_group__business_unit__pk=group_id) elif group_type == "machine_group": queryset = queryset.filter(machine_group__pk=group_id) elif is_global_admin(request.user): # GA users won't have business units, so just do nothing. pass else: # The 'all' / 'front' type is being requested. queryset = queryset.filter(machine_group__business_unit__in=request .user.businessunit_set.all()) return queryset
def get_queryset(self, request, **kwargs): """Get a filtered queryset for this plugin's Machine model. Filters machines to only members of the passed group. Filters undeployed machines if deployed is True. Filters out machines that don't match this plugin's supported_os_families. Args: request (Request): The request passed from the View. **kwargs: Expected kwargs follow group_type (str): One of 'all' (the default), 'business_unit', or 'machine_group'. group_id (int, str): ID of the group_type's object to filter by. Default to 0. deployed (bool): Filter by Machine.deployed. Defaults to True. Returns: Filtered queryset. """ group_type = kwargs.get('group_type', 'all') group_id = kwargs.get('group_id', 0) # Check access before doing anything else. handle_access(request, group_type, group_id) queryset = self.model.objects.filter(os_family__in=self.get_supported_os_families()) # By default, plugins filter out undeployed machines. if self.only_use_deployed_machines: queryset = self.model.objects.filter(deployed=True) if group_type == "business_unit": queryset = queryset.filter(machine_group__business_unit__pk=group_id) elif group_type == "machine_group": queryset = queryset.filter(machine_group__pk=group_id) elif is_global_admin(request.user): # GA users won't have business units, so just do nothing. pass else: # The 'all' / 'front' type is being requested. queryset = queryset.filter( machine_group__business_unit__in=request.user.businessunit_set.all()) return queryset
def test_is_global_admin(self): self.assertTrue(is_global_admin(self.ga_user)) self.assertFalse(is_global_admin(self.user))