Skip to content

Commit

Permalink
permissions plus robustes, et page pour demander les permissions au l…
Browse files Browse the repository at this point in the history
…ieu de throw...
  • Loading branch information
paxo-rch committed Nov 1, 2024
1 parent 693e7c9 commit db1ea47
Show file tree
Hide file tree
Showing 27 changed files with 145 additions and 110 deletions.
57 changes: 26 additions & 31 deletions lib/applications/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ namespace AppManager {

void App::run(const std::vector<std::string>& parameters) {
if (!auth) {
throw libsystem::exceptions::RuntimeError("App is not authorized to run");
requestAuth();

if (!auth)
return;
}

app_state = background ? RUNNING_BACKGROUND : RUNNING;
Expand Down Expand Up @@ -84,7 +87,7 @@ namespace AppManager {
std::string data = stream.read();
stream.close();

label->setText(data);
label->setText("Voulez-vous autoriser l'application " + fullName + " à accéder aux permissions suivantes:\n" + data);
win.addChild(label);

auto *btn = new Button(35, 420, 250, 38);
Expand All @@ -95,18 +98,16 @@ namespace AppManager {

while (true) {
win.updateAll();
eventHandlerApp.update();

if (btn->isTouched()) {
storage::FileStream streamP((storage::Path(PERMS_DIR) / "auth.list").str(), storage::APPEND);
streamP.write(path.str() + "\n");
streamP.close();

manifest = storage::Path(PERMS_DIR) / (name + ".json");
auth = true;
if(hardware::getHomeButton())
{
break;
}

storage::FileStream newPermCopy(manifest.str(), storage::WRITE);
newPermCopy.write(data);
newPermCopy.close();
if (btn->isTouched()) {
AppManager::addPermission(this);
break;
}
}
}
Expand Down Expand Up @@ -178,24 +179,20 @@ namespace AppManager {
return 0;
}

void askGui(const LuaFile* lua) {
App* app = lua->app;
void addPermission(App* app) {
app->auth = true;

/*if (lua->lua_gui.mainWindow == nullptr) {
for (auto it = appStack.begin(); it != appStack.end(); ++it) {
if (*it == app) {
app->app_state = App::AppState::NOT_RUNNING;
appStack.erase(it);
break;
}
}
storage::FileStream stream((storage::Path(PERMS_DIR) / "auth.list").str(), storage::APPEND);
stream.write(app->path.str() + "\n");
stream.close();

return;
}*/
storage::FileStream oman(app->manifest.str(), storage::READ);
std::string manifest = oman.read();
oman.close();

// if (appStack.empty() || appStack.back() != app) {
// appStack.push_back(app);
// }
storage::FileStream nman((storage::Path(PERMS_DIR) / (app->fullName + ".json")).str(), storage::WRITE);
nman.write(manifest);
nman.close();
}

void loadDir(const storage::Path& directory, bool root = false, std::string prefix = "") {
Expand Down Expand Up @@ -237,7 +234,7 @@ namespace AppManager {
directory / dir / "manifest.json",
true
);
} else if (allowedFiles.find(appPath.str()) != std::string::npos) {
} else if (allowedFiles.find(appPath.str() + "\n") != std::string::npos) {
app = std::make_shared<App>(
dir,
storage::Path(directory) / dir / "app.lua",
Expand All @@ -248,7 +245,7 @@ namespace AppManager {
app = std::make_shared<App>(
dir,
storage::Path(directory) / dir / "app.lua",
storage::Path(directory) / fullname / "manifest.json",
storage::Path(directory) / dir / "manifest.json",
false
);
}
Expand Down Expand Up @@ -279,8 +276,6 @@ namespace AppManager {
std::cerr << "Error: subdir \"" << (app.get()->path / "../" / manifest["subdir"]).str() << "\" does not exist" << std::endl;
}

// Add app to list
//libsystem::log("Loaded app : " + app->toString() + ".");
appList.push_back(app);

if (manifest["autorun"].is_boolean()) {
Expand Down
4 changes: 2 additions & 2 deletions lib/applications/src/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#define APP_DIR "/apps"
#define SYSTEM_APP_DIR "/sys_apps"
#define PERMS_DIR "/system"
#define PERMS_DIR "/system/permissions"


namespace AppManager
Expand Down Expand Up @@ -109,7 +109,7 @@ namespace AppManager
extern std::vector<App*> appStack; // stack of the apps that are using the GUI, the last one is shown on the screen

int pushError(lua_State* L, sol::optional<const std::exception&> maybe_exception, sol::string_view description);
void askGui(const LuaFile* lua);
void addPermission(App* app);
bool isAnyVisibleApp();

void init();
Expand Down
1 change: 0 additions & 1 deletion lib/lua/src/lua_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ std::string LuaGui::keyboard(const std::string &placeholder, const std::string &
void LuaGui::setMainWindow(LuaWindow *window)
{
this->mainWindow = window;
AppManager::askGui(this->lua);
}

LuaWindow *LuaGui::getMainWindow()
Expand Down
92 changes: 92 additions & 0 deletions lib/storage/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,96 @@ namespace storage
return (::rename(this->str().c_str(), to.str().c_str()) == 0);
#endif
}

bool Path::copy(const Path &to)
{
#if defined(__linux__) || defined(_WIN32) || defined(_WIN64) || defined(__APPLE__)
try {
if (this->isfile()) {
// Copy single file
std::filesystem::copy_file(this->str(), to.str(),
std::filesystem::copy_options::overwrite_existing);
}
else if (this->isdir()) {
// Copy directory and its contents recursively
std::filesystem::copy(this->str(), to.str(),
std::filesystem::copy_options::recursive |
std::filesystem::copy_options::overwrite_existing);
}
return true;
}
catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Copy failed: " << e.what() << std::endl;
return false;
}
#endif

#ifdef ESP_PLATFORM
if (!this->exists()) {
return false;
}

if (this->isfile()) {
// Copy single file
FILE* source = fopen(this->str().c_str(), "rb");
if (!source) {
return false;
}

FILE* dest = fopen(to.str().c_str(), "wb");
if (!dest) {
fclose(source);
return false;
}

const size_t bufferSize = 1024;
uint8_t buffer[bufferSize];
size_t bytesRead;

while ((bytesRead = fread(buffer, 1, bufferSize, source)) > 0) {
if (fwrite(buffer, 1, bytesRead, dest) != bytesRead) {
fclose(source);
fclose(dest);
return false;
}
}

fclose(source);
fclose(dest);
return true;
}
else if (this->isdir()) {
// Create destination directory
if (!to.exists() && !to.newdir()) {
return false;
}

// Copy directory contents recursively
std::vector<std::string> entries = this->listdir(false);
bool success = true;

for (const auto& entry : entries) {
Path sourcePath = *this / entry;
Path destPath = to / entry;

if (sourcePath.isfile()) {
success &= sourcePath.copy(destPath);
}
else if (sourcePath.isdir()) {
if (!destPath.exists() && !destPath.newdir()) {
success = false;
break;
}
success &= sourcePath.copy(destPath);
}
}

return success;
}

return false;
#endif

return false;
}
}
1 change: 1 addition & 0 deletions lib/storage/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace storage {
bool remove (void) const;

bool rename (const Path& to);
bool copy (const Path& to);

std::vector<std::string> m_steps;
private:
Expand Down
2 changes: 0 additions & 2 deletions storage/apps/alarme/sub/.task/app.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
global_list = {}

function checkAlarm()
print("checkAlarm")

for i, v in ipairs(global_list) do
local h = tonumber(v.time:sub(1, 2))
local m = tonumber(v.time:sub(4, 5))
print("Comparing: " .. h .. " with " .. time:get("h")[1] .. " and " .. m .. " with " .. time:get("mi")[1])
if(h == time:get("h")[1] and m == time:get("mi")[1] and v.enabled == 1) then
global_list[i].enabled = 0
saveTable("../../alarms.tab", global_list)
Expand Down
6 changes: 0 additions & 6 deletions storage/sys_apps/testbc/app.lua

This file was deleted.

9 changes: 0 additions & 9 deletions storage/sys_apps/testbc/manifest.json

This file was deleted.

6 changes: 0 additions & 6 deletions storage/sys_apps/testbc/sub/.sub1/app.lua

This file was deleted.

9 changes: 0 additions & 9 deletions storage/sys_apps/testbc/sub/.sub1/manifest.json

This file was deleted.

5 changes: 0 additions & 5 deletions storage/system/.keyboard_test.json

This file was deleted.

6 changes: 0 additions & 6 deletions storage/system/battery.json

This file was deleted.

3 changes: 0 additions & 3 deletions storage/system/demineur.json

This file was deleted.

3 changes: 0 additions & 3 deletions storage/system/horloge.json

This file was deleted.

5 changes: 0 additions & 5 deletions storage/system/keyboard_test.json

This file was deleted.

File renamed without changes.
File renamed without changes.
19 changes: 10 additions & 9 deletions storage/system/auth.list → storage/system/permissions/auth.list
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
./storage/apps/alarme/sub/.task/app.lua
./storage/apps/snake/app.lua
./storage/apps/demineur/app.lua
./storage/apps/phone/app.lua
./storage/apps/test/app.lua
./storage/apps/calendrier/app.lua
./storage/apps/alarme/sub/.task/app.lua
./storage/apps/alarme/app.lua
./storage/apps/calendrier/app.lua
./storage/apps/messages/app.lua
./storage/apps/snake/app.lua
./storage/apps/phone/app.lua
./storage/apps/demineur/app.lua
./storage/apps/contacts/app.lua
./storage/apps/battery/app.lua
./storage/apps/keyboard_test/app.lua
./storage/apps/horloge/app.lua
./storage/apps/alarme/app.lua
./storage/apps/alarme/sub/.task/app.lua
./storage/apps/snake/app.lua
./storage/apps/calculatrice/app.lua
./storage/apps/demineur/app.lua
./storage/apps/calendrier/app.lua
3 changes: 3 additions & 0 deletions storage/system/permissions/calculatrice.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"access": ["files", "files_root", "os", "gsm", "gui", "time"]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"]
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"],
"name": "Calendrier"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"]
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"]
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"],
"name": "Démineur"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"]
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"],
"name": "Messages"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"]
}
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"],
"name": "Téléphone"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"]
}
}
3 changes: 0 additions & 3 deletions storage/system/test.json

This file was deleted.

3 changes: 0 additions & 3 deletions storage/system/test2.json

This file was deleted.

0 comments on commit db1ea47

Please sign in to comment.