From 7f4be9776342d29ad2f29d000235ed28b815468c Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Tue, 25 Jan 2005 20:27:09 +0000 Subject: - Patch #16176 by Drumm based on mockup from Kika: improve access rules UI. --- modules/user.module | 140 +++++++++++++++++++++++++++-------------------- modules/user/user.module | 140 +++++++++++++++++++++++++++-------------------- 2 files changed, 164 insertions(+), 116 deletions(-) (limited to 'modules') diff --git a/modules/user.module b/modules/user.module index 749032206..d76e9c664 100644 --- a/modules/user.module +++ b/modules/user.module @@ -667,13 +667,7 @@ function user_menu($may_cache) { $items[] = array('path' => 'admin/user/configure/settings', 'title' => t('settings'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'admin/user/configure/access', 'title' => t('access rules'), - 'callback' => 'user_configure', 'access' => $access, - 'type' => MENU_LOCAL_TASK); - $items[] = array('path' => 'admin/user/configure/access/mail', 'title' => t('e-mail rules'), - 'callback' => 'user_configure', 'access' => $access, - 'type' => MENU_LOCAL_TASK); - $items[] = array('path' => 'admin/user/configure/access/user', 'title' => t('name rules'), - 'callback' => 'user_configure', 'access' => $access, + 'callback' => 'user_admin_access', 'access' => $access, 'type' => MENU_LOCAL_TASK); $items[] = array('path' => 'admin/user/configure/role', 'title' => t('roles'), 'callback' => 'user_configure', 'access' => $access, @@ -1311,60 +1305,99 @@ function user_admin_create($edit = array()) { return form($output); } -function user_admin_access($edit = array()) { - $type = arg(4); +function user_admin_access($op = NULL, $aid = 0) { + if ($_POST['op']) { + $op = $_POST['op']; + } + $edit = $_POST['edit']; - if (empty($type)) { - return; + // If a non-checking form was submitted, then it needs to have a mask. + if ($edit && !$edit['test'] && !$edit['mask']) { + form_set_error('mask', t('You must enter a mask.')); } - $op = $_POST['op']; - $id = arg(5); + switch ($op) { + case t('Add rule'): + if (!form_get_errors()) { + $aid = db_next_id('{access}_aid'); + db_query("INSERT INTO {access} (aid, mask, type, status) VALUES ('%s', '%s', '%s', %d)", $aid, $edit['mask'], $edit['type'], $edit['status']); + drupal_set_message(t('The access rule has been added.')); + // Since we are not refreshing the page we need to clear out $edit. + $edit = array(); + } + break; - if ($op == t('Add rule')) { - $aid = db_next_id('{access}_aid'); - db_query("INSERT INTO {access} (aid, mask, type, status) VALUES ('%s', '%s', '%s', %d)", $aid, $edit['mask'], $type, $edit['status']); - drupal_set_message(t('The access rule has been added.')); - } - else if ($op == t('Check')) { - if (user_deny($type, $edit['test'])) { - drupal_set_message(t('%test is not allowed.', array('%test' => '' .$edit['test'] .''))); - } - else { - drupal_set_message(t('%test is allowed.', array('%test' => ''. $edit['test'] .''))); - } - } - else if ($id) { - db_query('DELETE FROM {access} WHERE aid = %d', $id); - drupal_set_message(t('The access rule has been deleted.')); - } + case 'delete': + db_query('DELETE FROM {access} WHERE aid = %d', $aid); + drupal_set_message(t('The access rule has been deleted.')); + drupal_goto('admin/user/configure/access'); - $header = array(t('Type'), t('Mask'), t('Operations')); - $result = db_query("SELECT * FROM {access} WHERE type = '%s' AND status = 1 ORDER BY mask", $type); - while ($rule = db_fetch_object($result)) { - $rows[] = array(t('Allow'), $rule->mask, array('data' => l(t('delete'), "admin/user/configure/access/$type/$rule->aid"), 'align' => 'center')); + case t('Save rule'): + if (!form_get_errors()) { + db_query("UPDATE {access} SET mask = '%s', type = '%s', status = '%s' WHERE aid = %d", $edit['mask'], $edit['type'], $edit['status'], $aid); + drupal_set_message(t('The access rule has been saved.')); + drupal_goto('admin/user/configure/access'); + } + // Fall through to the edit form if there are errors. + case 'edit': + if (!$edit) { + $edit = db_fetch_array(db_query('SELECT aid, type, status, mask FROM {access} WHERE aid = %d', $aid)); + } + $form = _user_admin_access_form($edit); + $form .= form_submit(t('Save rule')); + print theme('page', form($form, 'post', NULL, array('id' => 'access-rules'))); + return; + + case t('Check username'): + case t('Check e-mail'): + if (user_deny($edit['type'], $edit['test'])) { + drupal_set_message(t('%test is not allowed.', array('%test' => ''. $edit['test'] .''))); + } + else { + drupal_set_message(t('%test is allowed.', array('%test' => ''. $edit['test'] .''))); + } + break; } - $result = db_query("SELECT * FROM {access} WHERE type = '%s' AND status = 0 ORDER BY mask", $type); + $header = array(array('data' => t('Access type'), 'field' => 'status'), array('data' => t('Rule type'), 'field' => 'type'), array('data' =>t('Mask'), 'field' => 'mask'), array('data' => t('Operations'), 'colspan' => 2)); + $result = db_query("SELECT aid, type, status, mask FROM {access}". tablesort_sql($header)); + $access_types = array('user' => t('username'), 'mail' => t('e-mail')); + $rows = array(); while ($rule = db_fetch_object($result)) { - $rows[] = array(t('Deny'), $rule->mask, l(t('delete'), "admin/user/configure/access/$type/$rule->aid")); + $rows[] = array($rule->status ? t('allow') : t('deny'), $access_types[$rule->type], $rule->mask, l(t('edit'), 'admin/user/configure/access/edit/'. $rule->aid), l(t('delete'), 'admin/user/configure/access/delete/'. $rule->aid)); + } + if (count($rows) == 0) { + $rows[] = array(array('data' => ''. t('There are currently no access rules.') .'', 'colspan' => 5)); } - - $options = array('1' => t('Allow'), '0' => t('Deny')); - $rows[] = array(form_radios(NUll, 'status', $edit['status'], $options), form_textfield(NULL, 'mask', $edit['mask'], 32, 64), form_submit(t('Add rule'))); $output .= theme('table', $header, $rows); - $output .= '

%: '. t('Matches any number of characters, even zero characters') .'.
_: '. t('Matches exactly one character.') .'

'; + $output .= '

'. t('Add new rule') ."

\n"; - if ($type != 'user') { - $title = t('Check e-mail address'); - } - else { - $title = t('Check username'); - } - $output .= form_textfield($title, 'test', $edit['test'], 32, 64). form_submit(t('Check')); + $form = _user_admin_access_form($edit); + $form .= form_submit(t('Add rule')); + $output .= form($form, 'post', NULL, array('id' => 'access-rules')); - return form($output); + $output .= '

'. t('Check rules') .'

'; + + $form = form_textfield(t('Username'), 'test', '', 32, 64, t('Enter a username to check if it will be denied or allowed.')); + $form .= form_hidden('type', 'user'); + $form .= form_submit('Check username'); + $output .= form($form); + + $form = form_textfield(t('E-mail'), 'test', '', 32, 64, t('Enter an e-mail address to check if it will be denied or allowed.')); + $form .= form_hidden('type', 'mail'); + $form .= form_submit('Check e-mail'); + $output .= form($form); + + print theme('page', $output); +} + +function _user_admin_access_form($edit) { + $output = '
'. form_radios(t('Access type'), 'status', $edit['status'], array('1' => t('Allow'), '0' => t('Deny'))) .'
'; + $output .= '
'. form_radios(t('Rule type'), 'type', $edit['type'] ? $edit['type'] : 'user', array('user' => t('Username'), 'mail' => t('E-mail'))) .'
'; + $output .= '
'. form_textfield(t('Mask'), 'mask', $edit['mask'], 32, 64, '%: '. t('Matches any number of characters, even zero characters') .'.
_: '. t('Matches exactly one character.'), NULL, TRUE) .'
'; + + return $output; } function user_roles($membersonly = 0, $permission = 0) { @@ -1565,11 +1598,6 @@ function user_configure() { } switch ($op) { - case t('Add rule'): - case t('Check'): - case 'access': - $output = user_admin_access($edit); - break; case t('Save permissions'): case 'permission': $output = user_admin_perm($edit); @@ -1623,11 +1651,7 @@ function user_help($section) { case 'admin/user/account/create': return t('

