Example #1
0
def create_app():
    app = flask.Flask(__name__)
    app.config.from_object('config')

    init_db(app)

    api.init_app(app)

    security = Security()
    security.init_app(app, users)

    principals = Principal()
    principals.init_app(app)

    configure_uploads(app, logos)

    from app.views import menu, bp

    menu.init_app(app)
    app.register_blueprint(bp)

    if False:
        login_manager = LoginManager(app)

        @login_manager.request_loader
        def load_user_from_request(request):
            if request.authorization:
                email, password = request.authorization.username, request.authorization.password
                user = User.query.filter_by(email=unicode(email)).first()

                if user is not None:
                    if verify_password(password, user.password):
                        return user

    @identity_loaded.connect_via(app)
    def on_identity_loaded(sender, identity):
        if not isinstance(identity, AnonymousIdentity):
            identity.provides.add(UserNeed(identity.id))

            for role in current_user.roles:
                identity.provides.add(RoleNeed(role.name))

    @principals.identity_loader
    def read_identity_from_flask_login():
        if current_user.is_authenticated():
            return Identity(current_user.id)
        return AnonymousIdentity()

    return app
Example #2
0
def init_app(acl, app, mtjacl_sessions=True,
        permission_denied_handler=handle_permission_denied, *a, **kw):

    # Not using the default session.
    principal = Principal(app, use_sessions=False, *a, **kw)

    @identity_loaded.connect_via(app)
    def on_identity_loaded(sender, identity):
        if not isinstance(identity, AclIdentity):
            # Not doing anything on identities we don't care for.
            return

        # the identity is actually the raw token
        access_token = identity.access_token
        if access_token is None:
            user = anonymous
        else:
            user = acl.getUserFromAccessToken(access_token)
        # cache this value.
        g.mtj_user = user
        if user is anonymous:
            return
        roles = acl.getUserRoles(user)
        # TODO figure out how to do lazy loading of roles.
        for role in roles:
            identity.provides.add(RoleNeed(role))

        identity.id = user.login

    if mtjacl_sessions:
        principal.identity_loader(acl_session_identity_loader)
        principal.identity_saver(acl_session_identity_saver)

    app.config['MTJ_ACL'] = acl
    if callable(permission_denied_handler):
        app.errorhandler(PermissionDenied)(permission_denied_handler)

    app.before_request(_on_before_request(acl))
Example #3
0
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:@127.0.0.1/flask'

db.init_app(app)

#with app.test_request_context():
#   load_db(db)

login_manager = LoginManager()
login_manager.init_app(app)

@login_manager.user_loader
def user_loader(username):
	return user.query.filter_by(username=username).first()

principal = Principal()
principal.init_app(app)

administrator_permission = Permission(RoleNeed('administrator'))
team_member_permission = Permission(RoleNeed('team member'))
project_manager_permission = Permission(RoleNeed('project manager'))
client_permission = Permission(RoleNeed('client'))

@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
	identity.user = current_user

	if hasattr(current_user, 'rolename'):
			identity.provides.add(RoleNeed(current_user.rolename))

class UserSchema(Schema):
Example #4
0
def _get_principal(app):
    p = Principal(app, use_sessions=False)
    p.identity_loader(_identity_loader)
    return p
Example #5
0
	'flask_debugtoolbar.panels.profiler.ProfilerDebugPanel',
        # Add the MongoDB panel
	'flask_debugtoolbar_mongo.panel.MongoDebugPanel',
	]

app.secret_key = 'secureKey'
app.config["SECRET_KEY"] = "secureKey"
app.debug=True


db = MongoEngine(app)
tool = DebugToolbarExtension(app)
thumb = Thumbnail(app)


principals = Principal(app)
normal_role = RoleNeed('normal')
normal_permission = Permission(normal_role)
admin_role = RoleNeed('admin')
admin_permission = Permission(admin_role)
principals._init_app(app)

# # User Information providers
# @identity_loaded.connect_via(app)
# def on_identity_loaded(sender, identity):
#     user = Usuario.objects.get(perfil=identity)


lm = LoginManager()
lm.login_view = "login"
lm.login_message = u"Es necesario que este registrado para acceder."
def create_app():
    app = flask.Flask(__name__)
    app.config.from_object("config")

    init_db(app)

    api.init_app(app)

    security = Security()
    security.init_app(app, users)

    principals = Principal()
    principals.init_app(app)

    configure_uploads(app, logos)

    from app.views import menu, bp

    from app.signals import init_signals

    init_signals(app)

    menu.init_app(app)
    app.register_blueprint(bp)

    if False:
        login_manager = LoginManager(app)

        @login_manager.request_loader
        def load_user_from_request(request):
            if request.authorization:
                email, password = request.authorization.username, request.authorization.password
                user = User.query.filter_by(email=unicode(email)).first()

                if user is not None:
                    if verify_password(password, user.password):
                        return user

    @identity_loaded.connect_via(app)
    def on_identity_loaded(sender, identity):
        if not isinstance(identity, AnonymousIdentity):
            identity.provides.add(UserNeed(identity.id))

            for role in current_user.roles:
                identity.provides.add(RoleNeed(role.name))

    @principals.identity_loader
    def read_identity_from_flask_login():
        if current_user.is_authenticated():
            return Identity(current_user.id)
        return AnonymousIdentity()

    @app.after_request
    def somefunc(response):
        if "cid" not in flask.request.cookies:
            cid = str(uuid.uuid4())
            expires = datetime.datetime.now() + datetime.timedelta(days=365 * 2)
            response.set_cookie("cid", cid, expires=expires)
        return response

    return app
def get_principals(app):
    p = Principal(app)
    p.identity_loader(load_identity)
    return p