我使用社交-auth-app-django来注册使用google oauth2身份验证的新用户。
在我的db中创建了一个新用户但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']。。
发布于 2021-09-16 09:53:30
根据Django的说法,布尔型is_active
指定此用户帐户是否应被视为活动帐户。我们建议您将此标志设置为False,而不是删除帐户;这样,如果应用程序对用户有任何外键,则外键不会中断。
在您的示例中,默认情况下我会将is_active作为True (如果您想删除一个帐户,您只需将其设置为False)。
听你的话
(对于使用电子邮件密码注册的其他用户,我通过发送帐户激活电子邮件来激活他们)
您可以添加一个布尔is_email_verified:如果用户是由社交用户创建的,这意味着is_email_verified是真;如果用户是在电子邮件密码之后创建的,则is_email_verified是假的,必须通过发送帐户激活电子邮件将其设置为True。
由于这一点,您可以使用两个布尔( booleans )、is_active和is_email_verified拥有4个状态:想要连接的用户必须将这两个状态都作为True。在我看来是安全的。
https://stackoverflow.com/questions/69206010
复制相似问题