authenticated = (empty($user) || $user == "me"); parent::__construct($api_url, $api_token); if (!$this->authenticated()) $this->username = $user; } /** * @see Base * @throws Exception\InvalidMethodRequest when create on loaded * @throws Exception\RequestErrorException when userdata is missing */ protected function set_scope(string $method) { switch($method) { case "create": if ($this->loaded) throw new Exception\InvalidMethodRequestException("Cannot create user of existing user"); $this->scope = "/admin/users"; break; case "delete": if (!$this->username) throw new Exception\RequestErrorException("Missing userdata 'username'."); $this->scope = "/admin/users/" . $this->username; break; case "get": case "load": if (!$this->authenticated && empty($this->username)) throw new Exception\RequestErrorException("Missing userdata 'username'."); $this->scope = ($this->authenticated ? "/user" : "/users/" . $this->username); break; default: return false; } return true; } /** * @see Base */ public function search(string $q) { $searchable = sprintf("%s %s %s", $this->full_name, $this->username, $this->login); return stripos($searchable, $q) !== false; } /** * Returns if the user is the authenticated user. * * @return bool */ public function authenticated() { return $this->authenticated; } /** * Returns every repo under user. * * @return Repos */ public function repos() { return new Repos($this->url, $this->token, $this); } /** * Return a single repo. * * Note: This will also load the repo. * * @param string $name Repo name * @return Repo */ public function repo(string $name) { return (new Repo($this->url, $this->token, $this, $name))->load(); } /** * Return every organization under user. * * @return Orgs */ public function organizations() { return new Orgs($this->url, $this->token, $this); } /** * @alias organizations */ public function orgs() { return $this->organizations(); } /** * Return a single organization. * * Note: This will also load the repo. * * @param string $name Organization name * @return Org */ public function organization(string $name) { return (new Org($this->url, $this->token, $this, $name))->load(); } /** * @alias organization */ public function org(string $name) { return $this->organization($name); } /** * Create a new user. * * Valid parameters * * 1. username * 2. email * 3. source_id * 4. login_name * 5. password * 6. send_notify * * This reflects the API v1 documentation, but is in an order * where the required fields are first. * * @param ...$args The parameter values. * @return bool */ public function create(...$args) { $params = array( "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null, "email" => isset($args[1]) && is_string($args[1]) ? $args[1] : null, "source_id" => isset($args[2]) && is_numeric($args[2]) ? $args[2] : null, "login_name" => isset($args[3]) && is_string($args[3]) ? $args[3] : null, "password" => isset($args[4]) && is_string($args[4]) ? $args[4] : null, "send_notify" => isset($args[5]) && is_bool($args[5]) ? $args[5] : null ); $params = array_filter($params, function($val) { return $val != null; }); parent::create($params); } /** * @see Base */ protected function json_set_property($obj) { foreach ($obj as $key => $value) { if ($this->property_exists($key)) { $this->{$key} = $value; } } $this->loaded = true; return true; } } } ?>