Commit 335104c5 authored by M.Nowocyn's avatar M.Nowocyn

Added support for deleting a User. It will delete also every post and Thread,...

Added support for deleting a User. It will delete also every post and Thread, which was owned by the "deleted" user.
Add Permissions support to control which Forum can be seen. Use iko.forum.category.view.ID for the Categories and if you want to hide a Board you only have to use iko.forum.board.view.ID . It works fully with Permission inheritance.
parent 8b39efda
......@@ -3,11 +3,17 @@
namespace Iko;
use Iko\CMS\Entity;
use Iko\CMS\Parser;
use Iko\Forum\Board;
use Iko\Forum\Category;
use Iko\Forum\Post;
use Iko\Forum\Thread;
$function = $_REQUEST['func'] ?? "";
$status = false;
$code = "";
$link = "";
$timer = 0;
switch ($function) {
case 'toggleClose':
$thread_id = definePost(
......@@ -52,7 +58,19 @@ switch ($function) {
"desc",
""
);
Category::create($name,$desc);
$status = Category::create(
$name,
$desc
);
if ($status) {
$link = Core::Link(
array (
"module" => "admin",
"sub" => "forum",
"page" => "all")
);
$timer = 2000;
}
break;
case 'createBoard':
$name = definePost(
......@@ -71,14 +89,67 @@ switch ($function) {
"parentType",
1
);
echo Board::create($name,$desc, $parent, $type);
$status = Board::create(
$name,
$desc,
$parent,
$type
);
if ($status) {
$link = Core::Link(
array (
"module" => "admin",
"sub" => "forum",
"page" => "all")
);
$timer = 2000;
}
break;
case 'getPostContent':
$post_id = definePost(
"id",
0
);
if ($post_id != 0) {
$post = Post::get($post_id);
if ($post instanceof Post) {
$code = $post->getContent();
$status = true;
}
}
break;
case 'setPostContent':
$post_id = definePost(
"id",
0
);
$content = definePost(
"content",
""
);
if ($post_id != 0 && $content != "") {
$post = Post::get($post_id);
if ($post instanceof Post) {
if ($post->setContent($content)) {
$status = true;
$code = "Beitrag wurde verändert.";
$timer = 2000;
$link = "";
}
else {
$code = "Error";
}
}
}
break;
default:
$status = false;
$code = "undefined";
$return = array (
"status" => $status,
"code" => $code);
echo json_encode($return);
break;
}
$return = array (
"status" => $status,
"code" => $code,
"link" => $link,
"timer" => $timer);
echo json_encode($return);
This diff is collapsed.
<?php
/**
* Created by PhpStorm.
* User: Matze
* Date: 14.09.2018
* Time: 00:56
*/
namespace Iko\Forum;
use Iko\User;
class Event
{
public static function deleteUser ($name, $args, $pre)
{
$user = User::get($args);
if ($user instanceof User\iUser) {
$posts = Post::searches(array (Post::column_user => $user->getID()));
if (is_array($posts)) {
foreach ($posts as $post) {
if ($post instanceof iPost) {
if (!Post::delete($post)) {
return false;
}
}
}
}
}
return true;
}
public static function deletePost ($name, $args, $pre)
{
if ($args instanceof iPost) {
$thread = Thread::get(
$args->getParentThread()->getID(),
true
);
if ($thread instanceof iThread) {
if (count($thread->getPosts()) == 0) {
Thread::delete($thread);
}
}
}
}
public static function deleteThread ($name, $args, $pre)
{
if ($args instanceof iThread) {
$posts = Post::searches(array (Post::column_thread_id => $args->getID()));
if (is_array($posts)) {
foreach ($posts as $post) {
Post::delete($post);
}
}
}
}
}
\ No newline at end of file
......@@ -15,8 +15,10 @@ namespace Iko\Forum;
use Iko\CMS\Parser;
use Iko\Core;
use Iko\Event\Handler;
use Iko\Exception;
use Iko\Lib\Multiton\Cache\Integer;
use Iko\log;
use Iko\User;
class Post extends Integer implements iPost
......@@ -53,6 +55,54 @@ class Post extends Integer implements iPost
);
}
/**
* @param \Iko\Forum\iPost $post
*
* @return bool
* @throws \iko\Exception
* @permissions iko.forum.thread.post.delete
* @event iko.forum.thread.post.delete
* @final iko.forum.thread.post.delete
*/
public static function delete (iPost $post): bool
{
if (User::getSession()->hasPermission("iko.forum.thread.post.delete") || $post->getUser() === User::getSession(
)) {
if (Post::exist(
$post->getID(),
true
)) {
if (Handler::event(
"iko.forum.thread.post.delete",
$post,
null,
true
)) {
$sql = "DELETE FROM " . self::TABLE . " WHERE " . self::ID . " = " . $post->getID();
$exec = Core::PDO()->exec($sql);
if ($exec > 0) {
log::add(
"forum",
"info",
1001,
"Post " . $post->getID() . " was deleted.",
$post
);
Handler::eventFinal(
"iko.forum.thread.post.delete",
$post,
null
);
return true;
}
}
}
}
return false;
}
private static function databaseInterface (int $function, string $content, User\iUser $user, iThread $thread)
{
$content = Parser::toText($content);
......@@ -108,6 +158,7 @@ class Post extends Integer implements iPost
}
}
public function __construct (int $id)
{
if (self::exist($id)) {
......@@ -179,10 +230,23 @@ class Post extends Integer implements iPost
return Thread::get($this->thread_id);
}
public function delete (): bool
public function setContent (string $content): bool
{
if (User::getSession()->hasPermission("iko.forum.")) {
if ($content != "") {
if ($content != $this->getContent()) {
if (User::getSession() === $this->getUser()) {
$sql = "UPDATE " . self::TABLE . " Set " . self::column_content . " = " . Core::PDO()->quote(
$content
) . " WHERE " . self::ID . " = " . $this->getID();
$exec = Core::PDO()->exec($sql);
if ($exec > 0) {
$this->content = $content;
return true;
}
}
}
}
return false;
......
......@@ -57,11 +57,11 @@ abstract class Structure extends Integer implements iStructure
if ($name != "" && $description != "") {
if (User::getSession()->hasPermission(
"iko.forum." . $temp_class . ".create"
"iko.forum." . strtolower($temp_class) . ".create"
) || ($parent !== null && (User\Permissions\Value::exist(
"iko.forum." . $temp_class . ".create.id." . $parent . ""
"iko.forum." . strtolower($temp_class) . ".create.id." . $parent . ""
) === false) || User::getSession()->hasPermission(
"iko.forum." . $temp_class . ".create.id." . $parent . ""
"iko.forum." . strtolower($temp_class) . ".create.id." . $parent . ""
))) {
if ($class::search(
array (
......@@ -79,19 +79,16 @@ abstract class Structure extends Integer implements iStructure
$sql = "INSERT INTO " . $class::TABLE . " (" . $columns . ") VALUE('" . $values . "')";
$statement = Core::PDO()->prepare($sql);
if ($statement->execute() === true) {
$created = $class::search(
array (
$class::COLUMN_NAME => $name,
$class::COLUMN_DESCRIPTION => $description)
);
$id = Core::PDO()->lastInsertId();
$created = $class::get($id);
if ($created !== false && $parent === null) {
User\Permissions\Value::addPermission(
"iko.forum." . $temp_class . ".id." . $created->getID() . ".create",
"iko.forum." . strtolower($temp_class) . ".create.id." . $created->getID() . "",
Module::get("forum"),
"Something"
);
User\Permissions\Value::addPermission(
"iko.forum." . $temp_class . ".id." . $created->getID() . ".delete",
"iko.forum." . strtolower($temp_class) . ".create.id." . $created->getID() . "",
Module::get("forum"),
"Something"
);
......@@ -121,11 +118,11 @@ abstract class Structure extends Integer implements iStructure
);
$temp_class = $temp_class[ count($temp_class) - 1 ];
if (User::getSession()->hasPermission("iko.forum." . $temp_class . ".delete") || ($id->getParent(
if (User::getSession()->hasPermission("iko.forum." . strtolower($temp_class) . ".delete") || ($id->getParent(
) !== false && (!User\Permissions\Value::exist(
"iko.forum." . $temp_class . ".delete.id." . $id->getParent()->getID() . ""
"iko.forum." . strtolower($temp_class) . ".delete.id." . $id->getParent()->getID() . ""
) || User::getSession()->hasPermission(
"iko.forum." . $temp_class . ".delete.id." . $id->getParent()->getID() . ""
"iko.forum." . strtolower($temp_class) . ".delete.id." . $id->getParent()->getID() . ""
)))) {
$key = Core::PDO()->quote($id->getID());
$sql = "DELETE FROM " . $class::TABLE . " WHERE " . $class::ID . " = " . $key;
......@@ -136,11 +133,11 @@ abstract class Structure extends Integer implements iStructure
$class::$cache_exist = false;
if ($id->getParent() === false) {
User\Permissions\Value::removePermission(
"iko.forum." . $temp_class . ".delete.id." . $id->getID() . "",
"iko.forum." . strtolower($temp_class) . ".delete.id." . $id->getID() . "",
Module::get("forum")
);
User\Permissions\Value::removePermission(
"iko.forum." . $temp_class . ".create.id." . $id->getID() . "",
"iko.forum." . strtolower($temp_class) . ".create.id." . $id->getID() . "",
Module::get("forum")
);
}
......
......@@ -15,8 +15,10 @@ namespace Iko\Forum;
use Iko\CMS\Template;
use Iko\Core;
use Iko\Event\Handler;
use Iko\Exception;
use Iko\Lib\Multiton\Cache\Integer;
use Iko\log;
use Iko\User;
class Thread extends Integer implements iThread
......@@ -42,11 +44,15 @@ class Thread extends Integer implements iThread
public static function create (string $title, string $content, User\iUser $user, iBoard $parent_board)
{
$thread = self::databaseInterface(
self::CREATE,
$title,
$parent_board
);
if (User::getSession()->hasPermission("iko.forum.thread.create") && User::getSession()->hasPermission(
"iko.forum.thread.create." . $parent_board->getID()
)) {
$thread = self::databaseInterface(
self::CREATE,
$title,
$parent_board
);
}
if (isset($title) && $title !== '') {
if (is_numeric($thread)) {
$thread = new Thread($thread);
......@@ -60,19 +66,48 @@ class Thread extends Integer implements iThread
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
return false;
}
public static function delete (iThread $thread): bool
{
if (User::getSession()->hasPermission("iko.forum.thread.delete") && User::getSession()->hasPermission(
"iko.forum.thread.delete." . $thread->getParentBoard()->getID()
) || count($thread->getPosts()) == 0 || User::getSession() === $thread->getFirstPost()->getUser()) {
if (Handler::event(
"iko.forum.thread.delete",
$thread,
null,
true
)) {
$sql = "DELETE FROM " . self::TABLE . " WHERE " . self::ID . " = " . $thread->getID();
$exec = Core::PDO()->exec($sql);
if ($exec > 0) {
log::add(
"forum",
"info",
1002,
"Thread " . $thread->getID() . " | " . $thread->getName() . " was deleted.",
$thread
);
Handler::eventFinal(
"iko.forum.thread.delete",
$thread,
null
);
return true;
}
}
}
return false;
}
private static function databaseInterface (int $function, string $title, iBoard $parent_board)
{
if ($function == self::CREATE) {
......@@ -242,6 +277,7 @@ class Thread extends Integer implements iThread
*/
public function getPosts ()
{
$results = array ();
try {
$sql = "SELECT " . Post::ID . " FROM " . Post::TABLE . " WHERE " . Post::column_thread_id . " = " . $this->id . " ORDER BY " . Post::column_time . " ASC";
$statement = Core::PDO()->query($sql);
......
......@@ -244,6 +244,8 @@
<h2>Create a board</h2>
<form action="javascript:iko.forum.create.board();" method="post" name="createBoard">
<div class="alert-info"><span></span></div>
<div class="alert-danger"><span></span></div>
<fieldset class="form-group bmd-form-group">
<label for="board_name" class="bmd-label-floating">Board name</label> <input type="text" name="board_name" class="form-control" id="board_name"> <span class="bmd-help">This is the displayed name of the board.</span>
</fieldset>
......@@ -265,6 +267,8 @@
<h2>Create a category</h2>
<form action="javascript:iko.forum.create.category();" method="post" name="createCategory">
<div class="alert-info"><span></span></div>
<div class="alert-danger"><span></span></div>
<fieldset class="form-group bmd-form-group">
<label for="category_name" class="bmd-label-floating">Category name</label> <input type="text" name="category_name" class="form-control" id="category_name"> <span class="bmd-help">This is the displayed name of the category.</span>
</fieldset>
......
......@@ -13,11 +13,7 @@ iko.forum = {
desc: boardDesc,
parentID: parentID,
parentType: parentType
}, function (data, status, xhr) {
$(".alert-info .container-fluid .text").text(data);
$(".alert-info").show("fast");
console.info(data);
});
}, iko.ajaxResponse);
},
......@@ -32,10 +28,7 @@ iko.forum = {
func: "createCategory",
name: category_name,
desc: category_description
}, function (data, status, xhr) {
$(".alert-info .container-fluid .text").text(data);
$(".alert-info").show("fast");
});
}, iko.ajaxResponse);
}
......@@ -67,16 +60,46 @@ iko.forum = {
addParent: function (parentID, parentType) {
document.getElementById("parentID").value = parentID;
document.getElementById("parentType").value = parentType;
}
},
edit: {
post: function (post_id, content) {
if (typeof document.forms["editPost"] == 'undefined') {
post: {
edit: function (post_id, content) {
if (typeof document.forms["editPost"] == 'undefined' && typeof post_id != 'undefined' && post_id != 0) {
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "javascript:iko.forum.post.edit();");
form.name = "editPost";
form.classList.add("editPost");
form.innerHTML = "<input type='hidden' name='post_id' value='" + post_id + "'><div class=\"form-group\">\n" +
" <label for=\"content\">Inhalt:</label><br> <textarea name=\"content\" id=\"content\" class=\"form-control\"></textarea>\n" +
" </div>\n" +
" <div class=\"form-group\">\n" +
" <button type=\"submit\" name=\"Post\" class=\"btn btn-default\">Speichern</button>\n" +
" </div>";
$(".post-" + post_id + " .col-sm-10 .post-content").hide();
$(form).insertBefore($(".post-" + post_id + " .col-sm-10 .post-content"));
$.post(iko.ajax, {module: "forum", func: "getPostContent", id: post_id}, function (data) {
console.info(data);
try {
var response = JSON.parse(data);
$("#content").val(response.code);
$("#content").wysibb();
} catch (ex) {
}
});
}
else {
$("#content").sync();
post_id = document.forms["editPost"]["post_id"].value;
content = document.forms["editPost"]["content"].value;
$.post(iko.ajax, {
module: "forum",
func: "setPostContent",
id: post_id,
content: content
}, iko.ajaxResponse);
}
boardName = typeof boardName !== 'undefined' ? boardName : document.forms["createBoard"]["board_name"].value;
}
}
};
......
......@@ -23,13 +23,35 @@ class Loader extends \iko\Module\Loader
"forum",
'iko.cms.register.module',
__NAMESPACE__ . "\\CMS",
'initPage' ),
'initPage'),
array (
"forum",
'iko.admin.register.module',
__NAMESPACE__ . "\\ACP",
'initPage' ), );
private $files = array (
'initPage'),
array (
"forum",
'iko.user.delete',
__NAMESPACE__ . "\\Event",
'deleteUser',
null,
true),);
protected $handler_final = array (
array (
"forum",
"iko.forum.thread.post.delete",
__NAMESPACE__ . "\\Event",
'deletePost',
null,
true),
array (
"forum",
"iko.forum.thread.delete",
__NAMESPACE__ . "\\Event",
'deleteThread',
null,
true),);
private $files = array (
"interfaces" => array (
"iStructure.php",
"*"),
......@@ -39,33 +61,45 @@ class Loader extends \iko\Module\Loader
protected $permissions = array (
array (
"iko.forum.Board.create",
"iko.forum.board.create",
"Something"),
array (
"iko.forum.Category.create",
"iko.forum.category.create",
""),
array (
"iko.forum.post.create",
""),
array (
"iko.forum.thread.create",
""),
array (
"iko.forum.Post.create",
"iko.forum.thread.create.*",
""),
array (
"iko.forum.Thread.create",
"iko.forum.thread.delete.*",
""),
array (
"iko.forum.Board.delete",
"iko.forum.board.delete",
"Something"),
array (
"iko.forum.Category.delete",
"iko.forum.category.delete",
""),
array (
"iko.forum.post.delete",
""),
array (
"iko.forum.thread.delete",
""),
array (
"iko.forum.Post.delete",
"iko.forum.moderation.thread",
""),
array (
"iko.forum.Thread.delete",
"iko.forum.thread.post.delete",
""),);
public function __construct ($module )
public function __construct ($module)
{
parent::__construct($module );
parent::__construct($module);
}
......@@ -73,17 +107,42 @@ class Loader extends \iko\Module\Loader
{
$tables = array ();
return $this->checkPDOTables($tables );
return $this->checkPDOTables($tables);
}
protected function preCheckFiles ()
{
return $this->checkFiles($this->files );
return $this->checkFiles($this->files);
}
public function preLoad ()
{
return parent::load($this->files );
return parent::load($this->files);
}
public function checkPermissions (): bool
{
$boards = Board::getAll();
foreach ($boards as $board) {
if ($board instanceof iBoard) {
$name = "iko.forum.thread.create." . $board->getID();
array_push(
$this->permissions,
array (
$name,
"Permissions for " . $board->getName())
);
$sec = "iko.forum.thread.delete." . $board->getID();
array_push(
$this->permissions,
array (
$sec,
"Permissions for " . $board->getName())
);
}
}
return parent::checkPermissions();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment