diff --git a/domain.module b/domain.module index a63bc1a..7c090c8 100644 --- a/domain.module +++ b/domain.module @@ -339,6 +339,9 @@ function domain_permission() { 'assign domain editors' => array( 'title' => t('Assign editors to domains'), ), + 'assign own domain editors' => array( + 'title' => t('Assign editors to own domains'), + ), 'set domain access' => array( 'title' => t('Set domain access status for all content'), ), @@ -631,7 +634,7 @@ function domain_user_view($account, $view_mode) { } // Only show trusted users. // TODO: Make this a new permission. - if (!user_access('assign domain editors')) { + if (!user_access('assign domain editors') && !user_access('assign own domain editors')) { return; } $output = ''; @@ -698,12 +701,20 @@ function domain_form_user_form_alter(&$form, &$form_state) { else { $default = $account->domain_user; } - if (user_access('assign domain editors')) { + $assign_all = user_access('assign domain editors'); + $assign_own = user_access('assign own domain editors'); + if ($assign_all || $assign_own) { + global $user; + $user_domains = domain_get_user_domains($user); // Set the form options. $domains = domain_domains(); $options = array(); foreach ($domains as $domain) { - $options[$domain['domain_id']] = check_plain($domain['sitename']); + // Add domain to options only if the admin has right to assign all domains + // or if he is assigned to the domain himself. + if ($assign_all || ($assign_own && !empty($user_domains[$domain['domain_id']]))) { + $options[$domain['domain_id']] = check_plain($domain['sitename']); + } } $format = domain_select_format(); @@ -724,6 +735,11 @@ function domain_form_user_form_alter(&$form, &$form_state) { $form['domain']['domain_user']['#multiple'] = TRUE; $form['domain']['domain_user']['#size'] = count($options) > 10 ? 10 : count($options); } + + // Add extra validation for domain assigning + if ( $assign_own ) { + $form['#validate'][] = 'domain_form_user_form_validate_assigning'; + } } else { $form['domain'] = array( @@ -736,10 +752,40 @@ function domain_form_user_form_alter(&$form, &$form_state) { } /** + * Check if the current user is not removing domain-assigns for domains he hasn't got the admin rights for. + * + * @param $form + * @param $form_state + */ +function domain_form_user_form_validate_assigning($form, &$form_state){ + $assign_all = user_access('assign domain editors'); + global $user; + // Skip the check if assign all is on or user is root + if (!$assign_all && 1 != $user->uid) { + if (isset($form_state['values']['domain_user'])) { + $current_editor_domains = domain_get_user_domains($user); + // Prevent fraudulent input values by comparing with the current editor's assigned domains + foreach ($form_state['values']['domain_user'] as $domain_id => $value) { + if (!isset($current_editor_domains[$domain_id])) { + unset($form_state['values']['domain_user'][$domain_id]); + } + } + if (isset($form_state['user']) && isset($form_state['user']->domain_user)) { + $modified_user_domains = $form_state['values']['domain_user']; + $original_user_domains = $form_state['user']->domain_user; + // Add the changed for the modified domains and leave the originals untouched. + $modified_user_domains += $original_user_domains; + $form_state['values']['domain_user'] = $modified_user_domains; + } + } + } +} + +/** * Implements hook_user_operations(). */ function domain_user_operations() { - if (!user_access('assign domain editors')) { + if (!user_access('assign domain editors') && !user_access('assign own domain editors')) { return; } return array( @@ -755,7 +801,10 @@ function domain_user_operations() { */ function domain_form_user_admin_account_alter(&$form, $form_state) { global $_domain; - if (!user_access('assign domain editors')) { + global $user; + $assign_all = user_access('assign domain editors'); + $assign_own = user_access('assign own domain editors'); + if (!$assign_all && !$assign_own) { return; } $form['options']['#weight'] = -2; @@ -765,8 +814,12 @@ function domain_form_user_admin_account_alter(&$form, $form_state) { foreach (domain_domains() as $data) { // The domain must be valid. if ($data['valid'] || user_access('access inactive domains')) { - // Filter checkbox output but not select list. - $options[$data['domain_id']] = empty($format) ? check_plain($data['sitename']) : $data['sitename']; + // Add domain to options only if the admin has right to assign all domains + // or if he is assigned to the domain himself. + if ($assign_all || ($assign_own && !empty($user->domain_user[$domain['domain_id']]))) { + // Filter checkbox output but not select list. + $options[$data['domain_id']] = empty($format) ? check_plain($data['sitename']) : $data['sitename']; + } } } $form['domain'] = array(