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 22class CControllerDashboardShareEdit extends CController { 23 24 private $dashboard; 25 26 protected function init() { 27 $this->disableSIDValidation(); 28 } 29 30 protected function checkInput() { 31 $fields = [ 32 'dashboardid' => 'required|db dashboard.dashboardid' 33 ]; 34 35 $ret = $this->validateInput($fields); 36 37 if (!$ret) { 38 $errors = CJs::encodeJson(['errors' => [getMessages()->toString()]]); 39 $this->setResponse( 40 (new CControllerResponseData(['main_block' => $errors]))->disableView() 41 ); 42 } 43 44 return $ret; 45 } 46 47 protected function checkPermissions() { 48 $dashboards = API::Dashboard()->get([ 49 'output' => ['dashboardid', 'private'], 50 'selectUsers' => ['userid', 'permission'], 51 'selectUserGroups' => ['usrgrpid', 'permission'], 52 'dashboardids' => $this->getInput('dashboardid'), 53 'editable' => true 54 ]); 55 56 $this->dashboard = reset($dashboards); 57 58 return true; 59 } 60 61 protected function doAction() { 62 if ($this->dashboard) { 63 $this->dashboard['users'] = $this->prepareUsers($this->dashboard['users']); 64 $this->dashboard['userGroups'] = $this->prepareUserGroups($this->dashboard['userGroups']); 65 66 $this->setResponse(new CControllerResponseData([ 67 'dashboard' => $this->dashboard, 68 'user' => [ 69 'debug_mode' => $this->getDebugMode() 70 ] 71 ])); 72 } 73 else { 74 error(_('No permissions to referred object or it does not exist!')); 75 76 $this->setResponse( 77 (new CControllerResponseData([ 78 'main_block' => CJs::encodeJson(['errors' => [getMessages()->toString()]]) 79 ]))->disableView() 80 ); 81 } 82 } 83 84 /** 85 * Extend dashboard users data. 86 * 87 * @param array $users 88 * @param array $users[]['userid'] 89 * @param array $users[]['permission'] 90 * 91 * @return array 92 */ 93 private function prepareUsers(array $users = []) { 94 $users = zbx_toHash($users, 'userid'); 95 96 $db_users = API::User()->get([ 97 'output' => ['userid', 'alias', 'name', 'surname'], 98 'userids' => array_keys($users) 99 ]); 100 101 $result = []; 102 foreach ($db_users as $db_user) { 103 $result[] = [ 104 'id' => $db_user['userid'], 105 'name' => getUserFullname($db_user), 106 'permission' => $users[$db_user['userid']]['permission'] 107 ]; 108 } 109 CArrayHelper::sort($result, ['name']); 110 111 return array_values($result); 112 } 113 114 /** 115 * Extend dashboard user groups data. 116 * 117 * @param array $usrgrps 118 * @param array $usrgrps[]['usrgrpid'] 119 * @param array $usrgrps[]['permission'] 120 * 121 * @return array 122 */ 123 private function prepareUserGroups(array $usrgrps = []) { 124 $usrgrps = zbx_toHash($usrgrps, 'usrgrpid'); 125 126 $db_usrgrps = API::UserGroup()->get([ 127 'output' => ['usrgrpid', 'name'], 128 'usrgrpids' => array_keys($usrgrps) 129 ]); 130 131 $result = []; 132 foreach ($db_usrgrps as $db_usrgrp) { 133 $result[] = [ 134 'usrgrpid' => $db_usrgrp['usrgrpid'], 135 'name' => $db_usrgrp['name'], 136 'permission' => $usrgrps[$db_usrgrp['usrgrpid']]['permission'] 137 ]; 138 } 139 CArrayHelper::sort($result, ['name']); 140 141 return array_values($result); 142 } 143} 144