User.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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
  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 = (strlen($user) == 0 || $user == "me");
  38. parent::__construct($api_url, $api_token);
  39. }
  40. /**
  41. * @see Base
  42. * @throws Exception\InvalidMethodRequest when create on loaded
  43. * @throws Exception\RequestErrorException when userdata is missing
  44. */
  45. protected function set_scope(string $method) {
  46. switch($method) {
  47. case "create":
  48. if ($this->loaded)
  49. throw new Exception\InvalidMethodRequestException("Cannot create user of existing user");
  50. $this->scope = "/admin/users";
  51. break;
  52. case "delete":
  53. if (!$this->username)
  54. throw new Exception\RequestErrorException("Missing userdata 'username'.");
  55. $this->scope = "/admin/users/" . $this->username;
  56. break;
  57. case "get":
  58. if (!$this->authenticated && empty($this->username))
  59. throw new Exception\RequestErrorException("Missing userdata 'username'.");
  60. $this->scope = ($this->authenticated ? "/user" : "/users/" . $this->username);
  61. break;
  62. default:
  63. return false;
  64. }
  65. return true;
  66. }
  67. /**
  68. * @see Base
  69. */
  70. public function search(string $q) {
  71. $searchable = sprintf("%s %s %s", $this->full_name, $this->username, $this->login);
  72. return stripos($searchable, $q) !== false;
  73. }
  74. /**
  75. * Returns if the user is the authenticated user.
  76. *
  77. * @return bool
  78. */
  79. public function authenticated() {
  80. return $this->authenticated;
  81. }
  82. /**
  83. * Returns every repo under user.
  84. *
  85. * @return Repos
  86. */
  87. public function repos() {
  88. return new Repos($this->url, $this->token, $this);
  89. }
  90. /**
  91. * Return a single repo.
  92. *
  93. * Note: This will also load the repo.
  94. *
  95. * @param string $name Repo name
  96. * @return Repo
  97. */
  98. public function repo(string $name) {
  99. return (new Repo($this->url, $this->token, $this, $name))->load();
  100. }
  101. /**
  102. * Return every organization under user.
  103. *
  104. * @return Orgs
  105. */
  106. public function organizations() {
  107. return new Orgs($this->url, $this->token, $this);
  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. * Create a new user.
  122. *
  123. * Valid parameters
  124. *
  125. * 1. username
  126. * 2. email
  127. * 3. source_id
  128. * 4. login_name
  129. * 5. password
  130. * 6. send_notify
  131. *
  132. * This reflects the API v1 documentation, but is in an order
  133. * where the required fields are first.
  134. *
  135. * @param ...$args The parameter values.
  136. * @return bool
  137. */
  138. public function create(...$args) {
  139. $params = array(
  140. "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  141. "email" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  142. "source_id" => isset($args[2]) && is_numeric($args[2]) ? $args[2] : null,
  143. "login_name" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
  144. "password" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
  145. "send_notify" => isset($args[5]) && is_bool($args[5]) ? $args[5] : null
  146. );
  147. $params = array_filter($params, function($val) {
  148. return $val != null;
  149. });
  150. parent::create($params);
  151. }
  152. /**
  153. * @see Base
  154. */
  155. protected function json_set_property($obj) {
  156. foreach ($obj as $key => $value) {
  157. if ($this->property_exists($key)) {
  158. $this->{$key} = $value;
  159. }
  160. }
  161. $this->loaded = true;
  162. return true;
  163. }
  164. }
  165. }
  166. ?>