/** * 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; }
/** * Removes a group from DB and groups list * * @param integer $id_group * * * @throws DatabaseException */ public function removeGroup($id_group) { try { $this->db->beginTransaction(); // Delete usergroup $this->db->qb(['table' => 'core_groups', 'method' => 'DELETE', 'filter' => 'id_group = :id_group', 'params' => [':id_group' => $id_group]]); $this->db->execute(); // Delete permissions related to this group $this->db->qb(['table' => 'core_permissions', 'method' => 'DELETE', 'filter' => 'id_group = :id_group', 'params' => [':id_group' => $id_group]]); $this->db->execute(); // Remove group from current grouplist unset($this->groups[$id_group]); $this->db->endTransaction(); } catch (\PDOException $e) { $this->db->cancelTransaction(); throw new GroupException($e->getMessage(), $e->getCode(), $e->getPrevious()); } }