This web page allows the administrators to register a new users by hand. Note that you cannot have a user where either the e-mail address or the username match another user in the system.

'); case 'admin/user/configure/access': - return t('

Access rules allow Drupal administrators to choose usernames and e-mail address that are prevented from using drupal. To enter the mask for e-mail addresses click on e-mail rules, for the username mask click on name rules.

', array('%email' => url('admin/user/configure/access/mail'), '%username' => url('admin/user/configure/access/user'))); - case 'admin/user/configure/access/mail': - return t('

Setup and test the e-mail access rules. The access function checks if you match a deny and not an allow. If you match only a deny then it is denied. Any other case, such as both a deny and an allow pattern matching, allows the pattern.

'); - case 'admin/user/configure/access/user': - return t('

Setup and test the username access rules. The access function checks if you match a deny and not an allow. If you do then it is denied. Any other case, such as a deny pattern and an allow pattern, allows the pattern.

'); + return '

'. t('Set up username and e-mail address access rules for new accounts. If a username or email address for a new account matches any deny rule, but not an allow rule, then the new account will not be allowed to be created.') .'

'; case 'admin/user/configure/permission': return t('

In this area you will define the permissions for each user role (role names are defined on the user roles page). Each permission describes a fine-grained logical operation, such as being able to access the administration pages, or adding/modifying a user account. You could say a permission represents access granted to a user to perform a set of operations.

