本文整理汇总了Python中flask_principal.RoleNeed方法的典型用法代码示例。如果您正苦于以下问题:Python flask_principal.RoleNeed方法的具体用法?Python flask_principal.RoleNeed怎么用?Python flask_principal.RoleNeed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask_principal
的用法示例。
在下文中一共展示了flask_principal.RoleNeed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_record_patron_create
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def test_record_patron_create(client, db, users, with_role_creator):
"""Test patron create."""
tests = [
({"foo": "bar"}, "create", True),
({"foo": "bar"}, "update", False),
({"foo": "bar"}, "delete", False),
]
@identity_loaded.connect
def add_roles_to_identity(sender, identity):
"""Provide additional role to the user."""
roles = [RoleNeed("records-creators")]
identity.provides |= set(roles)
for access, action, is_allowed in tests:
# create role to be able to create records
user_login(client, "patron1", users)
id = uuid.uuid4()
record = Record.create(access, id_=id)
factory = RecordPermission(record, action)
assert factory.can() if is_allowed else not factory.can()
示例2: on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def on_identity_loaded(sender, identity):
# Set the identity user object
identity.user = current_user
# Add the UserNeed to the identity
if hasattr(current_user, 'username'):
identity.provides.add(UserNeed(current_user.username))
# Assuming the User model has a list of roles, update the
# identity with the roles that the user provides
if hasattr(current_user, 'role'):
# for role in current_user.roles:
identity.provides.add(RoleNeed(current_user.role))
# if current_user.is_superuser:
if hasattr(current_user, 'is_superuser') and current_user.is_superuser:
identity.provides.add(su_need)
# return current_user.role
identity.allow_su = su_permission.allows(identity)
identity.allow_admin = admin_permission.allows(identity)
identity.allow_edit = editor_permission.allows(identity)
identity.allow_general = general_permission.allows(identity)
示例3: record_needs
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def record_needs(self):
"""Create needs of the record."""
needs = []
for access_entity in self.record_explicit_restrictions():
try:
if isinstance(access_entity, string_types):
needs.append(UserNeed(int(access_entity)))
elif isinstance(access_entity, int):
needs.append(UserNeed(access_entity))
except ValueError:
needs.append(RoleNeed(access_entity.lower()))
return needs
示例4: roles_required
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def roles_required(*roles):
"""Decorator which specifies that a user must have all the specified roles.
Example::
@app.route('/dashboard')
@roles_required('admin', 'editor')
def dashboard():
return 'Dashboard'
The current user must have both the `admin` role and `editor` role in order
to view the page.
:param roles: The required roles.
"""
def wrapper(fn):
@wraps(fn)
def decorated_view(*args, **kwargs):
perms = [Permission(RoleNeed(role)) for role in roles]
for perm in perms:
if not perm.can():
if _security._unauthorized_callback:
# Backwards compat - deprecated
return _security._unauthorized_callback()
return _security._unauthz_handler(roles_required, list(roles))
return fn(*args, **kwargs)
return decorated_view
return wrapper
示例5: roles_accepted
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def roles_accepted(*roles):
"""Decorator which specifies that a user must have at least one of the
specified roles. Example::
@app.route('/create_post')
@roles_accepted('editor', 'author')
def create_post():
return 'Create Post'
The current user must have either the `editor` role or `author` role in
order to view the page.
:param roles: The possible roles.
"""
def wrapper(fn):
@wraps(fn)
def decorated_view(*args, **kwargs):
perm = Permission(*[RoleNeed(role) for role in roles])
if perm.can():
return fn(*args, **kwargs)
if _security._unauthorized_callback:
# Backwards compat - deprecated
return _security._unauthorized_callback()
return _security._unauthz_handler(roles_accepted, list(roles))
return decorated_view
return wrapper
示例6: _on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def _on_identity_loaded(sender, identity):
if hasattr(current_user, "fs_uniquifier"):
identity.provides.add(UserNeed(current_user.fs_uniquifier))
for role in getattr(current_user, "roles", []):
identity.provides.add(RoleNeed(role.name))
for fsperm in role.get_permissions():
identity.provides.add(FsPermNeed(fsperm))
identity.user = current_user
示例7: __init__
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def __init__(self, *needs):
'''Let administrator bypass all permissions'''
super(Permission, self).__init__(RoleNeed('admin'), *needs)
示例8: blogger_permission
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def blogger_permission(self):
if self._blogger_permission is None:
if self.config.get("BLOGGING_PERMISSIONS", False):
self._blogger_permission = Permission(RoleNeed(
self.config.get("BLOGGING_PERMISSIONNAME", "blogger")))
else:
self._blogger_permission = Permission()
return self._blogger_permission
示例9: __init__
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def __init__(self):
needs = [RoleNeed("admin")]
sensitive_domain_roles = current_app.config.get("SENSITIVE_DOMAIN_ROLES", [])
if sensitive_domain_roles:
for role in sensitive_domain_roles:
needs.append(RoleNeed(role))
super(SensitiveDomainPermission, self).__init__(*needs)
示例10: on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def on_identity_loaded(sender, identity):
user = load_user(identity.id)
if user is None:
return
identity.provides.add(UserNeed(user.get_name()))
if user.is_user():
identity.provides.add(RoleNeed("user"))
if user.is_admin():
identity.provides.add(RoleNeed("admin"))
示例11: roles_accepted
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def roles_accepted(*roles):
"""
Decorator which specifies that a user must have at least one of the
specified roles.
Aborts with HTTP: 403 if the user doesn't have at least one of the roles.
Example::
@app.route('/create_post')
@roles_accepted('ROLE_ADMIN', 'ROLE_EDITOR')
def create_post():
return 'Create Post'
The current user must have either the `ROLE_ADMIN` role or `ROLE_EDITOR`
role in order to view the page.
:param roles: The possible roles.
"""
def wrapper(fn):
@wraps(fn)
def decorated_view(*args, **kwargs):
perm = Permission(*[RoleNeed(role) for role in roles])
if not perm.can():
abort(HTTPStatus.FORBIDDEN)
return fn(*args, **kwargs)
return decorated_view
return wrapper
示例12: roles_required
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def roles_required(*roles):
"""
Decorator which specifies that a user must have all the specified roles.
Aborts with HTTP 403: Forbidden if the user doesn't have the required roles.
Example::
@app.route('/dashboard')
@roles_required('ROLE_ADMIN', 'ROLE_EDITOR')
def dashboard():
return 'Dashboard'
The current user must have both the `ROLE_ADMIN` and `ROLE_EDITOR` roles
in order to view the page.
:param roles: The required roles.
"""
def wrapper(fn):
@wraps(fn)
def decorated_view(*args, **kwargs):
perms = [Permission(RoleNeed(role)) for role in roles]
for perm in perms:
if not perm.can():
abort(HTTPStatus.FORBIDDEN)
return fn(*args, **kwargs)
return decorated_view
return wrapper
示例13: _on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def _on_identity_loaded(self, sender, identity: Identity) -> None:
"""
Callback that runs whenever a new identity has been loaded.
"""
if hasattr(current_user, 'id'):
identity.provides.add(UserNeed(current_user.id))
for role in getattr(current_user, 'roles', []):
identity.provides.add(RoleNeed(role.name))
identity.user = current_user
示例14: init_app
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def init_app(app):
@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
'''基础权限'''
identity.user = current_user
if hasattr(current_user, 'id'):
identity.provides.add(UserNeed(current_user.id))
if hasattr(current_user, 'is_superuser'):
if current_user.is_superuser:
identity.provides.add(RoleNeed('super'))
if hasattr(current_user, 'is_confirmed'):
if current_user.is_confirmed:
identity.provides.add(RoleNeed('confirmed'))
if hasattr(current_user, 'is_authenticated'):
if current_user.is_authenticated:
identity.provides.add(RoleNeed('auth'))
else:
identity.provides.add(RoleNeed('guest'))
if hasattr(current_user, 'topics'):
for topic in current_user.topics:
identity.provides.add(TopicNeed(topic.id))
if hasattr(current_user, 'replies'):
for reply in current_user.replies:
identity.provides.add(ReplyNeed(reply.id))
if hasattr(current_user, 'collects'):
for collect in current_user.collects:
identity.provides.add(CollectNeed(collect.id))
示例15: on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import RoleNeed [as 别名]
def on_identity_loaded(sender, identity):
"""Helper for flask-principal."""
# Set the identity user object
identity.user = current_user
# Add the UserNeed to the identity
if hasattr(current_user, "id"):
identity.provides.add(UserNeed(current_user.id))
try:
if current_user.is_admin:
identity.provides.add(RoleNeed("admin"))
except AttributeError:
pass # Definitely not an admin