render( Views::ADMIN_SETTINGS, array( 'roles' => $this->get_role_settings(), 'is_logging_allowed' => $this->storage->get_options()->is_logging_allowed() ) ); } /** * @param Request $request * * @return Redirection_Response * * @throws UnexpectedValueException */ public function save_roles( Request $request ) { $roles = $request->post( 'roles' ); if ( is_null( $roles ) ) { $roles = array(); } if ( ! is_array( $roles ) ) { throw new UnexpectedValueException( 'Could not update configuration due to invalid data type.' ); } if ( ! $this->validate_roles( $roles ) ) { throw new Validation_Exception( 'Invalid role has been sent.' ); } $this->storage->get_options()->set_twofas_roles( $roles ); $this->flash->add_message( 'success', 'roles-saved' ); return $this->redirect( Route::SUBMENU_SETTINGS, Route::ACTION_DISPLAY_SETTINGS ); } /** * @param Request $request * * @return Redirection_Response */ public function save_logging( Request $request ) { $options_storage = $this->storage->get_options(); $is_logging_allowed = (bool) $request->post( 'logging_enabled' ); if ( $is_logging_allowed ) { $this->flash->add_message( 'success', 'logging-enabled' ); $options_storage->enable_logging(); } else { $this->flash->add_message( 'success', 'logging-disabled' ); $options_storage->disable_logging(); } return $this->redirect( Route::SUBMENU_SETTINGS, Route::ACTION_DISPLAY_SETTINGS ); } /** * @return array * * @throws UnexpectedValueException */ private function get_role_settings() { $wp_roles = $this->get_wp_roles(); $twofas_roles = $this->storage->get_options()->get_twofas_roles(); $roles = array(); foreach ( $wp_roles as $role_key => $role_name ) { $roles[] = array( 'key' => $role_key, 'name' => $role_name, 'obligatory' => in_array( $role_key, $twofas_roles, true ), ); } return $roles; } /** * @return array */ private function get_wp_roles() { $wp_roles = new WP_Roles(); return $wp_roles->role_names; } /** * @param array $roles * * @return bool */ private function validate_roles( array $roles ) { $wp_roles = array_keys( $this->get_wp_roles() ); $diff = array_diff( $roles, $wp_roles ); return empty( $diff ); } }