【问题标题】:Django : Set is_active flag for User created by social-auth-app-django and google_oauth2Django:为由 social-auth-app-django 和 google_oauth2 创建的用户设置 is_active 标志
【发布时间】:2021-11-11 07:17:39
【问题描述】:

我正在使用 social-auth-app-django 为使用 google oauth2 身份验证的新用户注册登录。

注册后,在我的数据库中创建了一个新用户,但 is_active 设置为 false,我想将 is_active 设置为 true,仅用于通过此 social_auth google 身份验证创建的用户

(对于使用电子邮件密码注册的其他用户,我通过发送帐户激活电子邮件来激活他们) 我已经尝试为所有没有密码的用户设置 is_active = True ,但我觉得这种方式是不安全和黑客的。 如何修改 social_auth_login 流程以激活用户? 我正在使用自定义用户模型:

class UserManager(BaseUserManager):
    def create_user(self, email, password=None, **extra_fields):

        if not email:
            raise ValueError('The Email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        if password:
            user.set_password(password)
        # else:
        #     user.is_active = True           <-------- tried this , worked too
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_active', True)
        extra_fields.setdefault('user_type', user_constants.SUPERUSER)
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        return self.create_user(email, password, **extra_fields)

..

class User(AbstractUser):
    username = None # remove username field, we will use email as unique identifier
    email = models.EmailField(unique=True, null=True, db_index=True)
    client_id = models.UUIDField(primary_key = True,
         default = uuid.uuid4,
         editable = False)
    name = models.CharField(max_length=255, default="")
    is_active = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    date_joined = models.DateTimeField(default=timezone.now)
    user_type = models.PositiveSmallIntegerField(choices=user_constants.USER_TYPE_CHOICES, default=user_constants.CLIENT_ADMIN)
    REQUIRED_FIELDS = []
    USERNAME_FIELD = 'email'

    objects = UserManager()

..

SOCIAL_AUTH_PIPELINE = (
  'social_core.pipeline.social_auth.social_details',
  'social_core.pipeline.social_auth.social_uid',
  'social_core.pipeline.social_auth.auth_allowed',
  'social_core.pipeline.social_auth.social_user',
  'social_core.pipeline.user.get_username',
  'social_core.pipeline.social_auth.associate_by_email',
  'social_core.pipeline.user.create_user',
  'social_core.pipeline.social_auth.associate_user',
  'social_core.pipeline.social_auth.load_extra_data',
  'social_core.pipeline.user.user_details',
)

SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = True
SOCIAL_AUTH_USER_MODEL = 'register.User'
SOCIAL_AUTH_GOOGLE_OAUTH2_USER_FIELDS = ['email']

..

【问题讨论】:

    标签: django django-forms social-auth-app-django


    【解决方案1】:

    根据 Django,布尔值 is_active

    指定是否应将此用户帐户视为活动帐户。我们 建议您将此标志设置为 False 而不是删除 账户;这样,如果您的应用程序有任何外键 用户,外键不会损坏。

    在您的情况下,我会将is_active 默认设置为 True(如果您想删除一个帐户,您只需将其设置为 False)。

    根据您的评论

    (对于使用电子邮件密码注册的其他用户,我通过以下方式激活它们 发送帐户激活电子邮件)

    你可以添加一个布尔值is_email_verified:如果用户是由社交认证创建的,则意味着is_email_verified是True;如果用户是使用电子邮件密码创建的,is_email_verified 为 False,必须通过发送帐户激活电子邮件将其设置为 True。

    感谢您可以使用 2 个布尔值 is_activeis_email_verified 拥有 4 种状态:想要连接的用户必须将它们都设置为 True。我觉得这很安全。

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 2013-12-11
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多