User.php 6.0 KB

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