Mercurial > repos > gga > apollo_create_account
changeset 5:356b43302b16 draft
planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit 85194fa009ead2c34720faab61a4143fc29d17c2
author | gga |
---|---|
date | Fri, 31 Aug 2018 09:34:20 -0400 |
parents | 08f8e19005a8 |
children | 2fb1e4193edd |
files | create_account.py create_features_from_gff3.py create_or_update_organism.py delete_features.py delete_organism.py export.py fetch_organism_jbrowse.py list_organisms.py webapollo.py |
diffstat | 9 files changed, 86 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/create_account.py Mon Mar 12 06:32:42 2018 -0400 +++ b/create_account.py Fri Aug 31 09:34:20 2018 -0400 @@ -2,17 +2,11 @@ from __future__ import print_function import argparse -import random import time from six.moves.builtins import str -from webapollo import WAAuth, WebApolloInstance - - -def pwgen(length): - chars = list('qwrtpsdfghjklzxcvbnm') - return ''.join(random.choice(chars) for _ in range(length)) +from webapollo import PasswordGenerator, WAAuth, WebApolloInstance if __name__ == '__main__': @@ -26,7 +20,7 @@ wa = WebApolloInstance(args.apollo, args.username, args.password) - password = pwgen(12) + password = PasswordGenerator(12) time.sleep(1) users = wa.users.loadUsers() user = [u for u in users
--- a/create_features_from_gff3.py Mon Mar 12 06:32:42 2018 -0400 +++ b/create_features_from_gff3.py Fri Aug 31 09:34:20 2018 -0400 @@ -8,7 +8,7 @@ from six.moves.builtins import str -from webapollo import AssertUser, GuessOrg, OrgOrGuess, WAAuth, WebApolloInstance, featuresToFeatureSchema, retry +from webapollo import GuessOrg, OrgOrGuess, PermissionCheck, WAAuth, WebApolloInstance, featuresToFeatureSchema, retry logging.basicConfig(level=logging.INFO) log = logging.getLogger(__name__) @@ -25,14 +25,15 @@ wa = WebApolloInstance(args.apollo, args.username, args.password) # User must have an account - gx_user = AssertUser(wa.users.loadUsers(email=args.email)) + gx_user = wa.users.assertOrCreateUser(args.email) # Get organism org_cn = GuessOrg(args, wa) if isinstance(org_cn, list): org_cn = org_cn[0] - # TODO: Check user perms on org. + if not PermissionCheck(gx_user, org_cn, "WRITE"): + raise Exception("Action not permitted") org = wa.organisms.findOrganismByCn(org_cn) bad_quals = ['date_creation', 'source', 'owner', 'date_last_modified', 'Name', 'ID']
--- a/create_or_update_organism.py Mon Mar 12 06:32:42 2018 -0400 +++ b/create_or_update_organism.py Fri Aug 31 09:34:20 2018 -0400 @@ -8,11 +8,10 @@ import sys import time -from webapollo import AssertUser, GuessOrg, OrgOrGuess, WAAuth, WebApolloInstance +from webapollo import GuessOrg, OrgOrGuess, PermissionCheck, WAAuth, WebApolloInstance logging.basicConfig(level=logging.INFO) log = logging.getLogger(__name__) - if __name__ == '__main__': parser = argparse.ArgumentParser(description='Create or update an organism in an Apollo instance') WAAuth(parser) @@ -33,8 +32,8 @@ if isinstance(org_cn, list): org_cn = org_cn[0] - # User must have an account - gx_user = AssertUser(wa.users.loadUsers(email=args.email)) + # User must have an account, if not, create it + gx_user = wa.users.assertOrCreateUser(args.email) log.info("Determining if add or update required") try: @@ -43,14 +42,9 @@ org = None if org: - has_perms = False old_directory = org['directory'] - for user_owned_organism in gx_user.organismPermissions: - if 'WRITE' in user_owned_organism['permissions']: - has_perms = True - break - if not has_perms: + if not PermissionCheck(gx_user, org_cn, "WRITE"): print("Naming Conflict. You do not have permissions to access this organism. Either request permission from the owner, or choose a different name for your organism.") sys.exit(2)
--- a/delete_features.py Mon Mar 12 06:32:42 2018 -0400 +++ b/delete_features.py Fri Aug 31 09:34:20 2018 -0400 @@ -5,7 +5,7 @@ import logging import random -from webapollo import AssertUser, GuessOrg, OrgOrGuess, WAAuth, WebApolloInstance, retry +from webapollo import GuessOrg, OrgOrGuess, PermissionCheck, WAAuth, WebApolloInstance, retry logging.basicConfig(level=logging.INFO) log = logging.getLogger(__name__) @@ -21,14 +21,15 @@ wa = WebApolloInstance(args.apollo, args.username, args.password) # User must have an account - gx_user = AssertUser(wa.users.loadUsers(email=args.email)) + gx_user = wa.users.assertOrCreateUser(args.email) # Get organism org_cn = GuessOrg(args, wa) if isinstance(org_cn, list): org_cn = org_cn[0] - # TODO: Check user perms on org. + if not PermissionCheck(gx_user, org_cn, "WRITE"): + raise Exception("Action not permitted") org = wa.organisms.findOrganismByCn(org_cn) sequences = wa.organisms.getSequencesForOrganism(org['id'])
--- a/delete_organism.py Mon Mar 12 06:32:42 2018 -0400 +++ b/delete_organism.py Fri Aug 31 09:34:20 2018 -0400 @@ -4,7 +4,7 @@ import argparse import logging -from webapollo import AssertUser, GuessOrg, OrgOrGuess, WAAuth, WebApolloInstance +from webapollo import GuessOrg, OrgOrGuess, PermissionCheck, WAAuth, WebApolloInstance logging.basicConfig(level=logging.INFO) log = logging.getLogger(__name__) @@ -19,14 +19,15 @@ wa = WebApolloInstance(args.apollo, args.username, args.password) # User must have an account - gx_user = AssertUser(wa.users.loadUsers(email=args.email)) + gx_user = wa.users.assertOrCreateUser(args.email) # Get organism org_cn = GuessOrg(args, wa) if isinstance(org_cn, list): org_cn = org_cn[0] - # TODO: Check user perms on org. + if not PermissionCheck(gx_user, org_cn, "WRITE"): + raise Exception("You do not have write permission on this organism") org = wa.organisms.findOrganismByCn(org_cn) # Call setSequence to tell apollo which organism we're working with @@ -34,7 +35,9 @@ # Then get a list of features. features = wa.annotations.getFeatures() # For each feature in the features - for feature in features['features']: - # We see that deleteFeatures wants a uniqueName, and so we pass - # is the uniquename field in the feature. - print(wa.annotations.deleteFeatures([feature['uniquename']])) + # If it exists + if 'features' in features: + for feature in features['features']: + # We see that deleteFeatures wants a uniqueName, and so we pass + # is the uniquename field in the feature. + print(wa.annotations.deleteFeatures([feature['uniquename']]))
--- a/export.py Mon Mar 12 06:32:42 2018 -0400 +++ b/export.py Fri Aug 31 09:34:20 2018 -0400 @@ -9,7 +9,7 @@ from Bio import SeqIO -from webapollo import CnOrGuess, GuessCn, WAAuth, WebApolloInstance +from webapollo import CnOrGuess, GuessCn, PermissionCheck, WAAuth, WebApolloInstance try: import StringIO as io @@ -71,15 +71,21 @@ parser.add_argument('--gff', type=argparse.FileType('w')) parser.add_argument('--fasta', type=argparse.FileType('w')) parser.add_argument('--json', type=argparse.FileType('w')) - + parser.add_argument('email', help='User Email') args = parser.parse_args() wa = WebApolloInstance(args.apollo, args.username, args.password) org_cn_list, seqs = GuessCn(args, wa) + # User must have an apollo account, if not, create it + gx_user = wa.users.assertOrCreateUser(args.email) + org_data = [] for org_cn in org_cn_list: + # User must have read permission on organism + if not PermissionCheck(gx_user, org_cn, "READ"): + continue indiv_org_data = export(org_cn, seqs) org_data.append(indiv_org_data) args.json.write(json.dumps(org_data, indent=2))
--- a/fetch_organism_jbrowse.py Mon Mar 12 06:32:42 2018 -0400 +++ b/fetch_organism_jbrowse.py Fri Aug 31 09:34:20 2018 -0400 @@ -9,7 +9,7 @@ import sys import time -from webapollo import GuessOrg, OrgOrGuess, WAAuth, WebApolloInstance +from webapollo import GuessOrg, OrgOrGuess, PermissionCheck, WAAuth, WebApolloInstance logging.basicConfig(level=logging.INFO) log = logging.getLogger(__name__) @@ -55,6 +55,7 @@ WAAuth(parser) OrgOrGuess(parser) parser.add_argument('target_dir', help='Target directory') + parser.add_argument('email', help='User Email') args = parser.parse_args() @@ -65,6 +66,14 @@ org_cn = org_cn[0] org = wa.organisms.findOrganismByCn(org_cn) + # User must have an account, if not, create it + gx_user = wa.users.assertOrCreateUser(args.email) + + # User must have READ access + + if not PermissionCheck(gx_user, org_cn, "READ"): + raise Exception("READ permissions are required for this action") + if not os.path.exists(args.target_dir): os.makedirs(args.target_dir)
--- a/list_organisms.py Mon Mar 12 06:32:42 2018 -0400 +++ b/list_organisms.py Fri Aug 31 09:34:20 2018 -0400 @@ -4,7 +4,7 @@ import argparse import json -from webapollo import AssertUser, WAAuth, WebApolloInstance, accessible_organisms +from webapollo import WAAuth, WebApolloInstance, accessible_organisms if __name__ == '__main__': parser = argparse.ArgumentParser(description='List all organisms available in an Apollo instance') @@ -14,7 +14,8 @@ wa = WebApolloInstance(args.apollo, args.username, args.password) - gx_user = AssertUser(wa.users.loadUsers(email=args.email)) + gx_user = wa.users.assertOrCreateUser(args.email) + all_orgs = wa.organisms.findAllOrganisms() orgs = accessible_organisms(gx_user, all_orgs)
--- a/webapollo.py Mon Mar 12 06:32:42 2018 -0400 +++ b/webapollo.py Fri Aug 31 09:34:20 2018 -0400 @@ -5,6 +5,7 @@ import json import logging import os +import random import time from abc import abstractmethod @@ -478,6 +479,25 @@ raise Exception("User is not an administrator. Permission denied") +def PermissionCheck(user, org_cn, permission_type): + return any(org["organism"] == org_cn and permission_type in org["permissions"] for org in user.organismPermissions) + + +def PasswordGenerator(length): + chars = list('qwrtpsdfghjklzxcvbnm') + return ''.join(random.choice(chars) for _ in range(length)) + + +def IsRemoteUser(): + if 'GALAXY_WEBAPOLLO_REMOTE_USER' not in os.environ: + return False + value = os.environ['GALAXY_WEBAPOLLO_REMOTE_USER'] + if value.lower() in ('true', 't', '1'): + return True + else: + return False + + class WebApolloInstance(object): def __init__(self, url, username, password): @@ -1254,7 +1274,10 @@ return self.request('addOrganism', data) def findAllOrganisms(self): - return self.request('findAllOrganisms', {}) + orgs = self.request('findAllOrganisms', {}) + if not isinstance(orgs, (list,)): + orgs = [] + return orgs def findOrganismByCn(self, cn): orgs = self.findAllOrganisms() @@ -1352,7 +1375,7 @@ data = {'group': group.name, 'userId': user.userId} return self.request('removeUserFromGroup', data) - def createUser(self, email, firstName, lastName, newPassword, role="user", groups=None): + def createUser(self, email, firstName, lastName, newPassword, role="user", groups=None, addToHistory=False): data = { 'firstName': firstName, 'lastName': lastName, @@ -1363,7 +1386,19 @@ 'newPassword': newPassword, # 'organismPermissions': [], } - return self.request('createUser', data) + returnData = self.request('createUser', data) + if addToHistory and not IsRemoteUser(): + f = open("Apollo_credentials.txt", "w") + f.write('Username: %s\tPassword: %s' % (email, newPassword)) + return returnData + + def assertOrCreateUser(self, email): + try: + gx_user = AssertUser(self.loadUsers(email)) + except Exception: + self.createUser(email, email, email, PasswordGenerator(12), role='user', addToHistory=True) + gx_user = AssertUser(self.loadUsers(email)) + return gx_user def deleteUser(self, user): return self.request('deleteUser', {'userId': user.userId}) @@ -1532,11 +1567,6 @@ os.environ['GALAXY_WEBAPOLLO_USER'], os.environ['GALAXY_WEBAPOLLO_PASSWORD'] ) - # Assert that the email exists in apollo - try: - gx_user = wa.requireUser(email) - except UnknownUserException: - return [] # Key for cached data cacheKey = 'groups-' + email @@ -1545,7 +1575,7 @@ if cacheKey not in cache: # However if it ISN'T there, we know we're safe to fetch + put in # there. - data = _galaxy_list_groups(wa, gx_user, *args, **kwargs) + data = _galaxy_list_groups(wa, *args, **kwargs) cache[cacheKey] = data return data try: @@ -1558,12 +1588,12 @@ except KeyError: # If access fails due to eviction, we will fail over and can ensure that # data is inserted. - data = _galaxy_list_groups(wa, gx_user, *args, **kwargs) + data = _galaxy_list_groups(wa, *args, **kwargs) cache[cacheKey] = data return data -def _galaxy_list_groups(wa, gx_user, *args, **kwargs): +def _galaxy_list_groups(wa, *args, **kwargs): # Fetch the groups. group_data = [] for group in wa.groups.loadGroups():