Help us learn about your current experience with the documentation. Take the survey.

用户管理 Rake 任务

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab Self-Managed

GitLab 提供了用于管理用户的 Rake 任务。管理员也可以使用 管理员 区域来管理用户

将用户添加为所有项目的开发者

要将用户添加为所有项目的开发者,请运行:

# omnibus-gitlab
sudo gitlab-rake gitlab:import:user_to_projects[username@domain.tld]

# installation from source
bundle exec rake gitlab:import:user_to_projects[username@domain.tld] RAILS_ENV=production

将所有用户添加到所有项目

要将所有用户添加到所有项目,请运行:

# omnibus-gitlab
sudo gitlab-rake gitlab:import:all_users_to_all_projects

# installation from source
bundle exec rake gitlab:import:all_users_to_all_projects RAILS_ENV=production

管理员会被添加为维护者,而所有其他用户则被添加为开发者。

将用户添加为所有群组的开发者

要将用户添加为所有群组的开发者,请运行:

# omnibus-gitlab
sudo gitlab-rake gitlab:import:user_to_groups[username@domain.tld]

# installation from source
bundle exec rake gitlab:import:user_to_groups[username@domain.tld] RAILS_ENV=production

将所有用户添加到所有群组

要将所有用户添加到所有群组,请运行:

# omnibus-gitlab
sudo gitlab-rake gitlab:import:all_users_to_all_groups

# installation from source
bundle exec rake gitlab:import:all_users_to_all_groups RAILS_ENV=production

管理员会被添加为所有者,以便他们可以向群组中添加更多用户。

将指定群组中的所有用户更新为 project_limit:0can_create_group: false

要将指定群组中的所有用户更新为 project_limit: 0can_create_group: false,请运行:

# omnibus-gitlab
sudo gitlab-rake gitlab:user_management:disable_project_and_group_creation\[:group_id\]

# installation from source
bundle exec rake gitlab:user_management:disable_project_and_group_creation\[:group_id\] RAILS_ENV=production

此操作会更新指定群组、其子群组以及该群组命名空间中的项目中的所有用户,并应用上述限制。

控制可计费用户的数量

启用此设置可以阻止新用户,直到他们得到管理员的批准。 默认为 false

block_auto_created_users: false

为所有用户禁用双重认证

此任务会为所有已启用双重认证 (2FA) 的用户禁用该功能。例如,当 GitLab 的 config/secrets.yml 文件丢失导致用户无法登录时,此功能会很有用。

要为所有用户禁用双重认证,请运行:

# omnibus-gitlab
sudo gitlab-rake gitlab:two_factor:disable_for_all_users

# installation from source
bundle exec rake gitlab:two_factor:disable_for_all_users RAILS_ENV=production

轮换双重认证加密密钥

GitLab 将双重认证 (2FA) 所需的机密数据存储在加密的数据库列中。此数据的加密密钥称为 otp_key_base,存储在 config/secrets.yml 文件中。

如果该文件已泄露,但单个 2FA 密钥尚未泄露,则可以使用新的加密密钥重新加密这些密钥。这样您就可以更换已泄露的密钥,而无需强制所有用户更改其 2FA 设置。

要轮换双重认证加密密钥:

  1. config/secrets.yml 文件中查找旧密钥,但请确保您正在操作 production 部分。您需要关注的行如下所示:

    production:
      otp_key_base: fffffffffffffffffffffffffffffffffffffffffffffff
  2. 生成一个新的密钥:

    # omnibus-gitlab
    sudo gitlab-rake secret
    
    # installation from source
    bundle exec rake secret RAILS_ENV=production
  3. 停止 GitLab 服务器,备份现有的密钥文件,并更新数据库:

    # omnibus-gitlab
    sudo gitlab-ctl stop
    sudo cp config/secrets.yml config/secrets.yml.bak
    sudo gitlab-rake gitlab:two_factor:rotate_key:apply filename=backup.csv old_key=<old key> new_key=<new key>
    
    # installation from source
    sudo /etc/init.d/gitlab stop
    cp config/secrets.yml config/secrets.yml.bak
    bundle exec rake gitlab:two_factor:rotate_key:apply filename=backup.csv old_key=<old key> new_key=<new key> RAILS_ENV=production

    <old key> 值可以从 config/secrets.yml 中读取(<new key> 是之前生成的)。用户 2FA 密钥的加密值会被写入指定的 filename 中。如果发生错误,您可以使用此文件进行回滚。

  4. 修改 config/secrets.yml 文件,将 otp_key_base 设置为 <new key>,然后重启。同样,请确保您在 production 部分进行操作。

    # omnibus-gitlab
    sudo gitlab-ctl start
    
    # installation from source
    sudo /etc/init.d/gitlab start

