Include User and settings for the API

This commit is contained in:
Diego Najar 2019-09-25 20:19:34 +02:00
parent 21a48609c2
commit 3617ce9b88
1 changed files with 68 additions and 7 deletions

View File

@ -138,10 +138,6 @@ class pluginAPI extends Plugin {
$pageKey = $parameters[1]; $pageKey = $parameters[1];
$data = $this->editPage($pageKey, $inputs); $data = $this->editPage($pageKey, $inputs);
} }
// (PUT) /api/settings
elseif ( ($method==='PUT') && ($parameters[0]==='settings') && empty($parameters[1]) && $writePermissions ) {
$data = $this->editSettings($inputs);
}
// (DELETE) /api/pages/<key> // (DELETE) /api/pages/<key>
elseif ( ($method==='DELETE') && ($parameters[0]==='pages') && !empty($parameters[1]) && $writePermissions ) { elseif ( ($method==='DELETE') && ($parameters[0]==='pages') && !empty($parameters[1]) && $writePermissions ) {
$pageKey = $parameters[1]; $pageKey = $parameters[1];
@ -151,6 +147,10 @@ class pluginAPI extends Plugin {
elseif ( ($method==='POST') && ($parameters[0]==='pages') && empty($parameters[1]) && $writePermissions ) { elseif ( ($method==='POST') && ($parameters[0]==='pages') && empty($parameters[1]) && $writePermissions ) {
$data = $this->createPage($inputs); $data = $this->createPage($inputs);
} }
// (PUT) /api/settings
elseif ( ($method==='PUT') && ($parameters[0]==='settings') && empty($parameters[1]) && $writePermissions ) {
$data = $this->editSettings($inputs);
}
// (POST) /api/images // (POST) /api/images
elseif ( ($method==='POST') && ($parameters[0]==='images') && $writePermissions ) { elseif ( ($method==='POST') && ($parameters[0]==='images') && $writePermissions ) {
$data = $this->uploadImage($inputs); $data = $this->uploadImage($inputs);
@ -173,6 +173,15 @@ class pluginAPI extends Plugin {
$categoryKey = $parameters[1]; $categoryKey = $parameters[1];
$data = $this->getCategory($categoryKey); $data = $this->getCategory($categoryKey);
} }
// (GET) /api/users
elseif ( ($method==='GET') && ($parameters[0]==='users') && empty($parameters[1]) ) {
$data = $this->getUsers();
}
// (GET) /api/users/<username>
elseif ( ($method==='GET') && ($parameters[0]==='users') && !empty($parameters[1]) ) {
$username = $parameters[1];
$data = $this->getUser($username);
}
else { else {
$this->response(401, 'Unauthorized', array('message'=>'Access denied or invalid endpoint.')); $this->response(401, 'Unauthorized', array('message'=>'Access denied or invalid endpoint.'));
} }
@ -327,13 +336,14 @@ class pluginAPI extends Plugin {
$scheduled = (isset($args['scheduled'])?$args['scheduled']=='true':false); $scheduled = (isset($args['scheduled'])?$args['scheduled']=='true':false);
$untagged = (isset($args['untagged'])?$args['untagged']=='true':false); $untagged = (isset($args['untagged'])?$args['untagged']=='true':false);
$numberOfItems = $this->getValue('numberOfItems'); $numberOfItems = (isset($args['numberOfItems'])?$args['numberOfItems']:10);
$pageNumber = 1; $pageNumber = (isset($args['pageNumber'])?$args['pageNumber']:1);
$list = $pages->getList($pageNumber, $numberOfItems, $published, $static, $sticky, $draft, $scheduled); $list = $pages->getList($pageNumber, $numberOfItems, $published, $static, $sticky, $draft, $scheduled);
$tmp = array( $tmp = array(
'status'=>'0', 'status'=>'0',
'message'=>'List of pages, number of items: '.$numberOfItems, 'message'=>'List of pages',
'numberOfItems'=>$numberOfItems,
'data'=>array() 'data'=>array()
); );
@ -580,4 +590,55 @@ class pluginAPI extends Plugin {
'data'=>$data 'data'=>$data
); );
} }
/*
| Returns the user profile
|
| @username string Username
|
| @return array
*/
private function getUser($username)
{
try {
$user = new User($username);
} catch (Exception $e) {
return array(
'status'=>'1',
'message'=>'User not found by username: '.$username
);
}
$data = $user->json($returnsArray=true);
return array(
'status'=>'0',
'message'=>'User profile.',
'data'=>$data
);
}
/*
| Returns all the users
|
| @return array
*/
private function getUsers()
{
global $users;
$data = array();
foreach ($users->db as $username=>$profile) {
try {
$user = new User($username);
$data[$username] = $user->json($returnsArray=true);
} catch (Exception $e) {
continue;
}
}
return array(
'status'=>'0',
'message'=>'Users profiles.',
'data'=>$data
);
}
} }