"] = '/plugins/singlesignon/index.php'; $links[__s('Test link', 'singlesignon')] = '/plugins/singlesignon/index.php'; return $links; } // public function maybeTemplate() { // parent::maybeTemplate(); // } function defineTabs($options = array()) { $ong = array(); $this->addDefaultFormTab($ong); $this->addStandardTab('Link', $ong, $options); return $ong; } function showForm($ID, $options = array()) { global $CFG_GLPI; $this->initForm($ID, $options); $this->showFormHeader($options); echo "
' . $values[$field] . ''; } return ''; } static function getSpecificValueToSelect($field, $name = '', $values = '', array $options = array()) { if (!is_array($values)) { $values = array($field => $values); } $options['display'] = false; switch ($field) { case 'type': $options['value'] = $values[$field]; return self::dropdownType($name, $options); } return parent::getSpecificValueToSelect($field, $name, $values, $options); } /** * Get ticket types * * @return array of types * */ static function getTypes() { $options['generic'] = __sso('Generic'); $options['facebook'] = __sso('Facebook'); $options['github'] = __sso('GitHub'); $options['google'] = __sso('Google'); $options['instagram'] = __sso('Instagram'); $options['linkedin'] = __sso('LinkdeIn'); return $options; } /** * Get ticket type Name * * @param $value type ID * */ static function getTicketTypeName($value) { $tab = static::getTypes(); // Return $value if not defined return (isset($tab[$value]) ? $tab[$value] : $value); } /** * Dropdown of ticket type * * @param $name select name * @param $options array of options: * - value : integer / preselected value (default 0) * - toadd : array / array of specific values to add at the begining * - on_change : string / value to transmit to "onChange" * - display : boolean / display or get string (default true) * * @return string id of the select * */ static function dropdownType($name, $options = array()) { $params['value'] = 0; $params['toadd'] = array(); $params['on_change'] = ''; $params['display'] = true; if (is_array($options) && count($options)) { foreach ($options as $key => $val) { $params[$key] = $val; } } $items = array(); if (count($params['toadd']) > 0) { $items = $params['toadd']; } $items += self::getTypes(); return Dropdown::showFromArray($name, $items, $params); } /** * Get an history entry message * * @param $data Array from glpi_logs table * * @since GLPI version 0.84 * * @return string * */ static function getHistoryEntry($data) { switch ($data['linked_action'] - Log::HISTORY_PLUGIN) { case 0: return __('History from plugin example', 'example'); } return ''; } ////////////////////////////// ////// SPECIFIC MODIF MASSIVE FUNCTIONS /////// /** * @since version 0.85 * * @see CommonDBTM::getSpecificMassiveActions() * */ function getSpecificMassiveActions($checkitem = NULL) { $actions = parent::getSpecificMassiveActions($checkitem); $actions['Document_Item' . MassiveAction::CLASS_ACTION_SEPARATOR . 'add'] = _x('button', 'Add a document'); // GLPI core one $actions[__CLASS__ . MassiveAction::CLASS_ACTION_SEPARATOR . 'do_nothing'] = __('Do Nothing - just for fun', 'example'); // Specific one return $actions; } /** * @since version 0.85 * * @see CommonDBTM::showMassiveActionsSubForm() * */ static function showMassiveActionsSubForm(MassiveAction $ma) { switch ($ma->getAction()) { case 'DoIt': echo " " . Html::submit(_x('button', 'Post'), array('name' => 'massiveaction')) . " " . __('Write in item history', 'example'); return true; case 'do_nothing' : echo " " . Html::submit(_x('button', 'Post'), array('name' => 'massiveaction')) . " " . __('but do nothing :)', 'example'); return true; } return parent::showMassiveActionsSubForm($ma); } /** * @since version 0.85 * * @see CommonDBTM::processMassiveActionsForOneItemtype() * */ static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids) { global $DB; switch ($ma->getAction()) { case 'DoIt' : if ($item->getType() == 'Computer') { Session::addMessageAfterRedirect(__("Right it is the type I want...", 'example')); Session::addMessageAfterRedirect(__('Write in item history', 'example')); $changes = array(0, 'old value', 'new value'); foreach ($ids as $id) { if ($item->getFromDB($id)) { Session::addMessageAfterRedirect("- " . $item->getField("name")); Log::history($id, 'Computer', $changes, 'PluginExampleExample', Log::HISTORY_PLUGIN); $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); } else { // Example of ko count $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); } } } else { // When nothing is possible ... $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); } return; case 'do_nothing' : If ($item->getType() == 'PluginExampleExample') { Session::addMessageAfterRedirect(__("Right it is the type I want...", 'example')); Session::addMessageAfterRedirect(__("But... I say I will do nothing for:", 'example')); foreach ($ids as $id) { if ($item->getFromDB($id)) { Session::addMessageAfterRedirect("- " . $item->getField("name")); $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); } else { // Example for noright / Maybe do it with can function is better $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); } } } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); } Return; } parent::processMassiveActionsForOneItemtype($ma, $item, $ids); } /** * * @param string $type * @param array $options * @param array $collaborators * @return \League\OAuth2\Client\Provider\AbstractProvider */ public static function createInstance($type = 'generic', array $options = [], array $collaborators = []) { switch ($type) { case 'facebook': if (!isset($options['graphApiVersion'])) { $options['graphApiVersion'] = 'v2.8'; } return new League\OAuth2\Client\Provider\Facebook($options, $collaborators); case 'github': return new League\OAuth2\Client\Provider\Github($options, $collaborators); case 'google': return new League\OAuth2\Client\Provider\Google($options, $collaborators); case 'instagram': return new League\OAuth2\Client\Provider\Instagram($options, $collaborators); case 'linkedin': return new League\OAuth2\Client\Provider\LinkedIn($options, $collaborators); case 'generic': default: return new League\OAuth2\Client\Provider\GenericProvider($options, $collaborators); } } private function getCurrentURL() { $currentURL = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https://" : "http://"; $currentURL .= $_SERVER["SERVER_NAME"]; if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") { $currentURL .= ":" . $_SERVER["SERVER_PORT"]; } $currentURL .= $_SERVER["REQUEST_URI"]; return $currentURL; } public function prepareProviderInstance(array $options = [], array $collaborators = []) { global $CFG_GLPI; if ($this->_provider === null) { $redirect_uri = $this->getCurrentURL(); $type = $this->fields['type']; $default = [ 'clientId' => $this->fields['client_id'], 'clientSecret' => $this->fields['client_secret'], 'redirectUri' => $redirect_uri, ]; if ($type === 'generic') { $default['urlAuthorize'] = $this->fields['url_authorize']; $default['urlAccessToken'] = $this->fields['url_accessToken']; $default['urlResourceOwnerDetails'] = $this->fields['url_resource_owner_details']; } if (!empty($this->fields['extra_options'])) { try { $extra = json_decode($this->fields['extra_options'], true); if (!empty($extra)) { $default = array_merge($default, $extra); } } catch (Exception $ex) { } } $options = array_merge($default, $options); $this->_provider = self::createInstance($type, $options, $collaborators); } return $this->_provider; } /** * * @return boolean|string */ public function checkAuthorization() { if ($this->_provider === null) { return false; } if (!isset($_GET['code'])) { $scope = []; if (!empty($this->fields['scope'])) { $scope = explode(',', $this->fields['scope']); } $options = [ 'scope' => $scope, 'state' => Session::getNewCSRFToken(), ]; $this->_provider->authorize($options); } // Check given state against previously stored one to mitigate CSRF attack $state = isset($_GET['state']) ? $_GET['state'] : ''; Session::checkCSRF([ '_glpi_csrf_token' => $state, ]); $this->_code = $_GET['code']; return $_GET['code']; } /** * * @return boolean|\League\OAuth2\Client\Token\AccessToken */ public function getAccessToken() { if ($this->_token !== null) { return $this->_token; } if ($this->_provider === null || $this->_code === null) { return false; } $this->_token = $this->_provider->getAccessToken('authorization_code', [ 'code' => $this->_code ]); return $this->_token; } /** * * @return boolean|\League\OAuth2\Client\Provider\ResourceOwnerInterface */ public function getResourceOwner() { if ($this->_resource_owner !== null) { return $this->_resource_owner; } $token = $this->getAccessToken(); if (!$token) { return false; } $this->_resource_owner = $this->_provider->getResourceOwner($token); return $this->_resource_owner; } public function findUser() { $resource = $this->getResourceOwner(); $resource_array = $resource->toArray(); $user = new User(); //First: check linked user $email = false; $email_fields = ['email', 'e-mail']; foreach ($email_fields as $field) { if (isset($resource_array[$field]) && is_string($resource_array[$field])) { $email = $resource_array[$field]; break; } } if ($email && $user->getFromDBbyEmail($email, '')) { return $user; } $login = false; $login_fields = ['login', 'username']; foreach ($login_fields as $field) { if (isset($resource_array[$field]) && is_string($resource_array[$field])) { $login = $resource_array[$field]; break; } } if ($login && $user->getFromDBbyName($login)) { return $user; } return false; } public function login() { $user = $this->findUser(); if (!$user) { return false; } //Create fake auth $auth = new Auth(); $auth->user = $user; $auth->auth_succeded = true; $auth->extauth = 1; $auth->user_present = $auth->user->getFromDBbyName(addslashes($user->fields['name'])); $auth->user->fields['authtype'] = Auth::DB_GLPI; Session::init($auth); return $auth->auth_succeded; } }