-
Notifications
You must be signed in to change notification settings - Fork 5
/
stanford_cap_api.admin.inc
157 lines (138 loc) · 4.78 KB
/
stanford_cap_api.admin.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
154
155
156
157
<?php
/**
* @file
* Administrative pages for Stanford CAP API module.
*/
/**
* Form builder for details page.
*/
function stanford_cap_api_details_form($form, &$form_state) {
$form['#attached']['css'][] = drupal_get_path('module', 'stanford_cap_api') . '/css/stanford_cap_api.css';
// Status message.
$status = stanford_cap_api_auth_status();
if ($status) {
$status_message = t('Currently, your site can connect to CAP.');
$additional = NULL;
}
else {
$status_message = t("Currently, your site can't connect to CAP.");
$additional = t('Please check your settings on the !link.', array('!link' => l(t('settings page'), 'admin/config/cap/config/settings')));
}
$vars = array(
'status' => $status,
'message' => $status_message,
'additional' => $additional,
);
$form['status'] = array(
'#markup' => theme('cap_status_item', $vars),
);
return $form;
}
/**
* Form builder for settings page.
*/
function stanford_cap_api_settings_form($form, &$form_state) {
drupal_set_title(t('CAP API settings'));
$form['description_wrapper'] = array(
'#type' => 'container',
);
$form['description_wrapper']['description'] = array(
'#markup' => t('Welcome to CAP! The first step in setting this module up on your site is to synchronize the CAP settings. This will create a content type in your site that will be used to store information about CAP profiles.'),
);
$form['auth'] = array(
'#type' => 'fieldset',
'#title' => t('Authorization'),
);
$form['auth']['description_wrapper'] = array(
'#type' => 'container',
);
$form['auth']['description_wrapper']['description'] = array(
'#markup' => t('Please enter your authentication information for the CAP API.'),
);
$form['auth']['stanford_cap_api_username'] = array(
'#type' => 'textfield',
'#title' => t('Client ID:'),
'#default_value' => variable_get('stanford_cap_api_username', ''),
'#required' => TRUE,
);
$form['auth']['stanford_cap_api_password'] = array(
'#type' => 'password',
'#title' => t('Authz secret:'),
);
$form['auth']['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Advanced setting for CAP API and authentication URIs'),
);
$form['auth']['advanced']['stanford_cap_api_base_url'] = array(
'#type' => 'textfield',
'#title' => t('Endpoint'),
'#description' => t('CAP API endpoint URI, only useful when switching between development/production environment.'),
'#default_value' => variable_get('stanford_cap_api_base_url', 'https://api.stanford.edu'),
'#required' => TRUE,
);
$form['auth']['advanced']['stanford_cap_api_auth_uri'] = array(
'#type' => 'textfield',
'#title' => t('Authentication URI'),
'#description' => t('CAP API authentication URI.'),
'#default_value' => variable_get('stanford_cap_api_auth_uri', 'https://authz.stanford.edu/oauth/token'),
'#required' => TRUE,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
if (!variable_get('stanford_cap_api_configured', FALSE)) {
$form['actions']['submit']['#value'] = t('Next');
}
return $form;
}
/**
* Validation handler for settings form.
*/
function stanford_cap_api_settings_form_validate($form, &$form_state) {
if (!empty($form_state['values']['stanford_cap_api_username']) && !empty($form_state['values']['stanford_cap_api_password'])) {
$username = $form_state['values']['stanford_cap_api_username'];
$password = $form_state['values']['stanford_cap_api_password'];
$auth_uri = $form_state['values']['stanford_cap_api_auth_uri'];
$auth_token = stanford_cap_api_auth($username, $password, $auth_uri);
if (!$auth_token) {
form_set_error('stanford_cap_api_username', t("Error. Can't connect to Stanford CAP API. Please check your username and password."));
form_set_error('stanford_cap_api_password');
}
}
}
/**
* Submit handler for settings form.
*/
function stanford_cap_api_settings_form_submit($form, &$form_state) {
$config_vars = array(
'stanford_cap_api_username',
'stanford_cap_api_password',
'stanford_cap_api_base_url',
'stanford_cap_api_auth_uri',
);
$values = $form_state['values'];
foreach ($config_vars as $config_var) {
if (!empty($values[$config_var])) {
variable_set($config_var, $values[$config_var]);
}
}
drupal_set_message(t('Configuration saved.'));
variable_set('stanford_cap_api_configured', TRUE);
variable_set('menu_rebuild_needed', TRUE);
}
/**
* Checks that we can connect to CAP API.
*/
function stanford_cap_api_auth_status() {
if (stanford_cap_api_request('/profiles/v1')) {
return TRUE;
}
else {
return FALSE;
}
}