def test_is_anvil_authenticated(self, mock_is_google_authenticated, mock_anvil_enabled): mock_is_google_authenticated.return_value = True mock_anvil_enabled.return_value = True user = User.objects.get(username='******') r = is_anvil_authenticated(user) self.assertTrue(r) mock_is_google_authenticated.return_value = False r = is_anvil_authenticated(user) self.assertFalse(r) mock_is_google_authenticated.return_value = True mock_anvil_enabled.return_value = False r = is_anvil_authenticated(user) self.assertFalse(r)
def get_project_guids_user_can_view(user): cache_key = 'projects__{}'.format(user) project_guids = safe_redis_get_json(cache_key) if project_guids is not None: return project_guids is_data_manager = user_is_data_manager(user) if is_data_manager: projects = Project.objects.all() else: projects = get_local_access_projects(user) project_guids = [p.guid for p in projects.only('guid')] if is_anvil_authenticated(user) and not is_data_manager: workspaces = [ '/'.join([ws['workspace']['namespace'], ws['workspace']['name']]) for ws in list_anvil_workspaces(user) ] project_guids += [ p.guid for p in Project.objects.filter( workspace_name__isnull=False).exclude( workspace_name='').exclude( guid__in=project_guids).annotate( workspace=Concat('workspace_namespace', Value( '/'), 'workspace_name')).filter( workspace__in=workspaces).only('guid') ] safe_redis_set_json(cache_key, project_guids, expire=TERRA_WORKSPACE_CACHE_EXPIRE_SECONDS) return project_guids
def test_is_anvil_authenticated(self, mock_social_auth_key, mock_terra_url): mock_social_auth_key.__bool__.return_value = False mock_terra_url.__bool__.return_value = True user = User.objects.get(username='******') r = is_anvil_authenticated(user) self.assertFalse(r) mock_social_auth_key.__bool__.return_value = True mock_terra_url.__bool__.return_value = False r = is_anvil_authenticated(user) self.assertFalse(r) mock_terra_url.__bool__.return_value = True r = is_anvil_authenticated(user) self.assertTrue(r) remove_token(user) r = is_anvil_authenticated(user) self.assertFalse(r)
def get_projects_user_can_view(user): if user_is_data_manager(user): return Project.objects.all() projects = Project.objects.filter(can_view_group__user=user) if user_is_analyst(user): projects = (projects | _get_analyst_projects()) if is_anvil_authenticated(user): workspaces = ['/'.join([ws['workspace']['namespace'], ws['workspace']['name']]) for ws in list_anvil_workspaces(user)] anvil_permitted_projects = Project.objects.annotate( workspace = Concat('workspace_namespace', Value('/'), 'workspace_name')).filter(workspace__in=workspaces) return (anvil_permitted_projects | projects).distinct() else: return projects.distinct()
for field in model_class._meta.json_fields } MAIN_USER_FIELDS = [ 'username', 'email', 'first_name', 'last_name', 'last_login', 'date_joined', 'id' ] BOOL_USER_FIELDS = { 'is_superuser': False, 'is_active': True, } MODEL_USER_FIELDS = MAIN_USER_FIELDS + list(BOOL_USER_FIELDS.keys()) COMPUTED_USER_FIELDS = { 'is_anvil': lambda user, is_anvil=None, **kwargs: is_anvil_authenticated(user) if is_anvil is None else is_anvil, 'display_name': lambda user, **kwargs: user.get_full_name(), 'is_analyst': lambda user, analyst_users=None, **kwargs: user in analyst_users if analyst_users is not None else user_is_analyst(user), 'is_data_manager': lambda user, **kwargs: user_is_data_manager(user), 'is_pm': lambda user, pm_users=None, **kwargs: user in pm_users if pm_users is not None else user_is_pm(user), } DEFAULT_USER = {_to_camel_case(field): '' for field in MAIN_USER_FIELDS} DEFAULT_USER.update(
for field in model_class._meta.json_fields } MAIN_USER_FIELDS = [ 'username', 'email', 'first_name', 'last_name', 'last_login', 'date_joined', 'id' ] BOOL_USER_FIELDS = { 'is_superuser': False, 'is_active': True, } MODEL_USER_FIELDS = MAIN_USER_FIELDS + list(BOOL_USER_FIELDS.keys()) COMPUTED_USER_FIELDS = { 'is_anvil': lambda user, is_anvil=None: is_anvil_authenticated(user) if is_anvil is None else is_anvil, 'display_name': lambda user, **kwargs: user.get_full_name(), 'is_analyst': lambda user, **kwargs: user_is_analyst(user), 'is_data_manager': lambda user, **kwargs: user_is_data_manager(user), 'is_pm': lambda user, **kwargs: user_is_pm(user), } DEFAULT_USER = {_to_camel_case(field): '' for field in MAIN_USER_FIELDS} DEFAULT_USER.update( {_to_camel_case(field): val for field, val in BOOL_USER_FIELDS.items()})