Skip to content

Commit

Permalink
Readd inviteToChat method. Remove scope and wrong id. Fix ilChatroom:…
Browse files Browse the repository at this point in the history
…:checkPermissions
  • Loading branch information
lscharmer authored and mjansenDatabay committed Dec 7, 2023
1 parent 50ff7ed commit 902dfbf
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 57 deletions.
62 changes: 18 additions & 44 deletions components/ILIAS/Chatroom/classes/class.ilChatroom.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,58 +98,32 @@ public static function checkPermissionsOfUser(int $usr_id, $permissions, int $re
/**
* @param string[] $permissions
*/
protected static function checkPermissions(int $usrId, int $refId, array $permissions): bool
protected static function checkPermissions(int $usr_id, int $ref_id, array $permissions): bool
{
global $DIC;

$pub_ref_id = ilObjChatroom::_getPublicRefId();

foreach ($permissions as $permission) {
if ($pub_ref_id === $refId) {
$hasAccess = $DIC->rbac()->system()->checkAccessOfUser($usrId, $permission, $refId);
if ($hasAccess) {
$hasWritePermission = $DIC->rbac()->system()->checkAccessOfUser($usrId, 'write', $refId);
if ($hasWritePermission) {
continue;
}

$visible = null;
$a_obj_id = ilObject::_lookupObjId($refId);

switch ($permission) {
case 'visible':
if (!$active) {
$DIC->access()->addInfoItem(
ilAccessInfo::IL_NO_OBJECT_ACCESS,
$DIC->language()->txt('offline')
);
}

if (!$active && !$visible) {
return false;
}
break;

case 'read':
if (!$active) {
$DIC->access()->addInfoItem(
ilAccessInfo::IL_NO_OBJECT_ACCESS,
$DIC->language()->txt('offline')
);
return false;
}
break;
}
if ($pub_ref_id === $ref_id) {
$obj_id = ilObject::_lookupObjId($ref_id);
foreach ($permissions as $permission) {
$no_access = !$DIC->rbac()->system()->checkAccessOfUser($usr_id, $permission, $ref_id) || (
!$DIC->rbac()->system()->checkAccessOfUser($usr_id, 'write', $ref_id) &&
ilObjChatroomAccess::_isOffline($obj_id) &&
in_array($permission, ['read', 'visible'], true)
);

if ($no_access) {
return false;
}
} else {
$hasAccess = $DIC->access()->checkAccessOfUser($usrId, $permission, '', $refId);
}

if (!$hasAccess) {
return false;
} else {
foreach ($permissions as $permission) {
if (!$DIC->access()->checkAccessOfUser($usr_id, $permission, '', $ref_id)) {
return false;
}
}
}

return true;
}

Expand Down Expand Up @@ -693,7 +667,7 @@ public function sendInvitationNotification(
$notification->setVisibleForSeconds(ilNotificationConfig::DEFAULT_TTS);
$notification->setIdentification(new NotificationIdentification(
ChatInvitationNotificationProvider::NOTIFICATION_TYPE,
self::ROOM_INVITATION . '_' . $this->getRefIdByRoomId($this->getRoomId()) . '_' . $subScope,
self::ROOM_INVITATION . '_' . $this->getRefIdByRoomId($this->getRoomId()),
));
$notification->setHandlerParam('mail.sender', (string) $sender_id);

Expand Down
1 change: 0 additions & 1 deletion components/ILIAS/Chatroom/js/iliaschat.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,6 @@
const body = document.querySelector('#invite_users_container');
input.value = '';
const modal = window.il.Modal.dialogue({
id: 'heja',
header: txt('invite_users'),
show: true,
body: body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function testGotoCheckFails(): void
)->getMock();
$user->method('getId')->willReturn(6);

$this->setGlobalVariable('ilObjDataCache', $this->createMock(ilObjectDataCache::class));

$this->setGlobalVariable('ilUser', $user);

$chatroomSettings = $this->createMock(ilDBStatement::class);
Expand Down Expand Up @@ -105,6 +107,7 @@ public function testGotoCheckSucceeds(): void
)->getMock();
$user->method('getId')->willReturn(6);

$this->setGlobalVariable('ilObjDataCache', $this->createMock(ilObjectDataCache::class));
$this->setGlobalVariable('ilUser', $user);

$chatroomSettings = $this->createMock(ilDBStatement::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function populate(): void
protected function fillRow(array $a_set): void
{
if ($this->hasAccessToMailSystem) {
$a_set['chb'] = ilLegacyFormElementsUtil::formCheckbox(false, 'usr_id[]', (string) $a_set['usr_id']);
$a_set['chb'] = ilLegacyFormElementsUtil::formCheckbox(false, 'usr_ids[]', (string) $a_set['usr_id']);
}

$public_profile = ilObjUser::_lookupPref($a_set['usr_id'], 'public_profile');
Expand Down
167 changes: 156 additions & 11 deletions components/ILIAS/Contact/classes/class.ilContactGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

declare(strict_types=1);

use ILIAS\Refinery\Factory as Refinery;

/**
* @author Jens Conze
* @ingroup ServicesMail
Expand All @@ -41,7 +43,7 @@ class ilContactGUI
protected ilErrorHandling $error;
protected ilRbacSystem $rbacsystem;
protected bool $has_sub_tabs = false;
protected ILIAS\Refinery\Factory $refinery;
protected Refinery $refinery;
protected \ILIAS\UI\Factory $ui_factory;
protected \ILIAS\UI\Renderer $ui_renderer;
/** @var array<string, string> */
Expand Down Expand Up @@ -335,22 +337,18 @@ protected function showContacts(): void
$this->activateTab('my_contacts');

if ($this->http->wrapper()->query()->has('inv_room_ref_id') &&
$this->http->wrapper()->query()->has('inv_room_scope') &&
$this->http->wrapper()->query()->has('inv_usr_ids')) {
$inv_room_ref_id = $this->http->wrapper()->query()->retrieve(
'inv_room_ref_id',
$this->refinery->kindlyTo()->int()
);
$inv_room_scope = $this->http->wrapper()->query()->retrieve(
'inv_room_scope',
$this->refinery->kindlyTo()->int()
);
$inv_usr_ids = $this->http->wrapper()->query()->retrieve(
'inv_usr_ids',
$this->refinery->in()->series([
$this->refinery->kindlyTo()->string(),
$this->refinery->custom()->transformation(fn(string $value): array => explode(',', $value)),
$this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
$this->refinery->custom()->transformation(fn(string $s): array => explode(',', $s)),
$this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
$this->refinery->custom()->constraint(fn(array $a) => $a !== [], fn() => 'Empty array.'),
])
);

Expand All @@ -361,8 +359,7 @@ protected function showContacts(): void
}

if ($userlist !== []) {
$url = $inv_room_scope !== 0 ? ilLink::_getStaticLink($inv_room_ref_id, 'chtr', true, '_' . $inv_room_scope) : ilLink::_getStaticLink($inv_room_ref_id, 'chtr');

$url = ilLink::_getStaticLink($inv_room_ref_id, 'chtr');
$content[] = $this->ui_factory->messageBox()->success(
$this->lng->txt('chat_users_have_been_invited') . $this->ui_renderer->render(
$this->ui_factory->listing()->unordered($userlist)
Expand Down Expand Up @@ -408,7 +405,7 @@ protected function mailToUsers(): void

try {
$usr_ids = $this->http->wrapper()->post()->retrieve(
'usr_id',
'usr_ids',
$this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
);

Expand Down Expand Up @@ -450,4 +447,152 @@ protected function mailToUsers(): void

$this->ctrl->redirectToURL('ilias.php?baseClass=ilMailGUI&type=search_res');
}

public function submitInvitation(): void
{
try {
$usr_ids = $this->http->wrapper()->post()->retrieve('usr_ids', $this->refinery->in()->series([
$this->refinery->kindlyTo()->string(),
$this->refinery->custom()->transformation(fn(string $s) => explode(',', $s)),
$this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
$this->refinery->custom()->constraint(fn(array $a) => $a !== [], fn() => 'Empty array.'),
]));
} catch (Exception) {
$this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'), true);
$this->ctrl->redirect($this);
}

try {
$room_id = $this->http->wrapper()->post()->retrieve('room_id', $this->refinery->kindlyTo()->int());
} catch (Exception) {
$this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'));
$this->inviteToChat($usr_ids);
return;
}

$room = ilChatroom::byRoomId($room_id, true);

$no_access = [];
$no_login = [];
$valid_users = [];
$ref_id = $room->getRefIdByRoomId($room_id);

foreach ($usr_ids as $usr_id) {
$login = ilObjUser::_lookupLogin($usr_id);
if ($login === '') {
$no_login[] = $usr_id;
} elseif (
!ilChatroom::checkPermissionsOfUser($usr_id, 'read', $ref_id) ||
$room->isUserBanned($usr_id)
) {
$no_access[] = $login;
} else {
$valid_users[] = $usr_id;
}
}

$message = join('', [
$this->asErrorMessage($no_access, $this->lng->txt('chat_users_without_permission')),
$this->asErrorMessage($no_login, $this->lng->txt('chat_users_without_login')),
]);

if ($message !== '') {
$this->tpl->setOnScreenMessage('failure', $message);
$this->inviteToChat($usr_ids);
return;
}

foreach ($valid_users as $id) {
$room->sendInvitationNotification(
null,
$this->user->getId(),
$id,
ilLink::_getStaticLink($ref_id, 'chtr')
);
}

$this->ctrl->setParameter($this, 'inv_room_ref_id', $ref_id);
$this->ctrl->setParameter($this, 'inv_usr_ids', implode(',', $valid_users));

$this->ctrl->redirect($this);
}

/**
* @param null|list<int> $usr_ids
*/
protected function inviteToChat(?array $usr_ids = null): void
{
$this->tabs_gui->activateSubTab('buddy_view_table');
$this->activateTab('my_contacts');

$this->lng->loadLanguageModule('chatroom');

$usr_ids ??= $this->http->wrapper()->post()->retrieve('usr_ids', $this->refinery->byTrying([
$this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
$this->refinery->always([])
]));

if ([] === $usr_ids) {
$this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'), true);
$this->ctrl->redirect($this);
}

$chat_rooms = (new ilChatroom())->getAccessibleRoomIdByTitleMap($this->user->getId());

$options = array_filter(
$chat_rooms,
fn(int $room_id) => !(ilChatroom::byRoomId($room_id))->isUserBanned($this->user->getId()),
ARRAY_FILTER_USE_KEY
);

asort($options);

$this->tpl->setTitle($this->lng->txt('mail_invite_users_to_chat'));
$this->tpl->setContent($this->inviteToChatForm($options, $usr_ids)->getHTML());
$this->tpl->printToStdout();
}

/**
* @param list<string|int> $array
*/
private function asErrorMessage(array $array, string $title): string
{
if ($array === []) {
return '';
}

$items = array_map(
fn($s) => '<li>' . htmlspecialchars((string) $s) . '</li>',
$array
);

return sprintf(
'%s<br><ul>%s</ul>',
$title,
join('', $items)
);
}

/**
* @param array<string, string> $options
* @param list<int> $usr_ids
*/
private function inviteToChatForm(array $options, array $usr_ids): ilPropertyFormGUI
{
$form = new ilPropertyFormGUI();
$form->setTitle($this->lng->txt('mail_invite_users_to_chat'));
$form->addCommandButton('submitInvitation', $this->lng->txt('submit'));
$form->addCommandButton('showContacts', $this->lng->txt('cancel'));
$form->setFormAction($this->ctrl->getFormAction($this, 'showContacts'));

$sel = new ilSelectInputGUI($this->lng->txt('chat_select_room'), 'room_id');
$sel->setOptions($options);
$form->addItem($sel);

$hidden = new ilHiddenInputGUI('usr_ids');
$hidden->setValue(implode(',', $usr_ids));
$form->addItem($hidden);

return $form;
}
}
6 changes: 6 additions & 0 deletions lang/ilias_de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2919,6 +2919,7 @@ chatroom#:#chat_osc_sure_to_leave_grp_conv#:#Sind Sie sicher, dass Sie den Grupp
chatroom#:#chat_osc_user#:#Benutzer
chatroom#:#chat_osc_user_left_grp_conv#:#Der Benutzer '%s' hat den Gruppenchat verlassen.
chatroom#:#chat_osc_write_a_msg#:#Eine Nachricht verfassen …
chatroom#:#chat_select_room#:#Chatraum auswählen
chatroom#:#chat_settings#:#Chat-Einstellungen
chatroom#:#chat_settings_functions_header#:#Funktionen des Chats
chatroom#:#chat_show_auto_messages#:#Automatische Meldungen anzeigen
Expand Down Expand Up @@ -11172,10 +11173,14 @@ lti#:#user_provider_subtab#:#Von Benutzern definierte Provider
ltiv#:#ltiv_create#:#Zertifikat für LTI-Konsument-Objekt erstellen
ltiv#:#ltiv_create_info#:#Wählen Sie ein abgeschlossenes LTI-Konsument-Objekt aus, um ein Zertifikat dafür zu generieren.
mail#:#back_to_folder#:#Zurück zum Ordner
mail#:#chat_users_have_been_invited#:#Die Benutzer wurden eingeladen:
mail#:#chat_users_without_login#:#Die folgenden Benutzer haben kein ILIAS-Konto und können nicht eingeladen werden:
mail#:#chat_users_without_permission#:#Die folgenden Benutzer haben keinen Zugriff zum ausgewählten Raum:
mail#:#current_folder#:#Aktueller Ordner: %s
mail#:#deleteTemplate#:#Löschen
mail#:#first_email_missing_info#:#Auswahl nicht möglich, da keine E-Mail-Adresse eingetragen wurde
mail#:#forward#:#Weiterleiten
mail#:#goto_invitation_chat#:#Chatraum öffnen
mail#:#invite_to_chat#:#in Chat einladen
mail#:#link_check_affected_links#:#Betroffene Links
mail#:#link_check_introduction#:#die folgenden Externen Links sind ungültig:
Expand Down Expand Up @@ -11280,6 +11285,7 @@ mail#:#mail_incoming_mail#:#Maileingang
mail#:#mail_incoming_smtp#:#An eingetragene E-Mail-Adresse weiterleiten
mail#:#mail_insert_folder_name#:#Bitte geben Sie einen Ordnernamen ein
mail#:#mail_insert_query#:#Bitte geben Sie einen Suchbegriff ein
mail#:#mail_invite_users_to_chat#:#Benutzer in Chat einladen
mail#:#mail_is_read#:#gelesen
mail#:#mail_is_unread#:#ungelesen
mail#:#mail_list_members#:#Mitglieder auflisten
Expand Down
6 changes: 6 additions & 0 deletions lang/ilias_en.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2919,6 +2919,7 @@ chatroom#:#chat_osc_sure_to_leave_grp_conv#:#Are you sure you want to leave the
chatroom#:#chat_osc_user#:#User
chatroom#:#chat_osc_user_left_grp_conv#:#User '%s' has left the group conversation.
chatroom#:#chat_osc_write_a_msg#:#Write a Message ...
chatroom#:#chat_select_room#:#Select Chat Room
chatroom#:#chat_settings#:#Chat Settings
chatroom#:#chat_settings_functions_header#:#Chat Room Functions
chatroom#:#chat_show_auto_messages#:#Show Status Messages
Expand Down Expand Up @@ -11163,10 +11164,14 @@ lti#:#user_provider_subtab#:#Providers Defined by Users
ltiv#:#ltiv_create#:#Create Certificate for LTI Consumer Object
ltiv#:#ltiv_create_info#:#Select a completed LTI consumer object to generate a certificate for it
mail#:#back_to_folder#:#Back to Folder
mail#:#chat_users_have_been_invited#:#The following users have been invited:
mail#:#chat_users_without_login#:#The following users don't have an ILIAS account and can't be invited to a chat room:
mail#:#chat_users_without_permission#:#The following users don't have access to the selected chat room:
mail#:#current_folder#:#Current Folder: %s
mail#:#deleteTemplate#:#Delete
mail#:#first_email_missing_info#:#Selection not possible because no e-mail address has been entered
mail#:#forward#:#Forward
mail#:#goto_invitation_chat#:#Open Chat Room
mail#:#invite_to_chat#:#Invite to Chat
mail#:#link_check_affected_links#:#Affected Links
mail#:#link_check_introduction#:#the following weblinks are invalid:
Expand Down Expand Up @@ -11272,6 +11277,7 @@ mail#:#mail_incoming_smtp#:#Forward to E-Mail Address:
mail#:#mail_insert_folder_name#:#Please insert a folder name
mail#:#mail_insert_query#:#Please insert a query
mail#:#mail_invite_users_to_chat#:#Invite Users to Chat
mail#:#mail_invite_users_to_chat#:#Invite Users to Chat
mail#:#mail_is_read#:#Read
mail#:#mail_is_unread#:#Unread
mail#:#mail_list_members#:#List Members
Expand Down

0 comments on commit 902dfbf

Please sign in to comment.