简体   繁体   English

使用管理员权限,如何在没有密码的情况下验证或登录用户帐户?

[英]Using admin permission, how to authenticate or logged into the user account without a password?

I'm new to with django application, I want to check/login a users account without password through admin permission.我是 django 应用程序的新手,我想通过管理员权限检查/登录没有密码的用户帐户。

Is there any built-in feature in Django? Django 中是否有任何内置功能? Please help or guide me on how to do that?请帮助或指导我如何做到这一点?

This kind of feature provided by laravel in PHP PHP 中的 laravel 提供的这种功能

Here is the sample code of PHP:下面是 PHP 的示例代码:

use Illuminate\Http\Request;
use App\User;

public function loginAsUser(User $user){
    auth()->guard('admin')->logout();     
    auth()->guard('web')->loginUsingId($user->id);
    return redirect(route('user.dashboard'));
}

Here is the simplest logic that works for me.这是对我有用的最简单的逻辑。

[html / template page ]
<a href="{% url 'login_as' user.username %}">Login as User</a>

[urls.py]
path('login_as/<username>', views.login_as_user, name='login_as'),


[views.py]
from django.contrib.auth import login


@staff_member_required()
def login_as_user(request, username):
    try:
        # assuming the user name is passed in via get
        username = username
        user = User.objects.get(username=username)

        # manually set the backend attribute
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, user)
    except Exception as e:
        messages.warning(request, e)

    #location of landing 
    return redirect('/users/dashboard')


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户登录后如何在django中管理会话(不使用默认admin)? - how to manage sessions in django after the user is logged in (without using default admin )? 如何在不使用用户表单的情况下使用管理员操作在Django注册中创建非活动用户帐户 - How to create inactive user account in django-registration by using admin action without user form 使用电子邮件和密码验证用户 - Authenticate user using email and password Django 使用登录的 Windows 域用户进行身份验证 - Django authenticate using logged in windows domain user 在 django 管理面板中注册一个只有电子邮件 ID 的管理员用户,然后通过电子邮件发送帐户激活/密码链接以开始使用管理面板 - register an admin user in django admin panel with only email id and then email account activation/password link to start using the admin panel Django:如何在不使用默认管理员的情况下管理组和权限 - Django: How to manage groups and permission without using default admin 登录用户步骤的权限 - Permission for logged in user steps Django身份验证不保持用户登录 - Django authenticate not keeping user logged in 如何在Django通用视图中对登录的用户进行身份验证? - how can i authenticate logged in user in django generic views? 如何在Django管理网站上授予用户权限 - How to give user permission on django admin site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM