Skip to content

Commit

Permalink
Added contacts v1 create, update and delete routes
Browse files Browse the repository at this point in the history
Updated the model by following methods: getContact, deleteContact
Added new routes corresponding to the methods
  • Loading branch information
disc5 committed Apr 20, 2016
1 parent bd9bbc0 commit f7e078d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function addContactEntry($user_id, $login,$firstname,$lastname,$email)
* @param $a_email
* @return bool
*/
function updateContactEntry($user_id, $a_addr_id,$a_login,$a_firstname,$a_lastname,$a_email)
function updateContactEntry($user_id, $a_addr_id, $a_login, $a_firstname, $a_lastname, $a_email)
{
Libs\RESTilias::loadIlUser();
global $ilUser;
Expand Down Expand Up @@ -99,4 +99,25 @@ function deleteContactEntry($user_id, $addr_id)
return $abook->deleteEntry($addr_id);
}


/**
* Returns a contact entry.
* Return associate array: with keys "addr_id","login","firstname","lastname","email","auto_update"
*
* @param $user_id
* @param $addr_id
* @return array
*/
function getContactEntry($user_id, $addr_id)
{
Libs\RESTilias::loadIlUser();
global $ilUser;
$ilUser->setId($user_id);
$ilUser->read();
Libs\RESTilias::initAccessHandling();

$abook = new \ilAddressbook($ilUser->getId());
return $abook->getEntry($addr_id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,79 @@
});

/**
* Returns the personal ILIAS contacts for a user specified by id.
* Creates a new contact entry to the contact list of the authenticated user.
* Requires POST variables: login, firstname, lastname, email
*/
$app->post('/contacts/add', RESTAuth::checkAccess(RESTAuth::PERMISSION), function() use ($app) {
$accessToken = $app->request->getToken();
$request = $app->request();
$login = $request->params("login");
$firstname = $request->params("firstname");
$lastname = $request->params("lastname");
$email = $request->params("email");

$authorizedUserId = $accessToken->getUserId();

if ($authorizedUserId > -1) { // only the user is allowed to access the data
$id = $authorizedUserId;

$model = new ContactsModel();
$data = $model->addContactEntry($id,$login,$firstname,$lastname,$email);
$app->success($data);

}
else {
$app->halt(401, Libs\OAuth2Middleware::MSG_NO_ADMIN, Libs\OAuth2Middleware::ID_NO_ADMIN);
}
});

/**
* Deletes entry specified by addr_id from the contact list of the authenticated user.
*/
$app->delete('/contacts/:addr_id', RESTAuth::checkAccess(RESTAuth::PERMISSION), function ($addr_id) use ($app) {
$accessToken = $app->request->getToken();
$authorizedUserId = $accessToken->getUserId();


if ($authorizedUserId > -1) { // only the user is allowed to access the data
$id = $authorizedUserId;

$model = new ContactsModel();
$data = $model->deleteContactEntry($id, $addr_id);

$app->success($data);
}
else {
$app->halt(401, Libs\OAuth2Middleware::MSG_NO_ADMIN, Libs\OAuth2Middleware::ID_NO_ADMIN);
}
});

/**
* Updates contact entry addr_id of the authenticated user.
*/
$app->put('/contacts/:addr_id', RESTAuth::checkAccess(RESTAuth::PERMISSION), function ($addr_id) use ($app) {
$accessToken = $app->request->getToken();
$authorizedUserId = $accessToken->getUserId();
$request = $app->request();

if ($authorizedUserId > -1) { // only the user is allowed to access the data
$id = $authorizedUserId;
$model = new ContactsModel();
$data = $model->getContactEntry($id, $addr_id);
$new_login = $request->params("login",$data['login'],false);
$new_firstname = $request->params("firstname",$data['firstname'],false);
$new_lastname = $request->params("lastname",$data['lastname'],false);
$new_email = $request->params("email",$data['email'],false);
$success = $model->updateContactEntry($id, $addr_id, $new_login, $new_firstname, $new_lastname, $new_email);
$app->success($success);
}
else {
$app->halt(401, Libs\OAuth2Middleware::MSG_NO_ADMIN, Libs\OAuth2Middleware::ID_NO_ADMIN);
}
});

/**
* Admin: Returns all contacts of a user specified by id.
*/
$app->get('/contacts/:id', RESTAuth::checkAccess(RESTAuth::ADMIN), function ($id) use ($app) {
$accessToken = $app->request->getToken();
Expand Down

0 comments on commit f7e078d

Please sign in to comment.