def run(self, user): from app import db user_datastore = SQLAlchemyUserDatastore(db, User, Role) u = User.query.filter_by(email=user).first() role = user_datastore.find_or_create_role('admin') user_datastore.add_role_to_user(u, role) db.session.commit()
def do_setup(app): """Create a new settings database from scratch""" if config.SERVER_MODE is False: print("NOTE: Configuring authentication for DESKTOP mode.") email = config.DESKTOP_USER p1 = ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(32) ]) else: print("NOTE: Configuring authentication for SERVER mode.\n") # Prompt the user for their default username and password. print(""" Enter the email address and password to use for the initial pgAdmin user \ account:\n""") email = '' while email == '': email = input("Email address: ") def pprompt(): return getpass.getpass(), getpass.getpass('Retype password:'******'Passwords do not match. Try again') p1, p2 = pprompt() # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, User, Role) security = Security(app, user_datastore) with app.app_context(): password = encrypt_password(p1) db.create_all() user_datastore.create_role(name='Administrators', description='pgAdmin Administrators Role') user_datastore.create_user(email=email, password=password) db.session.flush() user_datastore.add_role_to_user(email, 'Administrators') # Get the user's ID and create the default server group user = User.query.filter_by(email=email).first() server_group = ServerGroup(user_id=user.id, name="Servers") db.session.merge(server_group) # Set the schema version version = Version(name='ConfigDB', value=config.SETTINGS_SCHEMA_VERSION) db.session.merge(version) db.session.commit() # Done! print("") print("The configuration database has been created at {0}".format( config.SQLITE_PATH))
def do_setup(app): """Create a new settings database from scratch""" if config.SERVER_MODE is False: print("NOTE: Configuring authentication for DESKTOP mode.") email = config.DESKTOP_USER p1 = "".join([random.choice(string.ascii_letters + string.digits) for n in xrange(32)]) else: print("NOTE: Configuring authentication for SERVER mode.\n") # Prompt the user for their default username and password. print("Enter the email address and password to use for the initial pgAdmin user account:\n") email = "" while email == "": email = raw_input("Email address: ") pprompt = lambda: (getpass.getpass(), getpass.getpass("Retype password: "******"Passwords do not match. Try again") p1, p2 = pprompt() # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, User, Role) security = Security(app, user_datastore) with app.app_context(): password = encrypt_password(p1) db.create_all() user_datastore.create_role(name="Administrators", description="pgAdmin Administrators Role") user_datastore.create_user(email=email, password=password) db.session.flush() user_datastore.add_role_to_user(email, "Administrators") # Get the user's ID and create the default server group user = User.query.filter_by(email=email).first() server_group = ServerGroup(user_id=user.id, name="Servers") db.session.merge(server_group) # Set the schema version version = Version(name="ConfigDB", value=config.SETTINGS_SCHEMA_VERSION) db.session.merge(version) db.session.commit() # Done! print("") print("The configuration database has been created at %s" % config.SQLITE_PATH)
def do_setup(app): """Create a new settings database from scratch""" if config.SERVER_MODE is False: print("NOTE: Configuring authentication for DESKTOP mode.") email = config.DESKTOP_USER p1 = ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(32) ]) else: print("NOTE: Configuring authentication for SERVER mode.\n") # Prompt the user for their default username and password. print(""" Enter the email address and password to use for the initial pgAdmin user \ account:\n""") email_filter = re.compile( "^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]" "(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9]" "(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") email = '' input("Email address: ") while email == '' or not email_filter.match(email): print('Invalid email address. Please try again.') email = input("Email address: ") def pprompt(): return getpass.getpass(), getpass.getpass('Retype password:'******'Passwords do not match. Please try again.') p1, p2 = pprompt() # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, User, Role) Security(app, user_datastore) with app.app_context(): password = encrypt_password(p1) db.create_all() user_datastore.create_role( name='Administrator', description='pgAdmin Administrator Role' ) user_datastore.create_role( name='User', description='pgAdmin User Role' ) user_datastore.create_user(email=email, password=password) db.session.flush() user_datastore.add_role_to_user(email, 'Administrator') # Get the user's ID and create the default server group user = User.query.filter_by(email=email).first() server_group = ServerGroup(user_id=user.id, name="Servers") db.session.merge(server_group) # Set the schema version version = Version( name='ConfigDB', value=config.SETTINGS_SCHEMA_VERSION ) db.session.merge(version) db.session.commit() # Done! print("") print( "The configuration database has been created at {0}".format( config.SQLITE_PATH ) )
def init_db(): fake_app = create_app() with fake_app.test_request_context(): db.create_all() datastore = SQLAlchemyUserDatastore(db, User, Role) print("\n** ADDING ROLES **") for role in ['admin', 'manager']: try: print("adding role '%s'" % role) datastore.create_role(name=role, description=role) datastore.commit() print(" ...OK") except IntegrityError as r: print(" '%s' role already exists" % role) db.session.rollback() print("\n** ADDING USERS **") users = [ fake_app.config['ADMIN_USER'], fake_app.config['MANAGER_USER'], fake_app.config['CLERK_USER'] ] for user in users: if user: name = user['name'] email = user['email'] role = user['role'] password = user['password'] try: print("adding user '%s'" % name) password = encrypt_password(password) datastore.create_user(email=email, password=password, name=name, active=True) datastore.commit() print(" ...OK") except IntegrityError as e: print(" user '%s' already exists" % name) db.session.rollback() if role: try: print("adding '%s' role to user '%s'" % (role, name)) this_user = db.session.query(User)\ .filter(User.name == name).first() this_role = db.session.query(Role).filter( Role.name == role).first() datastore.add_role_to_user(this_user, this_role) datastore.commit() print(" ...OK") except IntegrityError as e: print(" unable to add role '%s' to user '%s'" % (role, name)) from alembic.config import Config from alembic import command path = os.path.join(os.path.dirname(__file__), 'alembic.ini') alembic_cfg = Config(path) command.stamp(alembic_cfg, 'head')