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 CControllerUsergroupEdit extends CController { 23 24 /** 25 * @var array User group data from database. 26 */ 27 private $user_group = []; 28 29 protected function init() { 30 $this->disableSIDValidation(); 31 } 32 33 protected function checkInput() { 34 $fields = [ 35 'usrgrpid' => 'db usrgrp.usrgrpid', 36 'name' => 'db usrgrp.name', 37 'userids' => 'array_db users.userid', 38 'gui_access' => 'db usrgrp.gui_access|in '.implode(',', [GROUP_GUI_ACCESS_SYSTEM, GROUP_GUI_ACCESS_INTERNAL, GROUP_GUI_ACCESS_LDAP, GROUP_GUI_ACCESS_DISABLED]), 39 'users_status' => 'db usrgrp.users_status|in '.GROUP_STATUS_ENABLED.','.GROUP_STATUS_DISABLED, 40 'debug_mode' => 'db usrgrp.debug_mode|in '.GROUP_DEBUG_MODE_ENABLED.','.GROUP_DEBUG_MODE_DISABLED, 41 42 'group_rights' => 'array', 43 'tag_filters' => 'array', 44 45 'new_group_right' => 'array', 46 'new_tag_filter' => 'array', 47 48 'form_refresh' => 'int32' 49 ]; 50 51 $ret = $this->validateInput($fields); 52 53 if (!$ret) { 54 $this->setResponse(new CControllerResponseFatal()); 55 } 56 57 return $ret; 58 } 59 60 protected function checkPermissions() { 61 if (!$this->checkAccess(CRoleHelper::UI_ADMINISTRATION_USER_GROUPS)) { 62 return false; 63 } 64 65 if ($this->hasInput('usrgrpid')) { 66 $user_groups = API::UserGroup()->get([ 67 'output' => ['name', 'gui_access', 'users_status', 'debug_mode'], 68 'selectTagFilters' => ['groupid', 'tag', 'value'], 69 'usrgrpids' => $this->getInput('usrgrpid'), 70 'editable' => true 71 ]); 72 73 if (!$user_groups) { 74 return false; 75 } 76 77 $this->user_group = $user_groups[0]; 78 } 79 80 return true; 81 } 82 83 protected function doAction() { 84 // default values 85 $db_defaults = DB::getDefaults('usrgrp'); 86 $data = [ 87 'usrgrpid' => 0, 88 'name' => $db_defaults['name'], 89 'gui_access' => $db_defaults['gui_access'], 90 'users_status' => $db_defaults['users_status'], 91 'debug_mode' => $db_defaults['debug_mode'], 92 'form_refresh' => 0 93 ]; 94 95 // get values from the dabatase 96 if ($this->hasInput('usrgrpid')) { 97 $data['usrgrpid'] = $this->user_group['usrgrpid']; 98 $data['name'] = $this->user_group['name']; 99 $data['gui_access'] = $this->user_group['gui_access']; 100 $data['users_status'] = $this->user_group['users_status']; 101 $data['debug_mode'] = $this->user_group['debug_mode']; 102 } 103 104 // overwrite with input variables 105 $this->getInputs($data, ['name', 'gui_access', 'users_status', 'debug_mode', 'form_refresh']); 106 107 $data['group_rights'] = $this->getGroupRights(); 108 $data['new_group_right'] = $this->getInput('new_group_right', []) + [ 109 'groupids' => [], 110 'permission' => PERM_NONE, 111 'include_subgroups' => '0' 112 ]; 113 114 $data['tag_filters'] = $this->getTagFilters(); 115 $data['new_tag_filter'] = $this->getInput('new_tag_filter', []) + [ 116 'groupids' => [], 117 'tag' => '', 118 'value' => '', 119 'include_subgroups' => '0' 120 ]; 121 122 $data['host_groups_ms'] = self::getHostGroupsMs( 123 array_merge($data['new_group_right']['groupids'], $data['new_tag_filter']['groupids']) 124 ); 125 $data['users_ms'] = $this->getUsersMs(); 126 127 $data['can_update_group'] = (!$this->hasInput('usrgrpid') || granted2update_group($this->getInput('usrgrpid'))); 128 129 $response = new CControllerResponseData($data); 130 $response->setTitle(_('Configuration of user groups')); 131 $this->setResponse($response); 132 } 133 134 /** 135 * @return array 136 */ 137 private function getGroupRights() { 138 if ($this->hasInput('group_rights')) { 139 return $this->getInput('group_rights'); 140 } 141 142 return collapseHostGroupRights( 143 getHostGroupsRights($this->hasInput('usrgrpid') ? [$this->user_group['usrgrpid']] : []) 144 ); 145 } 146 147 /** 148 * @return array 149 */ 150 private function getTagFilters() { 151 if ($this->hasInput('tag_filters')) { 152 return collapseTagFilters($this->getInput('tag_filters')); 153 } 154 155 return collapseTagFilters($this->hasInput('usrgrpid') ? $this->user_group['tag_filters'] : []); 156 } 157 158 /** 159 * Returns all needed host groups formatted for multiselector. 160 * 161 * @param array $groupids 162 * 163 * @return array 164 */ 165 private static function getHostGroupsMs(array $groupids) { 166 if (!$groupids) { 167 return []; 168 } 169 170 $host_groups = API::HostGroup()->get([ 171 'output' => ['groupid', 'name'], 172 'groupids' => $groupids, 173 'preservekeys' => true 174 ]); 175 CArrayHelper::sort($host_groups, ['name']); 176 177 return CArrayHelper::renameObjectsKeys($host_groups, ['groupid' => 'id']); 178 } 179 180 /** 181 * Returns all needed user formatted for multiselector. 182 * 183 * @return array 184 */ 185 private function getUsersMs() { 186 $options = [ 187 'output' => ['userid', 'username', 'name', 'surname'] 188 ]; 189 190 if ($this->hasInput('usrgrpid') && !$this->hasInput('form_refresh')) { 191 $options['usrgrpids'] = $this->getInput('usrgrpid'); 192 } 193 else { 194 $options['userids'] = $this->getInput('userids', []); 195 } 196 197 $users = (array_key_exists('usrgrpids', $options) || $options['userids'] !== []) 198 ? API::User()->get($options) 199 : []; 200 201 $users_ms = []; 202 foreach ($users as $user) { 203 $users_ms[] = ['id' => $user['userid'], 'name' => getUserFullname($user)]; 204 } 205 206 CArrayHelper::sort($users_ms, ['name']); 207 208 return $users_ms; 209 } 210} 211