addauthority.php 5.53 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage admin
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
 *
 */
define('INTERNAL', 1);
define('ADMIN', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
14
require_once(get_config('docroot') . '/lib/htmloutput.php');
15
define('MENUITEM', 'manageinstitutions/institutions');
16
17

$institution = param_variable('i');
18
$plugin      = param_variable('p');
19
20
21
22
23
$add         = param_boolean('add');
$edit        = param_boolean('edit');
$json        = param_boolean('j');
$instanceid  = param_variable('id', 0);

24
25
define('TITLE', get_string($plugin . 'config', 'admin'));

26
// IF WE'RE EDITING OR CREATING AN AUTHORITY:
27
if ($institution && $plugin) {
28
29
    $classname = 'PluginAuth' . ucfirst(strtolower($plugin));
    safe_require('auth', strtolower($plugin));
30

31
32
    $has_instance_config = call_static_method($classname, 'has_instance_config');
    if (false == $has_instance_config && $add) {
33
34
35
36

        // We've been asked to add an instance of an auth plugin that has no
        // config options. We've been called by an AJAX request, so we just
        // add the instance and generate an acknowledgement.
37
38
39
40
41
42
43
44
45
46

        // The session key has not been checked yet, because this page doesn't
        // define JSON
        try {
            form_validate(param_alphanum('sesskey', null));
        }
        catch (UserException $e) {
            json_reply(true, $e->getMessage());
        }

47
48
49
50
51
52
53
54
55
56
57
58
59
        $authinstance = new stdClass();

        // Get the auth instance with the highest priority number (which is
        // the instance with the lowest priority).
        // TODO: rethink 'priority' as a fieldname... it's backwards!!
        $lastinstance = get_records_array('auth_instance', 'institution', $institution, 'priority DESC', '*', '0', '1');

        if ($lastinstance == false) {
            $authinstance->priority = 0;
        } else {
            $authinstance->priority = $lastinstance[0]->priority + 1;
        }

60
        $authinstance->instancename = $plugin;
61
        $authinstance->institution  = $institution;
62
        $authinstance->authname     = $plugin;
63
        $authinstance->active       = 1;
64
65
66
67
68
        $authinstance->id           = insert_record('auth_instance', $authinstance, 'id', true);
        json_reply(false, array('id' => $authinstance->id, 'name' => ucfirst($authinstance->authname), 'authname' => $authinstance->authname));
        exit;
    }

69
    $form = call_static_method($classname, 'get_instance_config_options', $institution, $instanceid);
70
71
    $form['name'] = 'auth_config';
    $form['plugintype'] = 'auth';
72
    $form['pluginname'] = strtolower($plugin);
73
74
75

    $form['elements']['submit'] = array(
        'type' => 'submitcancel',
76
        'class' => 'btn-primary',
77
        'value' => array(get_string('submit'), get_string('cancel')),
78
        'goto'  => get_config('wwwroot') . 'admin/users/institutions.php?i=' . $institution
79
80
    );

81
    $form = pieform($form);
82
    $smarty = smarty();
83
84
85
86
87
    if ($add) {
        $smarty->assign('PAGETITLE', get_string('addauthority', 'auth'));
    } else {
        $smarty->assign('PAGETITLE', get_string('editauthority', 'auth'));
    }
88
    $smarty->assign('auth_imap_form', $form);
89
90
}

91
92
93
94
95
function auth_config_validate(Pieform $form, $values) {
    $plugin = $values['authname'];
    $classname = 'PluginAuth' . ucfirst(strtolower($plugin));

    try {
96
        $values = call_static_method($classname, 'validate_instance_config_options', $values, $form);
97
98
99
100
101
102
103
    } catch (Exception $e) {
        if (!$form->has_errors()) {
            $form->set_error('instancename', "An unknown error occurred while processing this form");
        }
    }
}

104
function auth_config_submit(Pieform $form, $values) {
105
    global $SESSION;
106
107
    $plugin = $values['authname'];
    $classname = 'PluginAuth' . ucfirst(strtolower($plugin));
108

109
    safe_require('auth', strtolower($plugin));
110
    $values = call_static_method($classname, 'save_instance_config_options', $values, $form);
111

112
    redirect(get_config('wwwroot') . 'admin/users/institutions.php?i=' . $values['institution']);
113
114
}

115
$js = <<<EOF
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
jQuery(function($) {
  function authloginmsgVisibility() {
      // If Parent authority is 'None'
      if ($('#auth_config_parent').val() != 0) {
        $('#auth_config_authloginmsg_container').addClass('hidden');
      }
      else {
        $('#auth_config_authloginmsg_container').removeClass('hidden');
      }
  }
  var ssoAllOptions = {
      'updateuserinfoonlogin': 'theyssoin',
      'weautocreateusers': 'theyssoin',
      'theyautocreateusers': 'wessoout',
      'weimportcontent': 'theyssoin'
  };

  function updateSsoOptions() {
      var current = $('#auth_config_ssodirection').val();
      for (var opt in ssoAllOptions) {
          if (ssoAllOptions[opt] == current) {
              $('#auth_config_' + opt + '_container').removeClass('hidden');
          }
          else {
            $('#auth_config_' + opt + '_container').addClass('hidden');
          }
      }
  }

  if ($('#auth_config_parent').length) {
    $('#auth_config_parent').on('change', authloginmsgVisibility);
    authloginmsgVisibility();
  }
  if ($('#auth_config_ssodirection').length) {
    $('#auth_config_ssodirection').on('change', updateSsoOptions);
    updateSsoOptions();
  }
});
154
155
EOF;

156
$institution = get_record('institution', 'name', $institution);
157
$smarty->assign('INLINEJAVASCRIPT', $js);
158
$smarty->assign('SUBSECTIONHEADING', $institution->displayname);
159
$smarty->display('admin/users/addauthority.tpl');