HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/intranet.kauko.lt/wp-content/plugins/wise-chat/src/services/user/WiseChatAuthorization.php
<?php

/**
 * Wise Chat user authorization service.
 */
class WiseChatAuthorization {
    const PROPERTY_NAME = 'channel_authorization';

    /**
     * @var WiseChatUserService
     */
    private $userService;

    /**
     * @var WiseChatOptions
     */
    private $options;

    /**
     * WiseChatAuthorization constructor.
     */
    public function __construct() {
        $this->options = WiseChatOptions::getInstance();
        $this->userService = WiseChatContainer::get('services/user/WiseChatUserService');
    }

	/**
	 * Determines whether the current user is authorized to access the channel.
	 *
	 * @param WiseChatChannel $channel
	 *
	 * @return boolean
	 * @throws Exception
	 */
    public function isUserAuthorizedForChannel($channel) {
    	if (!$channel->getPassword()) {
    		return true;
	    }

        $grants = $this->userService->getProperty(self::PROPERTY_NAME);

        return is_array($grants) && array_key_exists($channel->getId(), $grants) && $grants[$channel->getId()] === $channel->getPassword();
    }

	/**
	 * Grants access to the channel for the current user.
	 *
	 * @param WiseChatChannel $channel
	 * @throws Exception
	 */
    public function markAuthorizedForChannel($channel) {
        $grants = $this->userService->getProperty(self::PROPERTY_NAME);
        if (!is_array($grants)) {
            $grants = array();
        }

        $grants[$channel->getId()] = $channel->getPassword();
        $this->userService->setProperty(self::PROPERTY_NAME, $grants);
    }
}