', array('%role' => url('admin/user/configure/role'))); case 'admin/user/configure/role': diff --git a/modules/user/user.module b/modules/user/user.module index 749032206..d76e9c664 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -667,13 +667,7 @@ function user_menu($may_cache) { $items[] = array('path' => 'admin/user/configure/settings', 'title' => t('settings'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'admin/user/configure/access', 'title' => t('access rules'), - 'callback' => 'user_configure', 'access' => $access, - 'type' => MENU_LOCAL_TASK); - $items[] = array('path' => 'admin/user/configure/access/mail', 'title' => t('e-mail rules'), - 'callback' => 'user_configure', 'access' => $access, - 'type' => MENU_LOCAL_TASK); - $items[] = array('path' => 'admin/user/configure/access/user', 'title' => t('name rules'), - 'callback' => 'user_configure', 'access' => $access, + 'callback' => 'user_admin_access', 'access' => $access, 'type' => MENU_LOCAL_TASK); $items[] = array('path' => 'admin/user/configure/role', 'title' => t('roles'), 'callback' => 'user_configure', 'access' => $access, @@ -1311,60 +1305,99 @@ function user_admin_create($edit = array()) { return form($output); } -function user_admin_access($edit = array()) { - $type = arg(4); +function user_admin_access($op = NULL, $aid = 0) { + if ($_POST['op']) { + $op = $_POST['op']; + } + $edit = $_POST['edit']; - if (empty($type)) { - return; + // If a non-checking form was submitted, then it needs to have a mask. + if ($edit && !$edit['test'] && !$edit['mask']) { + form_set_error('mask', t('You must enter a mask.')); } - $op = $_POST['op']; - $id = arg(5); + switch ($op) { + case t('Add rule'): + if (!form_get_errors()) { + $aid = db_next_id('{access}_aid'); + db_query("INSERT INTO {access} (aid, mask, type, status) VALUES ('%s', '%s', '%s', %d)", $aid, $edit['mask'], $edit['type'], $edit['status']); + drupal_set_message(t('The access rule has been added.')); + // Since we are not refreshing the page we need to clear out $edit. + $edit = array(); + } + break; - if ($op == t('Add rule')) { - $aid = db_next_id('{access}_aid'); - db_query("INSERT INTO {access} (aid, mask, type, status) VALUES ('%s', '%s', '%s', %d)", $aid, $edit['mask'], $type, $edit['status']); - drupal_set_message(t('The access rule has been added.')); - } - else if ($op == t('Check')) { - if (user_deny($type, $edit['test'])) { - drupal_set_message(t('%test is not allowed.', array('%test' => '' .$edit['test'] .''))); - } - else { - drupal_set_message(t('%test is allowed.', array('%test' => ''. $edit['test'] .''))); - } - } - else if ($id) { - db_query('DELETE FROM {access} WHERE aid = %d', $id); - drupal_set_message(t('The access rule has been deleted.')); - } + case 'delete': + db_query('DELETE FROM {access} WHERE aid = %d', $aid); + drupal_set_message(t('The access rule has been deleted.')); + drupal_goto('admin/user/configure/access'); - $header = array(t('Type'), t('Mask'), t('Operations')); - $result = db_query("SELECT * FROM {access} WHERE type = '%s' AND status = 1 ORDER BY mask", $type); - while ($rule = db_fetch_object($result)) { - $rows[] = array(t('Allow'), $rule->mask, array('data' => l(t('delete'), "admin/user/configure/access/$type/$rule->aid"), 'align' => 'center')); + case t('Save rule'): + if (!form_get_errors()) { + db_query("UPDATE {access} SET mask = '%s', type = '%s', status = '%s' WHERE aid = %d", $edit['mask'], $edit['type'], $edit['status'], $aid); + drupal_set_message(t('The access rule has been saved.')); + drupal_goto('admin/user/configure/access'); + } + // Fall through to the edit form if there are errors. + case 'edit': + if (!$edit) { + $edit = db_fetch_array(db_query('SELECT aid, type, status, mask FROM {access} WHERE aid = %d', $aid)); + } + $form = _user_admin_access_form($edit); + $form .= form_submit(t('Save rule')); + print theme('page', form($form, 'post', NULL, array('id' => 'access-rules'))); + return; + + case t('Check username'): + case t('Check e-mail'): + if (user_deny($edit['type'], $edit['test'])) { + drupal_set_message(t('%test is not allowed.', array('%test' => ''. $edit['test'] .''))); + } + else { + drupal_set_message(t('%test is allowed.', array('%test' => ''. $edit['test'] .''))); + } + break; } - $result = db_query("SELECT * FROM {access} WHERE type = '%s' AND status = 0 ORDER BY mask", $type); + $header = array(array('data' => t('Access type'), 'field' => 'status'), array('data' => t('Rule type'), 'field' => 'type'), array('data' =>t('Mask'), 'field' => 'mask'), array('data' => t('Operations'), 'colspan' => 2)); + $result = db_query("SELECT aid, type, status, mask FROM {access}". tablesort_sql($header)); + $access_types = array('user' => t('username'), 'mail' => t('e-mail')); + $rows = array(); while ($rule = db_fetch_object($result)) { - $rows[] = array(t('Deny'), $rule->mask, l(t('delete'), "admin/user/configure/access/$type/$rule->aid")); + $rows[] = array($rule->status ? t('allow') : t('deny'), $access_types[$rule->type], $rule->mask, l(t('edit'), 'admin/user/configure/access/edit/'. $rule->aid), l(t('delete'), 'admin/user/configure/access/delete/'. $rule->aid)); + } + if (count($rows) == 0) { + $rows[] = array(array('data' => ''. t('There are currently no access rules.') .'', 'colspan' => 5)); } - - $options = array('1' => t('Allow'), '0' => t('Deny')); - $rows[] = array(form_radios(NUll, 'status', $edit['status'], $options), form_textfield(NULL, 'mask', $edit['mask'], 32, 64), form_submit(t('Add rule'))); $output .= theme('table', $header, $rows); - $output .= '

