Skip to content

Commit

Permalink
Allow API to use any service and new api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Aug 23, 2024
1 parent 9ac2732 commit 7cb0199
Show file tree
Hide file tree
Showing 20 changed files with 997 additions and 422 deletions.
1 change: 1 addition & 0 deletions app/Core/Middleware/ApiAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Leantime\Core\Frontcontroller;
use Leantime\Core\IncomingRequest;
use Leantime\Domain\Api\Services\Api as ApiService;
use Leantime\Domain\Auth\Models\Roles;
use Leantime\Domain\Auth\Services\Auth as AuthService;
use Leantime\Domain\Projects\Services\Projects as ProjectsService;
use Symfony\Component\HttpFoundation\Response;
Expand Down
1 change: 1 addition & 0 deletions app/Core/Middleware/InitialHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function handle(IncomingRequest $request, Closure $next): Response
"frame-src 'self' *.google.com *.microsoft.com *.live.com",
"frame-ancestors 'self' *.google.com *.microsoft.com *.live.com",
];
$cspParts = self::dispatch_filter('cspParts', $cspParts);
$csp = implode(";", $cspParts);

foreach (
Expand Down
Empty file added app/Core/Session.php
Empty file.
3 changes: 2 additions & 1 deletion app/Core/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ public function displayJson(array|object|string $jsonContent, int $statusCode =
$response->headers->set('Content-Type', 'application/json; charset=utf-8');

if (is_array($jsonContent) || is_object($jsonContent)) {
$jsonContent = json_encode($jsonContent);
$collection = collect($jsonContent);
$jsonContent = $collection->toJson();

if (json_last_error() !== JSON_ERROR_NONE) {
return $response;
Expand Down
38 changes: 29 additions & 9 deletions app/Domain/Api/Controllers/Jsonrpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ private function executeApiRequest(array $params): Response

$jsonRpcVer = $params['jsonrpc'] ?? null;

$moduleName = Str::studly($methodparts['module']);
$serviceName = Str::studly($methodparts['service']);

$domainServiceNamespace = app()->getNamespace() . "Domain\\$serviceName\\Services\\$serviceName";
$pluginServiceNamespace = app()->getNamespace() . "Plugins\\$serviceName\\Services\\$serviceName";
$domainServiceNamespace = app()->getNamespace() . "Domain\\$moduleName\\Services\\$serviceName";
$pluginServiceNamespace = app()->getNamespace() . "Plugins\\$moduleName\\Services\\$serviceName";

$methodName = Str::camel($methodparts['method']);

Expand All @@ -181,6 +182,9 @@ private function executeApiRequest(array $params): Response
return $this->returnMethodNotFound("Method doesn't exist: $methodName", $id);
}

//Check method attributes
//TODO: Check if method is available for api

if ($jsonRpcVer == null) {
return $this->returnInvalidRequest("You must include a \"jsonrpc\" parameter with a value of \"2.0\"", $id);
}
Expand All @@ -204,7 +208,7 @@ private function executeApiRequest(array $params): Response
// can be null
try {
$method_response = app()->make($serviceName)->$methodName(...$preparedParams);
} catch (\Error $e) {
} catch (Exception $e) {
return $this->returnServerError($e, $id);
}

Expand Down Expand Up @@ -236,15 +240,31 @@ private function parseMethodString(string $methodstring): array
throw new Exception("Method string doesn't start with \"leantime.rpc.\"");
}

//method parameter breakdown
//00000000.111.22222222.3333333333333.444444444444
//leantime.rpc.{module}.{servicename}.{methodname}
$methodStringPieces = explode('.', $methodstring);
if (count($methodStringPieces) !== 4) {
throw new Exception("Method is case sensitive and must follow the following naming convention: \"leantime.rpc.{servicename}.{methodname}\"");

if (count($methodStringPieces) !== 4 && count($methodStringPieces) !== 5) {
throw new Exception("Method is case sensitive and must follow the following naming convention: \"leantime.rpc.{domain}.{servicename}.{methodname}\"");
}

if (count($methodStringPieces) === 4){
return [
'module' => $methodStringPieces[2],
'service' => $methodStringPieces[2],
'method' => $methodStringPieces[3],
];
}

if (count($methodStringPieces) === 5){
return [
'module' => $methodStringPieces[2],
'service' => $methodStringPieces[3],
'method' => $methodStringPieces[4],
];
}

return [
'service' => $methodStringPieces[2],
'method' => $methodStringPieces[3],
];
}

/**
Expand Down
5 changes: 4 additions & 1 deletion app/Domain/Api/Services/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function getAPIKeyUser(string $apiKey): bool|array

if (!is_array($apiKeyParts) || count($apiKeyParts) != 3) {
report("Not a valid API Key format");

return false;
}

Expand Down Expand Up @@ -202,4 +201,8 @@ public function getCaseCorrectPathFromManifest(string $filepath): string|false

return $basePath . array_search($referenceValue, $correctManifest);
}

public function healthCheck() {
return true;
}
}
40 changes: 38 additions & 2 deletions app/Domain/Comments/Repositories/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public function editComment($text, $id): bool
return $result;
}

public function getAllAccountComments(): array|false
public function getAllAccountComments(?int $projectId, ?int $moduleId): array|false
{
$sql = "SELECT comment.id,
comment.module,
Expand All @@ -272,10 +272,46 @@ public function getAllAccountComments(): array|false
comment.userId,
comment.commentParent,
comment.status
FROM zp_comment as comment";
FROM zp_comment as comment
LEFT JOIN zp_tickets ON comment.moduleId = zp_tickets.id
LEFT JOIN zp_canvas_items ON comment.moduleId = zp_tickets.id
LEFT JOIN zp_canvas ON zp_canvas.id = zp_canvas_items.canvasId
LEFT JOIN zp_projects ON zp_canvas.projectId = zp_projects.id OR zp_tickets.projectId = zp_projects.id
WHERE zp_projects.id IN (SELECT projectId FROM zp_relationuserproject WHERE zp_relationuserproject.userId = :userId)
OR zp_projects.psettings = 'all'
OR (zp_projects.psettings = 'client' AND zp_projects.clientId = :clientId)
OR (:requesterRole = 'admin' OR :requesterRole = 'manager') ";

if (isset($projectId) && $projectId > 0) {
$sql .= " AND (zp_projects.id = :projectId)";
}

if (isset($moduleId) && $moduleId > 0) {
$sql .= " AND ( comment.moduleId = :moduleId)";
}

$sql .= " GROUP BY comment.id";

$stmn = $this->db->database->prepare($sql);

$stmn->bindValue(':userId', session("userdata.id") ?? '-1', PDO::PARAM_INT);
$stmn->bindValue(':clientId', session("userdata.clientId") ?? '-1', PDO::PARAM_INT);


if (session()->exists("userdata")) {
$stmn->bindValue(':requesterRole', session("userdata.role"), PDO::PARAM_INT);
} else {
$stmn->bindValue(':requesterRole', -1, PDO::PARAM_INT);
}

if (isset($projectId) && $projectId > 0) {
$stmn->bindValue(':projectId', $projectId, PDO::PARAM_INT);
}

if (isset($moduleId) && $moduleId > 0) {
$stmn->bindValue(':moduleId', $moduleId, PDO::PARAM_INT);
}

$stmn->execute();
$values = $stmn->fetchAll();
$stmn->closeCursor();
Expand Down
15 changes: 13 additions & 2 deletions app/Domain/Comments/Services/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,20 @@ public function deleteComment($commentId): bool
return $this->commentRepository->deleteComment($commentId);
}

public function pollComments(): array | false
public function pollComments(?int $projectId = null, ?int $moduleId = null): array | false
{
return $this->commentRepository->getAllAccountComments();

$comments = $this->commentRepository->getAllAccountComments($projectId, $moduleId);

foreach ($comments as $key => $comment) {
if(dtHelper()->isValidDateString($comment['date'])) {
$comments[$key]['date'] = dtHelper()->parseDbDateTime($comment['date'])->toIso8601ZuluString();
}else{
$comments[$key]['date'] = null;
}
}

return $comments;
}
}
}
3 changes: 1 addition & 2 deletions app/Domain/Connector/Services/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,6 @@ private function parseMilestones($fields, $values)
return $flags;
}



public function importValues($fields, $values, $entity)
{

Expand Down Expand Up @@ -738,5 +736,6 @@ private function cacheSerializedFieldValues($fields, $values)
session(["serFields" => $serializedFields]);
session(["serValues" => $serializedValues]);
}

}
}
147 changes: 143 additions & 4 deletions app/Domain/Goalcanvas/Repositories/Goalcanvas.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function getSingleCanvas($canvasId): false|array
*
* @return array|false
*/
public function getAllAccountGoals(): false|array
public function getAllAccountGoals(?int $projectId, ?int $boardId): false|array
{
$sql = "SELECT
zp_canvas_items.id,
Expand Down Expand Up @@ -202,19 +202,158 @@ public function getAllAccountGoals(): false|array
zp_canvas_items.action,
zp_canvas_items.assignedTo,
zp_canvas_items.parent,
zp_canvas_items.tags
zp_canvas_items.tags,
zp_canvas.projectId
FROM
zp_canvas_items
LEFT JOIN zp_canvas ON zp_canvas_items.canvasId = zp_canvas.id
LEFT JOIN zp_projects ON zp_canvas.projectId = zp_projects.id
WHERE zp_canvas_items.box = 'goal' AND (
zp_canvas.projectId IN (SELECT projectId FROM zp_relationuserproject WHERE zp_relationuserproject.userId = :userId)
OR zp_projects.psettings = 'all'
OR (zp_projects.psettings = 'client' AND zp_projects.clientId = :clientId)
OR (:requesterRole = 'admin' OR :requesterRole = 'manager')
)
";

WHERE zp_canvas_items.box = 'goal'
if (isset($projectId) && $projectId > 0) {
$sql .= " AND (zp_canvas.projectId = :projectId)";
}

";
if (isset($boardId) && $boardId > 0) {
$sql .= " AND (zp_canvas.id = :boardId)";
}

$stmn = $this->db->database->prepare($sql);

if (session()->exists("userdata")) {
$stmn->bindValue(':requesterRole', session("userdata.role"), PDO::PARAM_INT);
} else {
$stmn->bindValue(':requesterRole', -1, PDO::PARAM_INT);
}

$stmn->bindValue(':clientId', session("userdata.clientId") ?? '-1', PDO::PARAM_INT);
$stmn->bindValue(':userId', session("userdata.id") ?? '-1', PDO::PARAM_INT);

if (isset($projectId) && $projectId > 0) {
$stmn->bindValue(':projectId', $projectId, PDO::PARAM_INT);
}

if (isset($boardId) && $boardId > 0) {
$stmn->bindValue(':boardId', $boardId, PDO::PARAM_INT);
}

$stmn->execute();
$values = $stmn->fetchAll();
$stmn->closeCursor();
return $values;
}

/**
* @param $values
* @return false|string
*/
public function createGoal($values): false|string
{

$query = "INSERT INTO zp_canvas_items (
description,
title,
assumptions,
data,
conclusion,
box,
author,
created,
modified,
canvasId,
status,
relates,
milestoneId,
kpi,
data1,
startDate,
endDate,
setting,
metricType,
impact,
effort,
probability,
action,
assignedTo,
startValue,
currentValue,
endValue,
parent,
tags
) VALUES (
:description,
:title,
:assumptions,
:data,
:conclusion,
:box,
:author,
NOW(),
NOW(),
:canvasId,
:status,
:relates,
:milestoneId,
:kpi,
:data1,
:startDate,
:endDate,
:setting,
:metricType,
:impact,
:effort,
:probability,
:action,
:assignedTo,
:startValue,
:currentValue,
:endValue,
:parent,
:tags
)";

$stmn = $this->db->database->prepare($query);

$stmn->bindValue(':description', $values['description'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':title', $values['title'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':assumptions', $values['assumptions'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':data', $values['data'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':conclusion', $values['conclusion'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':box', $values['box'], PDO::PARAM_STR);
$stmn->bindValue(':author', $values['author'], PDO::PARAM_INT);
$stmn->bindValue(':canvasId', $values['canvasId'], PDO::PARAM_INT);
$stmn->bindValue(':status', $values['status'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':relates', $values['relates'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':milestoneId', $values['milestoneId'] ?? "", PDO::PARAM_STR);
$stmn->bindValue(':kpi', $values['kpi'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':data1', $values['data1'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':startDate', $values['startDate'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':endDate', $values['endDate'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':setting', $values['setting'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':metricType', $values['metricType'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':impact', $values['impact'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':effort', $values['effort'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':probability', $values['probability'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':action', $values['action'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':assignedTo', $values['assignedTo'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':startValue', $values['startValue'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':currentValue', $values['currentValue'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':endValue', $values['endValue'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':parent', $values['parent'] ?? '', PDO::PARAM_STR);
$stmn->bindValue(':tags', $values['tags'] ?? '', PDO::PARAM_STR);

$stmn->execute();
$id = $this->db->database->lastInsertId();
$stmn->closeCursor();

return $id;
}

}
}
Loading

0 comments on commit 7cb0199

Please sign in to comment.