Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP API Endpoints for Organization management #276

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 25 additions & 64 deletions application/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,51 +266,57 @@

//_______________________________________________
//HTTP API
$route['api/doc'] = 'api/doc';
$route['api/token'] = 'api/token';
$route['api/contracts/(:num)'] = 'api/contracts/$1';
$route['api/contracts/'] = 'api/contracts';
$route['api/contracts'] = 'api/contracts';
$route['api/entitleddayscontract/(:num)'] = 'api/entitleddayscontract/$1';
$route['api/addentitleddayscontract/(:num)'] = 'api/addentitleddayscontract/$1';
$route['api/entitleddaysemployee/(:num)'] = 'api/entitleddaysemployee/$1';
$route['api/addentitleddaysemployee/(:num)'] = 'api/addentitleddaysemployee/$1';
$route['api/leavessummary/(:num)/(:num)'] = 'api/leavessummary/$1/$2';
$route['api/leavessummary/(:num)'] = 'api/leavessummary/$1';
$route['api/leaves/(:any)/(:any)'] = 'api/leavesInRange/$1/$2';
$route['api/leavetypes/'] = 'api/leavetypes';
$route['api/leaves/(:num)/(:num)'] = 'api/leaves/$1/$2';
$route['api/leavetypes'] = 'api/leavetypes';
$route['api/positions/'] = 'api/positions';
$route['api/positions'] = 'api/positions';
$route['api/userdepartment/(:num)'] = 'api/userdepartment/$1';
$route['api/userextras/(:num)'] = 'api/userextras/$1';
$route['api/userleaves/(:num)'] = 'api/userleaves/$1';
$route['api/users/(:num)/leaves/'] = 'api/userleaves/$1';
$route['api/users/(:num)']['GET'] = 'api/users/$1';
$route['api/users/']['GET'] = 'api/users';
$route['api/users/(:num)'] = 'api/users/$1';
$route['api/users'] = 'api/users';
//v0.4.0
$route['api/monthlypresence/(:num)/(:num)/(:num)'] = 'api/monthlypresence/$1/$2/$3';
$route['api/deleteuser/(:num)'] = 'api/deleteuser/$1';
$route['api/users/(:num)']['DELETE'] = 'api/deleteuser/$1';
$route['api/updateuser/(:num)'] = 'api/updateuser/$1';
$route['api/users/(:num)']['PATCH'] = 'api/updateuser/$1';
$route['api/createuser/(:any)'] = 'api/createuser/$1';
$route['api/createuser'] = 'api/createuser';
$route['api/users/']['POST'] = 'api/createuser/$1';
$route['api/users/(:any)']['POST'] = 'api/createuser/$1';
$route['api/createleave'] = 'api/createleave';
$route['api/leaves']['POST'] = 'api/createleave';
$route['api/leaves/']['POST'] = 'api/createleave';
//v0.4.3
$route['api/getListOfEmployeesInEntity/(:num)/(:any)'] = 'api/getListOfEmployeesInEntity/$1/$2';
//v0.4.4
$route['api/acceptleaves/(:num)'] = 'api/acceptleave/$1';
$route['api/acceptleave/(:num)'] = 'api/acceptleave/$1';
$route['api/rejectleaves/(:num)'] = 'api/rejectleave/$1';
$route['api/rejectleave/(:num)'] = 'api/rejectleave/$1';
$route['api/acceptleaves/(:num)'] = 'api/acceptleaves/$1';
$route['api/rejectleaves/(:num)'] = 'api/rejectleaves/$1';
//v0.6.0
$route['api/users/ext'] = 'api/usersExt';
//MRM
$route['api/organization_node/(:num)'] = 'api/organization_node/$1';
$route['api/organization_children/(:num)'] = 'api/organization_children/$1';
$route['api/createnodeorganization/'] = 'api/createnodeorganization';
$route['api/movenodeorganization/(:num)/(:num)'] = 'api/movenodeorganization/$1/$2';
$route['api/deletenodeorganization/(:num)'] = 'api/deletenodeorganization/$1';
$route['api/renamenodeorganization/(:num)'] = 'api/renamenodeorganization/$1';
$route['api/attachEmployee/(:num)/(:num)'] = 'api/attachEmployee/$1/$2';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REST: api/entities/(:num)/employees/(:num)/attach Or (POST) api/entities/(:num)/employees/(:num)

$route['api/detachEmployee/(:num)/(:num)'] = 'api/detachEmployee/$1/$2';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The route has two parameters but only one is used

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REST: api/entities/employees/(:num)/detach


//_______________________________________________
//REST API (for mobile/HTML Clients)
$route['api/rest/leaves']['OPTIONS'] = 'rest/options';
$route['api/rest/leaves']['GET'] = 'rest/leaves';

//User properties : is admin, is manager, id, id of manager, manager, contract..
$route['api/rest/users/self']['GET'] = 'rest/getPropertiesOfConnectedUser';

$route['api/rest/contracts']['OPTIONS'] = 'rest/options';
$route['api/rest/users']['OPTIONS'] = 'rest/options';

//_______________________________________________
//ICS Feeds
Expand Down Expand Up @@ -339,53 +345,8 @@
$route['api/authorization/login'] = 'authorization/login';
$route['api/authorization/userinfo'] = 'authorization/userinfo';

//_______________________________________________
//REST API (for mobile/HTML Clients)
$route['api/rest/self']['OPTIONS'] = 'Rest/options';
$route['api/rest/self']['GET'] = 'Rest/self';
$route['api/rest/profile']['OPTIONS'] = 'Rest/options';
$route['api/rest/profile']['GET'] = 'Rest/profile';

$route['api/rest/config']['OPTIONS'] = 'Rest/options';
$route['api/rest/config']['GET'] = 'Rest/config';
$route['api/rest/checksum/(:any)']['OPTIONS'] = 'Rest/options';
$route['api/rest/checksum/(:any)']['GET'] = 'Rest/checksum/$1';
$route['api/rest/checksum']['OPTIONS'] = 'Rest/options';
$route['api/rest/checksum']['GET'] = 'Rest/checksum';
$route['api/rest/submissions']['OPTIONS'] = 'Rest/options';
$route['api/rest/submissions']['GET'] = 'Rest/submissions';

$route['api/rest/leaves']['OPTIONS'] = 'RestLeaves/options';
$route['api/rest/leaves']['GET'] = 'RestLeaves/leaves';
$route['api/rest/leaves']['POST'] = 'RestLeaves/create';
$route['api/rest/leaves/(:num)']['OPTIONS'] = 'RestLeaves/options';
$route['api/rest/leaves/(:num)']['GET'] = 'RestLeaves/leaves/$1';
$route['api/rest/leaves/(:num)']['PATCH'] = 'RestLeaves/edit/$1';
$route['api/rest/leaves/(:num)']['DELETE'] = 'RestLeaves/delete/$1';

$route['api/rest/requests']['OPTIONS'] = 'RestRequests/options';
$route['api/rest/requests']['GET'] = 'RestRequests/requests';
$route['api/rest/requests/(:any)']['OPTIONS'] = 'RestRequests/requests/$1';
$route['api/rest/requests/(:any)']['GET'] = 'RestRequests/requests/$1';

//User properties : is admin, is manager, id, id of manager, manager, contract..
$route['api/rest/users/(:num)']['OPTIONS'] = 'RestUsers/options';
$route['api/rest/users/(:num)']['GET'] = 'RestUsers/users/$1';
$route['api/rest/users']['OPTIONS'] = 'RestUsers/options';
$route['api/rest/users']['GET'] = 'RestUsers/users';

$route['api/rest/users/(:num)/enable']['OPTIONS'] = 'RestUsers/options';
$route['api/rest/users/(:num)/enable']['GET'] = 'RestUsers/enable/$1';
$route['api/rest/users/(:num)/disable']['OPTIONS'] = 'RestUsers/options';
$route['api/rest/users/(:num)/disable']['GET'] = 'RestUsers/disable/$1';