%: '. t('Matches any number of characters, even zero characters') .'.
_: '. t('Matches exactly one character.') .'

'; + $output .= '

'. t('Add new rule') ."

\n"; - if ($type != 'user') { - $title = t('Check e-mail address'); - } - else { - $title = t('Check username'); - } - $output .= form_textfield($title, 'test', $edit['test'], 32, 64). form_submit(t('Check')); + $form = _user_admin_access_form($edit); + $form .= form_submit(t('Add rule')); + $output .= form($form, 'post', NULL, array('id' => 'access-rules')); - return form($output); + $output .= '

'. t('Check rules') .'

'; + + $form = form_textfield(t('Username'), 'test', '', 32, 64, t('Enter a username to check if it will be denied or allowed.')); + $form .= form_hidden('type', 'user'); + $form .= form_submit('Check username'); + $output .= form($form); + + $form = form_textfield(t('E-mail'), 'test', '', 32, 64, t('Enter an e-mail address to check if it will be denied or allowed.')); + $form .= form_hidden('type', 'mail'); + $form .= form_submit('Check e-mail'); + $output .= form($form); + + print theme('page', $output); +} + +function _user_admin_access_form($edit) { + $output = '
'. form_radios(t('Access type'), 'status', $edit['status'], array('1' => t('Allow'), '0' => t('Deny'))) .'
'; + $output .= '
'. form_radios(t('Rule type'), 'type', $edit['type'] ? $edit['type'] : 'user', array('user' => t('Username'), 'mail' => t('E-mail'))) .'
'; + $output .= '
'. form_textfield(t('Mask'), 'mask', $edit['mask'], 32, 64, '%: '. t('Matches any number of characters, even zero characters') .'.
_: '. t('Matches exactly one character.'), NULL, TRUE) .'
'; + + return $output; } function user_roles($membersonly = 0, $permission = 0) { @@ -1565,11 +1598,6 @@ function user_configure() { } switch ($op) { - case t('Add rule'): - case t('Check'): - case 'access': - $output = user_admin_access($edit); - break; case t('Save permissions'): case 'permission': $output = user_admin_perm($edit); @@ -1623,11 +1651,7 @@ function user_help($section) { case 'admin/user/account/create': return t('

This web page allows the administrators to register a new users by hand. Note that you cannot have a user where either the e-mail address or the username match another user in the system.

'); case 'admin/user/configure/access': - return t('

Access rules allow Drupal administrators to choose usernames and e-mail address that are prevented from using drupal. To enter the mask for e-mail addresses click on e-mail rules, for the username mask click on name rules.

', array('%email' => url('admin/user/configure/access/mail'), '%username' => url('admin/user/configure/access/user'))); - case 'admin/user/configure/access/mail': - return t('

Setup and test the e-mail access rules. The access function checks if you match a deny and not an allow. If you match only a deny then it is denied. Any other case, such as both a deny and an allow pattern matching, allows the pattern.

'); - case 'admin/user/configure/access/user': - return t('

Setup and test the username access rules. The access function checks if you match a deny and not an allow. If you do then it is denied. Any other case, such as a deny pattern and an allow pattern, allows the pattern.

'); + return '

'. t('Set up username and e-mail address access rules for new accounts. If a username or email address for a new account matches any deny rule, but not an allow rule, then the new account will not be allowed to be created.') .'

'; case 'admin/user/configure/permission': return t('

In this area you will define the permissions for each user role (role names are defined on the user roles page). Each permission describes a fine-grained logical operation, such as being able to access the administration pages, or adding/modifying a user account. You could say a permission represents access granted to a user to perform a set of operations.

', array('%role' => url('admin/user/configure/role'))); case 'admin/user/configure/role': -- cgit v1.2.3