def test_authenticated_user_has_perm_on_project_related_object(): user1 = factories.UserFactory() project = factories.ProjectFactory() project.public_permissions = ["test"] us = factories.UserStoryFactory(project=project) assert services.user_has_perm(user1, "test", us) is True assert services.user_has_perm(user1, "fail", us) is False
def list(self, request, **kwargs): text = request.QUERY_PARAMS.get('text', "") project_id = request.QUERY_PARAMS.get('project', None) project = self._get_project(project_id) result = {} with futures.ThreadPoolExecutor(max_workers=4) as executor: futures_list = [] if user_has_perm(request.user, "view_epics", project): epics_future = executor.submit(self._search_epics, project, text) epics_future.result_key = "epics" futures_list.append(epics_future) if user_has_perm(request.user, "view_us", project): uss_future = executor.submit(self._search_user_stories, project, text) uss_future.result_key = "userstories" futures_list.append(uss_future) if user_has_perm(request.user, "view_tasks", project): tasks_future = executor.submit(self._search_tasks, project, text) tasks_future.result_key = "tasks" futures_list.append(tasks_future) if user_has_perm(request.user, "view_issues", project): issues_future = executor.submit(self._search_issues, project, text) issues_future.result_key = "issues" futures_list.append(issues_future) if user_has_perm(request.user, "view_wiki_pages", project): wiki_pages_future = executor.submit(self._search_wiki_pages, project, text) wiki_pages_future.result_key = "wikipages" futures_list.append(wiki_pages_future) for future in futures.as_completed(futures_list): data = [] try: data = future.result() except Exception as exc: print('%s generated an exception: %s' % (future.result_key, exc)) finally: result[future.result_key] = data result["count"] = sum(map(lambda x: len(x), result.values())) return response.Ok(result)
def _filter_by_permissions(obj, user): UserStory = apps.get_model("userstories", "UserStory") Issue = apps.get_model("issues", "Issue") Task = apps.get_model("tasks", "Task") Epic = apps.get_model("epics", "Epic") WikiPage = apps.get_model("wiki", "WikiPage") if isinstance(obj, UserStory): return user_has_perm(user, "view_us", obj, cache="project") elif isinstance(obj, Issue): return user_has_perm(user, "view_issues", obj, cache="project") elif isinstance(obj, Task): return user_has_perm(user, "view_tasks", obj, cache="project") elif isinstance(obj, Epic): return user_has_perm(user, "view_epics", obj, cache="project") elif isinstance(obj, WikiPage): return user_has_perm(user, "view_wiki_pages", obj, cache="project") return False
def get_allowed_sections(obj): sections = [Section.timeline] active_modules = { 'epics': 'view_epics', 'backlog': 'view_us', 'kanban': 'view_us', 'wiki': 'view_wiki_pages', 'issues': 'view_issues' } for key in active_modules: module_name = "is_{}_activated".format(key) if getattr(obj.project, module_name) and \ user_has_perm(obj.user, active_modules[key], obj.project): sections.append(getattr(Section, key)) return sections
def test_authenticated_user_has_perm_on_invalid_object(): user1 = factories.UserFactory() assert services.user_has_perm(user1, "test", user1) is False
def test_anon_user_has_perm(): project = factories.ProjectFactory() project.anon_permissions = ["test"] assert services.user_has_perm(AnonymousUser(), "test", project) is True assert services.user_has_perm(AnonymousUser(), "fail", project) is False
def check_permissions(self, request, view, obj=None): return user_has_perm(request.user, self.project_perm, obj)
def list(self, request, **kwargs): validator = ResolverValidator(data=request.QUERY_PARAMS) if not validator.is_valid(): raise exc.BadRequest(validator.errors) data = validator.data project_model = apps.get_model("projects", "Project") project = get_object_or_404(project_model, slug=data["project"]) self.check_permissions(request, "list", project) result = {"project": project.pk} if data["epic"] and user_has_perm(request.user, "view_epics", project): result["epic"] = get_object_or_404(project.epics.all(), ref=data["epic"]).pk if data["us"] and user_has_perm(request.user, "view_us", project): result["us"] = get_object_or_404(project.user_stories.all(), ref=data["us"]).pk if data["task"] and user_has_perm(request.user, "view_tasks", project): result["task"] = get_object_or_404(project.tasks.all(), ref=data["task"]).pk if data["issue"] and user_has_perm(request.user, "view_issues", project): result["issue"] = get_object_or_404(project.issues.all(), ref=data["issue"]).pk if data["milestone"] and user_has_perm(request.user, "view_milestones", project): result["milestone"] = get_object_or_404(project.milestones.all(), slug=data["milestone"]).pk if data["wikipage"] and user_has_perm(request.user, "view_wiki_pages", project): result["wikipage"] = get_object_or_404(project.wiki_pages.all(), slug=data["wikipage"]).pk if data["ref"]: ref_found = False # No need to continue once one ref is found try: value = int(data["ref"]) if user_has_perm(request.user, "view_epics", project): epic = project.epics.filter(ref=value).first() if epic: result["epic"] = epic.pk ref_found = True if ref_found is False and user_has_perm( request.user, "view_us", project): us = project.user_stories.filter(ref=value).first() if us: result["us"] = us.pk ref_found = True if ref_found is False and user_has_perm( request.user, "view_tasks", project): task = project.tasks.filter(ref=value).first() if task: result["task"] = task.pk ref_found = True if ref_found is False and user_has_perm( request.user, "view_issues", project): issue = project.issues.filter(ref=value).first() if issue: result["issue"] = issue.pk except: value = data["ref"] if user_has_perm(request.user, "view_wiki_pages", project): wiki_page = project.wiki_pages.filter(slug=value).first() if wiki_page: result["wikipage"] = wiki_page.pk return response.Ok(result)