限制seafile注册邮箱域名


seafile本身没有限制注册邮箱域名的功能,看社区里官方提到也不考虑增加这个功能,说实话挺遗憾,多么实用的功能。
既然是开源的,那就想办法改动下代码吧,以对接OAUTH的场景为例,其他注册方式都有对应的backends:

1、修改backends处理流程

root@drive:~# vim <root of seafile>/seafile-server-latest/seahub/seahub/oauth/backends.py

需要用到正则表达式,在文件开头引入re模块

import re

在def authenticate(self, remote_user) 函数中,create user语句之前,增加以下内容:

pattern = r'^(.)+@gmail\.com$'
email_m = re.match(pattern, username)
# 不允许匹配到的邮箱域名注册
if email_m is not None:
    return

保存,最终代码

def authenticate(self, remote_user):
    """
    The username passed as ``remote_user`` is considered trusted.  This
    method simply returns the ``User`` object with the given username,
    creating a new ``User`` object if ``create_unknown_user`` is ``True``.

    Returns None if ``create_unknown_user`` is ``False`` and a ``User``
    object with the given username is not found in the database.
    """
    if not remote_user:
        return

    username = self.clean_username(remote_user)

    #Modify Start
    pattern = r'^(.)+@gmail.com$'
    email_m = re.match(pattern, username)
    # 不允许匹配到的邮箱域名注册
    if email_m is not None:
        return
    #Modify End
    
    try:
        user = User.objects.get(email=username)
    except User.DoesNotExist:
        if self.create_unknown_user:
            user = User.objects.create_user(
                email=username, is_active=self.activate_after_creation)
            if not self.activate_after_creation:
                notify_admins_on_activate_request(username)
            elif settings.NOTIFY_ADMIN_AFTER_REGISTRATION:
                notify_admins_on_register_complete(username)
        else:
            user = None

    return user

2、重启seahub生效

用gmail.com邮箱登录的用户会提示没有该用户
更复杂的处理逻辑,自由发挥吧


文章作者: Netcat-fan
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Netcat-fan !
  目录