Gitlab: API方式删除用户

男娘i 2023-02-28 01:51 76阅读 0赞

在这里插入图片描述
Gitlab中提供了REST方式进行用户的创建和删除,删除时由于关联的存在,Gitlab缺省方式会生成一个ghost用户,这篇文章对相关内容通过示例进行说明。

环境准备

docker-compose文件

  1. liumiaocn:gitlab liumiao$ cat docker-compose.yml
  2. version: '2'
  3. services:
  4. # Version Control service: Gitlab
  5. gitlab:
  6. image: gitlab/gitlab-ce:12.10.5-ce.0
  7. ports:
  8. - "32001:80"
  9. volumes:
  10. - ./log/:/var/log/gitlab
  11. - ./data/:/var/opt/gitlab
  12. - ./conf/:/etc/gitlab
  13. restart: "no"
  14. liumiaocn:gitlab liumiao$

事前准备

创建如下目录:

  1. liumiaocn:gitlab liumiao$ ls
  2. docker-compose.yml
  3. liumiaocn:gitlab liumiao$ mkdir -p log data conf
  4. liumiaocn:gitlab liumiao$ ls
  5. conf data docker-compose.yml log
  6. liumiaocn:gitlab liumiao$

启动

启动命令:docker-compose up -d

登录并创建apitoken

登录URL

  • http://localhost:32001

在这里插入图片描述
注:此处设定root用户密码,由于后续直接使用token进行用户创建,示例说明中不再直接需要使用root设定的密码。

创建api用的token

通过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:// accesst​oken”http://\{gitlab\_url\}/api/v4/users

执行日志示例:

  1. liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq .
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 808 100 808 0 0 5897 0 --:--:-- --:--:-- --:--:-- 5897
  5. [
  6. {
  7. "id": 1,
  8. "name": "Administrator",
  9. "username": "root",
  10. "state": "active",
  11. "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
  12. "web_url": "http://e3f6f5252e08/root",
  13. "created_at": "2020-07-19T08:29:18.581Z",
  14. "bio": null,
  15. "location": null,
  16. "public_email": "",
  17. "skype": "",
  18. "linkedin": "",
  19. "twitter": "",
  20. "website_url": "",
  21. "organization": null,
  22. "job_title": "",
  23. "work_information": null,
  24. "last_sign_in_at": "2020-07-19T09:46:42.956Z",
  25. "confirmed_at": "2020-07-19T08:29:18.120Z",
  26. "last_activity_on": "2020-07-19",
  27. "email": "admin@example.com",
  28. "theme_id": 1,
  29. "color_scheme_id": 1,
  30. "projects_limit": 100000,
  31. "current_sign_in_at": "2020-07-19T09:46:42.956Z",
  32. "identities": [],
  33. "can_create_group": true,
  34. "can_create_project": true,
  35. "two_factor_enabled": false,
  36. "external": false,
  37. "private_profile": false,
  38. "is_admin": true
  39. }
  40. ]
  41. liumiaocn:gitlab liumiao$

创建新用户

执行日志示例:

  1. liumiaocn:gitlab liumiao$ curl -X POST -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users \
  2. > -H 'cache-control: no-cache' \
  3. > -H 'content-type: application/json' \
  4. > -d '{ "email": "liumiaocn@outlook.com",
  5. > "username": "liumiao",
  6. > "password": "12341234",
  7. > "name": "liumiao",
  8. > "skip_confirmation": "true"
  9. > }' |jq .
  10. % Total % Received % Xferd Average Speed Time Time Time Current
  11. Dload Upload Total Spent Left Speed
  12. 100 898 100 759 100 139 1671 306 --:--:-- --:--:-- --:--:-- 1973
  13. {
  14. "id": 2,
  15. "name": "liumiao",
  16. "username": "liumiao",
  17. "state": "active",
  18. "avatar_url": "https://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80&d=identicon",
  19. "web_url": "http://e3f6f5252e08/liumiao",
  20. "created_at": "2020-07-19T09:49:08.349Z",
  21. "bio": null,
  22. "location": null,
  23. "public_email": "",
  24. "skype": "",
  25. "linkedin": "",
  26. "twitter": "",
  27. "website_url": "",
  28. "organization": null,
  29. "job_title": "",
  30. "work_information": null,
  31. "last_sign_in_at": null,
  32. "confirmed_at": "2020-07-19T09:49:08.178Z",
  33. "last_activity_on": null,
  34. "email": "liumiaocn@outlook.com",
  35. "theme_id": 1,
  36. "color_scheme_id": 1,
  37. "projects_limit": 100000,
  38. "current_sign_in_at": null,
  39. "identities": [],
  40. "can_create_group": true,
  41. "can_create_project": true,
  42. "two_factor_enabled": false,
  43. "external": false,
  44. "private_profile": false,
  45. "is_admin": false
  46. }
  47. liumiaocn:gitlab liumiao$

