Ejemplo n.º 1
0
    def init_app(self, app, sessionstore=None, register_blueprint=True):
        """Flask application initialization.

        The following actions are executed:

        #. Initialize the configuration.

        #. Monkey-patch Flask-Security.

        #. Create the user datastore.

        #. Create the sessionstore.

        #. Initialize the extension, the forms to register users and
           confirms their emails, the CLI and, if ``ACCOUNTS_USE_CELERY`` is
           ``True``, register a celery task to send emails.

        #. Override Flask-Security's default login view function.

        #. Warn if inconsistent configuration is detected

        :param app: The Flask application.
        :param sessionstore: store for sessions. Passed to
            ``flask-kvsession``. If ``None`` then Redis is configured.
            (Default: ``None``)
        :param register_blueprint: If ``True``, the application registers the
            blueprints. (Default: ``True``)
        """
        self.init_config(app)

        # Monkey-patch Flask-Security
        InvenioAccounts.monkey_patch_flask_security()

        # Create user datastore
        if not self.datastore:
            self.datastore = SessionAwareSQLAlchemyUserDatastore(
                db, User, Role)

        if app.config["ACCOUNTS_SESSION_ACTIVITY_ENABLED"]:
            self._enable_session_activity(app=app)

        # Initialize extension.
        _register_blueprint = app.config.get("ACCOUNTS_REGISTER_BLUEPRINT")
        if _register_blueprint is not None:
            register_blueprint = _register_blueprint

        state = self.security.init_app(app,
                                       datastore=self.datastore,
                                       register_blueprint=register_blueprint)

        # Override Flask-Security's default login view function
        new_login_view = obj_or_import_string(
            app.config.get("ACCOUNTS_LOGIN_VIEW_FUNCTION"))
        if new_login_view is not None:
            app.view_functions["security.login"] = new_login_view

        self.register_anonymous_identity_loader(state)

        app.extensions["security"].register_form = register_form_factory(
            app.extensions["security"].register_form, app)

        app.extensions[
            "security"].confirm_register_form = confirm_register_form_factory(
                app.extensions["security"].confirm_register_form, app)

        app.extensions["security"].login_form = login_form_factory(
            app.extensions["security"].login_form, app)

        if app.config["ACCOUNTS_USE_CELERY"]:
            from invenio_accounts.tasks import send_security_email

            @state.send_mail_task
            def delay_security_email(msg):
                send_security_email.delay(msg.__dict__)

        # Register context processor
        if app.config["ACCOUNTS_JWT_DOM_TOKEN"]:
            from invenio_accounts.context_processors.jwt import jwt_proccessor

            app.context_processor(jwt_proccessor)

        # Register signal receiver
        if app.config.get("ACCOUNTS_USERINFO_HEADERS"):
            request_finished.connect(set_session_info, app)

        # Set Session KV store
        session_kvstore_factory = obj_or_import_string(
            app.config["ACCOUNTS_SESSION_STORE_FACTORY"])
        session_kvstore = session_kvstore_factory(app)

        self.kvsession_extension = KVSessionExtension(session_kvstore, app)

        self.check_configuration_consistency(app)

        app.extensions["invenio-accounts"] = self
Ejemplo n.º 2
0
    def init_app(self, app, sessionstore=None, register_blueprint=True):
        """Flask application initialization.

        The following actions are executed:

        #. Initialize the configuration.

        #. Monkey-patch Flask-Security.

        #. Create the user datastore.

        #. Create the sessionstore.

        #. Initialize the extension, the forms to register users and
           confirms their emails, the CLI and, if ``ACCOUNTS_USE_CELERY`` is
           ``True``, register a celery task to send emails.

        :param app: The Flask application.
        :param sessionstore: store for sessions. Passed to
            ``flask-kvsession``. If ``None`` then Redis is configured.
            (Default: ``None``)
        :param register_blueprint: If ``True``, the application registers the
            blueprints. (Default: ``True``)
        """
        self.init_config(app)

        # Monkey-patch Flask-Security
        InvenioAccounts.monkey_patch_flask_security()

        # Create user datastore
        if not self.datastore:
            self.datastore = SessionAwareSQLAlchemyUserDatastore(
                db, User, Role)

        if app.config['ACCOUNTS_SESSION_ACTIVITY_ENABLED']:
            self._enable_session_activity(app=app)

        # Initialize extension.
        _register_blueprint = app.config.get('ACCOUNTS_REGISTER_BLUEPRINT')
        if _register_blueprint is not None:
            register_blueprint = _register_blueprint

        state = self.security.init_app(app,
                                       datastore=self.datastore,
                                       register_blueprint=register_blueprint)

        self.register_anonymous_identity_loader(state)

        app.extensions['security'].register_form = register_form_factory(
            app.extensions['security'].register_form, app)

        app.extensions['security'].confirm_register_form = \
            confirm_register_form_factory(
                app.extensions['security'].confirm_register_form, app
            )

        app.extensions['security'].login_form = login_form_factory(
            app.extensions['security'].login_form, app)

        if app.config['ACCOUNTS_USE_CELERY']:
            from invenio_accounts.tasks import send_security_email

            @state.send_mail_task
            def delay_security_email(msg):
                send_security_email.delay(msg.__dict__)

        # Register context processor
        if app.config['ACCOUNTS_JWT_DOM_TOKEN']:
            from invenio_accounts.context_processors.jwt import \
                jwt_proccessor
            app.context_processor(jwt_proccessor)

        app.extensions['invenio-accounts'] = self