/** * Creates user * * @param array $array Array of parameters * @throws ModelException */ public function create(array $array) { $user = new User(); $this->convertDates($array, ['birth_date']); $user->populate($array); $existingUser = $this->dao->getUserByLogin($user->login); if (!is_null($existingUser)) { throw new ModelException('This login already exists'); } $passwordManager = new PasswordManager(); $salt = $passwordManager->getRandomSalt(); $password = $passwordManager->getHash($array['password'], $salt); $user->password = $password; $user->salt = $salt; $this->dao->save($user); }
/** * Removes file * * @param string $hash File hash * @param int $userId User ID * @throws ModelException */ public function removeFile($hash, $userId) { $path = UPLOADED_FILES . $hash; $file = $this->dao->get($hash); if ($file->userId != $userId) { throw new ModelException('File unavailable'); } if (file_exists($path)) { if (!unlink($path)) { throw new ModelException('Can\'t remove file'); } } $this->dao->delete($file); }