ILIAS  release_8 Revision v8.24
class.ilChatroomGUIHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26use ILIAS\Refinery\Factory as Refinery;
28use ILIAS\UI\Factory as UIFactory;
29use ILIAS\UI\Renderer as UIRenderer;
30
38{
40 protected ilObjUser $ilUser;
42 protected ilLanguage $ilLng;
48 protected ILIAS $ilias;
50 protected ilTree $tree;
51 protected ilTabsGUI $tabs;
52 protected UIFactory $uiFactory;
53 protected UIRenderer $uiRenderer;
55 protected Refinery $refinery;
56
60 public function __construct(ilChatroomObjectGUI $gui)
61 {
63 global $DIC;
64
65 $this->gui = $gui;
66 $this->ilUser = $DIC->user();
67 $this->ilCtrl = $DIC->ctrl();
68 $this->ilLng = $DIC->language();
69 $this->rbacsystem = $DIC->rbac()->system();
70 $this->mainTpl = $DIC->ui()->mainTemplate();
71 $this->upload = $DIC->upload();
72 $this->webDirectory = $DIC->filesystem()->web();
73 $this->obj_service = $DIC->object();
74 $this->ilias = $DIC['ilias'];
75 $this->tabs = $DIC->tabs();
76 $this->navigationHistory = $DIC['ilNavigationHistory'];
77 $this->tree = $DIC['tree'];
78 $this->uiFactory = $DIC->ui()->factory();
79 $this->uiRenderer = $DIC->ui()->renderer();
80 $this->http = $DIC->http();
81 $this->refinery = $DIC->refinery();
82 }
83
90 protected function getRequestValue(string $key, Transformation $trafo, $default = null)
91 {
92 if ($this->http->wrapper()->query()->has($key)) {
93 return $this->http->wrapper()->query()->retrieve($key, $trafo);
94 }
95
96 if ($this->http->wrapper()->post()->has($key)) {
97 return $this->http->wrapper()->post()->retrieve($key, $trafo);
98 }
99
100 return $default;
101 }
102
103 protected function hasRequestValue(string $key): bool
104 {
105 if ($this->http->wrapper()->query()->has($key)) {
106 return true;
107 }
108
109 return $this->http->wrapper()->post()->has($key);
110 }
111
112 public function execute(string $method): void
113 {
114 $this->ilLng->loadLanguageModule('chatroom');
115
116 if (method_exists($this, $method)) {
117 $this->$method();
118 return;
119 }
120
121 $this->executeDefault($method);
122 }
123
124 abstract public function executeDefault(string $requestedMethod): void;
125
130 public function redirectIfNoPermission($permission): void
131 {
132 if (!ilChatroom::checkUserPermissions($permission, $this->gui->getRefId())) {
133 $this->ilCtrl->setParameterByClass(ilRepositoryGUI::class, 'ref_id', ROOT_FOLDER_ID);
134 $this->ilCtrl->redirectByClass(ilRepositoryGUI::class);
135 }
136 }
137
143 public function isSuccessful($response): bool
144 {
145 if (!is_string($response)) {
146 return false;
147 }
148
149 $response = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
150
151 return $response !== null && array_key_exists('success', $response) && $response['success'];
152 }
153
154 protected function getRoomByObjectId(int $objectId): ?ilChatroom
155 {
156 return ilChatroom::byObjectId($objectId);
157 }
158
163 protected function exitIfNoRoomExists(?ilChatroom $room): void
164 {
165 if (null === $room) {
166 $this->sendResponse([
167 'success' => false,
168 'reason' => 'unknown room',
169 ]);
170 }
171 }
172
178 public function sendResponse($response, bool $isJson = false): void
179 {
180 $this->http->saveResponse(
181 $this->http->response()
182 ->withHeader(ResponseHeader::CONTENT_TYPE, 'application/json')
183 ->withBody(Streams::ofString($isJson ? $response : json_encode($response, JSON_THROW_ON_ERROR)))
184 );
185 $this->http->sendResponse();
186 $this->http->close();
187 }
188
195 protected function exitIfNoRoomModeratePermission(ilChatroom $room, int $subRoom, ilChatroomUser $chatUser): void
196 {
197 if (!$this->canModerate($room, $subRoom, $chatUser->getUserId())) {
198 $this->sendResponse([
199 'success' => false,
200 'reason' => 'not owner of private room',
201 ]);
202 }
203 }
204
205 protected function canModerate(ilChatroom $room, int $subRoom, int $usrId): bool
206 {
207 return (
208 $this->isMainRoom($subRoom) ||
209 $room->isOwnerOfPrivateRoom($usrId, $subRoom) ||
210 $this->hasPermission('moderate')
211 );
212 }
213
214 protected function isMainRoom(int $subRoomId): bool
215 {
216 return $subRoomId === 0;
217 }
218
219 public function hasPermission(string $permission): bool
220 {
221 return ilChatroom::checkUserPermissions($permission, $this->gui->getRefId());
222 }
223}
Builds data types.
Definition: Factory.php:21
Class ilChatroomGUIHandler.
ilGlobalTemplateInterface $mainTpl
redirectIfNoPermission($permission)
Checks for requested permissions and redirects if the permission check failed.
exitIfNoRoomModeratePermission(ilChatroom $room, int $subRoom, ilChatroomUser $chatUser)
Check if user can moderate a chatroom.
exitIfNoRoomExists(?ilChatroom $room)
Checks if a ilChatroom exists.
canModerate(ilChatroom $room, int $subRoom, int $usrId)
executeDefault(string $requestedMethod)
sendResponse($response, bool $isJson=false)
Sends a json encoded response and exits the php process.
ilNavigationHistory $navigationHistory
isSuccessful($response)
Checks for success param in an json decoded response.
getRequestValue(string $key, Transformation $trafo, $default=null)
Class ilChatroomUser.
getUserId()
Returns Ilias User ID.
Class ilChatroom.
static checkUserPermissions($permissions, int $ref_id, bool $send_info=true)
Checks user permissions by given array and ref_id.
isOwnerOfPrivateRoom(int $user_id, int $proom_id)
static byObjectId(int $object_id)
Class ilCtrl provides processing control methods.
redirectByClass( $a_class, string $a_cmd=null, string $a_anchor=null, bool $is_async=false)
@inheritDoc
setParameterByClass(string $a_class, string $a_parameter, $a_value)
@inheritDoc
language handling
Navigation History of Repository Items.
User class.
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const ROOT_FOLDER_ID
Definition: constants.php:32
global $DIC
Definition: feed.php:28
Interface Filesystem.
Definition: Filesystem.php:40
Interface GlobalHttpState.
Interface ResponseHeader.
A transformation is a function from one datatype to another.
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193
Class ChatMainBarProvider \MainMenu\Provider.
header include for all ilias files.
$response