WIP AddressDB + api
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user