librelist archives

« back to archive

Flask Principal

Flask Principal

From:
Alessio Civitillo
Date:
2011-07-19 @ 18:50
Hello,

someone could help me out with this small example? It should be
allowed to log in admin, but it doesn't.

from flask import Flask, Response
from flaskext.principal import Principal, Permission, RoleNeed,
identity_loaded, UserNeed

app = Flask(__name__)

# load the extension
principals = Principal(app)
# Create a permission with a single Need, in this case a RoleNeed.
admin_permission = Permission(RoleNeed('admin'))

@principals.identity_loader
def load_identity_from_weird_usecase():
    return Identity('ali')

@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
    # Get the user information from the db
    # Update the roles that a user can provide
	 identity.provides.add(RoleNeed('admin'))


# protect a view with a principal for that need
@app.route('/admin')
@admin_permission.require()
def do_admin_index():
    return Response('Only if you are an admin')

# this time protect with a context manager
@app.route('/')
def do_articles():
    return 'test'

app.run()

Thanks
--

Re: [flask] Flask Principal

From:
Col Wilson
Date:
2011-07-20 @ 06:36
You could have a look at

http://terse-words.blogspot.com/2011/06/flask-extensions-for-authorization-with.htmlwhich
I use myself when I'm setting up auth in Flask.

Col

On Tue, Jul 19, 2011 at 7:50 PM, Alessio Civitillo <
alessiocivitillo@gmail.com> wrote:

> Hello,
>
> someone could help me out with this small example? It should be
> allowed to log in admin, but it doesn't.
>
> from flask import Flask, Response
> from flaskext.principal import Principal, Permission, RoleNeed,
> identity_loaded, UserNeed
>
> app = Flask(__name__)
>
> # load the extension
> principals = Principal(app)
> # Create a permission with a single Need, in this case a RoleNeed.
> admin_permission = Permission(RoleNeed('admin'))
>
> @principals.identity_loader
> def load_identity_from_weird_usecase():
>    return Identity('ali')
>
> @identity_loaded.connect_via(app)
> def on_identity_loaded(sender, identity):
>    # Get the user information from the db
>    # Update the roles that a user can provide
>         identity.provides.add(RoleNeed('admin'))
>
>
> # protect a view with a principal for that need
> @app.route('/admin')
> @admin_permission.require()
> def do_admin_index():
>    return Response('Only if you are an admin')
>
> # this time protect with a context manager
> @app.route('/')
> def do_articles():
>    return 'test'
>
> app.run()
>
> Thanks
> --
>



-- 
Col Wilson
http://terse-words.blogspot.com/

Re: [flask] Flask Principal

From:
Alessio Civitillo
Date:
2011-07-20 @ 12:00
Can't reach your link.

On Wed, Jul 20, 2011 at 8:36 AM, Col Wilson <colwilson@bcs.org> wrote:

> You could have a look at
> 
http://terse-words.blogspot.com/2011/06/flask-extensions-for-authorization-with.htmlwhich
I use myself when I'm setting up auth in Flask.
>
> Col
>
>
> On Tue, Jul 19, 2011 at 7:50 PM, Alessio Civitillo <
> alessiocivitillo@gmail.com> wrote:
>
>> Hello,
>>
>> someone could help me out with this small example? It should be
>> allowed to log in admin, but it doesn't.
>>
>> from flask import Flask, Response
>> from flaskext.principal import Principal, Permission, RoleNeed,
>> identity_loaded, UserNeed
>>
>> app = Flask(__name__)
>>
>> # load the extension
>> principals = Principal(app)
>> # Create a permission with a single Need, in this case a RoleNeed.
>> admin_permission = Permission(RoleNeed('admin'))
>>
>> @principals.identity_loader
>> def load_identity_from_weird_usecase():
>>    return Identity('ali')
>>
>> @identity_loaded.connect_via(app)
>> def on_identity_loaded(sender, identity):
>>    # Get the user information from the db
>>    # Update the roles that a user can provide
>>         identity.provides.add(RoleNeed('admin'))
>>
>>
>> # protect a view with a principal for that need
>> @app.route('/admin')
>> @admin_permission.require()
>> def do_admin_index():
>>    return Response('Only if you are an admin')
>>
>> # this time protect with a context manager
>> @app.route('/')
>> def do_articles():
>>    return 'test'
>>
>> app.run()
>>
>> Thanks
>> --
>>
>
>
>
> --
> Col Wilson
> http://terse-words.blogspot.com/
>



-- 
Regards,
------------------------------------
Alessio Civitillo
alessiocivitillo@gmail.com
Mobile: (0045) 52645608
Linkedin: http://it.linkedin.com/in/alessiocivitillo

Re: [flask] Flask Principal

From:
Col Wilson
Date:
2011-07-20 @ 12:52
Really? it's on Google's Blogger so unless you're in China?

Col

On Wed, Jul 20, 2011 at 1:00 PM, Alessio Civitillo <
alessiocivitillo@gmail.com> wrote:

> Can't reach your link.
>
>
> On Wed, Jul 20, 2011 at 8:36 AM, Col Wilson <colwilson@bcs.org> wrote:
>
>> You could have a look at
>> 
http://terse-words.blogspot.com/2011/06/flask-extensions-for-authorization-with.htmlwhich
I use myself when I'm setting up auth in Flask.
>>
>> Col
>>
>>
>> On Tue, Jul 19, 2011 at 7:50 PM, Alessio Civitillo <
>> alessiocivitillo@gmail.com> wrote:
>>
>>> Hello,
>>>
>>> someone could help me out with this small example? It should be
>>> allowed to log in admin, but it doesn't.
>>>
>>> from flask import Flask, Response
>>> from flaskext.principal import Principal, Permission, RoleNeed,
>>> identity_loaded, UserNeed
>>>
>>> app = Flask(__name__)
>>>
>>> # load the extension
>>> principals = Principal(app)
>>> # Create a permission with a single Need, in this case a RoleNeed.
>>> admin_permission = Permission(RoleNeed('admin'))
>>>
>>> @principals.identity_loader
>>> def load_identity_from_weird_usecase():
>>>    return Identity('ali')
>>>
>>> @identity_loaded.connect_via(app)
>>> def on_identity_loaded(sender, identity):
>>>    # Get the user information from the db
>>>    # Update the roles that a user can provide
>>>         identity.provides.add(RoleNeed('admin'))
>>>
>>>
>>> # protect a view with a principal for that need
>>> @app.route('/admin')
>>> @admin_permission.require()
>>> def do_admin_index():
>>>    return Response('Only if you are an admin')
>>>
>>> # this time protect with a context manager
>>> @app.route('/')
>>> def do_articles():
>>>    return 'test'
>>>
>>> app.run()
>>>
>>> Thanks
>>> --
>>>
>>
>>
>>
>> --
>> Col Wilson
>> http://terse-words.blogspot.com/
>>
>
>
>
> --
> Regards,
> ------------------------------------
>
> Alessio Civitillo
> alessiocivitillo@gmail.com
> Mobile: (0045) 52645608
> Linkedin: http://it.linkedin.com/in/alessiocivitillo
>



-- 
Col Wilson
http://terse-words.blogspot.com/

Re: [flask] Flask Principal

From:
Alessio Civitillo
Date:
2011-07-20 @ 13:38
Sorry, my bad. It works now.
Anyway, I understand all the part below:

# flask-principal
principals = Principal()
normal_role = RoleNeed('normal')
normal_permission = Permission(normal_role)
principals._init_app(app)

But now you have:

@normal_permission.require(http_exception=403)@app.route('/')
def home():
    return Response("Hello World!")

How do you pass the normal role to the current user?


On Wed, Jul 20, 2011 at 2:52 PM, Col Wilson <colwilson@bcs.org> wrote:

> Really? it's on Google's Blogger so unless you're in China?
>
> Col
>
>
> On Wed, Jul 20, 2011 at 1:00 PM, Alessio Civitillo <
> alessiocivitillo@gmail.com> wrote:
>
>> Can't reach your link.
>>
>>
>> On Wed, Jul 20, 2011 at 8:36 AM, Col Wilson <colwilson@bcs.org> wrote:
>>
>>> You could have a look at
>>> 
http://terse-words.blogspot.com/2011/06/flask-extensions-for-authorization-with.htmlwhich
I use myself when I'm setting up auth in Flask.
>>>
>>> Col
>>>
>>>
>>> On Tue, Jul 19, 2011 at 7:50 PM, Alessio Civitillo <
>>> alessiocivitillo@gmail.com> wrote:
>>>
>>>> Hello,
>>>>
>>>> someone could help me out with this small example? It should be
>>>> allowed to log in admin, but it doesn't.
>>>>
>>>> from flask import Flask, Response
>>>> from flaskext.principal import Principal, Permission, RoleNeed,
>>>> identity_loaded, UserNeed
>>>>
>>>> app = Flask(__name__)
>>>>
>>>> # load the extension
>>>> principals = Principal(app)
>>>> # Create a permission with a single Need, in this case a RoleNeed.
>>>> admin_permission = Permission(RoleNeed('admin'))
>>>>
>>>> @principals.identity_loader
>>>> def load_identity_from_weird_usecase():
>>>>    return Identity('ali')
>>>>
>>>> @identity_loaded.connect_via(app)
>>>> def on_identity_loaded(sender, identity):
>>>>    # Get the user information from the db
>>>>    # Update the roles that a user can provide
>>>>         identity.provides.add(RoleNeed('admin'))
>>>>
>>>>
>>>> # protect a view with a principal for that need
>>>> @app.route('/admin')
>>>> @admin_permission.require()
>>>> def do_admin_index():
>>>>    return Response('Only if you are an admin')
>>>>
>>>> # this time protect with a context manager
>>>> @app.route('/')
>>>> def do_articles():
>>>>    return 'test'
>>>>
>>>> app.run()
>>>>
>>>> Thanks
>>>> --
>>>>
>>>
>>>
>>>
>>> --
>>> Col Wilson
>>> http://terse-words.blogspot.com/
>>>
>>
>>
>>
>> --
>> Regards,
>> ------------------------------------
>>
>> Alessio Civitillo
>> alessiocivitillo@gmail.com
>> Mobile: (0045) 52645608
>> Linkedin: http://it.linkedin.com/in/alessiocivitillo
>>
>
>
>
> --
> Col Wilson
> http://terse-words.blogspot.com/
>



