User.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Gogs\API\Request {
  3. final class User extends Base {
  4. private $authenticated;
  5. public $uid;
  6. public $ulogin;
  7. public $ufull_name;
  8. public $uemail;
  9. public $uavatar_url;
  10. public $uusername;
  11. public function __construct(string $api_url, string $api_token, string $user = "") {
  12. $this->uusername = (strlen($user) == 0 || $user == "me" ? "me" : $user);
  13. $this->authenticated = $this->uusername == "me";
  14. parent::__construct($api_url, $api_token);
  15. }
  16. protected function set_scope(string $method) {
  17. switch($method) {
  18. case "create":
  19. if ($this->loaded)
  20. throw new Exception\InvalidMethodRequest("Cannot create user of existing user");
  21. $this->scope = "/admin/users";
  22. return true;
  23. case "delete":
  24. $this->scope = "/admin/users/" . $this->uusername;
  25. return true;
  26. default:
  27. $this->scope = ($this->authenticated ? "/user" : "/users/" . $this->uusername);
  28. }
  29. }
  30. public function authenticated() {
  31. return $this->authenticated;
  32. }
  33. public function repos() {
  34. return new Repos($this->url, $this->token, $this);
  35. }
  36. public function repo(string $name) {
  37. return (new Repo($this->url, $this->token, $this, $name))->load();
  38. }
  39. public function organizations() {
  40. return new Orgs($this->url, $this->token, $this);
  41. }
  42. public function create(...$args) {
  43. $params = array(
  44. "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  45. "email" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  46. "source_id" => isset($args[2]) && is_numeric($args[2]) ? $args[2] : null,
  47. "login_name" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
  48. "password" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
  49. "send_notify" => isset($args[5]) && is_bool($args[5]) ? $args[5] : null
  50. );
  51. $params = array_filter($params, function($val) {
  52. return $val != null;
  53. });
  54. parent::create($params);
  55. }
  56. protected function json_set_property($obj) {
  57. foreach ($obj as $key => $value) {
  58. $key = 'u' . $key;
  59. if (property_exists($this, $key))
  60. $this->{$key} = $value;
  61. }
  62. $this->loaded = true;
  63. }
  64. }
  65. }
  66. ?>