diff --git a/src/Tor/Controller/#GroupApi.php b/src/Tor/Controller/#GroupApi.php new file mode 100644 index 0000000..686e2d2 --- /dev/null +++ b/src/Tor/Controller/#GroupApi.php @@ -0,0 +1,183 @@ +exists(Group::class, $groupName); + + $post_data = Utils::FetchPostData(); + + if ($post_data != false) { + $new_default = trim($post_data['default'] ?? ""); + $new_service = trim($post_data['service'] ?? ""); + } + + if (!$exists) { + $group = new Group(); + $group->id = $groupName; + $group->default = false; + $group->service = $api_service; + } else { + $group = $data->load(Group::class, $groupName); + } + + if (!empty($new_default)) + $group->default = boolval($new_default); + + if (!empty($new_service)) + { + if ($data->exists(Service::class, $new_service)) + $group->service = $data->load(Service::class, $new_service); + } + + $data->save($group); + + $result = [ + 'group' => $group->flatIdentifier(), + 'default' => $group->default, + 'service' => $group->service->flatIdentifier(), + ]; + + if (!$exists) { + $result['result'] = 'created'; + $request->status(201, 'Created') + ->send($result); + } else { + $result['result'] = 'provisioned'; + $request->status(200, 'OK') + ->send($result); + } + } + + // System: Delete a group and remove the permission from all users + function delete(\Gac\Routing\Request $request, string $groupName) { + global $data; + + $exists = $data->exists(Group::class, $groupName); + + if (!$exists) { + $request->status(404, 'Not Found')->send(["error" => ["message" => "not found"]]); + } + else { + $users = $data->fromIndex(Group::class, $groupName, findClass: User::class); + + foreach ($users as $user) + { + foreach ($user->groups as $key => $value) + { + if ($value->flatIdentifier() == JsonDatabase::SanitizeIdentifier($groupName)) + unset($user->groups[$key]); + } + + $data->save($user); + } + + $data->delete(Group::class, $groupName); + $request->status(200, 'OK')->send([ 'result' => 'destroyed' ]); + } + } + + // System: Fetch info about a Group + function info(\Gac\Routing\Request $request, string $groupName) { + global $data; + + $exists = $data->exists(Group::class, $groupName); + + if (!$exists) { + $request->status(404, 'Not Found')->send(["error" => ["message" => "not found"]]); + } + else { + $group = $data->load(Group::class, $groupName); + $users = $data->fromIndex(Group::class, $groupName, User::class); + + $result = [ + 'group' => $group->flatIdentifier(), + 'service' => $group->service->flatIdentifier(), + 'default' => $group->default, + 'users' => $users, + 'result' => 'ok' + ]; + + $request->status(200, 'OK')->send($result); + } + } + + // System: Assign user to a group + function assign(\Gac\Routing\Request $request, string $groupName, string $userName) { + global $data; + + $exists = $data->exists(Group::class, $groupName); + $existsUser = $data->exists(User::class, $userName); + + if (!$exists) { + $request->status(404, 'Not Found')->send(["error" => ["message" => "group not found"]]); + } + else if (!$existsUser) { + $request->status(404, 'Not Found')->send(["error" => ["message" => "user not found"]]); + } + else { + $group = $data->load(Group::class, $groupName); + $user = $data->load(User::class, $userName); + // TODO: Continue here <----- + $users = $data->fromIndex(Group::class, $groupName, User::class); + + $result = [ + 'group' => $group->flatIdentifier(), + 'service' => $group->service->flatIdentifier(), + 'default' => $group->default, + 'users' => $users, + 'result' => 'ok' + ]; + + $request->status(200, 'OK')->send($result); + } + } + + // System: List all Groups + function list(\Gac\Routing\Request $request) { + global $data; + + $groups = $data->listAll(Group::class); + + $result = [ + 'services' => $groups, + 'result' => 'ok' + ]; + + $request->status(200, 'OK') + ->send($result); + } + + // System: List all Groups by service + function listByService(\Gac\Routing\Request $request, string $serviceName) { + global $data; + + $groups = $data->fromIndex(Service::class, $serviceName, Group::class); + + $result = [ + 'groups' => $groups, + 'result' => 'ok' + ]; + + $request->status(200, 'OK') + ->send($result); + } + + // Default response if no action is defined + function response(\Gac\Routing\Request $request) { + $request->status(200, 'OK')->send([ 'result' => 'ok' ]); + } + +} \ No newline at end of file