-- 
Regards,
------------------------------------
Alessio Civitillo
alessiocivitillo@gmail.com
Mobile: (0045) 52645608
Linkedin: http://it.linkedin.com/in/alessiocivitillo

Re: [flask] Flask Principal

From:
Charlie Evett
Date:
2011-07-20 @ 13:00
Link works for me.

On Wed, Jul 20, 2011 at 8:52 AM, Col Wilson <colwilson@bcs.org> wrote:

> Really? it's on Google's Blogger so unless you're in China?
>
> Col
>
>
> On Wed, Jul 20, 2011 at 1:00 PM, Alessio Civitillo <
> alessiocivitillo@gmail.com> wrote:
>
>> Can't reach your link.
>>
>>
>> On Wed, Jul 20, 2011 at 8:36 AM, Col Wilson <colwilson@bcs.org> wrote:
>>
>>> You could have a look at
>>> 
http://terse-words.blogspot.com/2011/06/flask-extensions-for-authorization-with.htmlwhich
I use myself when I'm setting up auth in Flask.
>>>
>>> Col
>>>
>>>
>>> On Tue, Jul 19, 2011 at 7:50 PM, Alessio Civitillo <
>>> alessiocivitillo@gmail.com> wrote:
>>>
>>>> Hello,
>>>>
>>>> someone could help me out with this small example? It should be
>>>> allowed to log in admin, but it doesn't.
>>>>
>>>> from flask import Flask, Response
>>>> from flaskext.principal import Principal, Permission, RoleNeed,
>>>> identity_loaded, UserNeed
>>>>
>>>> app = Flask(__name__)
>>>>
>>>> # load the extension
>>>> principals = Principal(app)
>>>> # Create a permission with a single Need, in this case a RoleNeed.
>>>> admin_permission = Permission(RoleNeed('admin'))
>>>>
>>>> @principals.identity_loader
>>>> def load_identity_from_weird_usecase():
>>>>    return Identity('ali')
>>>>
>>>> @identity_loaded.connect_via(app)
>>>> def on_identity_loaded(sender, identity):
>>>>    # Get the user information from the db
>>>>    # Update the roles that a user can provide
>>>>         identity.provides.add(RoleNeed('admin'))
>>>>
>>>>
>>>> # protect a view with a principal for that need
>>>> @app.route('/admin')
>>>> @admin_permission.require()
>>>> def do_admin_index():
>>>>    return Response('Only if you are an admin')
>>>>
>>>> # this time protect with a context manager
>>>> @app.route('/')
>>>> def do_articles():
>>>>    return 'test'
>>>>
>>>> app.run()
>>>>
>>>> Thanks
>>>> --
>>>>
>>>
>>>
>>>
>>> --
>>> Col Wilson
>>> http://terse-words.blogspot.com/
>>>
>>
>>
>>
>> --
>> Regards,
>> ------------------------------------
>>
>> Alessio Civitillo
>> alessiocivitillo@gmail.com
>> Mobile: (0045) 52645608
>> Linkedin: http://it.linkedin.com/in/alessiocivitillo
>>
>
>
>
> --
> Col Wilson
> http://terse-words.blogspot.com/
>

Re: Flask Principal

From:
Alessio Civitillo
Date:
2011-07-20 @ 05:20
Nobody with an advice on this?

On 7/19/11, Alessio Civitillo <alessiocivitillo@gmail.com> wrote:
> Hello,
>
> someone could help me out with this small example? It should be
> allowed to log in admin, but it doesn't.
>
> from flask import Flask, Response
> from flaskext.principal import Principal, Permission, RoleNeed,
> identity_loaded, UserNeed
>
> app = Flask(__name__)
>
> # load the extension
> principals = Principal(app)
> # Create a permission with a single Need, in this case a RoleNeed.
> admin_permission = Permission(RoleNeed('admin'))
>
> @principals.identity_loader
> def load_identity_from_weird_usecase():
>     return Identity('ali')
>
> @identity_loaded.connect_via(app)
> def on_identity_loaded(sender, identity):
>     # Get the user information from the db
>     # Update the roles that a user can provide
> 	 identity.provides.add(RoleNeed('admin'))
>
>
> # protect a view with a principal for that need
> @app.route('/admin')
> @admin_permission.require()
> def do_admin_index():
>     return Response('Only if you are an admin')
>
> # this time protect with a context manager
> @app.route('/')
> def do_articles():
>     return 'test'
>
> app.run()
>
> Thanks
> --
>


-- 
Regards,
------------------------------------
Alessio Civitillo
alessiocivitillo@gmail.com
Mobile: (0045) 52645608
Linkedin: http://it.linkedin.com/in/alessiocivitillo