-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathAgentAssignmentAPI.php
More file actions
135 lines (114 loc) · 4.08 KB
/
Copy pathAgentAssignmentAPI.php
File metadata and controls
135 lines (114 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Hashtopolis\inc\apiv2\model;
use Exception;
use Hashtopolis\dba\AbstractModel;
use Hashtopolis\inc\utils\AccessUtils;
use Hashtopolis\inc\utils\AgentUtils;
use Hashtopolis\inc\utils\AssignmentUtils;
use Hashtopolis\dba\models\AccessGroupAgent;
use Hashtopolis\dba\ExistsFilter;
use Hashtopolis\dba\ContainFilter;
use Hashtopolis\dba\Factory;
use Hashtopolis\dba\models\Hashlist;
use Hashtopolis\dba\JoinFilter;
use Hashtopolis\dba\models\Agent;
use Hashtopolis\dba\models\Assignment;
use Hashtopolis\dba\models\Task;
use Hashtopolis\dba\models\TaskWrapper;
use Hashtopolis\dba\models\User;
use Hashtopolis\inc\apiv2\common\AbstractModelAPI;
use Hashtopolis\inc\apiv2\error\HttpError;
use Hashtopolis\inc\HTException;
use Hashtopolis\inc\Util;
/**
* @extends AbstractModelAPI<Assignment>
*/
class AgentAssignmentAPI extends AbstractModelAPI {
public static function getBaseUri(): string {
return "/api/v2/ui/agentassignments";
}
public static function getAvailableMethods(): array {
return ['POST', 'GET', 'DELETE', 'PATCH'];
}
public static function getDBAclass(): string {
return Assignment::class;
}
/**
* @param Assignment $object
* @throws Exception
*/
protected function getSingleACL(User $user, AbstractModel $object): bool {
$accessGroupsUser = Util::arrayOfIds(AccessUtils::getAccessGroupsOfUser($user));
$agent = Factory::getAgentFactory()->get($object->getAgentId());
$accessGroupsAgent = Util::arrayOfIds(AccessUtils::getAccessGroupsOfAgent($agent));
return count(array_intersect($accessGroupsAgent, $accessGroupsUser)) > 0;
}
/**
* @throws Exception
*/
protected function getFilterACL(): array {
$accessGroups = Util::arrayOfIds(AccessUtils::getAccessGroupsOfUser($this->getCurrentUser()));
return [
Factory::JOIN => [
new JoinFilter(Factory::getTaskFactory(), Assignment::TASK_ID, Task::TASK_ID),
new JoinFilter(Factory::getTaskWrapperFactory(), Task::TASK_WRAPPER_ID, TaskWrapper::TASK_WRAPPER_ID, Factory::getTaskFactory()),
new JoinFilter(Factory::getHashlistFactory(), TaskWrapper::HASHLIST_ID, Hashlist::HASHLIST_ID, Factory::getTaskWrapperFactory()),
],
Factory::FILTER => [
new ExistsFilter(Factory::getAccessGroupAgentFactory(), AccessGroupAgent::AGENT_ID, Assignment::AGENT_ID, [new ContainFilter(AccessGroupAgent::ACCESS_GROUP_ID, $accessGroups, Factory::getAccessGroupAgentFactory())]),
new ContainFilter(Hashlist::ACCESS_GROUP_ID, $accessGroups, Factory::getHashlistFactory()),
]
];
}
public static function getToOneRelationships(): array {
return [
'agent' => [
'key' => Assignment::AGENT_ID,
'relationType' => Agent::class,
'relationKey' => Agent::AGENT_ID,
],
'task' => [
'key' => Assignment::TASK_ID,
'relationType' => Task::class,
'relationKey' => Task::TASK_ID,
],
];
}
/**
* @throws HTException
* @throws HttpError
*/
protected function createObject(array $data): int {
$assignment = AgentUtils::assign($data[Assignment::AGENT_ID], $data[Assignment::TASK_ID], $this->getCurrentUser());
assert($assignment !== null);
return $assignment->getId();
}
protected function getUpdateHandlers($id, $current_user): array {
return [
Assignment::BENCHMARK => fn($value) => AssignmentUtils::setBenchmark($id, $value, $current_user)
];
}
public function getAggregateFieldsets(): array {
return [
'assignment' => [
'crackingTime' => [$this, 'getAggregateCrackingTime'],
]
];
}
/**
* @param Assignment $object
* @return int
* @throws Exception
*/
protected function getAggregateCrackingTime(AbstractModel $object): int {
return AgentUtils::getAggregateCrackingTime($object->getAgentId(), $object->getTaskId());
}
/**
* @param Assignment $object
* @throws HTException
* @throws HttpError
*/
protected function deleteObject(AbstractModel $object): void {
AgentUtils::assign($object->getAgentId(), 0, $this->getCurrentUser());
}
}