本文整理汇总了Python中flask_principal.UserNeed方法的典型用法代码示例。如果您正苦于以下问题:Python flask_principal.UserNeed方法的具体用法?Python flask_principal.UserNeed怎么用?Python flask_principal.UserNeed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask_principal
的用法示例。
在下文中一共展示了flask_principal.UserNeed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [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)
示例2: patron_permission
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [as 别名]
def patron_permission(patron_pid):
"""Return a permission for the given patron."""
return Permission(UserNeed(int(patron_pid)), backoffice_access_action)
示例3: __init__
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [as 别名]
def __init__(self, record):
"""Constructor."""
super(PatronOwnerPermission, self).__init__(
UserNeed(int(record["patron_pid"])), backoffice_access_action
)
示例4: record_needs
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [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
示例5: _on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [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
示例6: on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [as 别名]
def on_identity_loaded(sender, identity):
"""Method for Flask Principal identity load listener
"""
# set the identity user object
identity.user = current_user
if current_user.is_authenticated:
# add UserNeed to identity
identity.provides.add(UserNeed(current_user.id))
示例7: on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [as 别名]
def on_identity_loaded(sender, identity):
"""
Sets the identity of a given option, assigns additional permissions based on
the role that the user is a part of.
:param sender:
:param identity:
"""
# load the user
user = user_service.get(identity.id)
# add the UserNeed to the identity
identity.provides.add(UserNeed(identity.id))
# identity with the roles that the user provides
if hasattr(user, "roles"):
for role in user.roles:
identity.provides.add(RoleNeed(role.name))
identity.provides.add(RoleMemberNeed(role.id))
# apply ownership for authorities
if hasattr(user, "authorities"):
for authority in user.authorities:
identity.provides.add(AuthorityCreatorNeed(authority.id))
g.user = user
示例8: on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [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"))
示例9: _on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [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
示例10: populate_identity_roles
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [as 别名]
def populate_identity_roles(identity, user=None):
identity.user = user
if user is None or user.is_anonymous:
if current_app.config['POLICY_ANONYMOUS_VIEW_INDEX']:
identity.provides.add(roles.index_view)
if current_app.config['POLICY_ANONYMOUS_VIEW_POST']:
identity.provides.add(roles.post_view)
if current_app.config['POLICY_ANONYMOUS_VIEW_STATS']:
identity.provides.add(roles.stats_view)
if current_app.config['POLICY_ANONYMOUS_VIEW_TAGS']:
identity.provides.add(roles.tags_view)
if current_app.config['POLICY_ANONYMOUS_DOWNLOADS']:
identity.provides.add(roles.post_download)
else:
identity.provides.add(UserNeed(user.identifier))
identity.provides.add(roles.index_view)
identity.provides.add(roles.post_view)
identity.provides.add(roles.post_edit)
identity.provides.add(roles.post_comment)
identity.provides.add(roles.post_download)
identity.provides.add(roles.stats_view)
identity.provides.add(roles.tags_view)
# TODO: Populate group permissions, and port existing group admin
# code to roles.
return identity
示例11: init_app
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [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))
示例12: on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [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
示例13: auth_required_same_user
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [as 别名]
def auth_required_same_user(*args, **kwargs):
"""Decorator for requiring an authenticated user to be the same as the
user in the URL parameters. By default the user url parameter name to
lookup is 'id', but this can be customized by passing an argument:
@auth_require_same_user('user_id')
@bp.route('/users/<int:user_id>/foo/<int:id>')
def get(user_id, id):
# do stuff
Any keyword arguments are passed along to the @auth_required decorator,
so roles can also be specified in the same was as it, eg:
@auth_required_same_user('user_id', role='ROLE_ADMIN')
Aborts with HTTP 403: Forbidden if the user-check fails
"""
auth_kwargs = {}
user_id_parameter_name = 'id'
if not was_decorated_without_parenthesis(args):
auth_kwargs = kwargs
if args and isinstance(args[0], str):
user_id_parameter_name = args[0]
def wrapper(fn):
@wraps(fn)
@auth_required(**auth_kwargs)
def decorated(*args, **kwargs):
try:
user_id = request.view_args[user_id_parameter_name]
except KeyError:
raise KeyError('Unable to find the user lookup parameter '
f'{user_id_parameter_name} in the url args')
if not Permission(UserNeed(user_id)).can():
abort(HTTPStatus.FORBIDDEN)
return fn(*args, **kwargs)
return decorated
if was_decorated_without_parenthesis(args):
return wrapper(args[0])
return wrapper
示例14: on_identity_loaded
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [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, 'id'):
identity.provides.add(UserNeed(current_user.id))
# Assuming the User model has a list of groups, update the
# identity with the groups that the user provides
if hasattr(current_user, 'flicket_groups'):
the_user = FlicketUser.query.filter_by(id=current_user.id).first()
for g in the_user.flicket_groups:
identity.provides.add(RoleNeed('{}'.format(g.group_name)))
示例15: owner_permission_factory
# 需要导入模块: import flask_principal [as 别名]
# 或者: from flask_principal import UserNeed [as 别名]
def owner_permission_factory(record=None):
"""Permission factory with owner access."""
return Permission(UserNeed(record["owner"]))