用户确认

  1. liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq .
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 1568 100 1568 0 0 20631 0 --:--:-- --:--:-- --:--:-- 20363
  5. [
  6. {
  7. "id": 2,
  8. "name": "liumiao",
  9. "username": "liumiao",
  10. "state": "active",
  11. "avatar_url": "https://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80&d=identicon",
  12. "web_url": "http://e3f6f5252e08/liumiao",
  13. "created_at": "2020-07-19T09:49:08.349Z",
  14. "bio": null,
  15. "location": null,
  16. "public_email": "",
  17. "skype": "",
  18. "linkedin": "",
  19. "twitter": "",
  20. "website_url": "",
  21. "organization": null,
  22. "job_title": "",
  23. "work_information": null,
  24. "last_sign_in_at": null,
  25. "confirmed_at": "2020-07-19T09:49:08.178Z",
  26. "last_activity_on": null,
  27. "email": "liumiaocn@outlook.com",
  28. "theme_id": 1,
  29. "color_scheme_id": 1,
  30. "projects_limit": 100000,
  31. "current_sign_in_at": null,
  32. "identities": [],
  33. "can_create_group": true,
  34. "can_create_project": true,
  35. "two_factor_enabled": false,
  36. "external": false,
  37. "private_profile": false,
  38. "is_admin": false
  39. },
  40. {
  41. "id": 1,
  42. "name": "Administrator",
  43. "username": "root",
  44. "state": "active",
  45. "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
  46. "web_url": "http://e3f6f5252e08/root",
  47. "created_at": "2020-07-19T08:29:18.581Z",
  48. "bio": null,
  49. "location": null,
  50. "public_email": "",
  51. "skype": "",
  52. "linkedin": "",
  53. "twitter": "",
  54. "website_url": "",
  55. "organization": null,
  56. "job_title": "",
  57. "work_information": null,
  58. "last_sign_in_at": "2020-07-19T09:46:42.956Z",
  59. "confirmed_at": "2020-07-19T08:29:18.120Z",
  60. "last_activity_on": "2020-07-19",
  61. "email": "admin@example.com",
  62. "theme_id": 1,
  63. "color_scheme_id": 1,
  64. "projects_limit": 100000,
  65. "current_sign_in_at": "2020-07-19T09:46:42.956Z",
  66. "identities": [],
  67. "can_create_group": true,
  68. "can_create_project": true,
  69. "two_factor_enabled": false,
  70. "external": false,
  71. "private_profile": false,
  72. "is_admin": true
  73. }
  74. ]
  75. liumiaocn:gitlab liumiao$

删除用户

  1. liumiaocn:gitlab liumiao$ userid=2
  2. liumiaocn:gitlab liumiao$ curl -X DELETE -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users/${userid}
  3. liumiaocn:gitlab liumiao$ echo $?
  4. 0
  5. liumiaocn:gitlab liumiao$

结果确认

