1<?php 2 3/** 4 * DokuWiki Plugin oauth (Action Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Andreas Gohr <andi@splitbrain.org> 8 */ 9class action_plugin_oauth_user extends DokuWiki_Action_Plugin 10{ 11 /** @var helper_plugin_oauth */ 12 protected $hlp; 13 14 /** 15 * Constructor 16 * 17 * Initializes the helper 18 */ 19 public function __construct() 20 { 21 $this->hlp = plugin_load('helper', 'oauth'); 22 } 23 24 /** 25 * Registers a callback function for a given event 26 * 27 * @param Doku_Event_Handler $controller DokuWiki's event controller object 28 * @return void 29 */ 30 public function register(Doku_Event_Handler $controller) 31 { 32 global $conf; 33 if ($conf['authtype'] != 'oauth') return; 34 35 $conf['profileconfirm'] = false; // password confirmation doesn't work with oauth only users 36 37 $controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'BEFORE', $this, 'handleProfileform'); 38 $controller->register_hook('AUTH_USER_CHANGE', 'BEFORE', $this, 'handleUsermod'); 39 } 40 /** 41 * Save groups for all the services a user has enabled 42 * 43 * @param Doku_Event $event event object by reference 44 * @return void 45 */ 46 public function handleUsermod(Doku_Event $event) 47 { 48 global $ACT; 49 global $USERINFO; 50 global $auth; 51 global $INPUT; 52 53 if ($event->data['type'] != 'modify') return; 54 if ($ACT != 'profile') return; 55 56 // we want to modify the user's groups 57 $groups = $USERINFO['grps']; //current groups 58 if (isset($event->data['params'][1]['grps'])) { 59 // something already defined new groups 60 $groups = $event->data['params'][1]['grps']; 61 } 62 63 // get enabled and configured services 64 $enabled = $INPUT->arr('oauth_group'); 65 $services = array_keys($this->hlp->listServices()); 66 $services = array_map([$auth, 'cleanGroup'], $services); 67 68 // add all enabled services as group, remove all disabled services 69 foreach ($services as $service) { 70 if (isset($enabled[$service])) { 71 $groups[] = $service; 72 } else { 73 $idx = array_search($service, $groups); 74 if ($idx !== false) unset($groups[$idx]); 75 } 76 } 77 $groups = array_unique($groups); 78 79 // add new group array to event data 80 $event->data['params'][1]['grps'] = $groups; 81 } 82 83 /** 84 * Add service selection to user profile 85 * 86 * @param Doku_Event $event event object by reference 87 * @return void 88 */ 89 public function handleProfileform(Doku_Event $event) 90 { 91 global $USERINFO; 92 /** @var auth_plugin_authplain $auth */ 93 global $auth; 94 95 /** @var Doku_Form $form */ 96 $form = $event->data; 97 $pos = $form->findElementByAttribute('type', 'submit'); 98 99 $services = $this->hlp->listServices(); 100 if (!$services) return; 101 102 $form->insertElement($pos, form_closefieldset()); 103 $form->insertElement( 104 ++$pos, 105 form_openfieldset(['_legend' => $this->getLang('loginwith'), 'class' => 'plugin_oauth']) 106 ); 107 foreach ($services as $service) { 108 $group = $auth->cleanGroup($service->getServiceID()); 109 $elem = form_makeCheckboxField( 110 'oauth_group[' . $group . ']', 111 1, $service->getServiceLabel(), '', 'simple', 112 [ 113 'checked' => (in_array($group, $USERINFO['grps'])) ? 'checked' : '', 114 ] 115 ); 116 117 $form->insertElement(++$pos, $elem); 118 } 119 $form->insertElement(++$pos, form_closefieldset()); 120 $form->insertElement(++$pos, form_openfieldset([])); 121 } 122} 123