def test_is_pkgdb_admin(self): """ Test the is_pkgdb_admin function of pkgdb. """ user = FakeFasUser() out = pkgdb.is_pkgdb_admin(user) self.assertEqual(out, False) user.groups = [] out = pkgdb.is_pkgdb_admin(user) self.assertEqual(out, False) user = FakeFasUser() out = pkgdb.is_pkgdb_admin(None) self.assertEqual(out, False) user = FakeFasUserAdmin() out = pkgdb.is_pkgdb_admin(user) self.assertEqual(out, True) pkgdb.APP.config['ADMIN_GROUP'] = 'sysadmin-main' out = pkgdb.is_pkgdb_admin(user) self.assertEqual(out, False) # Reset the ADMIN_GROUP for the other tests pkgdb.APP.config['ADMIN_GROUP'] = ('sysadmin-main', 'sysadmin-cvs')
def edit_collection(session, collection, clt_name=None, clt_version=None, clt_status=None, clt_publishurl=None, clt_pendingurl=None, clt_summary=None, clt_description=None, clt_branchname=None, clt_disttag=None, clt_gitbranch=None, user=None): """ Edit a specified collection """ if not pkgdb.is_pkgdb_admin(user): raise PkgdbException('You are now allowed to edit collections') edited = False if clt_name and clt_name != collection.name: collection.name = clt_name edited = True if clt_version and clt_version != collection.version: collection.version = clt_version edited = True if clt_status and clt_status != collection.status: collection.status = clt_status edited = True if clt_publishurl and clt_publishurl != collection.publishURLTemplate: collection.publishURLTemplate = clt_publishurl edited = True if clt_pendingurl and clt_pendingurl != collection.pendingURLTemplate: collection.pendingURLTemplate = clt_pendingurl edited = True if clt_summary and clt_summary != collection.summary: collection.summary = clt_summary edited = True if clt_description and clt_description != collection.description: collection.description = clt_description edited = True if clt_branchname and clt_branchname != collection.branchname: collection.branchname = clt_branchname edited = True if clt_disttag and clt_disttag != collection.distTag: collection.distTag = clt_disttag edited = True if clt_gitbranch and clt_gitbranch != collection.git_branch_name: collection.git_branch_name = clt_gitbranch edited = True if edited: try: session.add(collection) session.flush() model.Log.insert( session, user.username, None, 'user: %s edited collection: %s' % ( user.username, collection.name) ) return 'Collection "%s" edited' % collection.branchname except SQLAlchemyError, err: # pragma: no cover print err.message raise PkgdbException('Could not edit Collection.')
def update_collection_status(session, clt_branchname, clt_status, user): """ Update the status of a collection. :arg session: session with which to connect to the database :arg clt_branchname: branchname of the collection :arg clt_status: status of the collection """ if not pkgdb.is_pkgdb_admin(user): raise PkgdbException('You are now allowed to edit collections') try: collection = model.Collection.by_name(session, clt_branchname) if collection.status != clt_status: prev_status = collection.status collection.status = clt_status message = 'Collection updated to "%s"' % clt_status session.add(collection) session.flush() model.Log.insert( session, user.username, None, 'user: %s changed Collection: %s status from %s to %s' % (user.username, collection.name, prev_status, clt_status)) else: message = 'Collection "%s" already had this status' % \ clt_branchname return message except NoResultFound: # pragma: no cover raise PkgdbException('Could not find collection "%s"' % clt_branchname) except SQLAlchemyError, err: # pragma: no cover raise PkgdbException('Could not update the status of collection' '"%s".' % clt_branchname)
def add_collection(session, clt_name, clt_version, clt_status, clt_publishurl, clt_pendingurl, clt_summary, clt_description, clt_branchname, clt_disttag, clt_gitbranch, user): """ Add a new collection """ if not pkgdb.is_pkgdb_admin(user): raise PkgdbException('You are now allowed to create collections') collection = model.Collection( name=clt_name, version=clt_version, status=clt_status, owner=user.username, publishURLTemplate=clt_publishurl, pendingURLTemplate=clt_pendingurl, summary=clt_summary, description=clt_description, branchname=clt_branchname, distTag=clt_disttag, git_branch_name=clt_gitbranch, ) try: session.add(collection) session.flush() model.Log.insert( session, user.username, None, 'user: %s created collection: %s' % (user.username, collection.name)) return 'Collection "%s" created' % collection.branchname except SQLAlchemyError, err: # pragma: no cover print err.message raise PkgdbException('Could not add Collection to the database.')
def update_pkg_poc(session, pkg_name, clt_name, pkg_poc, user): """ Change the point of contact of a package. :arg session: session with which to connect to the database :arg pkg_name: the name of the package :arg clt_name: the branchname of the collection :arg pkg_poc: name of the new point of contact for the package. :arg user: the user making the action """ try: package = model.Package.by_name(session, pkg_name) except NoResultFound: raise PkgdbException('No package found by this name') try: collection = model.Collection.by_name(session, clt_name) except NoResultFound: raise PkgdbException('No collection found by this name') pkglisting = model.PackageListing.by_pkgid_collectionid( session, package.id, collection.id) prev_poc = pkglisting.point_of_contact if pkg_poc.startswith('group::') and not pkg_poc.endswith('-sig'): raise PkgdbException( 'Invalid group "%s" all groups in pkgdb should end with ' '"-sig".' % pkg_poc) if pkglisting.point_of_contact != user.username \ and pkglisting.point_of_contact != 'orphan' \ and not pkgdb.is_pkgdb_admin(user) \ and not pkglisting.point_of_contact.startswith('group::'): raise PkgdbException( 'You are not allowed to change the point of contact.') if pkglisting.point_of_contact.startswith('group::'): group = pkglisting.point_of_contact.split('group::')[1] if not group in user.groups: raise PkgdbException( 'You are not part of the group "%s", you are not allowed to' ' change the point of contact.' % group) pkglisting.point_of_contact = pkg_poc if pkg_poc == 'orphan': pkglisting.status = 'Orphaned' elif pkglisting.status in ('Orphaned', 'Deprecated'): pkglisting.status = 'Approved' session.add(pkglisting) session.flush() model.Log.insert( session, user.username, package, 'user: %s change PoC of package: %s from: %s to: %s on: %s' % (user.username, package.name, prev_poc, pkg_poc, clt_name)) return 'Point of contact of branch: %s of package: %s has been changed ' \ 'to %s' % (clt_name, pkg_name, pkg_poc)
def add_package(session, pkg_name, pkg_summary, pkg_status, pkg_collection, pkg_poc, user, pkg_reviewURL=None, pkg_shouldopen=None, pkg_upstreamURL=None): """ Create a new Package in the database and adds the corresponding PackageListing entry. :arg session: session with which to connect to the database :arg pkg_name: ... """ if user is None or not pkgdb.is_pkgdb_admin(user): raise PkgdbException("You're not allowed to add a package") if isinstance(pkg_collection, (str, unicode)): if ',' in pkg_collection: pkg_collection = [ item.strip() for item in pkg_collection.split(',') ] else: pkg_collection = [pkg_collection] package = model.Package(name=pkg_name, summary=pkg_summary, status=pkg_status, review_url=pkg_reviewURL, shouldopen=pkg_shouldopen, upstream_url=pkg_upstreamURL) session.add(package) for collec in pkg_collection: collection = model.Collection.by_name(session, collec) pkglisting = package.create_listing(point_of_contact=pkg_poc, collection=collection, statusname=pkg_status) session.add(pkglisting) try: session.flush() except SQLAlchemyError, err: # pragma: no cover raise PkgdbException('Could not add packages to collections')
def add_package(session, pkg_name, pkg_summary, pkg_status, pkg_collection, pkg_poc, user, pkg_reviewURL=None, pkg_shouldopen=None, pkg_upstreamURL=None): """ Create a new Package in the database and adds the corresponding PackageListing entry. :arg session: session with which to connect to the database :arg pkg_name: ... """ if user is None or not pkgdb.is_pkgdb_admin(user): raise PkgdbException("You're not allowed to add a package") if isinstance(pkg_collection, (str, unicode)): if ',' in pkg_collection: pkg_collection = [item.strip() for item in pkg_collection.split(',')] else: pkg_collection = [pkg_collection] package = model.Package(name=pkg_name, summary=pkg_summary, status=pkg_status, review_url=pkg_reviewURL, shouldopen=pkg_shouldopen, upstream_url=pkg_upstreamURL ) session.add(package) for collec in pkg_collection: collection = model.Collection.by_name(session, collec) pkglisting = package.create_listing(point_of_contact=pkg_poc, collection=collection, statusname=pkg_status) session.add(pkglisting) try: session.flush() except SQLAlchemyError, err: # pragma: no cover raise PkgdbException('Could not add packages to collections')
def add_collection(session, clt_name, clt_version, clt_status, clt_publishurl, clt_pendingurl, clt_summary, clt_description, clt_branchname, clt_disttag, clt_gitbranch, user): """ Add a new collection """ if not pkgdb.is_pkgdb_admin(user): raise PkgdbException('You are now allowed to create collections') collection = model.Collection( name=clt_name, version=clt_version, status=clt_status, owner=user.username, publishURLTemplate=clt_publishurl, pendingURLTemplate=clt_pendingurl, summary=clt_summary, description=clt_description, branchname=clt_branchname, distTag=clt_disttag, git_branch_name=clt_gitbranch, ) try: session.add(collection) session.flush() model.Log.insert( session, user.username, None, 'user: %s created collection: %s' % ( user.username, collection.name) ) return 'Collection "%s" created' % collection.branchname except SQLAlchemyError, err: # pragma: no cover print err.message raise PkgdbException('Could not add Collection to the database.')
def update_collection_status(session, clt_branchname, clt_status, user): """ Update the status of a collection. :arg session: session with which to connect to the database :arg clt_branchname: branchname of the collection :arg clt_status: status of the collection """ if not pkgdb.is_pkgdb_admin(user): raise PkgdbException('You are now allowed to edit collections') try: collection = model.Collection.by_name(session, clt_branchname) if collection.status != clt_status: prev_status = collection.status collection.status = clt_status message = 'Collection updated to "%s"' % clt_status session.add(collection) session.flush() model.Log.insert( session, user.username, None, 'user: %s changed Collection: %s status from %s to %s' % ( user.username, collection.name, prev_status, clt_status) ) else: message = 'Collection "%s" already had this status' % \ clt_branchname return message except NoResultFound: # pragma: no cover raise PkgdbException('Could not find collection "%s"' % clt_branchname) except SQLAlchemyError, err: # pragma: no cover raise PkgdbException('Could not update the status of collection' '"%s".' % clt_branchname)
def package_give(package): ''' Gives the PoC of a package to someone else. ''' packagename = package package = None try: package_acl = pkgdblib.get_acl_package(SESSION, packagename) package = pkgdblib.search_package(SESSION, packagename, limit=1)[0] except NoResultFound: SESSION.rollback() flask.flash('No package of this name found.', 'errors') return flask.render_template('msg.html') except IndexError: flask.flash('No package of this name found.', 'errors') return flask.render_template('msg.html') # Restrict the branch to the one current user is PoC of (unless admin # or group) collect_name = [] for acl in package_acl: if acl.point_of_contact != flask.g.fas_user.username and \ not is_pkgdb_admin(flask.g.fas_user) and \ not acl.point_of_contact.startswith('group::'): pass else: if acl.point_of_contact.startswith('group::'): group = acl.point_of_contact.split('group::')[0] if group not in flask.g.fas_user.groups: pass else: collect_name.append(acl.collection.branchname) form = pkgdb.forms.GivePoCForm(collections=collect_name) acls = ['commit', 'watchbugzilla', 'watchcommits', 'approveacls'] if form.validate_on_submit(): collections = form.pkg_branch.data pkg_poc = form.pkg_poc.data if pkg_poc.startswith('group::'): acls = ['commit', 'watchbugzilla', 'watchcommits'] try: for pkg_collection in collections: message = pkgdblib.update_pkg_poc( SESSION, pkg_name=packagename, clt_name=pkg_collection, pkg_poc=pkg_poc, user=flask.g.fas_user, ) flask.flash(message) for acl in acls: pkgdblib.set_acl_package( SESSION, pkg_name=packagename, clt_name=pkg_collection, pkg_user=pkg_poc, acl=acl, status='Approved', user=flask.g.fas_user ) SESSION.commit() except pkgdblib.PkgdbException, err: SESSION.rollback() flask.flash(err.message, 'error') return flask.redirect( flask.url_for('.package_info', package=packagename) )
def inject_is_admin(): """ Inject whether the user is a nuancier admin or not in every page (every template). """ return dict(is_admin=is_pkgdb_admin(flask.g.fas_user), version=__version__)
def edit_collection(session, collection, clt_name=None, clt_version=None, clt_status=None, clt_publishurl=None, clt_pendingurl=None, clt_summary=None, clt_description=None, clt_branchname=None, clt_disttag=None, clt_gitbranch=None, user=None): """ Edit a specified collection """ if not pkgdb.is_pkgdb_admin(user): raise PkgdbException('You are now allowed to edit collections') edited = False if clt_name and clt_name != collection.name: collection.name = clt_name edited = True if clt_version and clt_version != collection.version: collection.version = clt_version edited = True if clt_status and clt_status != collection.status: collection.status = clt_status edited = True if clt_publishurl and clt_publishurl != collection.publishURLTemplate: collection.publishURLTemplate = clt_publishurl edited = True if clt_pendingurl and clt_pendingurl != collection.pendingURLTemplate: collection.pendingURLTemplate = clt_pendingurl edited = True if clt_summary and clt_summary != collection.summary: collection.summary = clt_summary edited = True if clt_description and clt_description != collection.description: collection.description = clt_description edited = True if clt_branchname and clt_branchname != collection.branchname: collection.branchname = clt_branchname edited = True if clt_disttag and clt_disttag != collection.distTag: collection.distTag = clt_disttag edited = True if clt_gitbranch and clt_gitbranch != collection.git_branch_name: collection.git_branch_name = clt_gitbranch edited = True if edited: try: session.add(collection) session.flush() model.Log.insert( session, user.username, None, 'user: %s edited collection: %s' % (user.username, collection.name)) return 'Collection "%s" edited' % collection.branchname except SQLAlchemyError, err: # pragma: no cover print err.message raise PkgdbException('Could not edit Collection.')
def package_give(package): ''' Gives the PoC of a package to someone else. ''' packagename = package package = None try: package_acl = pkgdblib.get_acl_package(SESSION, packagename) package = pkgdblib.search_package(SESSION, packagename, limit=1)[0] except NoResultFound: SESSION.rollback() flask.flash('No package of this name found.', 'errors') return flask.render_template('msg.html') except IndexError: flask.flash('No package of this name found.', 'errors') return flask.render_template('msg.html') # Restrict the branch to the one current user is PoC of (unless admin # or group) collect_name = [] for acl in package_acl: if acl.point_of_contact != flask.g.fas_user.username and \ not is_pkgdb_admin(flask.g.fas_user) and \ not acl.point_of_contact.startswith('group::'): pass else: if acl.point_of_contact.startswith('group::'): group = acl.point_of_contact.split('group::')[0] if group not in flask.g.fas_user.groups: pass else: collect_name.append(acl.collection.branchname) form = pkgdb.forms.GivePoCForm(collections=collect_name) acls = ['commit', 'watchbugzilla', 'watchcommits', 'approveacls'] if form.validate_on_submit(): collections = form.pkg_branch.data pkg_poc = form.pkg_poc.data if pkg_poc.startswith('group::'): acls = ['commit', 'watchbugzilla', 'watchcommits'] try: for pkg_collection in collections: message = pkgdblib.update_pkg_poc( SESSION, pkg_name=packagename, clt_name=pkg_collection, pkg_poc=pkg_poc, user=flask.g.fas_user, ) flask.flash(message) for acl in acls: pkgdblib.set_acl_package(SESSION, pkg_name=packagename, clt_name=pkg_collection, pkg_user=pkg_poc, acl=acl, status='Approved', user=flask.g.fas_user) SESSION.commit() except pkgdblib.PkgdbException, err: SESSION.rollback() flask.flash(err.message, 'error') return flask.redirect( flask.url_for('.package_info', package=packagename))
def update_pkg_poc(session, pkg_name, clt_name, pkg_poc, user): """ Change the point of contact of a package. :arg session: session with which to connect to the database :arg pkg_name: the name of the package :arg clt_name: the branchname of the collection :arg pkg_poc: name of the new point of contact for the package. :arg user: the user making the action """ try: package = model.Package.by_name(session, pkg_name) except NoResultFound: raise PkgdbException('No package found by this name') try: collection = model.Collection.by_name(session, clt_name) except NoResultFound: raise PkgdbException('No collection found by this name') pkglisting = model.PackageListing.by_pkgid_collectionid(session, package.id, collection.id) prev_poc = pkglisting.point_of_contact if pkg_poc.startswith('group::') and not pkg_poc.endswith('-sig'): raise PkgdbException( 'Invalid group "%s" all groups in pkgdb should end with ' '"-sig".' % pkg_poc) if pkglisting.point_of_contact != user.username \ and pkglisting.point_of_contact != 'orphan' \ and not pkgdb.is_pkgdb_admin(user) \ and not pkglisting.point_of_contact.startswith('group::'): raise PkgdbException( 'You are not allowed to change the point of contact.') if pkglisting.point_of_contact.startswith('group::'): group = pkglisting.point_of_contact.split('group::')[1] if not group in user.groups: raise PkgdbException( 'You are not part of the group "%s", you are not allowed to' ' change the point of contact.' % group) pkglisting.point_of_contact = pkg_poc if pkg_poc == 'orphan': pkglisting.status = 'Orphaned' elif pkglisting.status in ('Orphaned', 'Deprecated'): pkglisting.status = 'Approved' session.add(pkglisting) session.flush() model.Log.insert( session, user.username, package, 'user: %s change PoC of package: %s from: %s to: %s on: %s' % ( user.username, package.name, prev_poc, pkg_poc, clt_name) ) return 'Point of contact of branch: %s of package: %s has been changed ' \ 'to %s' % (clt_name, pkg_name, pkg_poc)
def update_pkg_status(session, pkg_name, clt_name, status, user, poc='orphan'): """ Update the status of a package. :arg session: session with which to connect to the database :arg pkg_name: the name of the package :arg clt_name: the name of the collection :arg user: the user making the action """ try: package = model.Package.by_name(session, pkg_name) except NoResultFound: raise PkgdbException('No package found by this name') try: collection = model.Collection.by_name(session, clt_name) except NoResultFound: raise PkgdbException('No collection found by this name') if status not in ['Approved', 'Removed', 'Deprecated', 'Orphaned']: raise PkgdbException('Status not allowed for a package : %s' % status) pkglisting = model.PackageListing.by_pkgid_collectionid(session, package.id, collection.id) prev_status = pkglisting.status if status == 'Deprecated': # Admins can deprecate everything # Users can deprecate Fedora devel and EPEL branches if pkgdb.is_pkgdb_admin(user) \ or (collection.name == 'Fedora' and collection.version == 'devel') \ or collection.name == 'EPEL': pkglisting.status = 'Deprecated' pkglisting.point_of_contact = 'orphan' session.add(pkglisting) session.flush() else: raise PkgdbException( 'You are now allowed to deprecate the ' 'package: %s on branch %s.' % ( package.name, collection.branchname)) elif status == 'Orphaned': pkglisting.status = 'Orphaned' pkglisting.point_of_contact = 'orphan' session.add(pkglisting) session.flush() elif pkgdb.is_pkgdb_admin(user): if status == 'Approved': if pkglisting.status == 'Orphaned' and poc == 'orphan': raise PkgdbException( 'You need to specify the point of contact of this ' 'package for this branch to un-orphan it') pkglisting.point_of_contact = poc pkglisting.status = status session.add(pkglisting) session.flush() else: raise PkgdbException( 'You are now allowed to update the status of ' 'the package: %s on branch %s to %s.' % ( package.name, collection.branchname, status) ) model.Log.insert( session, user.username, package, 'user: %s updated Package: %s status from %s to %s' % ( user.username, package.name, prev_status, status) )
def update_pkg_status(session, pkg_name, clt_name, status, user, poc='orphan'): """ Update the status of a package. :arg session: session with which to connect to the database :arg pkg_name: the name of the package :arg clt_name: the name of the collection :arg user: the user making the action """ try: package = model.Package.by_name(session, pkg_name) except NoResultFound: raise PkgdbException('No package found by this name') try: collection = model.Collection.by_name(session, clt_name) except NoResultFound: raise PkgdbException('No collection found by this name') if status not in ['Approved', 'Removed', 'Deprecated', 'Orphaned']: raise PkgdbException('Status not allowed for a package : %s' % status) pkglisting = model.PackageListing.by_pkgid_collectionid( session, package.id, collection.id) prev_status = pkglisting.status if status == 'Deprecated': # Admins can deprecate everything # Users can deprecate Fedora devel and EPEL branches if pkgdb.is_pkgdb_admin(user) \ or (collection.name == 'Fedora' and collection.version == 'devel') \ or collection.name == 'EPEL': pkglisting.status = 'Deprecated' pkglisting.point_of_contact = 'orphan' session.add(pkglisting) session.flush() else: raise PkgdbException('You are now allowed to deprecate the ' 'package: %s on branch %s.' % (package.name, collection.branchname)) elif status == 'Orphaned': pkglisting.status = 'Orphaned' pkglisting.point_of_contact = 'orphan' session.add(pkglisting) session.flush() elif pkgdb.is_pkgdb_admin(user): if status == 'Approved': if pkglisting.status == 'Orphaned' and poc == 'orphan': raise PkgdbException( 'You need to specify the point of contact of this ' 'package for this branch to un-orphan it') pkglisting.point_of_contact = poc pkglisting.status = status session.add(pkglisting) session.flush() else: raise PkgdbException('You are now allowed to update the status of ' 'the package: %s on branch %s to %s.' % (package.name, collection.branchname, status)) model.Log.insert( session, user.username, package, 'user: %s updated Package: %s status from %s to %s' % (user.username, package.name, prev_status, status))