WIP AddressDB + api

This commit is contained in:
Frank Schubert
2022-08-25 16:59:54 +02:00
parent 4b39d5a158
commit 36b41b4e5a
23 changed files with 1373 additions and 34 deletions

View File

@@ -43,6 +43,65 @@ class User extends mfBaseModel {
return false;
}
public function loadByUsername($username) {
$username = $this->db->escape($username);
if(!$username) {
return false;
}
$res = $this->db->select($this->table, "*", "username='$username' LIMIT 1");
if($this->db->num_rows($res)) {
$data = $this->db->fetch_object($res);
$this->load($data);
return true;
}
return false;
}
public function loadByApikey($key) {
$key = $this->db->escape($key);
if(!$key) {
return false;
}
$res = $this->db->select($this->table, "*", "apikey='$key'");
if($this->db->num_rows($res) === 1) {
$data = $this->db->fetch_object($res);
$this->load($data);
return true;
}
return false;
}
public function createApiKey() {
$tries = 3;
$key = false;
while(!$key) {
$source = random_bytes(128);
$key = base64_encode($source);
$key = str_replace(["/","=","+"], "", $key);
$key = substr($key, 0, 32);
$res = $this->db->select($this->table, "id", "apikey='$key'");
if($this->db->num_rows($res) || strlen($key) < 32) {
$key = false;
$tries--;
$this->log->error("new api key not unique ($tries)");
if($tries < 1) {
break;
}
}
}
if(!$key) {
$this->log->error("unable to create unique api key");
return null;
}
return $key;
}
public function getAbbrName() {
if(strpos($this->name, " ") === false) {
return $this->name;

View File

@@ -73,6 +73,30 @@ class UserController extends mfBaseController {
$this->layout()->set("addresses", $addresses);
}
protected function generateApikey($request) {
if(!$this->isAdmin()) {
$this->redirect("Dashboard");
}
$id = $request['id'];
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("User nicht gefunden.", "error");
$this->redirect("User");
}
$user = new User($id);
if(!$user->id) {
$this->layout()->setFlash("User nicht gefunden.", "error");
$this->redirect("User");
}
$user->apikey = $user->createApiKey();
$user->save();
$this->layout()->setFlash("API Key erfolgreich generiert.", "success");
$this->redirect("User", "edit", ['id' => $id]);
}
protected function profileAction($request) {
}
@@ -92,6 +116,17 @@ class UserController extends mfBaseController {
}
$user = new User($id);
// check if new user already exits
if($this->isAdmin() && !$r->id) {
$tu = new User();
$tu->loadByUsername($r->username);
if($tu->id) {
$this->layout()->setFlash("Benutzer mit diesem Benutzername bereits vorhanden!", "error");
$this->redirect("User");
}
}
if(!$user->permissions) {
$user->permissions = new WorkerPermission();
}

View File

@@ -6,6 +6,7 @@ class UserModel {
public $password = null;
public $name = null;
public $email = null;
public $apikey = null;
public $ip = null;
public $sessionid = null;