コード例 #1
0
ファイル: setup.py プロジェクト: vnthang/pgadmin4
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))
コード例 #2
0
ファイル: setup.py プロジェクト: arunkollanedb/pgadmin4
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)
コード例 #3
0
ファイル: views.py プロジェクト: napplebee/culog
def data():
    return
    db.drop_all()
    db.create_all()
    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    from flask_security.utils import encrypt_password
    role = user_datastore.create_role(name="root", description="Site administrator")
    db.session.commit()
    return "OK"
コード例 #4
0
ファイル: setup.py プロジェクト: datallysis/pgadmin4
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
        )
    )
コード例 #5
0
ファイル: sandbox.py プロジェクト: napplebee/culog
# -*- coding: utf-8 -*-

from flask.ext.security import SQLAlchemyUserDatastore

from app import create_app
from app.core import db
from app.data.users import User, Role

app = create_app("dev")
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
# security = Security(app, user_datastore)

# Create a user to test with
# db.drop_all()
# db.create_all()
role = user_datastore.create_role(name="root", description="Site administrator")
user_datastore.create_user(email='*****@*****.**', password='******', roles=[role])
user_datastore.create_user(email='*****@*****.**', password='******')

db.session.commit()

class Test(object):
    id = 1
    en_title = "title"
    ru_title = u"тайтл"
コード例 #6
0
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')