Giter VIP home page Giter VIP logo

Comments (6)

HarryPayne avatar HarryPayne commented on May 20, 2024 3

I'm using permissions and needs and adding the needs in the same place:

def identity_loader(payload):
    """Return user object referred to in payload."""
    username = payload["identity"]["username"] or None
    current_user = User(username=username)
    identity = Identity(username)
    if hasattr(current_user, "roles"):
        for role in current_user.roles:
             identity.provides.add(RoleNeed(role))
    identity_changed.send(current_app._get_current_object(),
                          identity=identity)
    return current_user


curator_permission = Permission(RoleNeed("Curator"))
curator_permission.description = "User must be a Curator"


@app.route("/projectEdit/<projectID>/<tableName>", methods=["POST"])
@jwt_required()
@curator_permission.require(http_exception=403)
def projectEdit(projectID, tableName):
    """ Update specified table for specified project."""

Just make sure that @jwt_required() comes ahead of the permission.

from flask-principal.

orarbel avatar orarbel commented on May 20, 2024 1

Here is how I do it:
JWT(app, authenticate, identity_loader)


def identity_loader(payload):
    user_id = payload['identity']

    try:
        current_user = User.query.filter_by(id=user_id).first()
        identity_changed.send(current_app._get_current_object(),
                              identity=Identity(user_id))
    except Exception as e:
        db_session.rollback()
        db_session.flush()
        raise e

    return current_user

from flask-principal.

AbdealiLoKo avatar AbdealiLoKo commented on May 20, 2024 1

Is there any documentation on how to do this now ?
Wanted to do the same thing and was hoping to read up on it :)

from flask-principal.

iyn avatar iyn commented on May 20, 2024

I'm also interested in this.

@mattupstate can you just provide a general overview how one would approach using flask-princpal with flask-jwt? Thanks!

from flask-principal.

 avatar commented on May 20, 2024

If this would still be valued, I'm going to tackle it over the next few weeks or so. I could write it up and do a PR if all goes well.

from flask-principal.

krisfris avatar krisfris commented on May 20, 2024

Would the following be a good identity loader as well?

@principal.identity_loader
def _identity_loader():
    if hasattr(current_user, 'id'):
        return Identity(current_user.id)

@user_loaded_from_request.connect_via(app)
def on_user_loaded_from_request(sender, user):
    identity_changed.send(
        flask.current_app._get_current_object(),
        identity=Identity(user.id)
    )

@identity_loaded.connect
def on_identity_loaded(sender, identity):

    # Set the identity user object
    identity.user = current_user

    if not hasattr(current_user, 'id'):
        return

    # Add the UserNeed to the identity
    identity.provides.add(UserNeed(current_user.id))

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

Not quite sure why it's necessary to add an identity loader as Identity is already being instantiated in the user_loaded_from_request function when sending the identity_changed signal and I check for the presence of id in the identity_loaded function already but otherwise it doesn't work.

from flask-principal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.