USER_TYPE_ZABBIX_USER, 'rules' => ['api' => []] ]; return; } $roles = API::Role()->get([ 'output' => ['roleid', 'name', 'type'], 'selectRules' => ['ui', 'ui.default_access', 'modules', 'modules.default_access', 'api.access', 'api.mode', 'api', 'actions', 'actions.default_access' ], 'roleids' => $roleid ]); if (!$roles) { throw new Exception(_('Specified role was not found.')); } $role = $roles[0]; $rules = [ 'ui.default_access' => (bool) $role['rules']['ui.default_access'], 'modules.default_access' => (bool) $role['rules']['modules.default_access'], 'api.access' => (bool) $role['rules']['api.access'], 'api.mode' => (bool) $role['rules']['api.mode'], 'api' => $role['rules']['api'], 'actions.default_access' => (bool) $role['rules']['actions.default_access'] ]; foreach ($role['rules']['ui'] as $rule) { $rules['ui.'.$rule['name']] = (bool) $rule['status']; } foreach ($role['rules']['modules'] as $module) { $rules['modules.module.'.$module['moduleid']] = (bool) $module['status']; } foreach ($role['rules']['actions'] as $rule) { $rules['actions.'.$rule['name']] = (bool) $rule['status']; } $role['type'] = (int) $role['type']; $role['rules'] = $rules; self::$roles[$roleid] = $role; } /** * Gets all available UI elements rules for specific user type. * * @static * * @param integer $user_type User type. * * @return array Returns the array of rule names for specified user type. */ public static function getUiElementsByUserType(int $user_type): array { $rules = [ self::UI_MONITORING_DASHBOARD, self::UI_MONITORING_PROBLEMS, self::UI_MONITORING_HOSTS, self::UI_MONITORING_LATEST_DATA, self::UI_MONITORING_MAPS, self::UI_SERVICES_SERVICES, self::UI_SERVICES_SLA_REPORT, self::UI_INVENTORY_OVERVIEW, self::UI_INVENTORY_HOSTS, self::UI_REPORTS_AVAILABILITY_REPORT, self::UI_REPORTS_TOP_TRIGGERS ]; if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $rules = array_merge($rules, [ self::UI_MONITORING_DISCOVERY, self::UI_SERVICES_SLA, self::UI_REPORTS_SCHEDULED_REPORTS, self::UI_REPORTS_NOTIFICATIONS, self::UI_CONFIGURATION_TEMPLATE_GROUPS, self::UI_CONFIGURATION_HOST_GROUPS, self::UI_CONFIGURATION_TEMPLATES, self::UI_CONFIGURATION_HOSTS, self::UI_CONFIGURATION_MAINTENANCE, self::UI_CONFIGURATION_DISCOVERY, self::UI_CONFIGURATION_TRIGGER_ACTIONS, self::UI_CONFIGURATION_SERVICE_ACTIONS, self::UI_CONFIGURATION_DISCOVERY_ACTIONS, self::UI_CONFIGURATION_AUTOREGISTRATION_ACTIONS, self::UI_CONFIGURATION_INTERNAL_ACTIONS ]); } if ($user_type === USER_TYPE_SUPER_ADMIN) { $rules = array_merge($rules, [ self::UI_REPORTS_SYSTEM_INFO, self::UI_REPORTS_AUDIT, self::UI_REPORTS_ACTION_LOG, self::UI_CONFIGURATION_EVENT_CORRELATION, self::UI_ADMINISTRATION_MEDIA_TYPES, self::UI_ADMINISTRATION_SCRIPTS, self::UI_ADMINISTRATION_USER_GROUPS, self::UI_ADMINISTRATION_USER_ROLES, self::UI_ADMINISTRATION_USERS, self::UI_ADMINISTRATION_API_TOKENS, self::UI_ADMINISTRATION_AUTHENTICATION, self::UI_ADMINISTRATION_GENERAL, self::UI_ADMINISTRATION_AUDIT_LOG, self::UI_ADMINISTRATION_HOUSEKEEPING, self::UI_ADMINISTRATION_PROXIES, self::UI_ADMINISTRATION_MACROS, self::UI_ADMINISTRATION_QUEUE ]); } return $rules; } /** * Gets all available actions rules for specific user type. * * @static * * @param integer $user_type User type. * * @return array Returns the array of rule names for specified user type. */ public static function getActionsByUserType(int $user_type): array { $rules = [ self::ACTIONS_EDIT_DASHBOARDS, self::ACTIONS_EDIT_MAPS, self::ACTIONS_ACKNOWLEDGE_PROBLEMS, self::ACTIONS_SUPPRESS_PROBLEMS, self::ACTIONS_CLOSE_PROBLEMS, self::ACTIONS_CHANGE_SEVERITY, self::ACTIONS_ADD_PROBLEM_COMMENTS, self::ACTIONS_EXECUTE_SCRIPTS, self::ACTIONS_MANAGE_API_TOKENS ]; if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $rules[] = self::ACTIONS_EDIT_MAINTENANCE; $rules[] = self::ACTIONS_MANAGE_SCHEDULED_REPORTS; $rules[] = self::ACTIONS_MANAGE_SLA; } $rules = array_merge($rules, [self::ACTIONS_INVOKE_EXECUTE_NOW, self::ACTIONS_CHANGE_PROBLEM_RANKING]); return $rules; } /** * Gets labels of all available UI sections for specific user type in order as it need to display in UI. * * @static * * @param integer $user_type User type. * * @return array Returns the array where key of each element is UI section name and value is UI section label. */ public static function getUiSectionsLabels(int $user_type): array { $sections = [ self::UI_SECTION_DASHBOARDS => _('Dashboards'), self::UI_SECTION_MONITORING => _('Monitoring'), self::UI_SECTION_SERVICES => _('Services'), self::UI_SECTION_INVENTORY => _('Inventory'), self::UI_SECTION_REPORTS => _('Reports') ]; if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $sections += [ self::UI_SECTION_DATA_COLLECTION => _('Data collection'), self::UI_SECTION_ALERTS => _('Alerts') ]; } if ($user_type === USER_TYPE_SUPER_ADMIN) { $sections += [ self::UI_SECTION_USERS => _('Users'), self::UI_SECTION_ADMINISTRATION => _('Administration') ]; } return $sections; } /** * Gets labels of all available rules for specific UI section and user type in order as it need to display in UI. * * @static * * @param string $ui_section_name UI section name. * @param integer $user_type User type. * * @return array Returns the array where key of each element is rule name and value is rule label. */ public static function getUiSectionRulesLabels(string $ui_section_name, int $user_type): array { switch ($ui_section_name) { case self::UI_SECTION_DASHBOARDS: return [ self::UI_MONITORING_DASHBOARD => 'Dashboards' ]; case self::UI_SECTION_MONITORING: $labels = [ self::UI_MONITORING_PROBLEMS => _('Problems'), self::UI_MONITORING_HOSTS => _('Hosts'), self::UI_MONITORING_LATEST_DATA => _('Latest data'), self::UI_MONITORING_MAPS => _('Maps') ]; if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $labels += [self::UI_MONITORING_DISCOVERY => _('Discovery')]; } return $labels; case self::UI_SECTION_SERVICES: $labels = [self::UI_SERVICES_SERVICES => _('Services')]; if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $labels += [self::UI_SERVICES_SLA => _('SLA')]; } $labels += [self::UI_SERVICES_SLA_REPORT => _('SLA report')]; return $labels; case self::UI_SECTION_INVENTORY: return [ self::UI_INVENTORY_OVERVIEW => _('Overview'), self::UI_INVENTORY_HOSTS => _('Hosts') ]; case self::UI_SECTION_REPORTS: $labels = []; if ($user_type === USER_TYPE_SUPER_ADMIN) { $labels += [self::UI_REPORTS_SYSTEM_INFO => _('System information')]; } if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $labels += [ self::UI_REPORTS_SCHEDULED_REPORTS => _('Scheduled reports') ]; } $labels += [ self::UI_REPORTS_AVAILABILITY_REPORT => _('Availability report'), self::UI_REPORTS_TOP_TRIGGERS => _('Top 100 triggers') ]; if ($user_type === USER_TYPE_SUPER_ADMIN) { $labels += [ self::UI_REPORTS_AUDIT => _('Audit log'), self::UI_REPORTS_ACTION_LOG => _('Action log') ]; } if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $labels += [ self::UI_REPORTS_NOTIFICATIONS => _('Notifications') ]; } return $labels; case self::UI_SECTION_DATA_COLLECTION: $labels = []; if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $labels = [ self::UI_CONFIGURATION_TEMPLATE_GROUPS => _('Template groups'), self::UI_CONFIGURATION_HOST_GROUPS => _('Host groups'), self::UI_CONFIGURATION_TEMPLATES => _('Templates'), self::UI_CONFIGURATION_HOSTS => _('Hosts'), self::UI_CONFIGURATION_MAINTENANCE => _('Maintenance') ]; } if ($user_type === USER_TYPE_SUPER_ADMIN) { $labels += [self::UI_CONFIGURATION_EVENT_CORRELATION => _('Event correlation')]; } if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $labels += [self::UI_CONFIGURATION_DISCOVERY => _('Discovery')]; } return $labels; case self::UI_SECTION_ALERTS: $labels = []; if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $labels += [ self::UI_CONFIGURATION_TRIGGER_ACTIONS => _('Trigger actions'), self::UI_CONFIGURATION_SERVICE_ACTIONS => _('Service actions'), self::UI_CONFIGURATION_DISCOVERY_ACTIONS => _('Discovery actions'), self::UI_CONFIGURATION_AUTOREGISTRATION_ACTIONS => _('Autoregistration actions'), self::UI_CONFIGURATION_INTERNAL_ACTIONS => _('Internal actions') ]; } if ($user_type === USER_TYPE_SUPER_ADMIN) { $labels += [ self::UI_ADMINISTRATION_MEDIA_TYPES => _('Media types'), self::UI_ADMINISTRATION_SCRIPTS => _('Scripts') ]; } return $labels; case self::UI_SECTION_USERS: $labels = []; if ($user_type === USER_TYPE_SUPER_ADMIN) { $labels = [ self::UI_ADMINISTRATION_USER_GROUPS => _('User groups'), self::UI_ADMINISTRATION_USER_ROLES => _('User roles'), self::UI_ADMINISTRATION_USERS => _('Users'), self::UI_ADMINISTRATION_API_TOKENS => _('API tokens'), self::UI_ADMINISTRATION_AUTHENTICATION => _('Authentication') ]; } return $labels; case self::UI_SECTION_ADMINISTRATION: $labels = []; if ($user_type === USER_TYPE_SUPER_ADMIN) { $labels = [ self::UI_ADMINISTRATION_GENERAL => _('General'), self::UI_ADMINISTRATION_AUDIT_LOG => _('Audit log'), self::UI_ADMINISTRATION_HOUSEKEEPING => _('Housekeeping'), self::UI_ADMINISTRATION_PROXIES => _('Proxies'), self::UI_ADMINISTRATION_MACROS => _('Macros'), self::UI_ADMINISTRATION_QUEUE => _('Queue') ]; } return $labels; default: return []; } } /** * Gets labels of all available actions rules for specific user type in order as it need to display on user roles * page. * * @static * * @param integer $user_type User type. * * @return array Returns the array where key of each element is rule name and value is rule label. */ public static function getActionsLabels(int $user_type): array { $labels = [ self::ACTIONS_EDIT_DASHBOARDS => _('Create and edit dashboards'), self::ACTIONS_EDIT_MAPS => _('Create and edit maps') ]; if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $labels += [self::ACTIONS_EDIT_MAINTENANCE => _('Create and edit maintenance')]; } $labels += [ self::ACTIONS_ADD_PROBLEM_COMMENTS => _('Add problem comments'), self::ACTIONS_CHANGE_SEVERITY => _('Change severity'), self::ACTIONS_ACKNOWLEDGE_PROBLEMS => _('Acknowledge problems'), self::ACTIONS_SUPPRESS_PROBLEMS => _('Suppress problems'), self::ACTIONS_CLOSE_PROBLEMS => _('Close problems'), self::ACTIONS_EXECUTE_SCRIPTS => _('Execute scripts'), self::ACTIONS_MANAGE_API_TOKENS => _('Manage API tokens') ]; if ($user_type === USER_TYPE_ZABBIX_ADMIN || $user_type === USER_TYPE_SUPER_ADMIN) { $labels += [ self::ACTIONS_MANAGE_SCHEDULED_REPORTS => _('Manage scheduled reports'), self::ACTIONS_MANAGE_SLA => _('Manage SLA') ]; } $labels += [ self::ACTIONS_INVOKE_EXECUTE_NOW => _('Invoke "Execute now" on read-only hosts'), self::ACTIONS_CHANGE_PROBLEM_RANKING => _('Change problem ranking') ]; return $labels; } /** * Returns a list of all API methods by user type or API methods available only for the given user type. * * @static * * @param int|null $user_type * * @return array */ public static function getApiMethods(?int $user_type = null): array { if (!self::$api_methods) { self::loadApiMethods(); } return ($user_type !== null) ? self::$api_methods[$user_type] : self::$api_methods; } /** * Returns a list of API methods with masks for the given user type. * * @static * * @param int|null $user_type * * @return array */ public static function getApiMethodMasks(?int $user_type = null): array { if (!self::$api_method_masks) { self::loadApiMethods(); } return ($user_type !== null) ? self::$api_method_masks[$user_type] : self::$api_method_masks; } /** * Returns a list of API methods for each method mask for the given user type. * * @static * * @param int $user_type * * @return array */ public static function getApiMaskMethods(int $user_type): array { $api_methods = self::getApiMethods($user_type); $result = [ZBX_ROLE_RULE_API_WILDCARD => $api_methods, ZBX_ROLE_RULE_API_WILDCARD_ALIAS => $api_methods]; foreach ($api_methods as $api_method) { [$service, $method] = explode('.', $api_method, 2); $result[$service.self::API_ANY_METHOD][] = $api_method; $result[self::API_ANY_SERVICE.$method][] = $api_method; } return $result; } /** * Collects all API methods for all user types. * * @static */ private static function loadApiMethods(): void { $api_methods = [ USER_TYPE_ZABBIX_USER => [], USER_TYPE_ZABBIX_ADMIN => [], USER_TYPE_SUPER_ADMIN => [] ]; $api_method_masks = $api_methods; foreach (CApiServiceFactory::API_SERVICES as $service => $class_name) { foreach (constant($class_name.'::ACCESS_RULES') as $method => $rules) { if (array_key_exists('min_user_type', $rules)) { switch ($rules['min_user_type']) { case USER_TYPE_ZABBIX_USER: $api_methods[USER_TYPE_ZABBIX_USER][] = $service.'.'.$method; $api_method_masks[USER_TYPE_ZABBIX_USER][$service.self::API_ANY_METHOD] = true; $api_method_masks[USER_TYPE_ZABBIX_USER][self::API_ANY_SERVICE.$method] = true; // break; is not missing here case USER_TYPE_ZABBIX_ADMIN: $api_methods[USER_TYPE_ZABBIX_ADMIN][] = $service.'.'.$method; $api_method_masks[USER_TYPE_ZABBIX_ADMIN][$service.self::API_ANY_METHOD] = true; $api_method_masks[USER_TYPE_ZABBIX_ADMIN][self::API_ANY_SERVICE.$method] = true; // break; is not missing here case USER_TYPE_SUPER_ADMIN: $api_methods[USER_TYPE_SUPER_ADMIN][] = $service.'.'.$method; $api_method_masks[USER_TYPE_SUPER_ADMIN][$service.self::API_ANY_METHOD] = true; $api_method_masks[USER_TYPE_SUPER_ADMIN][self::API_ANY_SERVICE.$method] = true; } } } } foreach ($api_method_masks as $user_type => $masks) { $api_method_masks[$user_type] = array_merge([ZBX_ROLE_RULE_API_WILDCARD, ZBX_ROLE_RULE_API_WILDCARD_ALIAS], array_keys($masks) ); } self::$api_methods = $api_methods; self::$api_method_masks = $api_method_masks; } }