Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在flask stormpath中,如何在基于用户';登录后将用户路由到特定页面;s Stormpath组_Python_Flask_Flask Login_Stormpath - Fatal编程技术网

Python 在flask stormpath中,如何在基于用户';登录后将用户路由到特定页面;s Stormpath组

Python 在flask stormpath中,如何在基于用户';登录后将用户路由到特定页面;s Stormpath组,python,flask,flask-login,stormpath,Python,Flask,Flask Login,Stormpath,问题陈述 我正在开发一个使用Stormpath进行身份验证的flask应用程序。在我的应用程序中,我有两个用户组:普通用户和管理员。用户登录后,我想根据他们所属的组将他们重定向到特定页面。因此,如果用户是普通用户,他们将被重定向到/dashboard,如果他们是管理员,他们将被重定向到/admin\u dashboard。到目前为止,我有STORMPATH\u REDIRECT\u URL='/dashboard',因此每次登录时,他们都会被重定向到/dashboard,而不管他们属于哪个组如何

问题陈述

我正在开发一个使用Stormpath进行身份验证的flask应用程序。在我的应用程序中,我有两个用户组:
普通用户
管理员
。用户登录后,我想根据他们所属的组将他们重定向到特定页面。因此,如果用户是普通用户,他们将被重定向到
/dashboard
,如果他们是管理员,他们将被重定向到
/admin\u dashboard
。到目前为止,我有
STORMPATH\u REDIRECT\u URL='/dashboard'
,因此每次登录时,他们都会被重定向到
/dashboard
,而不管他们属于哪个组如何根据他们的Stormpath组将他们重定向到特定页面?

当前代码片段:

注意:我使用默认路径登录视图,该应用程序允许通过谷歌进行社交登录

/app/\uuuuu init\uuuuuuuu.py:

def create_app(config_name):
  ...
  # App config here
  ...
  stormpath_manager = StormpathManager()
  stormpath_manager.init_app(app)
  ...
  return app
class Config(Object):
  ...
  STORMPATH_REDIRECT_URL = '/dashboard'
  ...
@dashboard_blueprint.route('/dashboard')
@login_required
@groups_required(['normal-users'])
def dashboard():
  return render_template('dashboard/index.html', title="Dashboard")

@dashboard_blueprint.route('/admin_dashboard')
@login_required
@groups_required(['admins'])
def admin_dashboard():
  return render_template('dashboard/admin_index.html', title="Admin Dashboard")
/config.py:

def create_app(config_name):
  ...
  # App config here
  ...
  stormpath_manager = StormpathManager()
  stormpath_manager.init_app(app)
  ...
  return app
class Config(Object):
  ...
  STORMPATH_REDIRECT_URL = '/dashboard'
  ...
@dashboard_blueprint.route('/dashboard')
@login_required
@groups_required(['normal-users'])
def dashboard():
  return render_template('dashboard/index.html', title="Dashboard")

@dashboard_blueprint.route('/admin_dashboard')
@login_required
@groups_required(['admins'])
def admin_dashboard():
  return render_template('dashboard/admin_index.html', title="Admin Dashboard")
/app/dashboard/views.py:

def create_app(config_name):
  ...
  # App config here
  ...
  stormpath_manager = StormpathManager()
  stormpath_manager.init_app(app)
  ...
  return app
class Config(Object):
  ...
  STORMPATH_REDIRECT_URL = '/dashboard'
  ...
@dashboard_blueprint.route('/dashboard')
@login_required
@groups_required(['normal-users'])
def dashboard():
  return render_template('dashboard/index.html', title="Dashboard")

@dashboard_blueprint.route('/admin_dashboard')
@login_required
@groups_required(['admins'])
def admin_dashboard():
  return render_template('dashboard/admin_index.html', title="Admin Dashboard")

解决方案

如图所示,flask_stormpath有一个
用户
类,它将
帐户
作为参数。从
stormpath sdk python
中,我们可以看到
帐户有一个
has_组(self,resolvable)
函数,如图所示

因此,为了在用户登录后显示特定页面,根据用户所属的组,我对
/app/dashboard/views.py进行了以下更改,同时保持其他更改不变:

from flask_stormpath import User
from flask_stormpath import current_user

@dashboard_blueprint.route('/dashboard')
@login_required
def dashboard():
  if User.has_group(current_user, 'normal-users'):
    return render_template('dashboard/index.html', title="Dashboard")
  elif User.has_group(current_user, 'admins'):
    return render_template('dashboard/admin_index.html', title="Admin Dashboard")
  else:
      return render_template('dashboard/error.html', title="Error Page")

你能编辑帖子以包含你当前的代码吗?烧瓶视图以及您如何使用Stormpath。@HaraldNordgren请参见编辑的问题以获取所需信息。让我知道如果任何其他信息是必要的!