Skip to content

Commit

Permalink
New Feature
Browse files Browse the repository at this point in the history
New registered users approval feature: Administrator can also specify
exception rules
  • Loading branch information
crowdask0 committed Oct 29, 2014
1 parent 8596f9f commit 7d97d1b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 18 deletions.
103 changes: 96 additions & 7 deletions qa-include/qa-app-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
define('QA_USER_LEVEL_ADMIN', 100);
define('QA_USER_LEVEL_SUPER', 120);

/* added by :
/* added by zhengyd:
* Self defined thresholds of points for these user levels
* Levels above will not follow points system
*/
Expand Down Expand Up @@ -288,6 +288,87 @@ function qa_set_logged_in_user($userid, $handle='', $remember=false, $source=nul
qa_report_event('u_logout', $olduserid, $oldhandle, qa_cookie_get());
}
}

//zhengyd
function qa_approval_exception($email)
/*
* Check whether $email belongs to approval_exception list
* */
{
$exception_list = trim(qa_opt('approve_exception_list'));

if(empty($exception_list))
return false;

$domains = local_exception_tolist($exception_list);

foreach($domains as $domain)
{
if(local_validate_email($email, $domain))
return true;
}

return false;
}

//zhengyd
function local_exception_tolist($list)
{

$domains = explode(",", $list);
$size = count($domains);
for($i = 0; $i < $size; $i++)
{
$domains[$i] = trim($domains[$i]);
}

return $domains;
}

//zhengyd
function local_validate_email($email, $domain)
{
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
return false;

$at_pos = strpos($email, "@");

if($at_pos == false)
return false;

$domain_pos = strpos($email, $domain);

if($domain_pos == false || $domain_pos <= $at_pos)
return false;

return true;
}

//zhengyd
function local_approve_exception_list_explode($domainstring)
/* return valid domains as array*/
{
$pieces = explode(",", $domainstring);
$result = array();

foreach($pieces as $piece)
{
$piece = trim($piece);
if(!empty($piece) && local_is_a_domain($piece))
array_push($result, $piece);
}

return $result;
}

//zhengyd
function local_is_a_domain($domain)
/* check whether it is a valid domain */
{
return (preg_match('/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i', $domain) //valid chars check
&& preg_match('/^.{1,253}$/', $domain) //overall length check
&& preg_match('/^[^\.]{1,63}(\.[^\.]{1,63})*$/', $domain) );
}


function qa_log_in_external_user($source, $identifier, $fields)
Expand Down Expand Up @@ -333,9 +414,17 @@ function qa_log_in_external_user($source, $identifier, $fields)
unset($fields['confirmed']);
}
}

$userid=qa_create_new_user((string)@$fields['email'], null /* no password */, $handle,
isset($fields['level']) ? $fields['level'] : QA_USER_LEVEL_BASIC, @$fields['confirmed']);

//zhengyd
// If approve user required, we need to check against exception list
// Those users under exception list are not required to be approved
$level_0 = isset($fields['level']) ? $fields['level'] : QA_USER_LEVEL_BASIC;

$userid=qa_create_new_user((string)@$fields['email'], null /* no password */, $handle,
$level_0, @$fields['confirmed']);

if(qa_opt('moderate_users') && qa_opt('approve_user_required') && qa_approval_exception((string)@$fields['email']))
qa_set_user_level($userid, $handle, QA_USER_LEVEL_APPROVED, $level_0);

qa_db_user_login_add($userid, $source, $identifier);
qa_db_user_login_sync(false);
Expand Down Expand Up @@ -1160,7 +1249,7 @@ function qa_check_form_security_code($action, $value)
return (empty($silentproblems) && empty($reportproblems));
}

//
//zhengyd
function qa_brule_keys(){
return array(
'points','qposts','aposts','cposts','aselects','aselecteds','qupvotes','qdownvotes','aupvotes','adownvotes','qvoteds','avoteds','upvoteds','downvoteds','bonus',
Expand All @@ -1182,7 +1271,7 @@ function check_brule($long_def)
return true;
}

//
//zhengyd
//returns false if the long definition format is incorrect
//returns an array of brule conjective forms otherwise
function qa_brule_long_def($long_def)
Expand Down Expand Up @@ -1224,7 +1313,7 @@ function qa_brule_long_def($long_def)
return array($long_def);
}

