commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / cas_fields / cas_fields_server / cas_fields_server.module
1 <?php
2
3 /**
4 * @file
5 * Documentation for CAS Fields Server API.
6 */
7
8 /**
9 * Return additional CAS attributes when acting as a CAS server.
10 *
11 * This hook allows modules to add additional CAS attributes to the basic
12 * response by the CAS Server module.
13 *
14 * @param $account
15 * The user being logged in.
16 * @param $service
17 * The service URL of the site the user is logging in to.
18 * @param $ticket
19 * The login ticket the user provided.
20 *
21 * @return
22 * An associative array of CAS attributes for the user.
23 */
24
25 /**
26 * Does whitelist checking.
27 * Sets the data that will be transfered to cas_fields_client.
28 */
29 function cas_fields_server_cas_server_user_attributes($account, $service, $ticket) {
30 $whitelist = variable_get('cas_server_whitelist');
31
32 // Get the host name.
33 preg_match('@^(?:http://)?([^/]+)@i', $service, $matches);
34 $host = $matches[1];
35
36 // Get last two segments of host name.
37 preg_match('/[^.]+\.[^.]+$/', $host, $matches);
38 $domain = $matches[0];
39
40 // Check the whitelist if it's empty or if it includes the domain name.
41 if ((count($whitelist) == 1) || (in_array($domain, $whitelist))) {
42 $pass = TRUE;
43 }
44 else {
45 $pass = FALSE;
46 }
47
48 // Set user attributes if the domain has passed the clearence.
49 if ($pass == TRUE) {
50 $attributes = array();
51
52 $user_fields = user_load($account->uid);
53 $attributes['user_fields'] = json_encode($user_fields);
54
55 // Attributes can be single valued or multi-valued.
56 $attributes['service'] = $service;
57 $attributes['domain'] = $host;
58 $attributes['whitelist'] = 'passed whitelist check';
59 $attributes['ticket'] = $ticket;
60 $attributes['account'] = $account->uid;
61 $attributes['picture'] = file_create_url($account->picture->uri);
62
63 return $attributes;
64 }
65 // Domain has not passed security check, logout the user.
66 else {
67 user_logout();
68 }
69 }
70
71 /**
72 * Implements hook_menu().
73 */
74 function cas_fields_server_menu() {
75 $items = array();
76
77 $items['admin/config/people/cas/cas_server'] = array(
78 'title' => 'CAS Server Config',
79 'description' => 'Configuration for CAS Server module',
80 'page callback' => 'drupal_get_form',
81 'page arguments' => array('cas_fields_server_config_form'),
82 'access arguments' => array('access administration pages'),
83 'type' => MENU_NORMAL_ITEM,
84 );
85
86 return $items;
87 }
88
89 /**
90 * Building the cas server configuration form.
91 */
92 function cas_fields_server_config_form($form, &$form_state) {
93
94 $list = '';
95 foreach (array_filter(variable_get('cas_server_whitelist')) as $key => $value) {
96 $list .= $value . ", \n";
97 }
98
99 $form['cas_fields_server_config_form'] = array(
100 '#title' => t('CAS Clients top level domains that can access the server: '),
101 '#description' => t('Format : domain.com<br/> Separator: use commas<br/><br/>Note: do not add wildcard (*.domain.com) as all subdomains of the listed domains are cleared by the security check.<br/><br/> Note: All incoming requests pass if there is no domain set in the textfield above'),
102 '#type' => 'textarea',
103 '#default_value' => $list ,
104 );
105
106 $form['#submit'][] = 'cas_fields_server_config_form_submit';
107
108 return system_settings_form($form);
109 }
110
111 /**
112 * Set the whitelist variable value.
113 */
114 function cas_fields_server_config_form_submit($form, &$form_state) {
115 $whitelist = array_map('trim', explode(",", $form['cas_fields_server_config_form']['#value']));
116 variable_set('cas_server_whitelist', $whitelist);
117 }