-
-
Save remyd1/5524162 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from scripts.db_shell import * | |
from galaxy.util.bunch import Bunch | |
from galaxy.security import GalaxyRBACAgent | |
import argparse | |
import sys | |
import re | |
bunch = Bunch( **globals() ) | |
bunch.engine = engine | |
# model.flush() has been removed. | |
bunch.session = db_session | |
# For backward compatibility with "model.context.current" | |
bunch.context = db_session | |
security_agent = GalaxyRBACAgent( bunch ) | |
security_agent.sa_session = sa_session | |
def add_user(email, password, key=None): | |
""" | |
add_user : in galaxy | |
""" | |
query = sa_session.query( User ).filter_by( email=email ) | |
if query.count() > 0: | |
return query.first() | |
else: | |
user = User(email) | |
user.set_password_cleartext(password) | |
sa_session.add(user) | |
sa_session.flush() | |
security_agent.create_private_user_role( user ) | |
if not user.default_permissions: | |
security_agent.user_set_default_permissions( user, | |
history=True, dataset=True ) | |
if key is not None: | |
api_key = APIKeys() | |
api_key.user_id = user.id | |
api_key.key = key | |
sa_session.add(api_key) | |
sa_session.flush() | |
return user | |
def check_mail(user): | |
""" | |
check mail format | |
""" | |
if not re.match(r"[^@]+@[^@]+\.[^@]+", user): | |
sys.exit("The user does not have a correct email address") | |
if __name__=="__main__": | |
""" | |
Parser added to use directly the script to add user from bash | |
""" | |
description="This program allow galaxy administrator to create "\ | |
+"a galaxy user in a command line way..." | |
epilog="from jmchilton, github gist script create_galaxy_users.py" | |
parser = argparse.ArgumentParser(description=description\ | |
,epilog=epilog) | |
parser.add_argument("-m", "--email", \ | |
help="The email for the new user (will be the username).\n") | |
parser.add_argument("-p","--password", \ | |
help="The password for the new user.\n") | |
parser.add_argument('--version', action='version', \ | |
version='%(prog)s 0.2') | |
results = parser.parse_args() | |
if len(sys.argv) == 1: | |
parser.print_help() | |
sys.exit("\nYou do not specify any argument !!\n\n") | |
user = results.email | |
check_mail(user) | |
password = results.password | |
add_user(user, password) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment