Skip to content

Commit

Permalink
directory implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme Barile committed Apr 20, 2015
1 parent 3d02062 commit 2ac49f7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
5 changes: 2 additions & 3 deletions app/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@

use Objectiveweb\Router;

defined('OW_CONFIG') || define('OW_CONFIG', ROOT.'/ow-config.php');

Router::route("GET /?", function() {
return ow()->version();
});

Router::GET('/([a-z][a-z0-9_]*)/?', 'fetch');

Router::GET('/([a-z][a-z0-9_]*)/([\w-.\ ]+)?', "get");

Router::POST('/([a-z][a-z0-9_]*)/?', "post");

Router::PUT('/([a-z][a-z0-9_]*)/([\w-.\ ]+)?', "put");

Router::DELETE('/([a-z][a-z0-9_]*)/([\w-.\ ]+)?', "delete");

//Router::route("GET /([a-z][a-z0-9_]*)/?", 'fetch');
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 62 additions & 7 deletions src/Objectiveweb/Handler/Directory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Objectiveweb;
namespace Objectiveweb\Handler;

use Objectiveweb\Handler;

class Directory extends Handler {

Expand All @@ -16,25 +18,78 @@ public function init()
$this->gacl = new \gacl_api($this->params);
}

public function setup() {
public function setup($params = array()) {
$this->gacl->add_object_section('System', 'system', 0, 0, 'aco');
$this->gacl->add_object_section('Users', 'users', 0, 0, 'aro');
$this->gacl->add_object('users', 'anonymous', 'Anonymous', 0, true, 'aro');
$this->gacl->add_object('users', 'Anonymous', 'anonymous', 0, true, 'aro');

return true;
}

public function get($user, $params) {
if($user == 'setup') {
return $this->setup($params);
}

$id = $this->gacl->get_object_id('users', $user, 'aro');

return $this->gacl->get_object_data($id, 'aro')[0];

}

public function fetch() {
// TODO listar todos os users + sections do ARO
$q = $this->gacl->get_objects('users', 0, 'aro');

return $q['users'];
}

public function post($data) {
$username = trim(strtolower($data['username']));

// TODO criar ARO na section Users (section = group no json ?)
// TODO senha ?
$id = $this->gacl->add_object('users', $data['profile'], $username, 0, false, 'aro');

if($id === FALSE) {
throw new \Exception('Error adding user', 500);
}
elseif($id === TRUE) {
throw new \Exception('User already exists', 409);
}

return array('id' => $username);
}

public function put($id, $data) {
// TODO atualizar dados da ARO
public function put($user, $data) {

$id = $this->gacl->get_object_id('users', $user, 'aro');

if($id === FALSE) {
throw new \Exception('User not found', 404);
}

if($this->gacl->edit_object(
$id,
'users',
$data,
$user,
0,
0,
'aro') === FALSE) {
throw new \Exception('Error updating user', 500);
}

return array('id' => $user);
}

public function delete($id) {
// TODO Excluir ARO
public function delete($user) {
$id = $this->gacl->get_object_id('users', $user, 'aro');

if($this->gacl->del_object($id, 'aro', true) === FALSE) {
throw new \Exception('Error deleting entry', 500);
}

return array('id' => $user);
}
}
2 changes: 1 addition & 1 deletion src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

// Register the directory
register_domain('directory', array(
'handler' => '\Objectiveweb\Directory',
'handler' => '\Objectiveweb\Handler\Directory',
'db_table_prefix' => 'ow_',
'db_type' => OW_DB_DRIVER,
'db_host' => OW_DB_HOST,
Expand Down
3 changes: 1 addition & 2 deletions src/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ function insert($data)
*/
function update($key, $data)
{

global $mysqli;

if (empty($key)) throw new Exception('A condition is required for UPDATEs', 405);
Expand Down Expand Up @@ -636,7 +635,7 @@ function init()
{

if(empty($this->params['table'])) {
throw new Exception("Please specify the table for domain $this->id", 500);
throw new Exception("table parameter missing on $this->id", 500);
}

$this->hasOne = $this->params['hasOne'];
Expand Down

0 comments on commit 2ac49f7

Please sign in to comment.