$route['api/rest/admin/logs/(:any)']['OPTIONS'] = 'RestAdmin/options';
$route['api/rest/admin/logs/(:any)']['GET'] = 'RestAdmin/logs/$1';
$route['api/rest/admin/logs']['OPTIONS'] = 'RestAdmin/options';
$route['api/rest/admin/logs']['GET'] = 'RestAdmin/logs';

//_______________________________________________
//Default controllers
$route['default_controller'] = 'leaves';
$route['404_override'] = 'pages/notfound';
$route['notfound'] = 'pages/notfound';
$route['(:any)'] = 'pages/view/$1';
127 changes: 127 additions & 0 deletions application/controllers/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1792,4 +1792,131 @@ private function validateDate($date, $format = 'Y-m-d')
return $d && $d->format($format) === $date;
}

/**
* Get one node of the organization
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing OpenAPI notation, see 32dbe5a

* @author Mickael ROMMME
*/
public function organization_node($id) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use underscore and use meaningful name, such as getEntity as Organization is composed of entities

if (!$this->server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$this->server->getResponse()->send();
} else {
$this->load->model('Organization_model');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Organization_model should be in lower case everywhere

$result = $this->Organization_model->getName($id);
echo json_encode($result);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MIMETYPE (output encoding) is not set. You should rely on the framework, e.g.:

            $this->output
                ->set_content_type('application/json')
                ->set_output(json_encode($result));

}
}

/**
* Get id of all childrem of one node of the organization
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment more clearly with expected parameters passed in POST request

* @author Mickael ROMMME
*/
public function organization_children($id) {
if (!$this->server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$this->server->getResponse()->send();
} else {
$this->load->model('Organization_model');
$result = $this->Organization_model->getAllChildren($id);
echo json_encode($result);
}
}

/**
* Create one node of the organization
* @author Mickael ROMMME
*/
public function createnodeorganization() {
if (!$this->server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$this->server->getResponse()->send();
} else {
$this->load->model('Organization_model');

$name = $this->input->post('name');
$parent_id = $this->input->post('parent_id');

//Check mandatory fields
if ($name == FALSE || $parent_id == FALSE) {
$this->output->set_header("HTTP/1.1 422 Unprocessable entity");
log_message('error', 'Mandatory fields are missing.');
} else {
$result = $this->Organization_model->create($parent_id, $name);
echo json_encode($result);
}
}
}

/**
* Move one node of the organization
* @author Mickael ROMMME
*/
public function movenodeorganization($id, $parent_id) {
if (!$this->server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$this->server->getResponse()->send();
} else {
$this->load->model('Organization_model');
$result = $this->Organization_model->move($id, $parent_id);
echo json_encode($result);
}
}

/**
* Delete one node of the organization
* Delete all children and unset organization id for user
* @author Mickael ROMMME
*/
public function deletenodeorganization($id) {
if (!$this->server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$this->server->getResponse()->send();
} else {
$this->load->model('Organization_model');
$result = $this->Organization_model->delete($id);
echo json_encode($result);
}
}

/**
* Rename one node of the organization
* @author Mickael ROMMME
*/
public function renamenodeorganization($id) {
if (!$this->server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$this->server->getResponse()->send();
} else {
$this->load->model('Organization_model');
$name = $this->input->post('name');
$result = $this->Organization_model->rename($id, $name);
if (empty($result)) {
$this->output->set_header("HTTP/1.1 422 Unprocessable entity");
} else {
echo json_encode($result);
}
}
}

/**
* Attach an employee to one node of the organization
* @author Mickael ROMMME
*/
public function attachEmployee($id, $entity) {
if (!$this->server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$this->server->getResponse()->send();
} else {
$this->load->model('Organization_model');
$result = $this->Organization_model->attachEmployee($id, $entity);
echo json_encode($result);
}
}

/**
* Detach an employee to one node of the organization
* @author Mickael ROMMME
*/
public function detachEmployee($id) {
if (!$this->server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$this->server->getResponse()->send();
} else {
$this->load->model('Organization_model');
$result = $this->Organization_model->detachEmployee($id);
echo json_encode($result);
}
}
}