User.php 5.8 KB

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