Gitlab中提供了REST方式进行用户的创建和删除,删除时由于关联的存在,Gitlab缺省方式会生成一个ghost用户,这篇文章对相关内容通过示例进行说明。
liumiaocn:gitlab liumiao$ cat docker-compose.yml version: '2' services: # Version Control service: Gitlab gitlab: image: gitlab/gitlab-ce:12.10.5-ce.0 ports: - "32001:80" volumes: - ./log/:/var/log/gitlab - ./data/:/var/opt/gitlab - ./conf/:/etc/gitlab restart: "no" liumiaocn:gitlab liumiao$事前准备
创建如下目录:
liumiaocn:gitlab liumiao$ ls docker-compose.yml liumiaocn:gitlab liumiao$ mkdir -p log data conf liumiaocn:gitlab liumiao$ ls conf data docker-compose.yml log liumiaocn:gitlab liumiao$启动
启动命令:docker-compose up -d
登录并创建apitoken 登录URL- http://localhost:32001
注:此处设定root用户密码,由于后续直接使用token进行用户创建,示例说明中不再直接需要使用root设定的密码。
通过settings菜单或者直接使用如下URL,创建api用的token
- http://localhost:32001/profile/personal_access_tokens
执行命令 access_token=“7F2jdsYyeDsuhGnyTvPz” gitlab_url=“localhost:32001” curl -X GET -H "PRIVATE-TOKEN: a c c e s s t o k e n " h t t p : / / {access_token}" http:// accesstoken"http://{gitlab_url}/api/v4/users
执行日志示例:
liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 808 100 808 0 0 5897 0 --:--:-- --:--:-- --:--:-- 5897 [ { "id": 1, "name": "Administrator", "username": "root", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", "web_url": "http://e3f6f5252e08/root", "created_at": "2020-07-19T08:29:18.581Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": "2020-07-19T09:46:42.956Z", "confirmed_at": "2020-07-19T08:29:18.120Z", "last_activity_on": "2020-07-19", "email": "admin@example.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": "2020-07-19T09:46:42.956Z", "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": true } ] liumiaocn:gitlab liumiao$创建新用户
执行日志示例:
liumiaocn:gitlab liumiao$ curl -X POST -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users \ > -H 'cache-control: no-cache' \ > -H 'content-type: application/json' \ > -d '{ "email": "liumiaocn@outlook.com", > "username": "liumiao", > "password": "12341234", > "name": "liumiao", > "skip_confirmation": "true" > }' |jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 898 100 759 100 139 1671 306 --:--:-- --:--:-- --:--:-- 1973 { "id": 2, "name": "liumiao", "username": "liumiao", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80&d=identicon", "web_url": "http://e3f6f5252e08/liumiao", "created_at": "2020-07-19T09:49:08.349Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": null, "confirmed_at": "2020-07-19T09:49:08.178Z", "last_activity_on": null, "email": "liumiaocn@outlook.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": null, "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": false } liumiaocn:gitlab liumiao$用户确认
liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1568 100 1568 0 0 20631 0 --:--:-- --:--:-- --:--:-- 20363 [ { "id": 2, "name": "liumiao", "username": "liumiao", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80&d=identicon", "web_url": "http://e3f6f5252e08/liumiao", "created_at": "2020-07-19T09:49:08.349Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": null, "confirmed_at": "2020-07-19T09:49:08.178Z", "last_activity_on": null, "email": "liumiaocn@outlook.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": null, "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": false }, { "id": 1, "name": "Administrator", "username": "root", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", "web_url": "http://e3f6f5252e08/root", "created_at": "2020-07-19T08:29:18.581Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": "2020-07-19T09:46:42.956Z", "confirmed_at": "2020-07-19T08:29:18.120Z", "last_activity_on": "2020-07-19", "email": "admin@example.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": "2020-07-19T09:46:42.956Z", "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": true } ] liumiaocn:gitlab liumiao$删除用户
liumiaocn:gitlab liumiao$ userid=2 liumiaocn:gitlab liumiao$ curl -X DELETE -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users/${userid} liumiaocn:gitlab liumiao$ echo $? 0 liumiaocn:gitlab liumiao$结果确认
可以看到虽然删除了名为liumiao的用户,但是产生了一个ghost的用户。
liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1668 100 1668 0 0 9065 0 --:--:-- --:--:-- --:--:-- 9016 [ { "id": 3, "name": "Ghost User", "username": "ghost", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/4249f4df72b475e7894fabed1c5888cf?s=80&d=identicon", "web_url": "http://e3f6f5252e08/ghost", "created_at": "2020-07-19T09:54:31.653Z", "bio": "This is a \"Ghost User\", created to hold all issues authored by users that have since been deleted. This user cannot be removed.", "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": null, "confirmed_at": null, "last_activity_on": null, "email": "ghost@example.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": null, "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": false }, { "id": 1, "name": "Administrator", "username": "root", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", "web_url": "http://e3f6f5252e08/root", "created_at": "2020-07-19T08:29:18.581Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": "2020-07-19T09:46:42.956Z", "confirmed_at": "2020-07-19T08:29:18.120Z", "last_activity_on": "2020-07-19", "email": "admin@example.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": "2020-07-19T09:46:42.956Z", "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": true } ] liumiaocn:gitlab liumiao$ghost用户删除
ghost用户直接使用Rest API传入id是无法删除的,可以使用如下方式进行删除
liumiaocn:gitlab liumiao$ docker exec -it gitlab_gitlab_1 sh # gitlab-rails console -------------------------------------------------------------------------------- GitLab: 12.10.5 (ed53d560372) FOSS GitLab Shell: 12.2.0 PostgreSQL: 11.7 -------------------------------------------------------------------------------- Loading production environment (Rails 6.0.2) irb(main):001:0> user = User.find_by(username: "ghost") => # irb(main):002:0> User.delete(user.id) => 1 irb(main):003:0>
User.delete执行结果如果为1说明删除成功,如果为0说明删除失败,然后通过API可以确认到Ghost 用户已被删除
liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1668 100 1668 0 0 9065 0 --:--:-- --:--:-- --:--:-- 9016 [ { "id": 1, "name": "Administrator", "username": "root", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", "web_url": "http://e3f6f5252e08/root", "created_at": "2020-07-19T08:29:18.581Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": "2020-07-19T09:46:42.956Z", "confirmed_at": "2020-07-19T08:29:18.120Z", "last_activity_on": "2020-07-19", "email": "admin@example.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": "2020-07-19T09:46:42.956Z", "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": true } ] liumiaocn:gitlab liumiao$普通删除 vs 关联删除
上述方式缺省为普通删除,还有一种方式为关联删除,区别在于后者将会将当前用户关联的记录也一同删除,比如:
- 此用户创建的issue
- 此用户创建的Merge Request
- 此用户创建的Note
- 此用户提出的Abuse report
- 此用户创建的的Award emoji … 普通方式的删除,会讲上述记录移动至一个名为Ghost User的系统级用户中,而关联删除则会删除相关所有记录。
liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1568 100 1568 0 0 4020 0 --:--:-- --:--:-- --:--:-- 4020 [ { "id": 2, "name": "liumiao", "username": "liumiao", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80&d=identicon", "web_url": "http://866463f4202a/liumiao", "created_at": "2020-07-19T10:09:58.996Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": null, "confirmed_at": "2020-07-19T10:09:58.860Z", "last_activity_on": null, "email": "liumiaocn@outlook.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": null, "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": false }, { "id": 1, "name": "Administrator", "username": "root", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", "web_url": "http://866463f4202a/root", "created_at": "2020-07-19T10:07:10.205Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": "2020-07-19T10:09:01.060Z", "confirmed_at": "2020-07-19T10:07:09.640Z", "last_activity_on": "2020-07-19", "email": "admin@example.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": "2020-07-19T10:09:01.060Z", "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": true } ] liumiaocn:gitlab liumiao$删除id为2的用户(关联方式)
liumiaocn:gitlab liumiao$ userid=2 liumiaocn:gitlab liumiao$ curl -X DELETE -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users/${userid}?hard_delete=true liumiaocn:gitlab liumiao$结果确认
可以看到,此种方式(hard_delete=true)之下不会产生Ghost用户
liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 808 100 808 0 0 10631 0 --:--:-- --:--:-- --:--:-- 10631 [ { "id": 1, "name": "Administrator", "username": "root", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", "web_url": "http://866463f4202a/root", "created_at": "2020-07-19T10:07:10.205Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": "2020-07-19T10:09:01.060Z", "confirmed_at": "2020-07-19T10:07:09.640Z", "last_activity_on": "2020-07-19", "email": "admin@example.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": "2020-07-19T10:09:01.060Z", "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": true } ] liumiaocn:gitlab liumiao$