User.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Stores user data and methods related to a single user.
  5. *
  6. * By now the following are supported:
  7. *
  8. * * GET `/user`
  9. * * GET `/users/username`
  10. * * POST `/admin/users` (**Requires** admin rights. Curl will throw NotAuthorized exception if not).
  11. * * DELETE `/admin/users` (**Requires** admin rights. Curl will throw NotAuthorized exception if not).
  12. *
  13. * A user can also list it's repos and organizations.
  14. *
  15. * @see Repos
  16. * @see Orgs
  17. *
  18. * @author Joachim M. Giaever (joachim[]giaever.org)
  19. * @version 0.1.3
  20. */
  21. class User extends Base {
  22. private $authenticated;
  23. public $user_id;
  24. public $user_login;
  25. public $user_full_name;
  26. public $user_email;
  27. public $user_avatar_url;
  28. public $user_username;
  29. /**
  30. * Initialize an user object.
  31. *
  32. * @param string $api_url The api-url
  33. * @param string $api_token The api-token
  34. * @param string $user The username. "Empty" or "me" will return authenticated user
  35. */
  36. public function __construct(string $api_url, string $api_token, string $user = "") {
  37. $this->authenticated = (empty($user) || $user == "me");
  38. parent::__construct($api_url, $api_token);
  39. if (!$this->authenticated())
  40. $this->username = $user;
  41. }
  42. /**
  43. * @see Base
  44. * @throws Exception\InvalidMethodRequest when create on loaded
  45. * @throws Exception\RequestErrorException when userdata is missing
  46. */
  47. protected function set_scope(string $method) {
  48. switch($method) {
  49. case "create":
  50. if ($this->loaded)
  51. throw new Exception\InvalidMethodRequestException("Cannot create user of existing user");
  52. $this->scope = "/admin/users";
  53. break;
  54. case "delete":
  55. if (!$this->username)
  56. throw new Exception\RequestErrorException("Missing userdata 'username'.");
  57. $this->scope = "/admin/users/" . $this->username;
  58. break;
  59. case "get":
  60. case "load":
  61. $this->scope = empty($this->username) ? "/user" : "/users/" . $this->username;
  62. break;
  63. default:
  64. return false;
  65. }
  66. return true;
  67. }
  68. /**
  69. * Returns if the user is the authenticated user.
  70. *
  71. * @return bool
  72. */
  73. public function authenticated() {
  74. return $this->authenticated;
  75. }
  76. /**
  77. * Returns every repo under user.
  78. *
  79. * @return Repos
  80. */
  81. public function repos() {
  82. return new Repos($this->url, $this->token, $this);
  83. }
  84. /**
  85. * Return a single repo.
  86. *
  87. * Note: This will also load the repo.
  88. *
  89. * @param string $name Repo name
  90. * @return Repo
  91. */
  92. public function repo(string $name) {
  93. return (new Repo($this->url, $this->token, $this, $name))->load();
  94. }
  95. /**
  96. * Return every organization under user.
  97. *
  98. * @return Orgs
  99. */
  100. public function organizations() {
  101. return new Orgs($this->url, $this->token, $this);
  102. }
  103. /**
  104. * @alias organizations
  105. */
  106. public function orgs() {
  107. return $this->organizations();
  108. }
  109. /**
  110. * Return a single organization.
  111. *
  112. * Note: This will also load the repo.
  113. *
  114. * @param string $name Organization name
  115. * @return Org
  116. */
  117. public function organization(string $name) {
  118. return (new Org($this->url, $this->token, $this, $name))->load();
  119. }
  120. /**
  121. * @alias organization
  122. */
  123. public function org(string $name) {
  124. return $this->organization($name);
  125. }
  126. /**
  127. * Create a new user.
  128. *
  129. * Valid parameters
  130. *
  131. * 1. username
  132. * 2. email
  133. * 3. source_id
  134. * 4. login_name
  135. * 5. password
  136. * 6. send_notify
  137. *
  138. * This reflects the API v1 documentation, but is in an order
  139. * where the required fields are first.
  140. *
  141. * @param ...$args The parameter values.
  142. * @return bool
  143. */
  144. public function create(...$args) {
  145. $params = array(
  146. "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  147. "email" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  148. "source_id" => isset($args[2]) && is_numeric($args[2]) ? $args[2] : null,
  149. "login_name" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
  150. "password" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
  151. "send_notify" => isset($args[5]) && is_bool($args[5]) ? $args[5] : null
  152. );
  153. $params = array_filter($params, function($val) {
  154. return $val != null;
  155. });
  156. parent::create($params);
  157. }
  158. }
  159. }
  160. ?>