[技术博客]大闸蟹的技术博客,通过gitlab api进行用户批量创建

技术博客——通过gitlab api批量注册用户

gitlab登录界面本身提供了register功能,但需要手工一个个添加,对于一次性会添加整个班级的学生的软工平台来说并不科学合理。使用gitlab api来批量注册用户是一个比较好的选择。

gitlab api的官方文档中的user栏给出了create user的方法

User creation

Creates a new user. Note only administrators can create new users. Either password, reset_password, or force_random_password must be specified. If reset_password and force_random_password are both false, then password is required.

Note that force_random_password and reset_password take priority over password. In addition, reset_password and force_random_password can be used together.

POST /users

Parameters:

Attribute Required Description
admin No User is admin - true or false (default)
avatar No Image file for user’s avatar
bio No User’s biography
can_create_group No User can create groups - true or false
color_scheme_id No User’s color scheme for the file viewer (see the user preference docs for more information)
email Yes Email
extern_uid No External UID
external No Flags the user as external - true or false (default)
extra_shared_runners_minutes_limit No Extra pipeline minutes quota for this user
force_random_password No Set user password to a random value - true or false (default)
group_id_for_saml No ID of group where SAML has been configured
linkedin No LinkedIn
location No User’s location
name Yes Name
organization No Organization name
password No Password
private_profile No User’s profile is private - true, false (default), or null (will be converted to false)
projects_limit No Number of projects user can create
provider No External provider name
public_email No The public email of the user
reset_password No Send user password reset link - true or false(default)
shared_runners_minutes_limit No Pipeline minutes quota for this user
skip_confirmation No Skip confirmation - true or false (default)
skype No Skype ID
theme_id No The GitLab theme for the user (see the user preference docs for more information)
twitter No Twitter account
username Yes Username
website_url No Website URL

需要的信息有,用户的密码,用户名,邮箱,别名

需要注意的是,使用gitlab api创建的用户无法直接登陆,需要进行邮箱验证且修改密码,在csnd等寻找无果

但仔细阅读gitlab api的文档,发现其中有skip_confirmation字段,创建时将此字段置为true即可跳过邮箱验证正常登录。

同理,将reset_password置为false即可以去除登陆时修改密码的提示

在实现时,为了避免创建重复的用户或账号、密码格式不对等导致gitlab报错引起服务器报错,所以还需要调用gitlab api提供的List users方法,查找所有的用户信息,并与本地数据库存储的数据进行比对,来防止创建相同的用户。关于邮箱、密码等的格式,参考gitlab对于账号、密码邮箱等的规范,在vue的form组件中采用正则匹配的方法进行校对,就可以保证符合规范的操作不会使得界面崩溃。

但若不通过平台进行操作,直接在gitlab上进行注册的用户,由于本地数据库没有其信息,可能会导致意料之外的问题,,不仅包括用户的问题,还有项目等其他方面。故需要限制用户对于gitlab的随意操作,之后可能会在gitlab root用户的admin中对gitlab进行进一步的设置以限定用户行为。