New Stuff

This commit is contained in:
Kekskurse 2019-11-22 16:13:42 +01:00
parent d6982e95ad
commit 21ed7565d6
4 changed files with 81 additions and 3 deletions

View file

@ -31,4 +31,31 @@ class ServerController extends BaseController
return $response->withData(\App\Http\Resources\API\Setting::collection(collect($settings)));
}
public function saveSettings(Request $request, Response $response) {
if(!Auth::user()->admin) {
throw new HTTPException("Need Admin Access");
}
$settings = Setting::query()->get("*");
foreach($settings as $setting) {
switch ($setting->typ) {
case "checkbox":
if($request->input($setting->name, false)) {
$setting->value = true;
} else {
$setting->value = false;
}
$setting->saveOrFail();
case "textinput":
$setting->value = $request->input($setting->name, "");
$setting->saveOrFail();
case "password":
$setting->value = $request->input($setting->name, "");
$setting->saveOrFail();
}
}
return $response;
}
}

View file

@ -257,4 +257,49 @@ class UserController extends BaseController
$response->setMessage("Mail address deleted");
return $response;
}
public function changePrimaryMail(Request $request, Response $response, $id) {
if(!Auth::check()) {
abort(401);
}
$mail = Mail::query()->where("id", "=", $id)->firstOrFail();
if($mail->user_id != Auth::user()->id) {
abort(401);
}
if($mail->status != "valide") {
throw new HTTPException("Mail not validated, click the link in the mail first");
}
$mails = Mail::query()->where("user_id", "=", Auth::user()->id)->get();
foreach($mails as $m) {
$m->primary = false;
$m->saveOrFail();
}
$mail->primary = true;
$mail->saveOrFail();
$response->setMessage("Primary mail changed");
return $response;
}
public function changePassword(Request $request, Response $response) {
if(!Auth::check()) {
abort(401);
}
$this->validate($request, [
'password' => 'required|min:8'
]);
$user = Auth::user();
$user->password = password_hash($request->input("password"), PASSWORD_BCRYPT);
$user->saveOrFail();
$response->setMessage("Password changed");
return $response;
}
}

View file

@ -27,10 +27,10 @@ class Setting extends JsonResource
$data["value"] = (bool)$this->value;
}
if($this->typ == "password") {
/*if($this->typ == "password") {
$data["value"] = null;
}
}*/
return $data;
}
}
}

View file

@ -47,8 +47,10 @@ $router->group(['prefix' => 'api'], function () use ($router) {
$router->get("/me", ['uses' => 'API\UserController@me']);
$router->get("/me/mails", ['uses' => 'API\UserController@listMails']);
$router->post("/me/mails", ["uses" => "API\UserController@addMail"]);
$router->post("/me/changePassword", ["uses" => "API\UserController@changePassword"]);
$router->group(['prefix' => '/me/mail/{id}'], function () use ($router) {
$router->delete("", ['uses' => 'API\UserController@removeMail']);
$router->post("/markAsPrimary", ["uses" => "API\UserController@changePrimaryMail"]);
});
});
$router->group(['prefix' => 'app'], function () use ($router) {
@ -79,6 +81,10 @@ $router->group(['prefix' => 'api'], function () use ($router) {
$router->group(['prefix' => 'server'], function () use ($router) {
$router->get("/settings", ["uses" => "API\ServerController@getSettings"]);
});
$router->group(['prefix' => 'admin'], function () use ($router) {
$router->post("/settings", ["uses" => "API\ServerController@saveSettings"]);
});
});
});