def test_make_app_admin_only(): h.set_context("test", "wiki", neighborhood="Projects") anon = M.User.anonymous() dev = M.User.query.get(username="******") admin = M.User.query.get(username="******") c.project.add_user(dev, ["Developer"]) ThreadLocalORMSession.flush_all() Credentials.get().clear() assert has_access(c.app, "read", user=anon)() assert has_access(c.app, "read", user=dev)() assert has_access(c.app, "read", user=admin)() assert not has_access(c.app, "create", user=anon)() assert has_access(c.app, "create", user=dev)() assert has_access(c.app, "create", user=admin)() assert c.app.is_visible_to(anon) assert c.app.is_visible_to(dev) assert c.app.is_visible_to(admin) h.make_app_admin_only(c.app) ThreadLocalORMSession.flush_all() Credentials.get().clear() assert not has_access(c.app, "read", user=anon)() assert not has_access(c.app, "read", user=dev)() assert has_access(c.app, "read", user=admin)() assert not has_access(c.app, "create", user=anon)() assert not has_access(c.app, "create", user=dev)() assert has_access(c.app, "create", user=admin)() assert not c.app.is_visible_to(anon) assert not c.app.is_visible_to(dev) assert c.app.is_visible_to(admin)
def test_make_app_admin_only(): h.set_context('test', 'wiki', neighborhood='Projects') anon = M.User.anonymous() dev = M.User.query.get(username='******') admin = M.User.query.get(username='******') c.project.add_user(dev, ['Developer']) ThreadLocalORMSession.flush_all() Credentials.get().clear() assert has_access(c.app, 'read', user=anon)() assert has_access(c.app, 'read', user=dev)() assert has_access(c.app, 'read', user=admin)() assert not has_access(c.app, 'create', user=anon)() assert has_access(c.app, 'create', user=dev)() assert has_access(c.app, 'create', user=admin)() assert c.app.is_visible_to(anon) assert c.app.is_visible_to(dev) assert c.app.is_visible_to(admin) h.make_app_admin_only(c.app) ThreadLocalORMSession.flush_all() Credentials.get().clear() assert not has_access(c.app, 'read', user=anon)() assert not has_access(c.app, 'read', user=dev)() assert has_access(c.app, 'read', user=admin)() assert not has_access(c.app, 'create', user=anon)() assert not has_access(c.app, 'create', user=dev)() assert has_access(c.app, 'create', user=admin)() assert not c.app.is_visible_to(anon) assert not c.app.is_visible_to(dev) assert c.app.is_visible_to(admin)
def import_tool( self, project, user, project_name=None, mount_point=None, mount_label=None, user_name=None, tool_option=None, **kw): """ Import a GitHub wiki into a new Wiki Allura tool. """ project_name = "%s/%s" % (user_name, project_name) extractor = GitHubProjectExtractor(project_name, user=user) wiki_avail = extractor.has_wiki() if not wiki_avail: return self.github_wiki_url = extractor.get_page_url( 'wiki_url').replace('.wiki', '/wiki') self.app = project.install_app( "Wiki", mount_point=mount_point or 'wiki', mount_label=mount_label or 'Wiki', import_id={ 'source': self.source, 'project_name': project_name, } ) with_history = tool_option == 'import_history' ThreadLocalORMSession.flush_all() self.github_markdown_converter = GitHubMarkdownConverter( user_name, project_name) try: M.session.artifact_orm_session._get().skip_mod_date = True with h.push_config(c, app=self.app): try: wiki_url = extractor.get_page_url('wiki_url') self.import_pages(wiki_url, history=with_history) except git.GitCommandError: log.error( 'Unable to clone GitHub wiki: ' 'wiki_url=%s; ' 'wiki_avail=%s; ' 'avail_url=%s', wiki_url, wiki_avail, extractor.get_page_url('project_info'), exc_info=True) raise ThreadLocalORMSession.flush_all() M.AuditLog.log( 'import tool %s from %s on %s' % ( self.app.config.options.mount_point, project_name, self.source), project=project, user=user, url=self.app.url) g.post_event('project_updated') return self.app except Exception: h.make_app_admin_only(self.app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool(self, project, user, project_name, mount_point=None, mount_label=None, **kw): import_id_converter = ImportIdConverter.get() app = project.install_app( 'tickets', mount_point, mount_label, EnableVoting=True, open_status_names='New Accepted Started', closed_status_names='Fixed Verified Invalid Duplicate WontFix Done', import_id={ 'source': self.source, 'project_name': project_name, }, ) ThreadLocalORMSession.flush_all() try: M.session.artifact_orm_session._get().skip_mod_date = True with h.push_config(c, user=M.User.anonymous(), app=app): for ticket_num, issue in GoogleCodeProjectExtractor.iter_issues( project_name): self.max_ticket_num = max(ticket_num, self.max_ticket_num) ticket = TM.Ticket(app_config_id=app.config._id, custom_fields=dict(), ticket_num=ticket_num, import_id=import_id_converter.expand( ticket_num, app)) self.process_fields(ticket, issue) self.process_labels(ticket, issue) self.process_comments(ticket, issue) session(ticket).flush(ticket) session(ticket).expunge(ticket) app.globals.custom_fields = self.postprocess_custom_fields() app.globals.last_ticket_num = self.max_ticket_num ThreadLocalORMSession.flush_all() M.AuditLog.log( 'import tool %s from %s on %s' % ( app.config.options.mount_point, project_name, self.source, ), project=project, user=user, url=app.url, ) g.post_event('project_updated') app.globals.invalidate_bin_counts() return app except Exception as e: h.make_app_admin_only(app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool(self, project, user, project_name=None, mount_point=None, mount_label=None, trac_url=None, user_map=None, **kw): """ Import Trac tickets into a new Allura Tracker tool. """ mount_point = mount_point or 'tickets' app = project.install_app( 'Tickets', mount_point=mount_point, mount_label=mount_label or 'Tickets', open_status_names='new assigned accepted reopened', closed_status_names='closed', import_id={ 'source': self.source, 'trac_url': trac_url, }, ) session(app.config).flush(app.config) session(app.globals).flush(app.globals) try: with h.push_config(c, app=app): TracImportSupport().perform_import( json.dumps(export(trac_url), cls=DateJSONEncoder), json.dumps({ 'user_map': json.loads(user_map) if user_map else {}, 'usernames_match': self.usernames_match(trac_url), }), ) AuditLog.log( 'import tool %s from %s' % ( app.config.options.mount_point, trac_url, ), project=project, user=user, url=app.url, ) g.post_event('project_updated') return app except Exception: h.make_app_admin_only(app) raise
def import_tool(self, project, user, project_name, mount_point=None, mount_label=None, **kw): import_id_converter = ImportIdConverter.get() app = project.install_app('tickets', mount_point, mount_label, EnableVoting=True, open_status_names='New Accepted Started', closed_status_names='Fixed Verified Invalid Duplicate WontFix Done', import_id={ 'source': self.source, 'project_name': project_name, }, ) ThreadLocalORMSession.flush_all() try: M.session.artifact_orm_session._get().skip_mod_date = True with h.push_config(c, user=M.User.anonymous(), app=app): for ticket_num, issue in GoogleCodeProjectExtractor.iter_issues(project_name): self.max_ticket_num = max(ticket_num, self.max_ticket_num) ticket = TM.Ticket( app_config_id=app.config._id, custom_fields=dict(), ticket_num=ticket_num, import_id=import_id_converter.expand(ticket_num, app)) self.process_fields(ticket, issue) self.process_labels(ticket, issue) self.process_comments(ticket, issue) session(ticket).flush(ticket) session(ticket).expunge(ticket) app.globals.custom_fields = self.postprocess_custom_fields() app.globals.last_ticket_num = self.max_ticket_num ThreadLocalORMSession.flush_all() M.AuditLog.log( 'import tool %s from %s on %s' % ( app.config.options.mount_point, project_name, self.source, ), project=project, user=user, url=app.url, ) g.post_event('project_updated') app.globals.invalidate_bin_counts() return app except Exception: h.make_app_admin_only(app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool(self, project, user, project_name=None, mount_point=None, mount_label=None, user_name=None, tool_option=None, **kw): """ Import a GitHub wiki into a new Wiki Allura tool. """ project_name = "%s/%s" % (user_name, project_name) extractor = GitHubProjectExtractor(project_name) if not extractor.has_wiki(): return self.github_wiki_url = extractor.get_page_url('wiki_url').replace( '.wiki', '/wiki') self.app = project.install_app("Wiki", mount_point=mount_point or 'wiki', mount_label=mount_label or 'Wiki', import_id={ 'source': self.source, 'project_name': project_name, }) with_history = tool_option == 'import_history' ThreadLocalORMSession.flush_all() try: M.session.artifact_orm_session._get().skip_mod_date = True with h.push_config(c, app=self.app): self.import_pages(extractor.get_page_url('wiki_url'), history=with_history) ThreadLocalORMSession.flush_all() M.AuditLog.log('import tool %s from %s on %s' % (self.app.config.options.mount_point, project_name, self.source), project=project, user=user, url=self.app.url) g.post_event('project_updated') return self.app except Exception as e: h.make_app_admin_only(self.app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool( self, project, user, project_name=None, mount_point=None, mount_label=None, user_name=None, tool_option=None, **kw ): """ Import a GitHub wiki into a new Wiki Allura tool. """ project_name = "%s/%s" % (user_name, project_name) extractor = GitHubProjectExtractor(project_name, user=user) if not extractor.has_wiki(): return self.github_wiki_url = extractor.get_page_url("wiki_url").replace(".wiki", "/wiki") self.app = project.install_app( "Wiki", mount_point=mount_point or "wiki", mount_label=mount_label or "Wiki", import_id={"source": self.source, "project_name": project_name}, ) with_history = tool_option == "import_history" ThreadLocalORMSession.flush_all() self.github_markdown_converter = GitHubMarkdownConverter(user_name, project_name) try: M.session.artifact_orm_session._get().skip_mod_date = True with h.push_config(c, app=self.app): self.import_pages(extractor.get_page_url("wiki_url"), history=with_history) ThreadLocalORMSession.flush_all() M.AuditLog.log( "import tool %s from %s on %s" % (self.app.config.options.mount_point, project_name, self.source), project=project, user=user, url=self.app.url, ) g.post_event("project_updated") return self.app except Exception as e: h.make_app_admin_only(self.app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool(self, project, user, project_name=None, mount_point=None, mount_label=None, user_name=None, tool_option=None, **kw): """ Import a GitHub wiki into a new Wiki Allura tool. """ project_name = "%s/%s" % (user_name, project_name) extractor = GitHubProjectExtractor(project_name) if not extractor.has_wiki(): return self.github_wiki_url = extractor.get_page_url('wiki_url').replace('.wiki', '/wiki') self.app = project.install_app( "Wiki", mount_point=mount_point or 'wiki', mount_label=mount_label or 'Wiki', import_id={ 'source': self.source, 'project_name': project_name, } ) with_history = tool_option == 'import_history' ThreadLocalORMSession.flush_all() try: M.session.artifact_orm_session._get().skip_mod_date = True with h.push_config(c, app=self.app): self.import_pages(extractor.get_page_url('wiki_url'), history=with_history) ThreadLocalORMSession.flush_all() M.AuditLog.log( 'import tool %s from %s on %s' % ( self.app.config.options.mount_point, project_name, self.source), project=project, user=user, url=self.app.url) g.post_event('project_updated') return self.app except Exception as e: h.make_app_admin_only(self.app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool(self, project, user, project_name=None, mount_point=None, mount_label=None, trac_url=None, user_map=None, **kw): """ Import Trac tickets into a new Allura Tracker tool. """ trac_url = trac_url.rstrip('/') + '/' mount_point = mount_point or 'tickets' app = project.install_app( 'Tickets', mount_point=mount_point, mount_label=mount_label or 'Tickets', open_status_names='new assigned accepted reopened', closed_status_names='closed', import_id={ 'source': self.source, 'trac_url': trac_url, }, ) session(app.config).flush(app.config) session(app.globals).flush(app.globals) try: with h.push_config(c, app=app): TracImportSupport().perform_import( json.dumps(export(trac_url), cls=DateJSONEncoder), json.dumps({ 'user_map': json.loads(user_map) if user_map else {}, 'usernames_match': self.usernames_match(trac_url), }), ) AuditLog.log( 'import tool %s from %s' % ( app.config.options.mount_point, trac_url, ), project=project, user=user, url=app.url, ) g.post_event('project_updated') return app except Exception: h.make_app_admin_only(app) raise
def import_tool( self, project, user, project_name=None, mount_point=None, mount_label=None, trac_url=None, user_map=None, **kw ): """ Import Trac tickets into a new Allura Tracker tool. """ mount_point = mount_point or "tickets" app = project.install_app( "Tickets", mount_point=mount_point, mount_label=mount_label or "Tickets", open_status_names="new assigned accepted reopened", closed_status_names="closed", import_id={"source": self.source, "trac_url": trac_url}, ) session(app.config).flush(app.config) session(app.globals).flush(app.globals) try: with h.push_config(c, app=app): TracImportSupport().perform_import( json.dumps(export(trac_url), cls=DateJSONEncoder), json.dumps( { "user_map": json.loads(user_map) if user_map else {}, "usernames_match": self.usernames_match(trac_url), } ), ) AuditLog.log( "import tool %s from %s" % (app.config.options.mount_point, trac_url), project=project, user=user, url=app.url, ) g.post_event("project_updated") return app except Exception: h.make_app_admin_only(app) raise
def import_tool(self, project, user, mount_point=None, mount_label=None, **kw): import_id_converter = ImportIdConverter.get() tracker_json = self._load_json(project) tracker_json['tracker_config']['options'].pop('ordinal', None) tracker_json['tracker_config']['options'].pop('mount_point', None) tracker_json['tracker_config']['options'].pop('mount_label', None) tracker_json['tracker_config']['options'].pop('import_id', None) app = project.install_app('tickets', mount_point, mount_label, import_id={ 'source': self.source, 'app_config_id': tracker_json['tracker_config']['_id'], }, open_status_names=tracker_json[ 'open_status_names'], closed_status_names=tracker_json[ 'closed_status_names'], **tracker_json['tracker_config']['options'] ) ThreadLocalORMSession.flush_all() try: M.session.artifact_orm_session._get().skip_mod_date = True for ticket_json in tracker_json['tickets']: reporter = self.get_user(ticket_json['reported_by']) owner = self.get_user(ticket_json['assigned_to']) with h.push_config(c, user=reporter, app=app): self.max_ticket_num = max( ticket_json['ticket_num'], self.max_ticket_num) ticket = TM.Ticket( app_config_id=app.config._id, import_id=import_id_converter.expand( ticket_json['ticket_num'], app), description=self.annotate( self.annotate( ticket_json['description'], owner, ticket_json[ 'assigned_to'], label=' owned'), reporter, ticket_json[ 'reported_by'], label=' created'), created_date=dateutil.parser.parse( ticket_json['created_date']), mod_date=dateutil.parser.parse( ticket_json['mod_date']), ticket_num=ticket_json['ticket_num'], summary=ticket_json['summary'], custom_fields=ticket_json['custom_fields'], status=ticket_json['status'], labels=ticket_json['labels'], votes_down=ticket_json['votes_down'], votes_up=ticket_json['votes_up'], votes=ticket_json['votes_up'] - ticket_json['votes_down'], assigned_to_id=owner._id, ) # trigger the private property ticket.private = ticket_json['private'] self.process_comments( ticket, ticket_json['discussion_thread']['posts']) session(ticket).flush(ticket) session(ticket).expunge(ticket) app.globals.custom_fields = tracker_json['custom_fields'] self.process_bins(app, tracker_json['saved_bins']) app.globals.last_ticket_num = self.max_ticket_num M.AuditLog.log( 'import tool %s from exported Allura JSON' % ( app.config.options.mount_point, ), project=project, user=user, url=app.url, ) g.post_event('project_updated') app.globals.invalidate_bin_counts() ThreadLocalORMSession.flush_all() return app except Exception: h.make_app_admin_only(app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool(self, project, user, mount_point=None, mount_label=None, **kw): import_id_converter = ImportIdConverter.get() tracker_json = self._load_json(project) tracker_json['tracker_config']['options'].pop('ordinal', None) tracker_json['tracker_config']['options'].pop('mount_point', None) tracker_json['tracker_config']['options'].pop('mount_label', None) tracker_json['tracker_config']['options'].pop('import_id', None) app = project.install_app( 'tickets', mount_point, mount_label, import_id={ 'source': self.source, 'app_config_id': tracker_json['tracker_config']['_id'], }, open_status_names=tracker_json['open_status_names'], closed_status_names=tracker_json['closed_status_names'], **tracker_json['tracker_config']['options']) ThreadLocalORMSession.flush_all() try: M.session.artifact_orm_session._get().skip_mod_date = True for ticket_json in tracker_json['tickets']: reporter = self.get_user(ticket_json['reported_by']) owner = self.get_user(ticket_json['assigned_to']) with h.push_config(c, user=reporter, app=app): self.max_ticket_num = max(ticket_json['ticket_num'], self.max_ticket_num) ticket = TM.Ticket( app_config_id=app.config._id, import_id=import_id_converter.expand( ticket_json['ticket_num'], app), description=self.annotate(self.annotate( ticket_json['description'], owner, ticket_json['assigned_to'], label=' owned'), reporter, ticket_json['reported_by'], label=' created'), created_date=dateutil.parser.parse( ticket_json['created_date']), mod_date=dateutil.parser.parse( ticket_json['mod_date']), ticket_num=ticket_json['ticket_num'], summary=ticket_json['summary'], custom_fields=ticket_json['custom_fields'], status=ticket_json['status'], labels=ticket_json['labels'], votes_down=ticket_json['votes_down'], votes_up=ticket_json['votes_up'], votes=ticket_json['votes_up'] - ticket_json['votes_down'], assigned_to_id=owner._id, ) # add an attachment to the ticket ticket.add_multiple_attachments( [File(a['url']) for a in ticket_json['attachments']]) # trigger the private property ticket.private = ticket_json['private'] self.process_comments( ticket, ticket_json['discussion_thread']['posts']) session(ticket).flush(ticket) session(ticket).expunge(ticket) app.globals.custom_fields = tracker_json['custom_fields'] self.process_bins(app, tracker_json['saved_bins']) app.globals.last_ticket_num = self.max_ticket_num M.AuditLog.log( 'import tool %s from exported Allura JSON' % (app.config.options.mount_point, ), project=project, user=user, url=app.url, ) g.post_event('project_updated') app.globals.invalidate_bin_counts() ThreadLocalORMSession.flush_all() return app except Exception: h.make_app_admin_only(app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool(self, project, user, project_name=None, mount_point=None, mount_label=None, user_name=None, tool_option=None, **kw): """ Import a GitHub wiki into a new Wiki Allura tool. """ project_name = "%s/%s" % (user_name, project_name) extractor = GitHubProjectExtractor(project_name, user=user) wiki_avail = extractor.has_wiki() # has_wiki only indicates that wiki is enabled, but it does not mean # that it has any pages, so we should check if wiki repo actually # exists wiki_url = extractor.get_page_url('wiki_url') if not wiki_avail or not self.has_wiki_repo(wiki_url): return self.github_wiki_url = extractor.get_page_url('wiki_url').replace( '.wiki', '/wiki') self.app = project.install_app("Wiki", mount_point=mount_point or 'wiki', mount_label=mount_label or 'Wiki', import_id={ 'source': self.source, 'project_name': project_name, }) with_history = tool_option == 'import_history' ThreadLocalORMSession.flush_all() self.github_markdown_converter = GitHubMarkdownConverter( user_name, project_name) try: M.session.artifact_orm_session._get().skip_mod_date = True with h.push_config(c, app=self.app): try: self.import_pages(wiki_url, history=with_history) except git.GitCommandError: log.error( 'Unable to clone GitHub wiki: ' 'wiki_url=%s; ' 'wiki_avail=%s; ' 'avail_url=%s', wiki_url, wiki_avail, extractor.get_page_url('project_info'), exc_info=True) raise ThreadLocalORMSession.flush_all() M.AuditLog.log('import tool %s from %s on %s' % (self.app.config.options.mount_point, project_name, self.source), project=project, user=user, url=self.app.url) g.post_event('project_updated') return self.app except Exception: h.make_app_admin_only(self.app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool(self, project, user, mount_point=None, mount_label=None, **kw): discussion_json = self._load_json(project) mount_point = mount_point or 'discussion' mount_label = mount_label or 'Discussion' app = project.install_app('discussion', mount_point, mount_label, import_id={'source': self.source}) ThreadLocalORMSession.flush_all() with h.push_config(c, app=app): # Deleting the forums that are created by default self._clear_forums(app) try: M.session.artifact_orm_session._get().skip_mod_date = True for forum_json in discussion_json['forums']: new_forum = dict( app_config_id=app.config._id, shortname=forum_json['shortname'], description=forum_json['description'], name=forum_json['name'], create='on', parent='', members_only=False, anon_posts=False, monitoring_email=None, ) forum = utils.create_forum(app, new_forum=new_forum) if "import_id" in list(forum_json.keys()): forum.import_id = forum_json["import_id"] for thread_json in forum_json["threads"]: thread = forum.get_discussion_thread( dict(headers=dict( Subject=thread_json['subject'])))[0] if "import_id" in thread_json: thread.import_id = thread_json["import_id"] self.add_posts(thread, thread_json['posts'], app) session(forum).flush(forum) session(forum).expunge(forum) M.AuditLog.log( "import tool %s from exported Allura JSON" % (app.config.options.mount_point, ), project=project, user=user, url=app.url, ) g.post_event('project_updated') ThreadLocalORMSession.flush_all() return app except Exception: h.make_app_admin_only(app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False
def import_tool( self, project, user, project_name=None, mount_point=None, mount_label=None, user_name=None, tool_option=None, **kw ): """ Import a GitHub wiki into a new Wiki Allura tool. """ project_name = "%s/%s" % (user_name, project_name) extractor = GitHubProjectExtractor(project_name, user=user) wiki_avail = extractor.has_wiki() # has_wiki only indicates that wiki is enabled, but it does not mean # that it has any pages, so we should check if wiki repo actually # exists wiki_url = extractor.get_page_url("wiki_url") if not wiki_avail or not self.has_wiki_repo(wiki_url): return self.github_wiki_url = extractor.get_page_url("wiki_url").replace(".wiki", "/wiki") self.app = project.install_app( "Wiki", mount_point=mount_point or "wiki", mount_label=mount_label or "Wiki", import_id={"source": self.source, "project_name": project_name}, ) with_history = tool_option == "import_history" ThreadLocalORMSession.flush_all() self.github_markdown_converter = GitHubMarkdownConverter(user_name, project_name) try: M.session.artifact_orm_session._get().skip_mod_date = True with h.push_config(c, app=self.app): try: self.import_pages(wiki_url, history=with_history) except git.GitCommandError: log.error( "Unable to clone GitHub wiki: " "wiki_url=%s; " "wiki_avail=%s; " "avail_url=%s", wiki_url, wiki_avail, extractor.get_page_url("project_info"), exc_info=True, ) raise ThreadLocalORMSession.flush_all() M.AuditLog.log( "import tool %s from %s on %s" % (self.app.config.options.mount_point, project_name, self.source), project=project, user=user, url=self.app.url, ) g.post_event("project_updated") return self.app except Exception: h.make_app_admin_only(self.app) raise finally: M.session.artifact_orm_session._get().skip_mod_date = False