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 CControllerUserCreate extends CControllerUserUpdateGeneral { 23 24 protected function checkInput() { 25 $locales = array_keys(getLocales()); 26 $locales[] = LANG_DEFAULT; 27 $themes = array_keys(APP::getThemes()); 28 $themes[] = THEME_DEFAULT; 29 30 $fields = [ 31 'username' => 'required|db users.username|not_empty', 32 'name' => 'db users.name', 33 'surname' => 'db users.surname', 34 'password1' => 'required|string', 35 'password2' => 'required|string', 36 'user_groups' => 'required|array_id|not_empty', 37 'medias' => 'array', 38 'lang' => 'db users.lang|in '.implode(',', $locales), 39 'timezone' => 'db users.timezone|in '.implode(',', $this->timezones), 40 'theme' => 'db users.theme|in '.implode(',', $themes), 41 'autologin' => 'db users.autologin|in 0,1', 42 'autologout' => 'db users.autologout|not_empty', 43 'url' => 'db users.url', 44 'refresh' => 'required|db users.refresh|not_empty', 45 'rows_per_page' => 'required|db users.rows_per_page', 46 'roleid' => 'required|db users.roleid', 47 'form_refresh' => 'int32' 48 ]; 49 50 $ret = $this->validateInput($fields); 51 $error = $this->GetValidationError(); 52 53 if ($ret && (!$this->validatePassword() || !$this->validateUserRole())) { 54 $error = self::VALIDATION_ERROR; 55 $ret = false; 56 } 57 58 if (!$ret) { 59 switch ($error) { 60 case self::VALIDATION_ERROR: 61 $response = new CControllerResponseRedirect('zabbix.php?action=user.edit'); 62 $response->setFormData($this->getInputAll()); 63 CMessageHelper::setErrorTitle(_('Cannot add user')); 64 $this->setResponse($response); 65 break; 66 67 case self::VALIDATION_FATAL_ERROR: 68 $this->setResponse(new CControllerResponseFatal()); 69 break; 70 } 71 } 72 73 return $ret; 74 } 75 76 protected function checkPermissions() { 77 return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_USERS); 78 } 79 80 protected function doAction() { 81 $user = []; 82 83 $this->getInputs($user, ['username', 'name', 'surname', 'url', 'autologin', 'autologout', 'theme', 'refresh', 84 'rows_per_page', 'lang', 'timezone', 'roleid' 85 ]); 86 $user['usrgrps'] = zbx_toObject($this->getInput('user_groups'), 'usrgrpid'); 87 88 if ($this->getInput('password1', '') !== '' || !$this->allow_empty_password) { 89 $user['passwd'] = $this->getInput('password1'); 90 } 91 92 $user['medias'] = []; 93 94 foreach ($this->getInput('medias', []) as $media) { 95 $user['medias'][] = [ 96 'mediatypeid' => $media['mediatypeid'], 97 'sendto' => $media['sendto'], 98 'active' => $media['active'], 99 'severity' => $media['severity'], 100 'period' => $media['period'] 101 ]; 102 } 103 104 $result = (bool) API::User()->create($user); 105 106 if ($result) { 107 $response = new CControllerResponseRedirect((new CUrl('zabbix.php')) 108 ->setArgument('action', 'user.list') 109 ->setArgument('page', CPagerHelper::loadPage('user.list', null)) 110 ); 111 $response->setFormData(['uncheck' => '1']); 112 CMessageHelper::setSuccessTitle(_('User added')); 113 } 114 else { 115 $response = new CControllerResponseRedirect((new CUrl('zabbix.php')) 116 ->setArgument('action', 'user.edit') 117 ); 118 $response->setFormData($this->getInputAll()); 119 CMessageHelper::setErrorTitle(_('Cannot add user')); 120 } 121 $this->setResponse($response); 122 } 123} 124