//
//zhengyd
//returns false if badge rule has incorrect format
//otherwise return the badge rule with tokens form
function qa_brule_tokens($brule)
Expand Down
8 changes: 5 additions & 3 deletions qa-include/qa-lang-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
'blobs_to_disk_note' => '- migrate all uploaded images and documents from the database to disk files',
'block_button' => 'block',
'block_ips_note' => 'Use a hyphen for ranges or * to match any number. Examples: 192.168.0.4 , 192.168.0.0-192.168.0.31 , 192.168.0.*',
'block_words_note' => 'Use a * to match any letters. Examples: doh (will only match exact word doh) , doh* (will match doh or dohno) , do*h (will match doh, dooh, dough).',
//zhengyd
'approve_exception_list_note' => 'Use the domain name sperated by comma. Examples: microsoft.com, purdue.edu',
'block_words_note' => 'Use a * to match any letters. Examples: doh (will only match exact word doh) , doh* (will match doh or dohno) , do*h (will match doh, dooh, dough).',
'cancel_mailing_button' => 'Cancel Mailing',
'categories_introduction' => 'To get started with categories, click the \'Add Category\' button.',
'categories_not_shown' => 'Some questions have categories which will not be displayed.',
'categories_title' => 'Categories',
//
//zhengyd
'brules_page_title' => 'Badge Rules',
'bRule_title'=>'Badges ',
'bRule_name' => 'Badge name:',
Expand Down Expand Up @@ -278,7 +280,7 @@
'widget_not_available' => 'This widget is not available. This could be because the plugin providing the widget is no longer installed.',
'widget_pages_explanation' => 'Show widget in this position on the following pages:',
'widgets_explanation' => 'Available widgets:',
//
//zhengyd
'brule_saved' => 'Badge rule saved',
'brule_added' => 'Badge rule added',
'bRule_type' => 'Badge type',
Expand Down
4 changes: 3 additions & 1 deletion qa-include/qa-lang-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
'avatar_store_size' => 'Maximum size for storing avatars:',
'avatar_users_size' => 'Avatar size on top users page:',
'block_bad_words' => 'Censored words - separate by spaces or commas:',
//zhengyd
'approve_exception_list' => 'All new users must be approved - except the following domains:',
'block_ips_write' => 'Blocked IP addresses - separate by spaces or commas:',
'captcha_module' => 'Use captcha module:',
'captcha_on_anon_post' => 'Use captcha for anonymous posts:',
Expand Down Expand Up @@ -222,7 +224,7 @@
'points_vote_down_q' => 'Voting down a question:',
'points_vote_up_a' => 'Voting up an answer:',
'points_vote_up_q' => 'Voting up a question:',
//by
//by zhengyd
'min_points_to_assign_bounty' => 'Minimal points requirement for creating a bounty:',
'max_bounty' => 'Maximal bounty points:',
'min_bounty' => 'Minimal bounty points:',
Expand Down
19 changes: 16 additions & 3 deletions qa-include/qa-page-admin-default.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,10 @@
$showoptions=array();

$getoptions=qa_get_options(array('feedback_enabled', 'permit_post_q', 'permit_post_a', 'permit_post_c'));


//zhengyd
if (!QA_FINAL_EXTERNAL_USERS)
array_push($showoptions, 'confirm_user_emails', 'confirm_user_required', 'moderate_users', 'approve_user_required', 'register_notify_admin', 'suspend_register_users', '');
array_push($showoptions, 'confirm_user_emails', 'confirm_user_required', 'moderate_users', 'approve_user_required', 'approve_exception_list','register_notify_admin', 'suspend_register_users', '');

$captchamodules=qa_list_modules('captcha');

Expand Down Expand Up @@ -600,6 +601,7 @@
$checkboxtodisplay=array(
'confirm_user_required' => 'option_confirm_user_emails',
'approve_user_required' => 'option_moderate_users',
'approve_exception_list' => 'option_moderate_users',
'captcha_on_unapproved' => 'option_moderate_users',
'captcha_on_unconfirmed' => 'option_confirm_user_emails && !(option_moderate_users && option_captcha_on_unapproved)',
'captcha_module' => 'option_captcha_on_register || option_captcha_on_anon_post || (option_confirm_user_emails && option_captcha_on_unconfirmed) || (option_moderate_users && option_captcha_on_unapproved) || option_captcha_on_reset_password || option_captcha_on_feedback',
Expand Down Expand Up @@ -708,7 +710,12 @@
require_once QA_INCLUDE_DIR.'qa-app-limits.php';
$optionvalue=implode(' , ', qa_block_ips_explode($optionvalue));
break;


case 'approve_exception_list':
require_once QA_INCLUDE_DIR.'qa-app-users.php';
$optionvalue=implode(' , ', local_approve_exception_list_explode($optionvalue));
break;

case 'block_bad_words':
require_once QA_INCLUDE_DIR.'qa-util-string.php';
$optionvalue=implode(' , ', qa_block_words_explode($optionvalue));
Expand Down Expand Up @@ -1288,6 +1295,12 @@ function qa_optionfield_make_select(&$optionfield, $options, $value, $default)
$optionfield['rows']=4;
$optionfield['note']=qa_lang_html('admin/block_ips_note');
break;

case 'approve_exception_list':
$optionfield['style']='tall';
$optionfield['rows']=2;
$optionfield['note']=qa_lang_html('admin/approve_exception_list_note');
break;

case 'allow_view_q_bots':
$optionfield['note']=$optionfield['label'];
Expand Down
16 changes: 12 additions & 4 deletions qa-plugin/open-login-master/qa-open-overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function qa_log_in_external_user($source, $identifier, $fields)
* 'yahoo-xyt' when logging in with the other.
*/

//by
//by zhengyd
//must include this in order to be compatible with cas-login
require_once QA_PLUGIN_DIR.'open-login-master/qa-open-utils.php';

Expand Down Expand Up @@ -122,9 +122,17 @@ function qa_log_in_external_user($source, $identifier, $fields)
unset($fields['email']);
}
}

$userid=qa_create_new_user((string)@$fields['email'], null /* no password */, $handle,
isset($fields['level']) ? $fields['level'] : QA_USER_LEVEL_BASIC, @$fields['confirmed']);

//zhengyd
require_once QA_INCLUDE_DIR.'qa-app-users.php';

$level_0 = isset($fields['level']) ? $fields['level'] : QA_USER_LEVEL_BASIC;

$userid=qa_create_new_user((string)@$fields['email'], null /* no password */, $handle,
$level_0, @$fields['confirmed']);

if(qa_opt('moderate_users') && qa_opt('approve_user_required') && qa_approval_exception((string)@$fields['email']))
qa_set_user_level($userid, $handle, QA_USER_LEVEL_APPROVED, $level_0);

qa_db_user_set($userid, 'oemail', $oemail);
qa_db_user_login_add($userid, $source, $identifier);
Expand Down

0 comments on commit 7d97d1b

Please sign in to comment.