可以看到虽然删除了名为liumiao的用户,但是产生了一个ghost的用户。

  1. liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq .
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 1668 100 1668 0 0 9065 0 --:--:-- --:--:-- --:--:-- 9016
  5. [
  6. {
  7. "id": 3,
  8. "name": "Ghost User",
  9. "username": "ghost",
  10. "state": "active",
  11. "avatar_url": "https://www.gravatar.com/avatar/4249f4df72b475e7894fabed1c5888cf?s=80&d=identicon",
  12. "web_url": "http://e3f6f5252e08/ghost",
  13. "created_at": "2020-07-19T09:54:31.653Z",
  14. "bio": "This is a \"Ghost User\", created to hold all issues authored by users that have since been deleted. This user cannot be removed.",
  15. "location": null,
  16. "public_email": "",
  17. "skype": "",
  18. "linkedin": "",
  19. "twitter": "",
  20. "website_url": "",
  21. "organization": null,
  22. "job_title": "",
  23. "work_information": null,
  24. "last_sign_in_at": null,
  25. "confirmed_at": null,
  26. "last_activity_on": null,
  27. "email": "ghost@example.com",
  28. "theme_id": 1,
  29. "color_scheme_id": 1,
  30. "projects_limit": 100000,
  31. "current_sign_in_at": null,
  32. "identities": [],
  33. "can_create_group": true,
  34. "can_create_project": true,
  35. "two_factor_enabled": false,
  36. "external": false,
  37. "private_profile": false,
  38. "is_admin": false
  39. },
  40. {
  41. "id": 1,
  42. "name": "Administrator",
  43. "username": "root",
  44. "state": "active",
  45. "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
  46. "web_url": "http://e3f6f5252e08/root",
  47. "created_at": "2020-07-19T08:29:18.581Z",
  48. "bio": null,
  49. "location": null,
  50. "public_email": "",
  51. "skype": "",
  52. "linkedin": "",
  53. "twitter": "",
  54. "website_url": "",
  55. "organization": null,
  56. "job_title": "",
  57. "work_information": null,
  58. "last_sign_in_at": "2020-07-19T09:46:42.956Z",
  59. "confirmed_at": "2020-07-19T08:29:18.120Z",
  60. "last_activity_on": "2020-07-19",
  61. "email": "admin@example.com",
  62. "theme_id": 1,
  63. "color_scheme_id": 1,
  64. "projects_limit": 100000,
  65. "current_sign_in_at": "2020-07-19T09:46:42.956Z",
  66. "identities": [],
  67. "can_create_group": true,
  68. "can_create_project": true,
  69. "two_factor_enabled": false,
  70. "external": false,
  71. "private_profile": false,
  72. "is_admin": true
  73. }
  74. ]
  75. liumiaocn:gitlab liumiao$

ghost用户删除

ghost用户直接使用Rest API传入id是无法删除的,可以使用如下方式进行删除

  1. liumiaocn:gitlab liumiao$ docker exec -it gitlab_gitlab_1 sh
  2. # gitlab-rails console
  3. --------------------------------------------------------------------------------
  4. GitLab: 12.10.5 (ed53d560372) FOSS
  5. GitLab Shell: 12.2.0
  6. PostgreSQL: 11.7
  7. --------------------------------------------------------------------------------
  8. Loading production environment (Rails 6.0.2)
  9. irb(main):001:0> user = User.find_by(username: "ghost")
  10. => #<User id:3 @ghost>
  11. irb(main):002:0> User.delete(user.id)
  12. => 1
  13. irb(main):003:0>

User.delete执行结果如果为1说明删除成功,如果为0说明删除失败,然后通过API可以确认到Ghost 用户已被删除

  1. liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq .
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 1668 100 1668 0 0 9065 0 --:--:-- --:--:-- --:--:-- 9016
  5. [
  6. {
  7. "id": 1,
  8. "name": "Administrator",
  9. "username": "root",
  10. "state": "active",
  11. "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
  12. "web_url": "http://e3f6f5252e08/root",
  13. "created_at": "2020-07-19T08:29:18.581Z",
  14. "bio": null,
  15. "location": null,
  16. "public_email": "",
  17. "skype": "",
  18. "linkedin": "",
  19. "twitter": "",
  20. "website_url": "",
  21. "organization": null,
  22. "job_title": "",
  23. "work_information": null,
  24. "last_sign_in_at": "2020-07-19T09:46:42.956Z",
  25. "confirmed_at": "2020-07-19T08:29:18.120Z",
  26. "last_activity_on": "2020-07-19",
  27. "email": "admin@example.com",
  28. "theme_id": 1,
  29. "color_scheme_id": 1,
  30. "projects_limit": 100000,
  31. "current_sign_in_at": "2020-07-19T09:46:42.956Z",
  32. "identities": [],
  33. "can_create_group": true,
  34. "can_create_project": true,
  35. "two_factor_enabled": false,
  36. "external": false,
  37. "private_profile": false,
  38. "is_admin": true
  39. }
  40. ]
  41. liumiaocn:gitlab liumiao$

普通删除 vs 关联删除

上述方式缺省为普通删除,还有一种方式为关联删除,区别在于后者将会将当前用户关联的记录也一同删除,比如:

  • 此用户创建的issue
  • 此用户创建的Merge Request
  • 此用户创建的Note
  • 此用户提出的Abuse report
  • 此用户创建的的Award emoji

    普通方式的删除,会讲上述记录移动至一个名为Ghost User的系统级用户中,而关联删除则会删除相关所有记录。

