/** * Creates a new user * * Important: The passwort set in User object will be transformed into a hash value. After that the password of the * User object gets replaced with this hash. * * @param User $user * User object to create user for * @param boolean $state * Set to true if user should be autoactivated * * @throws UserException * * @return integer */ public function createUser(User $user) : int { $username = $user->getUsername(); if ($username == 'guest') { throw new UserException('Cannot create user without username.'); } if (empty($user->getPassword())) { throw new UserException('Cannot create user without a password'); } // Check for already existing username $exists = $this->db->count($this->table, 'username=:username', [':username' => $username]); if ($exists > 0) { throw new UserException(sprintf('The username "%s" is already in use.', $username)); } try { $this->db->beginTransaction(); $this->db->qb(['table' => $this->table, 'data' => ['username' => $username, 'display_name' => $user->getDisplayname(), 'state' => $user->getState()]], true); // Get our new user id $id = $this->db->lastInsertId(); if (!empty($id)) { // Set new id to users object $user->setId($id); // Create password hash $this->changePassword($user); $this->db->endTransaction(); } } catch (\Throwable $t) { throw new UserException($t->getMessage(), $t->getCode()); } return $id; }
/** * Creates log entry in Db and return log id * * @return int */ public function add() : int { if (empty($this->logdate) || empty($this->logstamp)) { $time = time(); if (empty($this->logdate)) { $this->logdate = date('Y-m-d H:i:s', $time); } if (empty($this->logstamp)) { $this->logstamp = $time; } } if (empty($this->client)) { $this->client = $_SERVER['HTTP_USER_AGENT']; } if (empty($this->ip)) { $this->ip = $_SERVER['REMOTE_ADDR']; } if (empty($this->url)) { $this->url = $_SERVER['REQUEST_URI']; } $this->db->qb(['table' => 'core_bans', 'data' => ['text' => $this->text, 'logdate' => $this->logdate, 'logstamp' => $this->logstamp, 'client' => $this->client, 'ip' => $this->ip, 'url' => $this->url, 'id_user' => $this->id_user, 'code' => $this->code]], true); return $this->db->lastInsertId(); }