如果出现任何问题(例如 old_key 值错误),您可以恢复 config/secrets.yml 的备份并回滚更改:

# omnibus-gitlab
sudo gitlab-ctl stop
sudo gitlab-rake gitlab:two_factor:rotate_key:rollback filename=backup.csv
sudo cp config/secrets.yml.bak config/secrets.yml
sudo gitlab-ctl start

# installation from source
sudo /etc/init.d/gitlab start
bundle exec rake gitlab:two_factor:rotate_key:rollback filename=backup.csv RAILS_ENV=production
cp config/secrets.yml.bak config/secrets.yml
sudo /etc/init.d/gitlab start

批量为用户分配 GitLab Duo

您可以使用包含用户名的 CSV 文件,为用户批量分配 GitLab Duo。CSV 文件必须有一个名为 username 的标题,其后每行跟随一个用户名。

username
user1
user2
user3
user4

GitLab Duo Pro

  • Tier: Premium, Ultimate
  • Offering: GitLab Self-Managed

要为 GitLab Duo Pro 执行批量用户分配,您可以使用以下 Rake 任务:

bundle exec rake duo_pro:bulk_user_assignment DUO_PRO_BULK_USER_FILE_PATH=path/to/your/file.csv

如果您希望在文件路径中使用方括号,可以对其进行转义或使用双引号:

bundle exec rake duo_pro:bulk_user_assignment\['path/to/your/file.csv'\]
# or
bundle exec rake "duo_pro:bulk_user_assignment[path/to/your/file.csv]"

GitLab Duo Pro and Enterprise

  • Tier: Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed

GitLab Self-Managed

此 Rake 任务会根据已购买的可用附加组件,从 CSV 文件中为用户列表在实例级别批量分配 GitLab Duo Pro 或 Enterprise 许可证。

要为 GitLab Self-Managed 实例执行批量用户分配:

bundle exec rake gitlab_subscriptions:duo:bulk_user_assignment DUO_BULK_USER_FILE_PATH=path/to/your/file.csv

如果您希望在文件路径中使用方括号,可以对其进行转义或使用双引号:

bundle exec rake gitlab_subscriptions:duo:bulk_user_assignment\['path/to/your/file.csv'\]
# or
bundle exec rake "gitlab_subscriptions:duo:bulk_user_assignment[path/to/your/file.csv]"

GitLab.com

GitLab.com 管理员也可以使用此 Rake 任务,根据该群组已购买的可用附加组件,为 GitLab.com 群组批量分配 GitLab Duo Pro 或 Enterprise 许可证。

要为 GitLab.com 群组执行批量用户分配:

bundle exec rake gitlab_subscriptions:duo:bulk_user_assignment DUO_BULK_USER_FILE_PATH=path/to/your/file.csv NAMESPACE_ID=<namespace_id>

如果您希望在文件路径中使用方括号,可以对其进行转义或使用双引号:

bundle exec rake gitlab_subscriptions:duo:bulk_user_assignment\['path/to/your/file.csv','<namespace_id>'\]
# or
bundle exec rake "gitlab_subscriptions:duo:bulk_user_assignment[path/to/your/file.csv,<namespace_id>]"

故障排除

批量用户分配期间的错误

在使用 Rake 任务进行批量用户分配时,您可能会遇到以下错误:

  • User is not found: 未找到指定用户。请确保提供的用户名与现有用户匹配。
  • ERROR_NO_SEATS_AVAILABLE: 没有更多可用的许可证用于分配用户。请参阅如何查看已分配的 GitLab Duo 用户以检查当前的许可证分配情况。
  • ERROR_INVALID_USER_MEMBERSHIP: 该用户不符合分配条件,原因可能是其处于非活跃状态、是机器人账户或幽灵账户。请确保该用户是活跃用户,并且如果在 GitLab.com 上,也是所提供命名空间的成员。

相关主题