关联删除

事前准备

  1. liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq .
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 1568 100 1568 0 0 4020 0 --:--:-- --:--:-- --:--:-- 4020
  5. [
  6. {
  7. "id": 2,
  8. "name": "liumiao",
  9. "username": "liumiao",
  10. "state": "active",
  11. "avatar_url": "https://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80&d=identicon",
  12. "web_url": "http://866463f4202a/liumiao",
  13. "created_at": "2020-07-19T10:09:58.996Z",
  14. "bio": null,
  15. "location": null,
  16. "public_email": "",
  17. "skype": "",
  18. "linkedin": "",
  19. "twitter": "",
  20. "website_url": "",
  21. "organization": null,
  22. "job_title": "",
  23. "work_information": null,
  24. "last_sign_in_at": null,
  25. "confirmed_at": "2020-07-19T10:09:58.860Z",
  26. "last_activity_on": null,
  27. "email": "liumiaocn@outlook.com",
  28. "theme_id": 1,
  29. "color_scheme_id": 1,
  30. "projects_limit": 100000,
  31. "current_sign_in_at": null,
  32. "identities": [],
  33. "can_create_group": true,
  34. "can_create_project": true,
  35. "two_factor_enabled": false,
  36. "external": false,
  37. "private_profile": false,
  38. "is_admin": false
  39. },
  40. {
  41. "id": 1,
  42. "name": "Administrator",
  43. "username": "root",
  44. "state": "active",
  45. "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
  46. "web_url": "http://866463f4202a/root",
  47. "created_at": "2020-07-19T10:07:10.205Z",
  48. "bio": null,
  49. "location": null,
  50. "public_email": "",
  51. "skype": "",
  52. "linkedin": "",
  53. "twitter": "",
  54. "website_url": "",
  55. "organization": null,
  56. "job_title": "",
  57. "work_information": null,
  58. "last_sign_in_at": "2020-07-19T10:09:01.060Z",
  59. "confirmed_at": "2020-07-19T10:07:09.640Z",
  60. "last_activity_on": "2020-07-19",
  61. "email": "admin@example.com",
  62. "theme_id": 1,
  63. "color_scheme_id": 1,
  64. "projects_limit": 100000,
  65. "current_sign_in_at": "2020-07-19T10:09:01.060Z",
  66. "identities": [],
  67. "can_create_group": true,
  68. "can_create_project": true,
  69. "two_factor_enabled": false,
  70. "external": false,
  71. "private_profile": false,
  72. "is_admin": true
  73. }
  74. ]
  75. liumiaocn:gitlab liumiao$

删除id为2的用户(关联方式)

  1. liumiaocn:gitlab liumiao$ userid=2
  2. liumiaocn:gitlab liumiao$ curl -X DELETE -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users/${userid}?hard_delete=true
  3. liumiaocn:gitlab liumiao$

结果确认

可以看到,此种方式(hard_delete=true)之下不会产生Ghost用户

  1. liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq .
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 808 100 808 0 0 10631 0 --:--:-- --:--:-- --:--:-- 10631
  5. [
  6. {
  7. "id": 1,
  8. "name": "Administrator",
  9. "username": "root",
  10. "state": "active",
  11. "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
  12. "web_url": "http://866463f4202a/root",
  13. "created_at": "2020-07-19T10:07:10.205Z",
  14. "bio": null,
  15. "location": null,
  16. "public_email": "",
  17. "skype": "",
  18. "linkedin": "",
  19. "twitter": "",
  20. "website_url": "",
  21. "organization": null,
  22. "job_title": "",
  23. "work_information": null,
  24. "last_sign_in_at": "2020-07-19T10:09:01.060Z",
  25. "confirmed_at": "2020-07-19T10:07:09.640Z",
  26. "last_activity_on": "2020-07-19",
  27. "email": "admin@example.com",
  28. "theme_id": 1,
  29. "color_scheme_id": 1,
  30. "projects_limit": 100000,
  31. "current_sign_in_at": "2020-07-19T10:09:01.060Z",
  32. "identities": [],
  33. "can_create_group": true,
  34. "can_create_project": true,
  35. "two_factor_enabled": false,
  36. "external": false,
  37. "private_profile": false,
  38. "is_admin": true
  39. }
  40. ]
  41. liumiaocn:gitlab liumiao$

发表评论

表情:
评论列表 (有 0 条评论,76人围观)

还没有评论,来说两句吧...

相关阅读