User.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = (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. if (!$this->authenticated && empty($this->username))
  61. throw new Exception\RequestErrorException("Missing userdata 'username'.");
  62. $this->scope = ($this->authenticated ? "/user" : "/users/" . $this->username);
  63. break;
  64. default:
  65. return false;
  66. }
  67. return true;
  68. }
  69. /**
  70. * @see Base
  71. */
  72. public function search(string $q) {
  73. $searchable = sprintf("%s %s %s", $this->full_name, $this->username, $this->login);
  74. return stripos($searchable, $q) !== false;
  75. }
  76. /**
  77. * Returns if the user is the authenticated user.
  78. *
  79. * @return bool
  80. */
  81. public function authenticated() {
  82. return $this->authenticated;
  83. }
  84. /**
  85. * Returns every repo under user.
  86. *
  87. * @return Repos
  88. */
  89. public function repos() {
  90. return new Repos($this->url, $this->token, $this);
  91. }
  92. /**
  93. * Return a single repo.
  94. *
  95. * Note: This will also load the repo.
  96. *
  97. * @param string $name Repo name
  98. * @return Repo
  99. */
  100. public function repo(string $name) {
  101. return (new Repo($this->url, $this->token, $this, $name))->load();
  102. }
  103. /**
  104. * Return every organization under user.
  105. *
  106. * @return Orgs
  107. */
  108. public function organizations() {
  109. return new Orgs($this->url, $this->token, $this);
  110. }
  111. /**
  112. * Return a single organization.
  113. *
  114. * Note: This will also load the repo.
  115. *
  116. * @param string $name Organization name
  117. * @return Org
  118. */
  119. public function organization(string $name) {
  120. return (new Org($this->url, $this->token, $this, $name))->load();
  121. }
  122. /**
  123. * Create a new user.
  124. *
  125. * Valid parameters
  126. *
  127. * 1. username
  128. * 2. email
  129. * 3. source_id
  130. * 4. login_name
  131. * 5. password
  132. * 6. send_notify
  133. *
  134. * This reflects the API v1 documentation, but is in an order
  135. * where the required fields are first.
  136. *
  137. * @param ...$args The parameter values.
  138. * @return bool
  139. */
  140. public function create(...$args) {
  141. $params = array(
  142. "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  143. "email" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  144. "source_id" => isset($args[2]) && is_numeric($args[2]) ? $args[2] : null,
  145. "login_name" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
  146. "password" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
  147. "send_notify" => isset($args[5]) && is_bool($args[5]) ? $args[5] : null
  148. );
  149. $params = array_filter($params, function($val) {
  150. return $val != null;
  151. });
  152. parent::create($params);
  153. }
  154. /**
  155. * @see Base
  156. */
  157. protected function json_set_property($obj) {
  158. foreach ($obj as $key => $value) {
  159. if ($this->property_exists($key)) {
  160. $this->{$key} = $value;
  161. }
  162. }
  163. $this->loaded = true;
  164. return true;
  165. }
  166. }
  167. }
  168. ?>