fixes: #1166272 by Dave Cohen, John Klehm: support for facebooks oauth 2. Mostly...
[project/fb.git] / fb_user.admin.inc
1 <?php
2
3 /**
4 * @file
5 * Admin pages and forms for user settings.
6 *
7 */
8
9 // TODO: add pages to view data in the fb_user_app table.
10
11 /**
12 * Form builder; Configure settings for this site.
13 *
14 * @ingroup forms
15 * @see system_settings_form()
16 */
17 function fb_user_admin_settings() {
18 $options = array(0 => t('<none>')) + fb_admin_get_app_options(FALSE);
19 if (count($options) == 1) {
20 $message = t('You must create an app first!');
21 drupal_set_message($message, 'error');
22 return array('help' => array('#markup' => $message));
23 }
24
25 $form[FB_USER_VAR_CHECK_SESSION] = array(
26 '#type' => 'checkbox',
27 '#title' => t('Validate session'),
28 '#description' => t('Before creating a user or mapping an account, verify the connection by calling into facebook. This adds overhead, requiring server-side request to facebook.com. The result is stored in local session, so should only happen when user first connects. A "signed request" is difficult to spoof, so this check may not be necessary.'),
29 '#default_value' => variable_get(FB_USER_VAR_CHECK_SESSION, NULL),
30 );
31
32 // Name options for automatically created user accounts.
33 $default = variable_get(FB_USER_VAR_USERNAME_STYLE, FB_USER_OPTION_USERNAME_FBU);
34
35 $form[FB_USER_VAR_USERNAME_STYLE] = array(
36 '#type' => 'radios',
37 '#title' => t('Username Style for Automatically Created Accounts'),
38 '#description' => t('Machine-friendly names include Facebook user ids to ensure uniqueness. Thanks to Drupal\'s theme layer, visitors to your site will <em>usually</em> see a proper name.<br/>Human-friendly names are like "John Smith". Because Drupal requires unique names, you may see "John Smith_2", "John Smith_3" and so on.'),
39 '#options' => array(
40 FB_USER_OPTION_USERNAME_FBU => t('Machine-friendly, i.e. "1234565789@facebook"'),
41 FB_USER_OPTION_USERNAME_FULL => t('Human-friendly, i.e. "John Smith"'),
42 ),
43 '#default_value' => $default,
44 );
45
46 $form['fb_user_alter'] = array(
47 '#type' => 'fieldset',
48 '#title' => t('Form alters'),
49 '#description' => t('Add connect button to forms. If user has connected, show the user\'s name and profile picture.'),
50 );
51 $form['fb_user_alter']['registration'] = array(
52 '#type' => 'fieldset',
53 '#title' => t('Registration page'),
54 );
55 $form['fb_user_alter']['registration'][FB_USER_VAR_ALTER_REGISTER] = array(
56 '#type' => 'checkbox',
57 '#title' => t('Do alter registration form'),
58 '#default_value' => variable_get(FB_USER_VAR_ALTER_REGISTER, TRUE),
59 );
60 $form['fb_user_alter']['registration'][FB_USER_VAR_TEXT_REGISTER] = array(
61 '#type' => 'textfield',
62 '#title' => t('Registration form button text'),
63 '#size' => 60,
64 '#default_value' => _fb_user_button_text('user_register_form'),
65 '#description' => t('Leave blank for facebook\'s default, currently "Login".'),
66 );
67 $form['fb_user_alter']['login'] = array(
68 '#type' => 'fieldset',
69 '#title' => t('Login page'),
70 );
71 $form['fb_user_alter']['login'][FB_USER_VAR_ALTER_LOGIN] = array(
72 '#type' => 'checkbox',
73 '#title' => t('Do alter login form'),
74 '#default_value' => variable_get(FB_USER_VAR_ALTER_LOGIN, TRUE),
75 );
76 $form['fb_user_alter']['login'][FB_USER_VAR_TEXT_LOGIN] = array(
77 '#type' => 'textfield',
78 '#title' => t('Login form button text'),
79 '#size' => 60,
80 '#default_value' => _fb_user_button_text('user_login'),
81 );
82 $form['fb_user_alter']['login_block'] = array(
83 '#type' => 'fieldset',
84 '#title' => t('Login block'),
85 );
86 $form['fb_user_alter']['login_block'][FB_USER_VAR_ALTER_LOGIN_BLOCK] = array(
87 '#type' => 'checkbox',
88 '#title' => t('Do alter login block'),
89 '#default_value' => variable_get(FB_USER_VAR_ALTER_LOGIN_BLOCK, TRUE),
90 );
91 $form['fb_user_alter']['login_block'][FB_USER_VAR_TEXT_LOGIN_BLOCK] = array(
92 '#type' => 'textfield',
93 '#title' => t('Login block button text'),
94 '#size' => 60,
95 '#default_value' => _fb_user_button_text('user_login_block'),
96 );
97 $form['fb_user_alter'][FB_USER_VAR_ALTER_CONTACT] = array(
98 '#type' => 'checkbox',
99 '#title' => t('Do alter contact form'),
100 '#default_value' => variable_get(FB_USER_VAR_ALTER_CONTACT, TRUE),
101 '#description' => t('Sets name and email (if available) from facebook.'),
102 );
103
104 return system_settings_form($form);
105 }
106
107 /**
108 * Not truly hook_form_alter(), this is called from fb_user_form_alter().
109 */
110 function fb_user_admin_form_alter(&$form, &$form_state, $form_id) {
111 // Add our settings to the fb_app edit form.
112 if (isset($form['fb_app_data'])) {
113 $fb_app = $form['#fb_app'];
114 $fb_user_data = _fb_user_get_config($fb_app);
115
116 $form['fb_app_data']['fb_user'] = array(
117 '#type' => 'fieldset',
118 '#title' => t('Facebook user settings'),
119 '#tree' => TRUE,
120 '#collapsible' => TRUE,
121 '#collapsed' => isset($fb_app->label),
122 );
123
124 $form['fb_app_data']['fb_user']['create_account'] = array(
125 '#type' => 'radios',
126 '#title' => t('Create local account'),
127 '#description' => t('This option will create a local account and an entry in the fb_user table when a user authorizes a canvas page or connects using Facebook Connect. If not, Drupal\'s built in user registration will still work.'),
128 '#options' => array(
129 FB_USER_OPTION_CREATE_NEVER => t('Do not create accounts automatically'),
130 FB_USER_OPTION_CREATE_LOGIN => t('If user has authorized the app'),
131 ),
132 '#default_value' => $fb_user_data['create_account'],
133 '#required' => TRUE,
134 );
135
136 $default = $fb_user_data['map_account'];
137 if (!is_array($default)) { // This check is for backward compatibility. Should be removed eventually.
138 $default = array($default);
139 }
140 $form['fb_app_data']['fb_user']['map_account'] = array(
141 '#type' => 'checkboxes',
142 '#title' => t('Map accounts'),
143 '#description' => t('Mapping an account means creating an entry in the fb_user table. This allows Drupal to know which Facebook id corresponds to which local uid. <br/>Matching based on email works when the email extended permission is requested and only if the user is not already mapped to another account.'),
144 '#options' => array(
145 FB_USER_OPTION_MAP_ALWAYS => t('Map account when both local uid and Facebook id are known'),
146 FB_USER_OPTION_MAP_EMAIL => t('Map account when Facebook email exactly matches local account'),
147 ),
148 '#default_value' => $default,
149 );
150
151 $options = array(0 => t('<none>')) + user_roles(1);
152 unset($options[DRUPAL_ANONYMOUS_RID]);
153 unset($options[DRUPAL_AUTHENTICATED_RID]);
154 // Choose a role to be granted to anyone who authorizes the app.
155 $form['fb_app_data']['fb_user']['new_user_rid'] = array(
156 '#type' => 'select',
157 '#title' => t('Permanent role'),
158 '#options' => $options,
159 '#description' => t('When a local user has authorized the app, the user will be <em>permanently</em> granted this role, in addition to the default <em>authenticated user</em>.'),
160 '#default_value' => $fb_user_data['new_user_rid'],
161 );
162
163 // Choose a role to be granted only while connected.
164 $form['fb_app_data']['fb_user']['connected_user_rid'] = array(
165 '#type' => 'select',
166 '#title' => t('Temporary role only when connected <em>(advanced feature, use caution)</em>'),
167 '#options' => $options,
168 '#description' => t('Role granted <em>temporarily</em> while any user (even <em>Anonymous</em>) is connected to facebook. <br/><strong>Important:</strong> Create a role explicitly for this purpose. Do not select a role that you assign to users on a permanent basis.'),
169 '#default_value' => $fb_user_data['connected_user_rid'],
170 );
171 }
172
173 }