1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22/**
23 * Controller to update dashboard
24 */
25class CControllerDashboardShareUpdate extends CController {
26	const EMPTY_USER = 'empty_user';
27	const EMPTY_GROUP = 'empty_group';
28
29	protected function checkInput() {
30		$fields = [
31			'dashboardid' => 'required|db dashboard.dashboardid',
32			'private' => 'db dashboard.private|in 0,1',
33			'users' => 'array',
34			'userGroups' => 'array'
35		];
36
37		$ret = $this->validateInput($fields);
38
39		if (!$ret) {
40			$this->setResponse(new CControllerResponseData([
41				'main_block' => json_encode(['errors' => getMessages()->toString()])
42			]));
43		}
44
45		return $ret;
46	}
47
48	protected function checkPermissions() {
49		return $this->checkAccess(CRoleHelper::UI_MONITORING_DASHBOARD)
50				&& $this->checkAccess(CRoleHelper::ACTIONS_EDIT_DASHBOARDS);
51	}
52
53	protected function doAction() {
54		$editable_dashboard = (bool) API::Dashboard()->get([
55			'output' => [],
56			'dashboardids' => [$this->getInput('dashboardid')],
57			'editable' => true
58		]);
59
60		$msg_box_title = null;
61		if ($editable_dashboard) {
62			$dashboard = ['dashboardid' => $this->getInput('dashboardid')];
63
64			if ($this->hasInput('private')) {
65				$dashboard['private'] = $this->getInput('private');
66			}
67			if ($this->hasInput('users')) {
68				$users = $this->getInput('users');
69				/**
70				 * Empty user needed to always POST the 'users' parameter.
71				 * If 'users' parameter is empty array (excluding empty user) then API deletes all users.
72				 */
73				unset($users[self::EMPTY_USER]);
74				$dashboard['users'] = $users;
75			}
76			if ($this->hasInput('userGroups')) {
77				$groups = $this->getInput('userGroups');
78				/**
79				 * Empty user group always needs POST the 'userGroups' parameter.
80				 * If 'userGroups' is empty array (excluding empty group) the API deletes all user groups.
81				 */
82				unset($groups[self::EMPTY_GROUP]);
83				$dashboard['userGroups'] = $groups;
84			}
85
86			$result = (bool) API::Dashboard()->update($dashboard);
87
88			if ($result) {
89				$msg_box_title = _('Dashboard updated');
90			}
91		}
92		else {
93			error(_('No permissions to referred object or it does not exist!'));
94			$result = false;
95		}
96
97		$response = [];
98
99		if (($messages = getMessages($result, $msg_box_title)) !== null) {
100			$response[$result ? 'messages' : 'errors'] = $messages->toString();
101		}
102
103		$this->setResponse(new CControllerResponseData(['main_block' => json_encode($response)]));
104	}
105}
106