From c3cd49438660e4bb8b7d6ea6d3a5d3fdbe4a8d61 Mon Sep 17 00:00:00 2001 From: William MARTIN Date: Mon, 7 Mar 2011 15:01:51 +0100 Subject: [PATCH] Add Project List API --- src/IDF/Precondition.php | 9 +++++++-- src/IDF/Views.php | 4 +++- src/IDF/Views/Api.php | 27 ++++++++++++++++++++++++--- src/IDF/conf/urls.php | 5 +++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/IDF/Precondition.php b/src/IDF/Precondition.php index fe290b7..314c68e 100644 --- a/src/IDF/Precondition.php +++ b/src/IDF/Precondition.php @@ -190,7 +190,12 @@ class IDF_Precondition return true; // Again need authentication error } $request->user = $users[0]; - IDF_Middleware::setRights($request); + + // Don't try to load projects rights access if we are not in one + if ($request->query !== "/api/") { + IDF_Middleware::setRights($request); + } + return true; } @@ -256,4 +261,4 @@ class IDF_Precondition $encrypted = trim($cr->encrypt($user->id.':'.$project->id), '~'); return substr(md5(Pluf::f('secret_key').$encrypted), 0, 2).$encrypted; } -} \ No newline at end of file +} diff --git a/src/IDF/Views.php b/src/IDF/Views.php index e79b510..cb44ce6 100644 --- a/src/IDF/Views.php +++ b/src/IDF/Views.php @@ -37,10 +37,12 @@ class IDF_Views * Only the public projects are listed or the private with correct * rights. */ - public function index($request, $match) + public function index($request, $match, $api=false) { $projects = self::getProjects($request->user); $stats = self::getProjectsStatistics ($projects); + + if ($api == true) return $projects; return Pluf_Shortcuts_RenderToResponse('idf/index.html', array('page_title' => __('Projects'), 'projects' => $projects, diff --git a/src/IDF/Views/Api.php b/src/IDF/Views/Api.php index 40753f3..2c5b0e1 100644 --- a/src/IDF/Views/Api.php +++ b/src/IDF/Views/Api.php @@ -64,7 +64,7 @@ class IDF_Views_Api $out = array(); if ($request->method == 'GET') { // We give the details of the form - $out['doc'] = 'A POST request against this url will allow you to create a new issue.'; + $out['help'] = 'A POST request against this url will allow you to create a new issue.'; if ($request->user->hasPerm('IDF.project-owner', $request->project) or $request->user->hasPerm('IDF.project-member', $request->project)) { $out['status'] = array(); @@ -76,16 +76,37 @@ class IDF_Views_Api } else { // We need to give back the results of the creation if (is_object($p) and 'IDF_Issue' == get_class($p)) { - $out['mess'] = 'success'; + $out['message'] = 'success'; $out['issue'] = $p->id; } else { - $out['mess'] = 'error'; + $out['message'] = 'error'; $out['errors'] = $p['form']->errors; } } return new Pluf_HTTP_Response_Json($out); } + /** + * List all the projects + */ + public $projectIndex_precond = array('IDF_Precondition::apiSetUser'); + + public function projectIndex($request, $match) + { + $view = new IDF_Views(); + $projects = $view->index($request, $match, true); + + $data = array(); + foreach ($projects as $p) { + $data[] = array("shortname" => $p->shortname, "name" => $p->name, "shortdesc" => $p->shortdesc, "private" => $p->private); + } + + $out = array(); + $out['message'] = 'success'; + $out['projects'] = $data; + return new Pluf_HTTP_Response_Json($out); + } + /** * Get the list of tags to give them to the end users when doing a * GET request against a form. That way it is possible for them to diff --git a/src/IDF/conf/urls.php b/src/IDF/conf/urls.php index 3c5af99..e813413 100644 --- a/src/IDF/conf/urls.php +++ b/src/IDF/conf/urls.php @@ -364,6 +364,11 @@ $ctl[] = array('regex' => '#^/api/p/([\-\w]+)/issues/create/$#', 'model' => 'IDF_Views_Api', 'method' => 'issueCreate'); +$ctl[] = array('regex' => '#^/api/$#', + 'base' => $base, + 'model' => 'IDF_Views_Api', + 'method' => 'projectIndex'); + // ---------- FORGE ADMIN -------------------------------- $ctl[] = array('regex' => '#^/admin/projects/$#',